#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 */
typedef int /*<<< orphan*/ addrlist;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned int backtrace(void **, int);
char **backtrace_symbols(void **, unsigned int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(char **);
void printStackTrace(FILE *out, unsigned int max_frames) {
  fprintf(out, "stack trace:\n");
  void *addrlist[max_frames + 1];
  unsigned int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void *));
  if (addrlen == 0) {
    fprintf(out, "  \n");
    return;
  }
  char **symbollist = backtrace_symbols(addrlist, addrlen);
  unsigned int i;
  for (i = 4; i < addrlen; i++)
    fprintf(out, "%s\n", symbollist[i]);
  free(symbollist);
}


x86 assembly (clang, -O0):
<printStackTrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,8),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
shl    $0x3,%rax
shr    $0x3,%rax
mov    %eax,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11c5 <printStackTrace+0x85>
mov    -0x8(%rbp),%rdi
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x28(%rbp)
jmp    1223 <printStackTrace+0xe3>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jae    1213 <printStackTrace+0xd3>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    (%rax,%rcx,8),%rdx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11dc <printStackTrace+0x9c>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printStackTrace>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xe95(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
add    $0x1,%r15d
mov    %rsp,%rbx
lea    0xf(,%r15,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <backtrace@plt>
test   %eax,%eax
je     11f4 <printStackTrace+0xa4>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,%r12
cmp    $0x5,%r15d
jb     11ea <printStackTrace+0x9a>
mov    %r15d,%r13d
mov    $0x4,%ebx
lea    0xe48(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r12,%rbx,8),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11d0 <printStackTrace+0x80>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    120d <printStackTrace+0xbd>
lea    0xe13(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"stack trace:\0A\00"
@.str.1 = [4 x i8] c" \0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @printStackTrace(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%11 = load i32*, i32** %3
%12 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %4
%14 = add i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %5
%17 = alloca i8*, i64 %15
store i64 %15, i64* %6
%18 = mul nuw i64 8, %15
%19 = udiv i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = call i32 @backtrace(i8** %17, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = load i32*, i32** %3
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %8
br label %48
27: ; preds = %2
%28 = load i32, i32* %7
%29 = call i8** @backtrace_symbols(i8** %17, i32 %28)
store i8** %29, i8*** %9
store i32 4, i32* %10
br label %30
30: ; preds = %42, %27
%31 = load i32, i32* %10
%32 = load i32, i32* %7
%33 = icmp ult i32 %31, %32
br i1 %33, label %34, label %45
34: ; preds = %30
%35 = load i32*, i32** %3
%36 = load i8**, i8*** %9
%37 = load i32, i32* %10
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %34
%43 = load i32, i32* %10
%44 = add i32 %43, 1
store i32 %44, i32* %10
br label %30
45: ; preds = %30
%46 = load i8**, i8*** %9
%47 = call i32 @free(i8** %46)
store i32 0, i32* %8
br label %48
48: ; preds = %45, %24
%49 = load i8*, i8** %5
%50 = load i32, i32* %8
switch i32 %50, label %52 [
i32 0, label %51
i32 1, label %51
]
51: ; preds = %48, %48
ret void
52: ; preds = %48
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"stack trace:\0A\00"
@.str.1 = [4 x i8] c" \0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @printStackTrace(i32* nocapture %0, i32 %1) {
%3 = tail call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 13, i64 1, i32* %0)
%4 = add i32 %1, 1
%5 = zext i32 %4 to i64
%6 = alloca i8*, i64 %5
%7 = call i32 @backtrace(i8** %6, i32 %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 3, i64 1, i32* %0)
br label %25
11: ; preds = %2
%12 = call i8** @backtrace_symbols(i8** %6, i32 %7)
%13 = icmp ugt i32 %7, 4
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = zext i32 %7 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 4, %14 ], [ %21, %16 ]
%18 = getelementptr inbounds i8*, i8** %12, i64 %17
%19 = load i8*, i8** %18
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = add nuw nsw i64 %17, 1
%22 = icmp eq i64 %21, %15
br i1 %22, label %23, label %16
23: ; preds = %16, %11
%24 = call i32 @free(i8** %12)
br label %25
25: ; preds = %23, %9
ret void
}