Abu al-Tufayl al-Bakri said: I heard Ibn Mas'ud say: The wretched one is he who was decreed wretched in his mother's womb, and the fortunate one is he who takes admonition from the state of others. I asked him: How can one who has not done any deeds be wretched? Then I met Hudhayfah bin Asid and told him what Ibn Mas'ud had said, and he told me that the Messenger of Allah (peace and blessings be upon him) said: "When Allah, the Mighty and Majestic, wishes to create a servant, the angel says: O Lord, is it male or female? So the Lord says whatever He wills and the angel writes it down. Then the angel says: O Lord, is it wretched or fortunate? So the Lord says whatever He wills and the angel writes it down. Then he says: O Lord, what will its outcome be? So the Lord says whatever He wills and the angel writes it down. Then the angel says: What is its provision? So Allah says whatever He wills and the angel writes it down. Then he says: O Lord, when will its death be? So the Lord says whatever He wills and the angel writes it down."
Hadith Referenceالمعجم الكبير للطبراني / باب الحاء / 3030