TExcelTAD
Home › Reverse Engineering & Binary Analysis › How Compilers Translate if, Loops and switch

How Compilers Translate if, Loops and switch

A disassembly has no if, no for and no switch; it has comparisons, conditional jumps and labels. Recognising the handful of shapes compilers use for each statement is what lets you turn twenty lines of jumps back into three lines of C in your head, which is also what a decompiler does. This lesson collects those shapes from real GCC output. The assembly course's Jumps & Conditionals, Loops and Reading Compiler Output lessons write the same patterns by hand.

The shapes

C Typical compiled shape
if (c) A; cmp/test, a jump on the opposite condition over A
if (c) A; else B; jump to B on not-c; A; jmp past B; B
x = c ? a : b; often no branch: cmov or setcc
for/while the test at the bottom of the loop with a backward jump to the top, plus one test before entry ("loop inversion")
do ... while body, test, backward jump
dense switch bounds check, then an indirect jmp through a jump table of offsets in .rodata
sparse switch a tree of comparisons (binary search on the case values)
&&, || chains of jumps that skip the rest of the condition (short-circuit evaluation)

The backward jump is the single most useful clue: a jump to a lower address is a loop.

Real output

The session compiles five small functions with GCC 13 at -O1 (readable but realistic) and prints the assembly without directives. The switch cases call external functions so the compiler can't turn them into a lookup table of constants.

bash · try it yourself
cat > flow.c <<'SRC'
int classify(int x) {
    if (x < 0) return -1;
    else if (x == 0) return 0;
    return 1;
}
int sum(const int *a, int n) {
    int s = 0;
    for (int i = 0; i < n; i++) s += a[i];
    return s;
}
int both(int a, int b) { return a > 0 && b > 0; }
int f0(int), f1(int), f2(int), f3(int), f4(int), f5(int);
int dense(int op, int x) {
    switch (op) {
    case 0: return f0(x);  case 1: return f1(x);  case 2: return f2(x);
    case 3: return f3(x);  case 4: return f4(x);  case 5: return f5(x);
    default: return -1;
    }
}
int sparse(int code) {
    switch (code) {
    case 7: return 1;  case 300: return 2;  case 5000: return 3;  case 90000: return 4;
    default: return 0;
    }
}
SRC
gcc -O1 -S -masm=intel -fno-asynchronous-unwind-tables -fcf-protection=none -o - flow.c \
  | grep -vE '^\s*\.(file|text|p2align|globl|type|size|ident|section|align)|^\s*
  

	.intel_syntax noprefix
classify:
	mov	eax, -1
	test	edi, edi
	js	.L1
	setne	al
	movzx	eax, al
.L1:
	ret
sum:
	test	esi, esi
	jle	.L7
	mov	rax, rdi
	movsx	rsi, esi
	lea	rcx, [rdi+rsi*4]
	mov	edx, 0
.L6:
	add	edx, DWORD PTR [rax]
	add	rax, 4
	cmp	rax, rcx
	jne	.L6
.L4:
	mov	eax, edx
	ret
.L7:
	mov	edx, 0
	jmp	.L4
both:
	test	edi, edi
	setg	dl
	test	esi, esi
	setg	al
	movzx	eax, al
	and	eax, edx
	ret
dense:
	mov	eax, edi
	cmp	edi, 5
	ja	.L19
	sub	rsp, 8
	mov	edi, esi
	mov	eax, eax
	lea	rdx, .L13[rip]
	movsx	rax, DWORD PTR [rdx+rax*4]
	add	rax, rdx
	jmp	rax
.L13:
	.long	.L18-.L13
	.long	.L17-.L13
	.long	.L16-.L13
	.long	.L15-.L13
	.long	.L14-.L13
	.long	.L12-.L13
.L18:
	call	f0@PLT
.L10:
	add	rsp, 8
	ret
.L17:
	call	f1@PLT
	jmp	.L10
.L16:
	call	f2@PLT
	jmp	.L10
.L15:
	call	f3@PLT
	jmp	.L10
.L14:
	call	f4@PLT
	jmp	.L10
.L12:
	call	f5@PLT
	jmp	.L10
.L19:
	mov	eax, -1
	ret
sparse:
	cmp	edi, 5000
	je	.L27
	jg	.L26
	mov	eax, 1
	cmp	edi, 7
	je	.L24
	cmp	edi, 300
	sete	al
	movzx	eax, al
	add	eax, eax
	ret
.L26:
	cmp	edi, 90000
	sete	al
	movzx	eax, al
	sal	eax, 2
	ret
