Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Last active August 29, 2015 14:04
Show Gist options
  • Save jhurliman/d172bae3355ddcba6bcd to your computer and use it in GitHub Desktop.
Save jhurliman/d172bae3355ddcba6bcd to your computer and use it in GitHub Desktop.
LLVM does not care about your C optimizations
## for (int i = 0; i < 10; i++) { }
LBL1:
cmpl $10, -4(%ebp) ## -4(%ebp) is the stack address of i. Check if it is less than 10
jge LBL3 ## If the previous compare had a greater than or equal to result, exit the loop
jmp LBL2 ## Otherwise, jump to the next address (this instruction will get optimized away)
LBL2:
movl -4(%ebp), %eax ## Load i into the EAX register (where the arithmetic happens!)
addl $1, %eax ## Add 1 to the EAX register
movl %eax, -4(%ebp) ## Copy the EAX register back on to the stack at the address for i.
jmp LBL1 ## Go back to the i < 10 comparison
LBL3:
... ## Loop has been exited
## for (int i = 0; i < 10; ++i) { }
LBL1: ## Hint: These instructions are identical
cmpl $10, -4(%ebp)
jge LBL3
jmp LBL2
LBL2:
movl -4(%ebp), %eax
addl $1, %eax
movl %eax, -4(%ebp)
jmp LBL1
LBL3:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment