Code generation is the process where a compiler translates an intermediate representation (IR) of the source code into machine code or assembly code. This step converts high-level constructs into low-level instructions that a computer’s processor can execute. While optimization is a separate process, its goal is to enhance the performance and efficiency of the generated code.