#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*/ free(char **);
int /*<<< orphan*/ printf(char *, ...);
void print_calltrace(void) {
  void *callstack[128];
  int i = 0;
  int num = 0;
  printf("----CallTrace Begin:\n");
  num = backtrace(callstack, 128);
  char **strs = backtrace_symbols(callstack, num);
  for (i = 0; i < num; i++) {
    printf("----CallTrace %d : %s\n", i, strs[i]);
  }
  free(strs);
}


x86 assembly (clang, -O0):
<print_calltrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x404(%rbp)
movl   $0x0,-0x408(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x400(%rbp),%rdi
mov    $0x80,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x408(%rbp)
lea    -0x400(%rbp),%rdi
mov    -0x408(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x410(%rbp)
movl   $0x0,-0x404(%rbp)
mov    -0x404(%rbp),%eax
cmp    -0x408(%rbp),%eax
jge    11f3 <print_calltrace+0xb3>
mov    -0x404(%rbp),%esi
mov    -0x410(%rbp),%rax
movslq -0x404(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x404(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x404(%rbp)
jmp    11a7 <print_calltrace+0x67>
mov    -0x410(%rbp),%rdi
call   1030 <free@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_calltrace>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
lea    0xeb2(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x80,%esi
call   1060 <backtrace@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <backtrace_symbols@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    11b9 <print_calltrace+0x69>
mov    %ebx,%r12d
lea    0xe69(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11a0 <print_calltrace+0x50>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str.1 = [23 x i8] c"----CallTrace %d : %s\0A\00"
@str = [21 x i8] c"----CallTrace Begin:\00"
define void @print_calltrace() {
%1 = alloca [128 x i8*]
%2 = bitcast [128 x i8*]* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%4 = getelementptr inbounds [128 x i8*], [128 x i8*]* %1, i64 0, i64 0
%5 = call i32 @backtrace(i8** %4, i32 128)
%6 = call i8** @backtrace_symbols(i8** %4, i32 %5)
%7 = icmp sgt i32 %5, 0
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = zext i32 %5 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %16, %10 ]
%12 = getelementptr inbounds i8*, i8** %6, i64 %11
%13 = load i8*, i8** %12
%14 = trunc i64 %11 to i32
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %14, i8* %13)
%16 = add nuw nsw i64 %11, 1
%17 = icmp eq i64 %16, %9
br i1 %17, label %18, label %10
18: ; preds = %10, %0
%19 = call i32 @free(i8** %6)
ret void
}