Lesson 3 of 13 · 22m
Control Flow: Finding the Shape of the Code
if/while/switch collapse into compare-and-jump; learn the five CFG shapes, what -O2 does to them, and why linear sweep and recursive descent disassemblers each go blind.
Assembly is a flat list of instructions; programs are trees, loops, and dispatch tables. The wrong mental model is that the compiler preserved your structure — it preserved your behavior, using three primitives: compare-and-jump, call, and return. Most of static analysis is pattern-matching those primitives back into the ifs, whiles, and switches the programmer wrote. Learn five shapes and the control-flow graph stops being noise.
Basic blocks and the CFG
A basic block is a run of instructions with one way in and one way out — it ends at a jump, a call, or a ret. The control-flow graph is basic blocks as nodes and jumps as edges. Everything you'll ever recognize is a shape in that graph.
The five shapes
; if/else — a diamond: compare, jump over one arm, both arms rejoin
cmp eax, 5
jle ELSE
; ... then-arm ...
jmp JOIN
ELSE:
; ... else-arm ...
JOIN:
; while — a back-edge: a jump that goes *up* the listing
LOOP:
cmp ecx, edx
jge DONE
; ... body ...
inc ecx
jmp LOOP ; backward jump = loop
DONE:
; switch — a bounds check, then one indirect jump through a table
mov eax, DWORD [rbp-0x4] ; the switch variable
cmp eax, 6
ja DEFAULT ; out of range -> default case
movsxd rax, eax ; the same variable, sign-extended to index the table
jmp QWORD [rax*8 + 0x402000] ; rax indexes a case-address tableTwo impostors to recognize. cmov folds a small if/else into a conditional move with no branch at all — when you expect a diamond and find straight-line code, that's why; ternaries compile this way constantly. And setcc (setz, setne) materializes a comparison as a 0/1 byte — that's bool ok = (x == 5) with no branch either.
Two details in that switch shape pay rent. The ja (unsigned-above) bounds check does double duty: a negative case value wraps to a huge unsigned and lands in DEFAULT too, so one compare guards both ends of the range. And the table only exists because the cases were dense — switch (x) { case 1: ... case 90000: ... } compiles back to compare-chains or a binary-search branch tree; the indirect jump is the compiler's payoff for a contiguous range.
How -O2 rearranges the shapes
- Loop rotation.
while (cond)becomes a guard test plus ado { ... } while (cond)— one compare jumps into the loop body, a second jumps backward from the bottom. Thefor/while/do-whiledistinction you wrote is gone; only the trip count and exit condition survive. - Tail calls.
return f(x)compiles tojmp f— reusing the current stack frame. It looks like a jump into the middle of nowhere until you notice the call-sitecallis missing. - No-return exits. Calls to
exit,abort, or__stack_chk_failend a path — the compiler may dropud2after them as a fencepost. Don't look for the block to rejoin; it doesn't.
How disassemblers find the code
- Linear sweep (
objdump -d): decode every byte of.textin order. Fast, and fooled the moment data sits between functions — those bytes decode as instructions and the stream desyncs for everything after. - Recursive descent (Ghidra, IDA): follow
jmp/calltargets, stop atretand unconditional jumps. Handles inline data, but indirect jumps —jmp rax, jump tables — leave blocks undiscovered until analysis proves where they point. - Emulation (angr, some IDA plugins): execute blocks over a fake machine to discover the targets the other two can't prove. Slower, and reserved for the jump tables recursive descent gave up on.
That's what Ghidra's minute of auto-analysis is doing: resolving every indirect target it can, including switch tables. Where it fails you get ?? bytes and unreachable regions — which is itself information: usually a jump table sitting in .rodata that you can define by hand.
One more shape hiding in plain sight: call printf@plt is a jump through the GOT — jmp [rip+0x2fe2] in the .plt that the dynamic linker filled in. In the listing it looks like a function; in reality it's a two-hop trampoline to libc. The call graph knows the difference.
Field notes
- Function boundaries are inferred: prologues, call targets, and the
int3/noppadding compilers insert between functions. Strip the symbols and boundary detection is a heuristic — occasionally wrong. - Bytes right after a
retthat no jump reaches are almost always padding or data, not dead logic. Don't spend an hour reversing an alignment sled. - A call graph is the zoomed-out CFG: functions as nodes, calls as edges. Ghidra's function call graph view answers "who could ever reach this" faster than the listing does.
Keep reading
Related articles from the SudoSecurity blog.
A Fake GTA 6 ISO Didn’t Install a Game. It Installed a Wiper.
A fake GTA 6 ISO used a believable installer error to cover remote-access malware, browser-data theft, and destructive file wiping. The “License not found” message wasn’t the crack failing. It was there so the victim would stop looking.
CVE-2025-62718: Critical Axios SSRF Vulnerability Explained
A critical SSRF flaw in Axios was used in a North Korean supply chain attack. Here's what it does and how to patch it today.
Hacktivism, 2021 to Today: From Steel Mills to Power Grids
Part 1 ended on a 70-gigabyte breach in 2021. Since then, hacktivists derailed trains, dumped molten steel on camera, and reached grids in three countries.
What is an Infostealer?
Infostealers are commodity malware that silently harvest credentials, session tokens, and crypto wallets from infected machines. The Lumma Stealer infection that triggered the Vercel breach started with one. Here is how they work and why they are so effective.