.L27:
	mov	eax, 3
.L24:
	ret

Read it function by function:

  • classify keeps one jump and replaces the other test: eax is preset to -1, js (jump if the sign flag is set) leaves with -1 for negative x, and for the rest setne al turns "x is not zero" into 0 or 1. Three return statements, one branch: an if in the source is not always a jump in the binary.
  • sum shows loop inversion: one test esi, esi / jle before the loop skips it when n <= 0; inside, the pointer walks the array (add rax, 4) and a cmp/jne at the bottom jumps backward to the top. The compiler replaced i with a pointer and an end address.
  • both evaluates a > 0 && b > 0 without branches here (setg twice and and); with side effects in the second operand it would need a jump.
  • dense is the jump table: cmp edi, 5 / ja sends out-of-range values to the default, then lea rdx, .L13[rip] loads the table's address, movsx rax, DWORD PTR [rdx+rax*4] reads a 32-bit offset from it and add rax, rdx / jmp rax jumps to the case. The .long .Lx-.L13 lines are the table itself: one entry per case value, relative to the table so the code works at any load address (PIE).
  • sparse compares against 5000 first and branches left (jg not taken: 7 or 300) or right (90000): a binary search over the four values. GCC even avoids a branch for the last test of each side: sete al then add eax, eax gives 2 or 0, sal eax, 2 gives 4 or 0.

Recovering a switch from a binary

When an indirect jmp reg appears after a bounds check:

  1. The bounds check (cmp edi, 5 / ja default) gives the number of cases (here 6, values 0-5). A preceding sub edi, k means the cases start at k.
  2. The table address (lea rdx, [rip+...]) and the entry size (4-byte offsets in PIE code, 8-byte absolute addresses in non-PIE code) let you read every target.
  3. Several entries pointing to the same target are case labels sharing code, often the default.

Disassemblers do this automatically for compiler-generated tables; a hand-written or obfuscated indirect jump defeats them, which is why obfuscators like indirect jumps (see Obfuscation).

How this matters for detection and review

  • Missing checks are visible as missing jumps: a length test with only an upper bound, a loop whose exit condition depends on data read from input, a switch whose bounds check uses a signed jg on a value that can be negative.
  • Loops over input buffers (a backward jump with a pointer increment and a load) are where parsers live; they are the first targets in vulnerability research and fuzzing.
  • Unusual control flow (indirect jumps without a table, jumps into the middle of instructions, a dispatcher loop that every block returns to) suggests obfuscation or hand-written code.

How mistakes are prevented

  • For authors: let the compiler warn about switch statements that miss enum values (-Wswitch-enum) and about sign comparisons; keep the default in every switch.
  • For analysts: annotate each recovered loop and switch with its source-level meaning as soon as you understand it; renaming is half of reverse engineering (Decompilers).

Pitfalls

  • Inverted conditions: if (x > 10) A; compiles to cmp x, 10; jle skip. Read the jump as "skip A when".
  • Loop inversion puts the test at the bottom; the top of the loop has no condition.
  • Tail calls: at -O2, return f0(x); becomes jmp f0, not call f0, so a "jump" may leave the function.
  • -O0 vs -O2 look completely different; the next lessons compare them.

Practice

1. A block ends with cmp rax, rcx / jne .L3, and .L3 is 20 bytes earlier. What is it?

The bottom of a loop: the backward jump repeats the body until rax equals rcx (a pointer reaching the end address).

2. Before an indirect jump you see sub edi, 10, cmp edi, 3, ja .Ldefault. Which case values does the switch have?

10, 11, 12 and 13 (four entries after subtracting 10); everything else goes to the default.

3. Why does the jump table hold offsets relative to the table rather than absolute addresses?

The program is position-independent (PIE): absolute addresses would need relocations at load time, relative offsets work at any address.

Test yourself

What is the most reliable sign of a loop in a disassembly?

  1. A call instruction
  2. A conditional jump backward to a lower address
  3. A ret instruction
  4. A cmov

A dense switch compiles to what?

  1. A chain of cmov
  2. A bounds check plus an indirect jump through a jump table
  3. A recursive call
  4. A system call

if (x > 10) A; usually compiles to cmp x, 10 followed by which jump over A (signed x)?

  1. jg
  2. jle
  3. ja
  4. je

How did GCC compile the sparse switch on 7, 300, 5000, 90000?

  1. As a jump table with 90,001 entries
  2. As a tree of comparisons
  3. As a loop
  4. As a hash table call