#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*/ FILE;

/* Variables and functions */
size_t backtrace(void **, size_t const);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ free(char **);
void print_trace(FILE *out, const char *function) {
  const size_t max_depth = 100;
  size_t stack_depth;
  void *stack_addrs[max_depth];
  char **stack_strings;
  stack_depth = backtrace(stack_addrs, max_depth);
  stack_strings = backtrace_symbols(stack_addrs, stack_depth);
  fprintf(out, "Call stack from %s:\n", function);
  size_t i;
  for (i = 1; i < stack_depth; i++) {
    fprintf(out, "    %s\n", stack_strings[i]);
  }
  free(stack_strings);
  fflush(out);
}


x86 assembly (clang, -O0):
<print_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x360,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x64,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
lea    -0x360(%rbp),%rdi
mov    $0x64,%esi
call   1050 <backtrace@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    -0x360(%rbp),%rdi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe56(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11f2 <print_trace+0xa2>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11b7 <print_trace+0x67>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
add    $0x360,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_trace>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x320,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsp,%rbx
mov    $0x64,%esi
mov    %rbx,%rdi
call   1050 <backtrace@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,%r13
lea    0xe72(%rip),%rsi
mov    %r14,%rdi
mov    %r12,%rdx
call   1060 <fprintf@plt>
cmp    $0x2,%r15
jb     11c9 <print_trace+0x79>
mov    $0x1,%ebx
lea    0xe6a(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13,%rbx,8),%rdx
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11b0 <print_trace+0x60>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
add    $0x320,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Call stack from %s:\0A\00"
@.str.1 = [8 x i8] c" %s\0A\00"
define void @print_trace(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i64
store i32* %0, i32** %3
store i8* %1, i8** %4
store i64 100, i64* %5
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %7
%11 = alloca i8*, i64 100
%12 = call i64 @backtrace(i8** %11, i64 100)
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = call i8** @backtrace_symbols(i8** %11, i64 %13)
store i8** %14, i8*** %8
%15 = load i32*, i32** %3
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %16)
store i64 1, i64* %9
br label %18
18: ; preds = %29, %2
%19 = load i64, i64* %9
%20 = load i64, i64* %6
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %32
22: ; preds = %18
%23 = load i32*, i32** %3
%24 = load i8**, i8*** %8
%25 = load i64, i64* %9
%26 = getelementptr inbounds i8*, i8** %24, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %22
%30 = load i64, i64* %9
%31 = add i64 %30, 1
store i64 %31, i64* %9
br label %18
32: ; preds = %18
%33 = load i8**, i8*** %8
%34 = call i32 @free(i8** %33)
%35 = load i32*, i32** %3
%36 = call i32 @fflush(i32* %35)
%37 = load i8*, i8** %7
ret void
}

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