Rewrite some recursive tail calls to loops.