Created
March 2, 2021 23:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int n = 8; | |
if (n < 10) { | |
int n = n * 2; | |
printf("%d\n", n); | |
} | |
printf("%d\n", n); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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