#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ stderr;
void print_backtrace(int off) {
  if (off <= 0)
    return;
  void *trace[50];
  int tracelen = backtrace(trace, 50);
  if (tracelen < off) {
    fprintf(stderr, "No backtrace available %i\n", off);
    return;
  }
  char **trace_strings = backtrace_symbols(trace, tracelen);
  if (!trace_strings) {
    fprintf(stderr, "No backtrace available\n");
    return;
  }
  fprintf(stderr, "Backtrace:\n");
  int i;
  for (i = off; i < tracelen; i++) {
    fprintf(stderr, "   %s\n", trace_strings[i]);
  }
  free(trace_strings);
}


x86 assembly (clang, -O0):
<print_backtrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jg     115d <print_backtrace+0x1d>
jmp    1260 <print_backtrace+0x120>
lea    -0x1a0(%rbp),%rdi
mov    $0x32,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x1a4(%rbp)
mov    -0x1a4(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a2 <print_backtrace+0x62>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1260 <print_backtrace+0x120>
lea    -0x1a0(%rbp),%rdi
mov    -0x1a4(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x1b0(%rbp)
cmpq   $0x0,-0x1b0(%rbp)
jne    11e5 <print_backtrace+0xa5>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1260 <print_backtrace+0x120>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1b4(%rbp)
mov    -0x1b4(%rbp),%eax
cmp    -0x1a4(%rbp),%eax
jge    1254 <print_backtrace+0x114>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    -0x1b0(%rbp),%rax
movslq -0x1b4(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b4(%rbp)
jmp    1205 <print_backtrace+0xc5>
mov    -0x1b0(%rbp),%rdi
call   1030 <free@plt>
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_backtrace>:
test   %edi,%edi
jle    1211 <print_backtrace+0xd1>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x190,%rsp
mov    %edi,%ebp
mov    %rsp,%rdi
mov    $0x32,%esi
call   1050 <backtrace@plt>
cmp    %ebp,%eax
jge    1185 <print_backtrace+0x45>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1202 <print_backtrace+0xc2>
mov    %eax,%r14d
mov    %rsp,%rdi
mov    %eax,%esi
call   1040 <backtrace_symbols@plt>
mov    0x2e47(%rip),%r15
mov    (%r15),%edi
test   %rax,%rax
je     11f4 <print_backtrace+0xb4>
mov    %rax,%r12
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    %ebp,%r14d
jle    11ea <print_backtrace+0xaa>
mov    %ebp,%ebp
movslq %r14d,%rbx
lea    0xe7c(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15),%edi
mov    (%r12,%rbp,8),%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11d0 <print_backtrace+0x90>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1202 <print_backtrace+0xc2>
lea    0xe20(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x190,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"No backtrace available %i\0A\00"
@.str.1 = [24 x i8] c"No backtrace available\0A\00"
@.str.2 = [12 x i8] c"Backtrace:\0A\00"
@.str.3 = [7 x i8] c" %s\0A\00"
define void @print_backtrace(i32 %0) {
%2 = alloca i32
%3 = alloca [50 x i8*]
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = icmp sle i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
br label %51
10: ; preds = %1
%11 = getelementptr inbounds [50 x i8*], [50 x i8*]* %3, i64 0, i64 0
%12 = call i32 @backtrace(i8** %11, i32 50)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* %2
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = load i32, i32* %2
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %51
20: ; preds = %10
%21 = getelementptr inbounds [50 x i8*], [50 x i8*]* %3, i64 0, i64 0
%22 = load i32, i32* %4
%23 = call i8** @backtrace_symbols(i8** %21, i32 %22)
store i8** %23, i8*** %5
%24 = load i8**, i8*** %5
%25 = icmp ne i8** %24, null
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %51
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %2
store i32 %32, i32* %6
br label %33
33: ; preds = %45, %29
%34 = load i32, i32* %6
%35 = load i32, i32* %4
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %48
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %37
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %33
48: ; preds = %33
%49 = load i8**, i8*** %5
%50 = call i32 @free(i8** %49)
br label %51
51: ; preds = %48, %26, %16, %9
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"No backtrace available %i\0A\00"
@.str.1 = [24 x i8] c"No backtrace available\0A\00"
@.str.2 = [12 x i8] c"Backtrace:\0A\00"
@.str.3 = [7 x i8] c" %s\0A\00"
define void @print_backtrace(i32 %0) {
%2 = alloca [50 x i8*]
%3 = icmp slt i32 %0, 1
br i1 %3, label %35, label %4
4: ; preds = %1
%5 = bitcast [50 x i8*]* %2 to i8*
%6 = getelementptr inbounds [50 x i8*], [50 x i8*]* %2, i64 0, i64 0
%7 = call i32 @backtrace(i8** %6, i32 50)
%8 = icmp slt i32 %7, %0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %34
12: ; preds = %4
%13 = call i8** @backtrace_symbols(i8** %6, i32 %7)
%14 = icmp eq i8** %13, null
%15 = load i32, i32* @stderr
br i1 %14, label %16, label %18
16: ; preds = %12
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %34
18: ; preds = %12
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp sgt i32 %7, %0
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = zext i32 %0 to i64
%23 = sext i32 %7 to i64
br label %24
24: ; preds = %21, %24
%25 = phi i64 [ %22, %21 ], [ %30, %24 ]
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds i8*, i8** %13, i64 %25
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = add nuw nsw i64 %25, 1
%31 = icmp eq i64 %30, %23
br i1 %31, label %32, label %24
32: ; preds = %24, %18
%33 = call i32 @free(i8** %13)
br label %34
34: ; preds = %16, %32, %9
br label %35
35: ; preds = %1, %34
ret void
}