I thought it was enough to push into the stack the equivalent recursive call.
Nope. Each time write() is called, it will call write(n-1), which will also call write(n-2), which calls write(n-3) etc etc. So simply pushing n-1 won't do the job... you'd need to push all of them.
What's more... the order you'd have to push them in incredibly weird. write(3) outputs this:
1 2 1 3 1 2 1
write(4) outputs this:
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
Honestly... given how freaking weird this function is... I really don't see a straightforward way to remove the recursion.