#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*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
void stacktrace(char *msg) {
  void *trace[30];
  char **traceline = (char **)NULL;
  int i, trace_size = 0;
  trace_size = backtrace(trace, 30);
  traceline = backtrace_symbols(trace, trace_size);
  fprintf(stderr, "%s\n", msg);
  fprintf(stderr, "Stack trace:\n");
  for (i = 0; i < trace_size; ++i)
    printf("- %s\n", traceline[i]);
}


x86 assembly (clang, -O0):
<stacktrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x108(%rbp)
movl   $0x0,-0x110(%rbp)
lea    -0x100(%rbp),%rdi
mov    $0x1e,%esi
call   1040 <backtrace@plt>
mov    %eax,-0x110(%rbp)
lea    -0x100(%rbp),%rdi
mov    -0x110(%rbp),%esi
call   1030 <backtrace_symbols@plt>
mov    %rax,-0x108(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x10c(%rbp)
mov    -0x10c(%rbp),%eax
cmp    -0x110(%rbp),%eax
jge    1214 <stacktrace+0xd4>
mov    -0x108(%rbp),%rax
movslq -0x10c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10c(%rbp)
jmp    11d0 <stacktrace+0x90>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stacktrace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xf8,%rsp
mov    %rdi,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1e,%esi
call   1040 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <backtrace_symbols@plt>
mov    %rax,%r15
mov    0x2e6a(%rip),%rbx
mov    (%rbx),%edi
lea    0xe91(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe6e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %ebp,%ebp
jle    11c5 <stacktrace+0x85>
mov    %ebp,%ebp
xor    %ebx,%ebx
lea    0xe66(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,8),%rsi
mov    %r14,%rdi
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11b0 <stacktrace+0x70>
add    $0xf8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [14 x i8] c"Stack trace:\0A\00"
@.str.2 = [6 x i8] c"- %s\0A\00"
define void @stacktrace(i8* %0) {
%2 = alloca [30 x i8*]
%3 = bitcast [30 x i8*]* %2 to i8*
%4 = getelementptr inbounds [30 x i8*], [30 x i8*]* %2, i64 0, i64 0
%5 = call i32 @backtrace(i8** %4, i32 30)
%6 = call i8** @backtrace_symbols(i8** %4, i32 %5)
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp sgt i32 %5, 0
br i1 %11, label %12, label %21
12: ; preds = %1
%13 = zext i32 %5 to i64
br label %14
14: ; preds = %12, %14
%15 = phi i64 [ 0, %12 ], [ %19, %14 ]
%16 = getelementptr inbounds i8*, i8** %6, i64 %15
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = add nuw nsw i64 %15, 1
%20 = icmp eq i64 %19, %13
br i1 %20, label %21, label %14
21: ; preds = %14, %1
ret void
}