#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*/ printf(char *, ...);
void print_backtrace(void) {
  void *buffer[1024];
  int size = backtrace(buffer, 1024);
  char **strings = backtrace_symbols(buffer, size);
  printf("%d\n", size);
  for (int i = 0; i < size; i++)
    printf("%s\n", 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    -0x2004(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x2010(%rbp)
mov    -0x2004(%rbp),%esi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x2014(%rbp)
mov    -0x2014(%rbp),%eax
cmp    -0x2004(%rbp),%eax
jge    11df <print_backtrace+0x9f>
mov    -0x2010(%rbp),%rax
movslq -0x2014(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2014(%rbp)
jmp    1199 <print_backtrace+0x59>
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    %eax,%esi
call   1050 <backtrace_symbols@plt>
mov    %rax,%r14
lea    0xe7f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebp,%ebp
jle    11b2 <print_backtrace+0x62>
mov    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11a0 <print_backtrace+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [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 = load i32, i32* %2
%9 = call i8** @backtrace_symbols(i8** %7, i32 %8)
store i8** %9, i8*** %3
%10 = load i32, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 0, i32* %4
br label %12
12: ; preds = %23, %0
%13 = load i32, i32* %4
%14 = load i32, i32* %2
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load i8**, i8*** %3
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8*, i8** %17, i64 %19
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %16
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %12
26: ; preds = %12
%27 = load i8**, i8*** %3
%28 = call i32 @free(i8** %27)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\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 %4)
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4)
%7 = icmp sgt i32 %4, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = zext i32 %4 to i64
br label %12
10: ; preds = %12, %0
%11 = call i32 @free(i8** %5)
ret void
12: ; preds = %8, %12
%13 = phi i64 [ 0, %8 ], [ %17, %12 ]
%14 = getelementptr inbounds i8*, i8** %5, i64 %13
%15 = load i8*, i8** %14
%16 = call i32 @puts(i8* dereferenceable(1) %15)
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, %9
br i1 %18, label %10, label %12
}