#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 nh_print_trace(FILE *out, const char *file, int line) {
  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:%d:\n", file, line);
  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):
<nh_print_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x360,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x64,-0x20(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
lea    -0x360(%rbp),%rdi
mov    $0x64,%esi
call   1050 <backtrace@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    -0x360(%rbp),%rdi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x1,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11fc <nh_print_trace+0xac>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    11bf <nh_print_trace+0x6f>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
add    $0x360,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nh_print_trace>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x320,%rsp
mov    %edx,%r12d
mov    %rsi,%rbp
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,%rbx
lea    0xe70(%rip),%rsi
mov    %r14,%rdi
mov    %rbp,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x2,%r15
jb     11da <nh_print_trace+0x8a>
mov    $0x1,%ebp
lea    0xe66(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rbx,%rbp,8),%rdx
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11c0 <nh_print_trace+0x70>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
add    $0x320,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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