The correct time to recite the “Al netilat yadayim” berachah is immediately after washing and before drying one’s hands. However, the general rule regarding all berachot recited upon the performance of mitzvot, is to recite the berachah first and then perform the mitzvah. Hence, we make a berachah on the wrapping of tefillin before binding them on our arms, and we make a berachah on the mezuzah before affixing it to the doorpost. Yet, regarding Al netilat yadayim the law is different. Since it is inappropriate to recite the berachah while one’s hands are unclean, its recital is delayed until after the washing. Still, we may not separate the berachah from the act of the mitzvah and therefore, immediately after washing, before drying one’s hands, the berachah must be recited.
In actuality, the berachah is usually not recited immediately following the first washing upon waking. This is because people generally need to relieve themselves upon waking up, and one should not recite a berachah while having to tend to one’s bodily needs. Furthermore, according to the Rosh, the washing was instituted to prepare the person for prayer; therefore, one is obligated to recite a berachah on the washing before praying. Hence, after relieving oneself in the morning, one washes his hands again and recites the berachah before drying them. One who does not need to relieve himself in the morning should recite the berachah on the first washing immediately upon waking up (Shulchan Aruch 6:2; Mishnah Berurah 4:4).
Regarding one who rises from his sleep long before he intends to pray, and knows for certain that prior to praying he will need to defecate, there is uncertainty concerning when it is proper to recite Al netilat yadayim. According to the Rashba, who maintains that one washes in honor of the new day, it is proper that he recite the berachah immediately upon waking up. According to the Rosh, who maintains that the washing was instituted as preparation for prayer, he should recite the berachah on the washing right before prayer. In this case, it is advisable to recite Birkot HaShachar immediately following the washing performed upon rising, and then, to a certain extent, Birkot HaShachar will be considered the beginning of prayer and the washing a preparation for them. That way, even according to the Rosh one may recite Al netilat yadayim immediately upon waking up.
Therefore, whoever wakes up after chatzot (halachic midnight) must wash his hands, recite Al netilat yadayim, and say all of Birkot HaShachar. However, if, after remaining awake for a few hours, he intends to go back to sleep until the time to pray arrives, after waking up the first time he must wash his hands without a berachah, and after the second time he must wash with a berachah, for that is the waking closest to prayer. (Regarding Birkot HaShachar see further in this book 9:5, where it is explained that it is preferable to recite them after the main waking, provided that it is after chatzot.)
Still, many Acharonim write that it is preferable to recite the blessing on the washing closer to one’s waking for that is the correct way according to the Rashba. Even according to the Rosh, if he recites Birkot HaShachar immediately after the first washing, it is possible that they will be considered the beginning of prayer, as write the Shlah, Graz (in his siddur, p. 1), Sha’arei Teshuvah, Derech HaChaim, and Aruch HaShulchan 4:5. The Aruch HaShulchan explains further that even according to the Rosh, the washing of one’s hands and the recital of the berachah were instituted to be performed adjacent to one’s waking, just like the other Birkot HaShachar. Even according to the Mishnah Berurah 6:9, it is possible to rely on those poskim. The Kaf HaChaim 4:52 writes as well that one must recite Al netilat yadayim immediately upon getting up. Additionally, he brings a dispute of the Acharonim as follows: there are those who say that one may recite Al netilat yadayim even before chatzot, which is the custom of the kabbalists (Ben Ish Chai, Toldot 13). However, the Graz and others write that one must recite Al netilat yadayim only after chatzot. I wrote that one should recite it only after chatzot in order to avoid uncertainty. If he intends to go back to sleep (when he woke up after chatzot), indeed according to the Ben Ish Chai, Kaf HaChaim 46:49, based on the Kabbalah, and the Graz, he should recite Al netilat yadayim after his first waking. In any case, in this situation, it seems better to practice according to the Mishnah Berurah, Chayei Adam, and the Gra, as I have written.