#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*/ exit(int);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void print_trace() {
  void *array[10];
  int size = 0;
  char **strings = NULL;
  int i = 0;
  size = backtrace(array, 10);
  strings = backtrace_symbols(array, size);
  if (strings == NULL) {
    perror("backtrace_symbols");
    exit(-1);
  }
  printf("Obtained %zd stack frames.\n", size);
  for (i = 0; i < size; i++) {
    printf("%s\n", strings[i]);
  }
  free(strings);
  strings = NULL;
}


x86 assembly (clang, -O0):
<print_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x54(%rbp)
movq   $0x0,-0x60(%rbp)
movl   $0x0,-0x64(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0xa,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    11c0 <print_trace+0x60>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x54(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    120c <print_trace+0xac>
mov    -0x60(%rbp),%rax
movslq -0x64(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    11d8 <print_trace+0x78>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x60(%rbp)
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_trace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1060 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <backtrace_symbols@plt>
test   %rax,%rax
je     11e3 <print_trace+0x73>
mov    %rax,%r14
lea    0xe6f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebp,%ebp
jle    11d2 <print_trace+0x62>
mov    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11c0 <print_trace+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

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

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