#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int backtrace(void **, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, void *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void stack_trace(int skip) {
  void *trace[32];
  int size = backtrace(trace, 32);
  char exe[128];
  snprintf(exe, 128, "/proc/%d/exe", getpid());
  pid_t child = fork();
  if (child == 0) {
    char *cmd[36];
    cmd[0] = "addr2line";
    cmd[1] = "-e";
    cmd[2] = exe;
    char addrs[64][32];
    int i;
    for (i = 0; i < size - skip; i++) {
      snprintf(addrs[i], 64, "%p", trace[i + skip]);
      cmd[3 + i] = addrs[i];
    }
    cmd[3 + size - skip] = NULL;
    execvp("addr2line", cmd);
    perror("addr2line");
    exit(1);
  }
  int status;
  waitpid(child, &status, 0);
  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
    fprintf(stderr, "raw trace:");
    for (int i = skip; i < size; i++) {
      fprintf(stderr, " %p", (char *)trace[i] - 1);
    }
    fprintf(stderr, "\n");
  }
}


x86 assembly (clang, -O0):
<stack_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0xaf0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0x20,%esi
call   1040 <backtrace@plt>
mov    %eax,-0x114(%rbp)
lea    -0x1a0(%rbp),%rax
mov    %rax,-0xae8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xae8(%rbp),%rdi
movslq %eax,%rcx
mov    $0x80,%esi
lea    0xe00(%rip),%rdx
call   1050 <snprintf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x1a8(%rbp)
cmpq   $0x0,-0x1a8(%rbp)
jne    1324 <stack_trace+0x174>
lea    0xde5(%rip),%rax
mov    %rax,-0x2d0(%rbp)
lea    0xde1(%rip),%rax
mov    %rax,-0x2c8(%rbp)
lea    -0x1a0(%rbp),%rax
mov    %rax,-0x2c0(%rbp)
movl   $0x0,-0xad4(%rbp)
mov    -0xad4(%rbp),%eax
mov    -0x114(%rbp),%ecx
sub    -0x4(%rbp),%ecx
cmp    %ecx,%eax
jge    12e1 <stack_trace+0x131>
movslq -0xad4(%rbp),%rax
lea    -0xad0(%rbp),%rdi
shl    $0x5,%rax
add    %rax,%rdi
mov    -0xad4(%rbp),%eax
add    -0x4(%rbp),%eax
cltq
mov    -0x110(%rbp,%rax,8),%rcx
ff
mov    $0x40,%esi
lea    0xd86(%rip),%rdx
call   1050 <snprintf@plt>
movslq -0xad4(%rbp),%rax
lea    -0xad0(%rbp),%rcx
shl    $0x5,%rax
add    %rax,%rcx
mov    -0xad4(%rbp),%eax
add    $0x3,%eax
cltq
mov    %rcx,-0x2d0(%rbp,%rax,8)
ff
mov    -0xad4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xad4(%rbp)
jmp    1255 <stack_trace+0xa5>
mov    -0x114(%rbp),%eax
add    $0x3,%eax
sub    -0x4(%rbp),%eax
cltq
movq   $0x0,-0x2d0(%rbp,%rax,8)
lea    -0x2d0(%rbp),%rsi
lea    0xd04(%rip),%rdi
call   10a0 <execvp@plt>
lea    0xcf8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1a8(%rbp),%rdi
lea    -0xad8(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0xad8(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1362 <stack_trace+0x1b2>
mov    -0xad8(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     13e9 <stack_trace+0x239>
mov    0x2c77(%rip),%rax
mov    (%rax),%edi
lea    0xca8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0xadc(%rbp)
mov    -0xadc(%rbp),%eax
cmp    -0x114(%rbp),%eax
jge    13d2 <stack_trace+0x222>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
movslq -0xadc(%rbp),%rax
mov    -0x110(%rbp,%rax,8),%rdx
ff
add    $0xffffffffffffffff,%rdx
lea    0xc6e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xadc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xadc(%rbp)
jmp    1382 <stack_trace+0x1d2>
mov    0x2c07(%rip),%rax
mov    (%rax),%edi
lea    0xc47(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0xaf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stack_trace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xaa8,%rsp
mov    %edi,%r14d
lea    0x120(%rsp),%rdi
mov    $0x20,%esi
call   1040 <backtrace@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
movslq %eax,%rcx
lea    0xe16(%rip),%rdx
lea    0x220(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x80,%esi
call   1050 <snprintf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     12c3 <stack_trace+0x113>
lea    0x2a0(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x2a0(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
je     1242 <stack_trace+0x92>
mov    0x2a0(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %rax,%rax
je     12b1 <stack_trace+0x101>
mov    0x2d97(%rip),%r12
mov    (%r12),%edi
lea    0xdc6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
sub    %r14d,%r15d
jle    129f <stack_trace+0xef>
movslq %r14d,%rax
lea    (%rsp,%rax,8),%rbx
add    $0x120,%rbx
lea    0xdb0(%rip),%r14
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    (%rbx,%rbp,8),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %ebp,%r15d
jne    1280 <stack_trace+0xd0>
mov    (%r12),%edi
lea    0xd7f(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0xaa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd43(%rip),%rax
mov    %rax,(%rsp)
lea    0xd42(%rip),%rax
mov    %rax,0x8(%rsp)
mov    %rbp,0x10(%rsp)
sub    %r14d,%r15d
test   %r15d,%r15d
jle    1341 <stack_trace+0x191>
movslq %r14d,%rax
mov    %r15d,%r12d
lea    (%rsp,%rax,8),%r13
add    $0x120,%r13
neg    %r12
mov    $0x3,%ebx
lea    0x2a0(%rsp),%rbp
lea    0xd17(%rip),%r14
nop
mov    -0x18(%r13,%rbx,8),%rcx
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %r14,%rdx
call   1050 <snprintf@plt>
mov    %ebx,%eax
mov    %rbp,(%rsp,%rax,8)
add    $0x20,%rbp
lea    (%r12,%rbx,1),%rax
add    $0x1,%rax
add    $0x1,%rbx
cmp    $0x3,%rax
jne    1310 <stack_trace+0x160>
add    $0x3,%r15d
movslq %r15d,%rax
movq   $0x0,(%rsp,%rax,8)
lea    0xcb6(%rip),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10a0 <execvp@plt>
mov    %rbx,%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [10 x i8] c"addr2line\00"
@.str.2 = [3 x i8] c"-e\00"
@.str.3 = [3 x i8] c"%p\00"
@stderr = global i32 0
@.str.4 = [11 x i8] c"raw trace:\00"
@.str.5 = [4 x i8] c" %p\00"
@.str.6 = [2 x i8] c"\0A\00"
define void @stack_trace(i32 %0) {
%2 = alloca i32
%3 = alloca [32 x i8*]
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca i64
%7 = alloca [36 x i8*]
%8 = alloca [64 x [32 x i8]]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %2
%12 = getelementptr inbounds [32 x i8*], [32 x i8*]* %3, i64 0, i64 0
%13 = call i32 @backtrace(i8** %12, i32 32)
store i32 %13, i32* %4
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%15 = call i32 (...) @getpid()
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i8*
%18 = call i32 @snprintf(i8* %14, i32 128, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i64 (...) @fork()
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %67
22: ; preds = %1
%23 = getelementptr inbounds [36 x i8*], [36 x i8*]* %7, i64 0, i64 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %23
%24 = getelementptr inbounds [36 x i8*], [36 x i8*]* %7, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %24
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%26 = getelementptr inbounds [36 x i8*], [36 x i8*]* %7, i64 0, i64 2
store i8* %25, i8** %26
store i32 0, i32* %9
br label %27
27: ; preds = %53, %22
%28 = load i32, i32* %9
%29 = load i32, i32* %4
%30 = load i32, i32* %2
%31 = sub nsw i32 %29, %30
%32 = icmp slt i32 %28, %31
br i1 %32, label %33, label %56
33: ; preds = %27
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [64 x [32 x i8]], [64 x [32 x i8]]* %8, i64 0, i64 %35
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %36, i64 0, i64 0
%38 = load i32, i32* %9
%39 = load i32, i32* %2
%40 = add nsw i32 %38, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [32 x i8*], [32 x i8*]* %3, i64 0, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 @snprintf(i8* %37, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [64 x [32 x i8]], [64 x [32 x i8]]* %8, i64 0, i64 %46
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %47, i64 0, i64 0
%49 = load i32, i32* %9
%50 = add nsw i32 3, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [36 x i8*], [36 x i8*]* %7, i64 0, i64 %51
store i8* %48, i8** %52
br label %53
53: ; preds = %33
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %27
56: ; preds = %27
%57 = load i32, i32* %4
%58 = add nsw i32 3, %57
%59 = load i32, i32* %2
%60 = sub nsw i32 %58, %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [36 x i8*], [36 x i8*]* %7, i64 0, i64 %61
store i8* null, i8** %62
%63 = getelementptr inbounds [36 x i8*], [36 x i8*]* %7, i64 0, i64 0
%64 = call i32 @execvp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %63)
%65 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %1
%68 = load i64, i64* %6
%69 = call i32 @waitpid(i64 %68, i32* %10, i32 0)
%70 = load i32, i32* %10
%71 = call i32 @WIFEXITED(i32 %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %67
%74 = load i32, i32* %10
%75 = call i64 @WEXITSTATUS(i32 %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %99
77: ; preds = %73, %67
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%80 = load i32, i32* %2
store i32 %80, i32* %11
br label %81
81: ; preds = %93, %77
%82 = load i32, i32* %11
%83 = load i32, i32* %4
%84 = icmp slt i32 %82, %83
br i1 %84, label %85, label %96
85: ; preds = %81
%86 = load i32, i32* @stderr
%87 = load i32, i32* %11
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [32 x i8*], [32 x i8*]* %3, i64 0, i64 %88
%90 = load i8*, i8** %89
%91 = getelementptr inbounds i8, i8* %90, i64 -1
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %91)
br label %93
93: ; preds = %85
%94 = load i32, i32* %11
%95 = add nsw i32 %94, 1
store i32 %95, i32* %11
br label %81
96: ; preds = %81
%97 = load i32, i32* @stderr
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %99
99: ; preds = %96, %73
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [10 x i8] c"addr2line\00"
@.str.2 = [3 x i8] c"-e\00"
@.str.3 = [3 x i8] c"%p\00"
@stderr = global i32 0
@.str.4 = [11 x i8] c"raw trace:\00"
@.str.5 = [4 x i8] c" %p\00"
@.str.6 = [2 x i8] c"\0A\00"
define void @stack_trace(i32 %0) {
%2 = alloca [32 x i8*]
%3 = alloca [128 x i8]
%4 = alloca [36 x i8*]
%5 = alloca [64 x [32 x i8]]
%6 = alloca i32
%7 = bitcast [32 x i8*]* %2 to i8*
%8 = getelementptr inbounds [32 x i8*], [32 x i8*]* %2, i64 0, i64 0
%9 = call i32 @backtrace(i8** %8, i32 32)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%11 = call i32 (...) @getpid()
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i8*
%14 = call i32 @snprintf(i8* %10, i32 128, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %48
17: ; preds = %1
%18 = bitcast [36 x i8*]* %4 to i8*
%19 = getelementptr inbounds [36 x i8*], [36 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds [36 x i8*], [36 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds [36 x i8*], [36 x i8*]* %4, i64 0, i64 2
store i8* %10, i8** %21
%22 = getelementptr inbounds [64 x [32 x i8]], [64 x [32 x i8]]* %5, i64 0, i64 0, i64 0
%23 = sub i32 %9, %0
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %40
25: ; preds = %17
%26 = sext i32 %0 to i64
%27 = zext i32 %23 to i64
br label %28
28: ; preds = %25, %28
%29 = phi i64 [ 0, %25 ], [ %38, %28 ]
%30 = getelementptr inbounds [64 x [32 x i8]], [64 x [32 x i8]]* %5, i64 0, i64 %29, i64 0
%31 = add nsw i64 %29, %26
%32 = getelementptr inbounds [32 x i8*], [32 x i8*]* %2, i64 0, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %30, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = add nuw i64 %29, 3
%36 = and i64 %35, 4294967295
%37 = getelementptr inbounds [36 x i8*], [36 x i8*]* %4, i64 0, i64 %36
store i8* %30, i8** %37
%38 = add nuw nsw i64 %29, 1
%39 = icmp eq i64 %38, %27
br i1 %39, label %40, label %28
40: ; preds = %28, %17
%41 = sub i32 3, %0
%42 = add i32 %41, %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [36 x i8*], [36 x i8*]* %4, i64 0, i64 %43
store i8* null, i8** %44
%45 = call i32 @execvp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %19)
%46 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %1
%49 = bitcast i32* %6 to i8*
%50 = call i32 @waitpid(i64 %15, i32* %6, i32 0)
%51 = load i32, i32* %6
%52 = call i32 @WIFEXITED(i32 %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %48
%55 = load i32, i32* %6
%56 = call i64 @WEXITSTATUS(i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %77, label %58
58: ; preds = %54, %48
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%61 = icmp sgt i32 %9, %0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = sext i32 %0 to i64
br label %67
64: ; preds = %67, %58
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %77
67: ; preds = %62, %67
%68 = phi i64 [ %63, %62 ], [ %74, %67 ]
%69 = load i32, i32* @stderr
%70 = getelementptr inbounds [32 x i8*], [32 x i8*]* %2, i64 0, i64 %68
%71 = load i8*, i8** %70
%72 = getelementptr inbounds i8, i8* %71, i64 -1
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %72)
%74 = add nsw i64 %68, 1
%75 = trunc i64 %74 to i32
%76 = icmp eq i32 %9, %75
br i1 %76, label %64, label %67
77: ; preds = %64, %54
ret void
}