#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 /*<<< orphan*/ EXIT_FAILURE;
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void print_backtrace(void) {
  void *buffer[1024];
  int count = backtrace(buffer, 1024);
  char **strings = backtrace_symbols(buffer, 1024);
  if (strings == NULL) {
    perror("backtrace_symboles");
    exit(EXIT_FAILURE);
  }
  printf("Backtrace:\n");
  for (int i = 0; i < count; i++)
    printf("%s\n", (char *)strings[i]);
  free(strings);
}


x86 assembly (clang, -O0):
<print_backtrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
lea    -0x2000(%rbp),%rdi
mov    $0x400,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x2004(%rbp)
lea    -0x2000(%rbp),%rdi
mov    $0x400,%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x2010(%rbp)
cmpq   $0x0,-0x2010(%rbp)
jne    11c2 <print_backtrace+0x62>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x2014(%rbp)
mov    -0x2014(%rbp),%eax
cmp    -0x2004(%rbp),%eax
jge    1220 <print_backtrace+0xc0>
mov    -0x2010(%rbp),%rax
movslq -0x2014(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2014(%rbp)
jmp    11da <print_backtrace+0x7a>
mov    -0x2010(%rbp),%rdi
call   1030 <free@plt>
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_backtrace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <backtrace_symbols@plt>
test   %rax,%rax
je     11d6 <print_backtrace+0x76>
mov    %rax,%r14
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
test   %ebp,%ebp
jle    11c2 <print_backtrace+0x62>
mov    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11b0 <print_backtrace+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"backtrace_symboles\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"Backtrace:\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @print_backtrace() {
%1 = alloca [1024 x i8*]
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %1, i64 0, i64 0
%6 = call i32 @backtrace(i8** %5, i32 1024)
store i32 %6, i32* %2
%7 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %1, i64 0, i64 0
%8 = call i8** @backtrace_symbols(i8** %7, i32 1024)
store i8** %8, i8*** %3
%9 = load i8**, i8*** %3
%10 = icmp eq i8** %9, null
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %17
17: ; preds = %28, %15
%18 = load i32, i32* %4
%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*** %3
%23 = load i32, i32* %4
%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 ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %21
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %17
31: ; preds = %17
%32 = load i8**, i8*** %3
%33 = call i32 @free(i8** %32)
ret void
}

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