Added write function codegen routine.