Sahih Muslim Book 33, Hadith Number 6395.

Chapter : The growth of a child in the womb of a mother and his destiny in regard to his livelihood, his deeds. both good and evil.

Abu Tufail reported: I visited Abu Sariha Hudhaifa b. Usaid al-Ghifari who said: I listened with these two ears of mine Allah’s Messenger (may peace be upon him) as saying: The semen stays in the womb for forty nights, then the angel, gives it a shape. Zubair said: I think that he said: One who fashions that and decides whether he would be male or female. Then he (the angel) says: Would his limbs be full or imperfect? And then the Lord makes thein full and perfect or otherwise as He desires. Then he says: My Lord, what about his livelihood, and his death and what about his disposition? And then the Lord decides about his misfortune and fortune.