#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 stack_trace() {
  void *trace[16];
  char **messages = (char **)NULL;
  int i, trace_size = 0;
  trace_size = backtrace(trace, 16);
  messages = backtrace_symbols(trace, trace_size);
  printf("[stack trace]>>>\n");
  for (i = 0; i < trace_size; i++)
    printf("%s\n", messages[i]);
  printf("<<<[stack trace]\n");
  free(messages);
}


x86 assembly (clang, -O0):
<stack_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movq   $0x0,-0x88(%rbp)
movl   $0x0,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x10,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x90(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x88(%rbp)
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x90(%rbp),%eax
jge    11e8 <stack_trace+0xa8>
mov    -0x88(%rbp),%rax
movslq -0x8c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    11a2 <stack_trace+0x62>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stack_trace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%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>
mov    %rax,%r14
lea    0xe8f(%rip),%rdi
call   1040 <puts@plt>
test   %ebp,%ebp
jle    1192 <stack_trace+0x52>
mov    %ebp,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
mov    (%r14,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1180 <stack_trace+0x40>
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@str = [17 x i8] c"[stack trace]>>>\00"
@str.3 = [17 x i8] c"<<<[stack trace]\00"
define void @stack_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 i8** @backtrace_symbols(i8** %3, i32 %4)
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%7 = icmp sgt i32 %4, 0
br i1 %7, label %8, label %17
8: ; preds = %0
%9 = zext i32 %4 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %15, %10 ]
%12 = getelementptr inbounds i8*, i8** %5, i64 %11
%13 = load i8*, i8** %12
%14 = call i32 @puts(i8* dereferenceable(1) %13)
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %9
br i1 %16, label %17, label %10
17: ; preds = %10, %0
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.3, i64 0, i64 0))
%19 = call i32 @free(i8** %5)
ret void
}