Reduce temp to scalar; add codegen subroutines