Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. asm-101
  3. compiler-optimizations
3 min read

Reading Optimized Code

Every disassembly you have read so far translated cleanly, almost line for line, back to its source. Real production code does not: the compiler's optimizer rewrites it until the shape of the original is gone. Reading that output — recognizing what the code does when it no longer looks like the source — is its own skill, and an essential one for reverse engineering.

What -O2 Does

Turn the optimizer on and several transformations fire at once: small functions are inlined into their callers, unreachable code is eliminated, values are kept in registers so stack frames vanish, and expensive operations are swapped for cheap ones — strength reduction. The clearest way to see it is to compile the same function both ways. Here is int mul6(int x) { return x * 6; } with no optimization:

objdump
$ objdump -d -M intel mul_O0.o0000000000000000 <mul6>:   0:   55                      push   rbp   1:   48 89 e5                mov    rbp,rsp   4:   89 7d fc                mov    DWORD PTR [rbp-0x4],edi   7:   8b 55 fc                mov    edx,DWORD PTR [rbp-0x4]   a:   89 d0                   mov    eax,edx   c:   01 c0                   add    eax,eax   e:   01 d0                   add    eax,edx  10:   01 c0                   add    eax,eax  12:   5d                      pop    rbp  13:   c3                      ret

And the same function at -O2:

objdump
$ objdump -d -M intel mul_O2.o0000000000000000 <mul6>:   0:   8d 04 7f                lea    eax,[rdi+rdi*2]   3:   01 c0                   add    eax,eax   5:   c3                      ret

The unoptimized version sets up a stack frame, spills the argument to [rbp-0x4], and reads it back before doing the math. The optimized version throws all of that away: no frame, no memory traffic, three instructions total. And it never multiplies — lea eax, [rdi + rdi*2] computes x*3, then add eax, eax doubles it to x*6, the exact lea trick from Arithmetic. That is strength reduction: a * in the source became two cheap address-unit operations.

Loop Unrolling and Vectorization

Loops get the heaviest reshaping. The optimizer may unroll them — doing several iterations per pass to cut the number of jumps — so a loop that reads as "once per element" in C runs two or four elements at a time in the disassembly, padded with alignment nops. It may go further and vectorize, using the SIMD registers (xmm/ymm) to process a whole batch of elements with a single instruction. A tidy for loop over an array can come out as a block of xmm operations with no recognizable per-element structure at all.

Tail-Call Optimization

When a function's last action is to call another and return its result, the compiler can reuse the current stack frame and turn the call into a plain jmp — a tail call. Two consequences for reading: a function call in the source can show up as a jmp with no matching ret in the caller, and tail-recursive functions collapse into ordinary loops.

Why Optimized Disassembly Doesn't Match the Source

Put together, these transformations break the line-for-line correspondence entirely. Locals live only in registers, so there is nothing in memory to map a variable to; operations are reordered, merged, or deleted; loops are restructured; whole functions are inlined out of existence. Do not try to match each instruction to a source line — read optimized code for what it computes, following the data, not the layout.

Compile the same function at -O0 vs -O2 and diff the disassembly (see the toolchain and radare2 guides). It is the fastest way to build an eye for what the optimizer does.