void handle_signal(int signum) {
  unsigned addr_signum = (unsigned)&signum;
  unsigned addr_retaddr = addr_signum + 24;
  unsigned *retaddr = (unsigned *)addr_retaddr;
  printf(1, "addr_signum = %d\n", addr_signum);
  printf(1, "addr_retaddr = %d\n", addr_retaddr);
  printf(1, "retaddr = %d\n", *retaddr);
  *retaddr += 4;
  __asm__("movl $0x0,%ecx\n\t");
}


x86 assembly (clang, -O0):
<handle_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x4(%rbp),%rax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x18,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
lea    0xebf(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
lea    0xebb(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    $0x1,%edi
lea    0xea7(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x4,%ecx
mov    %ecx,(%rax)
mov    $0x0,%ecx
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_signal>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,0xc(%rsp)
lea    0xc(%rsp),%rdx
lea    0x18(%rdx),%ebx
lea    0xed8(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xed7(%rip),%rsi
mov    $0x1,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rbx),%edx
lea    0xec5(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <printf@plt>
addl   $0x4,(%rbx)
mov    $0x0,%ecx
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"addr_signum = %d\0A\00"
@.str.1 = [19 x i8] c"addr_retaddr = %d\0A\00"
@.str.2 = [14 x i8] c"retaddr = %d\0A\00"
define void @handle_signal(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 %0, i32* %2
%6 = ptrtoint i32* %2 to i32
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = add i32 %7, 24
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = zext i32 %9 to i64
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %5
%12 = load i32, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %4
%15 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32*, i32** %5
%17 = load i32, i32* %16
%18 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
%21 = add i32 %20, 4
store i32 %21, i32* %19
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"addr_signum = %d\0A\00"
@.str.1 = [19 x i8] c"addr_retaddr = %d\0A\00"
@.str.2 = [14 x i8] c"retaddr = %d\0A\00"
define void @handle_signal(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = ptrtoint i32* %2 to i64
%4 = trunc i64 %3 to i32
%5 = add i32 %4, 24
%6 = zext i32 %5 to i64
%7 = inttoptr i64 %6 to i32*
%8 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%9 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%10 = load i32, i32* %7
%11 = call i32 (i8*, ...) @printf(i8* inttoptr (i64 1 to i8*), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %7
%13 = add i32 %12, 4
store i32 %13, i32* %7
ret void
}