codegen: factor out codegen_dotimes for other uses