I came across Undefined behavior in this C code below:
#include <stdio.h>
int main()
{
int x=0,y=5;
printf("x=%d,x_1=%d,sum=%d",x++,x,y+x);
return 0;
}
Just out of curiosity, i tried checking assembly of the same to see if i can understand how loading and execution has occurred but couldn't understand it much. Volunteers, could you please help me understand order in which the implementation has actually occured?
.file "example.c"
.text
.section .rodata
.LC0:
.string "x=%d,x_2=%d,y=%d\n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0, -8(%rbp)
movl $5, -4(%rbp)
movl -4(%rbp), %edx
movl -8(%rbp), %eax
leal (%rdx,%rax), %ecx
movl -8(%rbp), %eax
leal 1(%rax), %edx
movl %edx, -8(%rbp)
movl -8(%rbp), %edx
movl %eax, %esi
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits