Abu al-Tufayl Amir bin Wathilah said: Whenever Ibn Mas'ud addressed us in Kufah, he would say: The wretched one is he who was decreed wretched in his mother's womb, and the fortunate one is he who was decreed fortunate in his mother's womb. So I went to Hudhayfah bin Asid, one of the Companions of the Messenger of Allah (peace and blessings be upon him), and said: How astonishing is this saying of Ibn Umm Abd (Ibn Mas'ud), that the wretched one is he who was decreed wretched in his mother's womb! Hudhayfah said to me: O Abu al-Tufayl, what is there in that to astonish you? Shall I not tell you the reality of his wretchedness? And he narrated the hadith as a saying of the Prophet: "An angel is put in charge of the womb for forty-odd nights when Allah, by His will, wishes to create whatever He wills. He says: O Lord, is it male or female? So your Lord decrees and the angel writes it down. Then he says: O Lord, is it wretched or fortunate? So your Lord decrees and the angel writes it down. Then he says: O Lord, when will its death be? So your Lord decrees and the angel writes it down. Then the scroll is rolled up; nothing is added and nothing is taken away."
Hadith Referenceالمعجم الكبير للطبراني / باب الحاء / 3027