#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);
scalar_t__ backtrace_symbols(void **, int);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ printf(char *, ...);
void dump_trace() {
  void *stack_trace[16] = {0};
  char **stack_strings = NULL;
  int stack_depth = 0;
  int i = 0;
  stack_depth = backtrace(stack_trace, 16);
  stack_strings = (char **)backtrace_symbols(stack_trace, stack_depth);
  if (NULL == stack_strings) {
    printf(" Memory is not enough while dump Stack Trace! \r\n");
    return;
  }
  printf(" Stack Trace: \r\n");
  for (i = 0; i < stack_depth; ++i) {
    printf(" [%d] %s \r\n", i, stack_strings[i]);
  }
  free(stack_strings);
  stack_strings = NULL;
  return;
}


x86 assembly (clang, -O0):
<dump_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
movq   $0x0,-0x88(%rbp)
movl   $0x0,-0x8c(%rbp)
movl   $0x0,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x10,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x8c(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x8c(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x88(%rbp)
xor    %eax,%eax
cmp    -0x88(%rbp),%rax
jne    11d6 <dump_trace+0x86>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1251 <dump_trace+0x101>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x90(%rbp)
mov    -0x90(%rbp),%eax
cmp    -0x8c(%rbp),%eax
jge    123a <dump_trace+0xea>
mov    -0x90(%rbp),%esi
mov    -0x88(%rbp),%rax
movslq -0x90(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x90(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x90(%rbp)
jmp    11ee <dump_trace+0x9e>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x88(%rbp)
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_trace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1060 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <backtrace_symbols@plt>
test   %rax,%rax
je     11f3 <dump_trace+0xa3>
mov    %rax,%r15
lea    0xe5a(%rip),%rdi
call   1040 <puts@plt>
test   %ebp,%ebp
jle    11e9 <dump_trace+0x99>
mov    %ebp,%ebx
lea    0xe3c(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%r15,%rbp,8),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11d0 <dump_trace+0x80>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11ff <dump_trace+0xaf>
lea    0xe22(%rip),%rdi
call   1040 <puts@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c" Memory is not enough while dump Stack Trace! \0D\0A\00"
@.str.1 = [17 x i8] c" Stack Trace: \0D\0A\00"
@.str.2 = [12 x i8] c" [%d] %s \0D\0A\00"
define void @dump_trace() {
%1 = alloca [16 x i8*]
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
%5 = bitcast [16 x i8*]* %1 to i8*
store i8** null, i8*** %2
store i32 0, i32* %3
store i32 0, i32* %4
%6 = getelementptr inbounds [16 x i8*], [16 x i8*]* %1, i64 0, i64 0
%7 = call i32 @backtrace(i8** %6, i32 16)
store i32 %7, i32* %3
%8 = getelementptr inbounds [16 x i8*], [16 x i8*]* %1, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i64 @backtrace_symbols(i8** %8, i32 %9)
%11 = inttoptr i64 %10 to i8**
store i8** %11, i8*** %2
%12 = load i8**, i8*** %2
%13 = icmp eq i8** null, %12
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %36
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %18
18: ; preds = %30, %16
%19 = load i32, i32* %4
%20 = load i32, i32* %3
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load i32, i32* %4
%24 = load i8**, i8*** %2
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %23, i8* %28)
br label %30
30: ; preds = %22
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %18
33: ; preds = %18
%34 = load i8**, i8*** %2
%35 = call i32 @free(i8** %34)
store i8** null, i8*** %2
br label %36
36: ; preds = %33, %14
ret void
}

LLVM IR (-O2):
@.str.2 = [12 x i8] c" [%d] %s \0D\0A\00"
@str = [16 x i8] c" Stack Trace: \0D\00"
@str.3 = [48 x i8] c" Memory is not enough while dump Stack Trace! \0D\00"
define void @dump_trace() {
%1 = alloca [16 x i8*]
%2 = bitcast [16 x i8*]* %1 to i8*
%3 = getelementptr inbounds [16 x i8*], [16 x i8*]* %1, i64 0, i64 0
%4 = call i32 @backtrace(i8** %3, i32 16)
%5 = call i64 @backtrace_symbols(i8** %3, i32 %4)
%6 = inttoptr i64 %5 to i8**
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.3, i64 0, i64 0))
br label %25
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%12 = icmp sgt i32 %4, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = zext i32 %4 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %21, %15 ]
%17 = getelementptr inbounds i8*, i8** %6, i64 %16
%18 = load i8*, i8** %17
%19 = trunc i64 %16 to i32
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %19, i8* %18)
%21 = add nuw nsw i64 %16, 1
%22 = icmp eq i64 %21, %14
br i1 %22, label %23, label %15
23: ; preds = %15, %10
%24 = call i32 @free(i8** %6)
br label %25
25: ; preds = %23, %8
ret void
}