#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void print_stack(void) {
  int j, nptrs;
  void *buffer[100];
  char **strings;
  nptrs = backtrace(buffer, 100);
  printf("backtrace() returned %d addresses\n", nptrs);
  strings = backtrace_symbols(buffer, nptrs);
  if (strings == NULL) {
    perror("backtrace_symbols");
    return;
  }
  for (j = 0; j < nptrs; j++)
    printf("== %s\n", strings[j]);
  free(strings);
}


x86 assembly (clang, -O0):
<print_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
lea    -0x330(%rbp),%rdi
mov    $0x64,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x330(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x338(%rbp)
cmpq   $0x0,-0x338(%rbp)
jne    11b5 <print_stack+0x65>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
jmp    11ff <print_stack+0xaf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11f3 <print_stack+0xa3>
mov    -0x338(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <print_stack+0x6c>
mov    -0x338(%rbp),%rdi
call   1030 <free@plt>
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_stack>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x328,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <backtrace@plt>
mov    %eax,%ebp
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <backtrace_symbols@plt>
test   %rax,%rax
je     11cd <print_stack+0x7d>
mov    %rax,%r15
test   %ebp,%ebp
jle    11b7 <print_stack+0x67>
mov    %ebp,%ebp
lea    0xe97(%rip),%r14
xor    %ebx,%ebx
mov    (%r15,%rbx,8),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11a0 <print_stack+0x50>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x328,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
jmp    11bf <print_stack+0x6f>

LLVM IR (-O0):
@.str = [35 x i8] c"backtrace() returned %d addresses\0A\00"
@.str.1 = [18 x i8] c"backtrace_symbols\00"
@.str.2 = [7 x i8] c"== %s\0A\00"
define void @print_stack() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8*]
%4 = alloca i8**
%5 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 0
%6 = call i32 @backtrace(i8** %5, i32 100)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i8** @backtrace_symbols(i8** %9, i32 %10)
store i8** %11, i8*** %4
%12 = load i8**, i8*** %4
%13 = icmp eq i8** %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %34
16: ; preds = %0
store i32 0, i32* %1
br label %17
17: ; preds = %28, %16
%18 = load i32, i32* %1
%19 = load i32, i32* %2
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i8**, i8*** %4
%23 = load i32, i32* %1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %21
%29 = load i32, i32* %1
%30 = add nsw i32 %29, 1
store i32 %30, i32* %1
br label %17
31: ; preds = %17
%32 = load i8**, i8*** %4
%33 = call i32 @free(i8** %32)
br label %34
34: ; preds = %31, %14
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"backtrace() returned %d addresses\0A\00"
@.str.1 = [18 x i8] c"backtrace_symbols\00"
@.str.2 = [7 x i8] c"== %s\0A\00"
define void @print_stack() {
%1 = alloca [100 x i8*]
%2 = bitcast [100 x i8*]* %1 to i8*
%3 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 0
%4 = call i32 @backtrace(i8** %3, i32 100)
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i8** @backtrace_symbols(i8** %3, i32 %4)
%7 = icmp eq i8** %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = icmp sgt i32 %4, 0
br i1 %9, label %10, label %21
10: ; preds = %8
%11 = zext i32 %4 to i64
br label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %23
14: ; preds = %10, %14
%15 = phi i64 [ 0, %10 ], [ %19, %14 ]
%16 = getelementptr inbounds i8*, i8** %6, i64 %15
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = add nuw nsw i64 %15, 1
%20 = icmp eq i64 %19, %11
br i1 %20, label %21, label %14
21: ; preds = %14, %8
%22 = call i32 @free(i8** %6)
br label %23
23: ; preds = %21, %12
ret void
}