This question already has an answer here:
- What is inlining? 10 answers
- Why is inlining considered faster than a function call? 16 answers
- What exactly is the “as-if” rule? 3 answers
I'm learning to read assembly code. For that, I wrote this C program (named vebytesmain.c) with level 0 optimization:
typedef char *ptr;
char ret_byte(void *ender, int byte){
ptr point = (ptr)(ender + byte);
return *point;
}
int main(void){
int num=0x00010203;
num = ret_byte(&num,2);
return 0;
}
and got it's assembly code by gcc -O0 -S vebytesmain.c
. The main function was like that:
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
movl $66051, -12(%rbp)
leaq -12(%rbp), %rax
movl $2, %esi
movq %rax, %rdi
call ret_byte ; this is where the call happens (lvl 0 optimization)
movsbl %al, %eax
movl %eax, -12(%rbp)
movl $0, %eax
movq -8(%rbp), %rdx
xorq %fs:40, %rdx
je .L5
call __stack_chk_fail@PLT
.L5:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
which is ok, but when I apply at least level 2 optimization, what I get is:
main:
.LFB1:
.cfi_startproc
xorl %eax, %eax
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
How come there is no call instruction for the ret_bytes function? How do I know that the function is being called? How is it being called?
I would be grateful if someone could solve explain me what is going on.