Rewrite C code from recursive version to iteration version.