Skip to content

Instantly share code, notes, and snippets.

@airportyh
Created March 2, 2021 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airportyh/35c4066c77d166fd4d0c6b2b65c10e9c to your computer and use it in GitHub Desktop.
Save airportyh/35c4066c77d166fd4d0c6b2b65c10e9c to your computer and use it in GitHub Desktop.
Weird corner case in C where inner scope redeclares a var in the outer scope while at the same time trying to reference the outer one.
#include <stdio.h>
int main() {
int n = 8;
if (n < 10) {
int n = n * 2;
printf("%d\n", n);
}
printf("%d\n", n);
}
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 13
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
## %bb.0:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movl $0, -4(%rbp)
movl $8, -8(%rbp)
cmpl $10, -8(%rbp)
jge LBB0_2
## %bb.1:
leaq L_.str(%rip), %rdi
movl -12(%rbp), %eax
shll $1, %eax
movl %eax, -12(%rbp)
movl -12(%rbp), %esi
movb $0, %al
callq _printf
movl %eax, -16(%rbp) ## 4-byte Spill
LBB0_2:
leaq L_.str(%rip), %rdi
movl -8(%rbp), %esi
movb $0, %al
callq _printf
movl -4(%rbp), %esi
movl %eax, -20(%rbp) ## 4-byte Spill
movl %esi, %eax
addq $32, %rsp
popq %rbp
retq
## -- End function
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "%d\n"
.subsections_via_symbols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment