Skip to main content
Be Bitwise
x86 AssemblyReverse Engineering & Hardening

Reading Compiler Output

The most practical reverse engineering skill is reading compiler-emitted assembly. Every binary you'll ever look at — malware, a closed-source app, your own code through objdump for a perf review — came out of a compiler. Compilers fall into predictable patterns for common constructs: function prologues, conditionals, loops, switches, struct access. Once those patterns click, disassembly stops feeling like opaque noise and starts reading like code.

For the broader context on why reverse engineering matters, see Why Reverse Engineer.

Using gcc -S and Godbolt

Quickest way to internalise compiler patterns — compile your own C code and read what comes out:

bash
# Generate assembly output from C source
gcc -S -masm=intel -O0 -o output.s input.c
# -S : produce assembly output (not object code)
# -masm=intel : use Intel syntax (not AT&T)
# -O0 : no optimization (closest to source structure)
# -o output.s : output file

Godbolt's Compiler Explorer (godbolt.org) is the same idea on the web — an interactive view of assembly next to C, with colour-coded mappings linking each source line to its instructions.

Pattern: Function Prologue and Epilogue

Every function compiled at -O0 — and most at higher levels when a frame pointer is required — opens and closes with the same pair of moves:

c
int example(int a, int b) {
int result = a + b;
return result;
}
nasm
example:
; PROLOGUE -- same in every function
push rbp ; save caller's frame pointer
mov rbp, rsp ; establish our frame pointer
; ARGUMENT SAVE (common at -O0)
mov dword [rbp-20], edi ; save first arg (a) to stack
mov dword [rbp-24], esi ; save second arg (b) to stack
; BODY
mov edx, dword [rbp-20] ; load a
mov eax, dword [rbp-24] ; load b
add eax, edx ; result = a + b
mov dword [rbp-4], eax ; store result on stack
; RETURN VALUE
mov eax, dword [rbp-4] ; return value goes in eax
; EPILOGUE -- same in every function
pop rbp ; restore caller's frame pointer
ret ; return

At -O0 the compiler dutifully spills every argument to the stack and reloads it on each use. Verbose, but easy to read. At higher optimisation levels it keeps values in registers and binnes the redundant loads and stores.

Pattern: If-Else Statements

c
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
nasm
max:
push rbp
mov rbp, rsp
mov dword [rbp-4], edi ; a
mov dword [rbp-8], esi ; b
; if (a > b)
mov eax, dword [rbp-4] ; load a
cmp eax, dword [rbp-8] ; compare a with b
jle .L2 ; if a <= b, jump to else branch
; NOTE: compiler INVERTS the condition
; then branch: return a
mov eax, dword [rbp-4]
jmp .L3 ; skip the else branch
.L2:
; else branch: return b
mov eax, dword [rbp-8]
.L3:
pop rbp
ret
The Compiler Inverts Conditions

Write if (a > b) and the compiler emits cmp; jle — jump if less-or-equal. It flips the condition and jumps to the else branch when the source condition is FALSE. This is the single biggest source of confusion when reading disassembly: the jump targets the ELSE branch, not the THEN. Spot the inversion and the control flow snaps into focus; miss it and you'll misread half the code.

Pattern: Loops

For loop:

c
int sum(int n) {
int total = 0;
for (int i = 0; i < n; i++) {
total += i;
}
return total;
}
nasm
sum:
push rbp
mov rbp, rsp
mov dword [rbp-20], edi ; n
mov dword [rbp-4], 0 ; total = 0
mov dword [rbp-8], 0 ; i = 0
.L_check: ; loop condition check
mov eax, dword [rbp-8] ; load i
cmp eax, dword [rbp-20] ; compare i with n
jge .L_done ; if i >= n, exit loop
; loop body
mov eax, dword [rbp-8] ; load i
add dword [rbp-4], eax ; total += i
; increment
add dword [rbp-8], 1 ; i++
jmp .L_check ; back to condition
.L_done:
mov eax, dword [rbp-4] ; return total
pop rbp
ret

That's clang's shape at -O0: check the condition → conditional jump past the body if the condition fails → run the body → increment → unconditional jump back to the check.

GCC at -O0 turns it upside down. It jumps straight to a condition placed after the body, and the test there jumps backwards while the condition holds:

nasm
jmp .L2 ; go to the test first
.L3: ; loop body
mov eax, dword [rbp-8]
add dword [rbp-4], eax ; total += i
add dword [rbp-8], 1 ; i++
.L2: ; condition at the bottom
mov eax, dword [rbp-8]
cmp eax, dword [rbp-20]
jl .L3 ; i < n: go round again

Both are the same loop. Watch for a backward conditional jump, whichever end the test lives at.

While loops look identical in assembly. The compiler doesn't keep separate identities for for and while at the instruction level.

Pattern: Switch Statements

Small switches compile down to a chain of comparisons:

c
int classify(int x) {
switch (x) {
case 1: return 10;
case 2: return 20;
case 3: return 30;
default: return -1;
}
}
nasm
classify:
push rbp
mov rbp, rsp
mov dword [rbp-4], edi
cmp dword [rbp-4], 1 ; case 1
je .L_case1
cmp dword [rbp-4], 2 ; case 2
je .L_case2
cmp dword [rbp-4], 3 ; case 3
je .L_case3
jmp .L_default ; default
.L_case1:
mov eax, 10
jmp .L_end
.L_case2:
mov eax, 20
jmp .L_end
.L_case3:
mov eax, 30
jmp .L_end
.L_default:
mov eax, -1
.L_end:
pop rbp
ret

Bigger switches with dense case values compile to a jump table — an array of code addresses indexed by the switch value itself.

Pattern: Struct Access

c
struct Point {
int x; // offset 0
int y; // offset 4
int z; // offset 8
};
int get_y(struct Point *p) {
return p->y;
}
nasm
get_y:
push rbp
mov rbp, rsp
mov qword [rbp-8], rdi ; save pointer p
mov rax, qword [rbp-8] ; load p
mov eax, dword [rax+4] ; load p->y (offset 4 from base)
pop rbp
ret

Struct field access shows up as a load from a base pointer plus a constant offset. The offset tells you where the field sits in the layout. A run of [rax+4], [rax+8], [rax+12] is the unmistakable look of 4-byte fields in a struct.

Optimization Levels

Drop the same function through different optimisation levels and watch the diff:

-O0 (Debug) vs -O2 (Optimized)

x86-64
C Source
1add:
2 push rbp
3 mov rbp, rsp
4 mov [rbp-4], edi
5 mov [rbp-8], esi
6 mov eax, [rbp-4]
7 add eax, [rbp-8]
8 pop rbp
9 ret
Assembly
1add:
2 lea eax, [rdi+rsi]
3 ret

Hover over code lines or mapping badges to see correspondences

-O0 (no optimization):

nasm
; Every variable on the stack, frequent loads/stores
mov dword [rbp-4], edi
mov eax, dword [rbp-4]
add eax, dword [rbp-8]
mov dword [rbp-12], eax
mov eax, dword [rbp-12]

-O1 (basic optimization):

nasm
; Eliminates redundant loads/stores, keeps values in registers
lea eax, [rdi + rsi] ; a + b in one instruction

-O2 (full optimization):

nasm
; Aggressive inlining; clang also unrolls and vectorises loops here,
; GCC 12+ vectorises with a very cheap cost model (no -O2 unrolling)
; Functions may be inlined (disappear entirely)
; Loops may be replaced with closed-form expressions

-O3 (maximum optimization):

nasm
; Even more aggressive: GCC's full auto-vectorization (SIMD),
; loop peeling, function cloning for different call sites
; May use SSE/AVX instructions for scalar operations

At -O2 and above, the compiler can remix code beyond recognition. Loops might disappear entirely if the result can be computed at compile time. Functions might be inlined into every caller and stop existing on their own. Knowing these transformations is half the battle of reverse-engineering optimised binaries.

Reading Compiler Output Is the Most Practical RE Skill

You don't need Ghidra, IDA Pro, or any specialised tool to start. gcc -S -masm=intel on your own C code, read the output, match instruction patterns back to the constructs that produced them. The skill ports straight over to reading objdump output, debugging in GDB, and working with decompilers. Start with functions you wrote, graduate to functions you didn't, and with enough reps you'll be able to reconstruct an approximate C source from raw disassembly.

Recognizing Common Library Patterns

Stripped binaries hide symbol names but not instruction patterns. Common operations still wear obvious fingerprints:

nasm
; strlen pattern: scan for zero byte (hand-written or old code;
; modern compilers call libc's SIMD strlen instead)
repne scasb ; repeat scan byte until zero found in [rdi]
; memcpy pattern: block copy
rep movsb ; copy rcx bytes from [rsi] to [rdi]
; memset pattern: block fill
rep stosb ; fill rcx bytes at [rdi] with al

These instruction shapes are distinctive even without symbols. Don't lean on them too hard in modern binaries, though: compilers usually emit a call to libc for strlen and large copies, and glibc picks a SIMD version at load time, so repne scasb mostly turns up in hand-written assembly and older code. rep movsb/rep stosb still appear for small inline copies and fills.

What is Next

Now that compiler output reads as code, the next lesson sharpens the same skill into a security tool — identifying vulnerability patterns in disassembly. You'll learn to spot missing bounds checks, dangerous function calls, and the rest of the tells that flag exploitable bugs straight from the assembly.