#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 */
size_t backtrace(void **, int);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ stderr;
void print_stacktrace(void) {
  void *arr[10];
  size_t sz;
  char **strings;
  size_t i;
  sz = backtrace(arr, 10);
  strings = backtrace_symbols(arr, sz);
  for (i = 1; i < sz; i++)
    fprintf(stderr, "  Frame %d: %s\n", (int)i, strings[i]);
  fflush(stderr);
  free(strings);
}


x86 assembly (clang, -O0):
<print_stacktrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    -0x50(%rbp),%rdi
mov    $0xa,%esi
call   1050 <backtrace@plt>
mov    %rax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x60(%rbp)
movq   $0x1,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    -0x58(%rbp),%rax
jae    11c9 <print_stacktrace+0x79>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rax
mov    %eax,%edx
mov    -0x60(%rbp),%rax
mov    -0x68(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe4d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x68(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x68(%rbp)
jmp    1183 <print_stacktrace+0x33>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_stacktrace>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <backtrace@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,%r14
cmp    $0x2,%r15
jb     11bb <print_stacktrace+0x6b>
mov    $0x1,%ebx
mov    0x2e50(%rip),%r13
lea    0xe69(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    (%r14,%rbx,8),%rcx
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11a0 <print_stacktrace+0x50>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c" Frame %d: %s\0A\00"
define void @print_stacktrace() {
%1 = alloca [10 x i8*]
%2 = alloca i64
%3 = alloca i8**
%4 = alloca i64
%5 = getelementptr inbounds [10 x i8*], [10 x i8*]* %1, i64 0, i64 0
%6 = call i64 @backtrace(i8** %5, i32 10)
store i64 %6, i64* %2
%7 = getelementptr inbounds [10 x i8*], [10 x i8*]* %1, i64 0, i64 0
%8 = load i64, i64* %2
%9 = call i8** @backtrace_symbols(i8** %7, i64 %8)
store i8** %9, i8*** %3
store i64 1, i64* %4
br label %10
10: ; preds = %23, %0
%11 = load i64, i64* %4
%12 = load i64, i64* %2
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = load i64, i64* %4
%17 = trunc i64 %16 to i32
%18 = load i8**, i8*** %3
%19 = load i64, i64* %4
%20 = getelementptr inbounds i8*, i8** %18, i64 %19
%21 = load i8*, i8** %20
%22 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17, i8* %21)
br label %23
23: ; preds = %14
%24 = load i64, i64* %4
%25 = add i64 %24, 1
store i64 %25, i64* %4
br label %10
26: ; preds = %10
%27 = load i32, i32* @stderr
%28 = call i32 @fflush(i32 %27)
%29 = load i8**, i8*** %3
%30 = call i32 @free(i8** %29)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c" Frame %d: %s\0A\00"
define void @print_stacktrace() {
%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 i64 @backtrace(i8** %3, i32 10)
%5 = call i8** @backtrace_symbols(i8** %3, i64 %4)
%6 = icmp ugt i64 %4, 1
br i1 %6, label %7, label %16
7: ; preds = %0, %7
%8 = phi i64 [ %14, %7 ], [ 1, %0 ]
%9 = load i32, i32* @stderr
%10 = trunc i64 %8 to i32
%11 = getelementptr inbounds i8*, i8** %5, i64 %8
%12 = load i8*, i8** %11
%13 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %12)
%14 = add nuw i64 %8, 1
%15 = icmp eq i64 %14, %4
br i1 %15, label %16, label %7
16: ; preds = %7, %0
%17 = load i32, i32* @stderr
%18 = call i32 @fflush(i32 %17)
%19 = call i32 @free(i8** %5)
ret void
}