#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 /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ __gcov_flush();
size_t backtrace(void **, int);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int);
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void rtpp_stacktrace(int sig) {
  void *array[10];
  size_t size;
  char **strings;
  int i;
  size = backtrace(array, 10);
  strings = backtrace_symbols(array, size);
  fprintf(stderr, "Died on signal %d, obtained %lu stack frames.\n", sig,
          (unsigned long)size);
  for (i = 0; i < size; i++)
    fprintf(stderr, "%s\n", strings[i]);
  fflush(stderr);
  signal(sig, SIG_DFL);
  __gcov_flush();
  kill(getpid(), sig);
}


x86 assembly (clang, -O0):
<rtpp_stacktrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x60(%rbp),%rdi
mov    $0xa,%esi
call   1050 <backtrace@plt>
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
call   1030 <backtrace_symbols@plt>
mov    %rax,-0x70(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
mov    -0x68(%rbp),%rcx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x74(%rbp)
movslq -0x74(%rbp),%rax
cmp    -0x68(%rbp),%rax
jae    1215 <rtpp_stacktrace+0x95>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x70(%rbp),%rax
movslq -0x74(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11d6 <rtpp_stacktrace+0x56>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   10a0 <__gcov_flush@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    -0x4(%rbp),%esi
call   1080 <kill@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtpp_stacktrace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %edi,%r14d
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
call   1050 <backtrace@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1030 <backtrace_symbols@plt>
mov    %rax,%r13
mov    0x2e27(%rip),%r12
mov    (%r12),%edi
lea    0xe3c(%rip),%rsi
mov    %r14d,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %rbx,%rbx
je     11fc <rtpp_stacktrace+0x7c>
lea    0xe52(%rip),%r15
xor    %ebp,%ebp
nop
mov    (%r12),%edi
mov    0x0(%r13,%rbp,8),%rdx
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11e0 <rtpp_stacktrace+0x60>
mov    (%r12),%edi
call   1090 <fflush@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1060 <signal@plt>
xor    %eax,%eax
call   10a0 <__gcov_flush@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
mov    %r14d,%esi
call   1080 <kill@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [47 x i8] c"Died on signal %d, obtained %lu stack frames.\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@SIG_DFL = global i32 0
define void @rtpp_stacktrace(i32 %0) {
%2 = alloca i32
%3 = alloca [10 x i8*]
%4 = alloca i64
%5 = alloca i8**
%6 = alloca i32
store i32 %0, i32* %2
%7 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 0
%8 = call i64 @backtrace(i8** %7, i32 10)
store i64 %8, i64* %4
%9 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 0
%10 = load i64, i64* %4
%11 = call i8** @backtrace_symbols(i8** %9, i64 %10)
store i8** %11, i8*** %5
%12 = load i32, i32* @stderr
%13 = load i32, i32* %2
%14 = load i64, i64* %4
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %13, i64 %14)
store i32 0, i32* %6
br label %16
16: ; preds = %29, %1
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = load i64, i64* %4
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i8**, i8*** %5
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %21
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %16
32: ; preds = %16
%33 = load i32, i32* @stderr
%34 = call i32 @fflush(i32 %33)
%35 = load i32, i32* %2
%36 = load i32, i32* @SIG_DFL
%37 = call i32 @signal(i32 %35, i32 %36)
%38 = call i32 (...) @__gcov_flush()
%39 = call i32 (...) @getpid()
%40 = load i32, i32* %2
%41 = call i32 @kill(i32 %39, i32 %40)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [47 x i8] c"Died on signal %d, obtained %lu stack frames.\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@SIG_DFL = global i32 0
define void @rtpp_stacktrace(i32 %0) {
%2 = alloca [10 x i8*]
%3 = bitcast [10 x i8*]* %2 to i8*
%4 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 0
%5 = call i64 @backtrace(i8** %4, i32 10)
%6 = call i8** @backtrace_symbols(i8** %4, i64 %5)
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %0, i64 %5)
%9 = icmp eq i64 %5, 0
br i1 %9, label %18, label %10
10: ; preds = %1, %10
%11 = phi i64 [ %16, %10 ], [ 0, %1 ]
%12 = load i32, i32* @stderr
%13 = getelementptr inbounds i8*, i8** %6, i64 %11
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = add nuw i64 %11, 1
%17 = icmp eq i64 %16, %5
br i1 %17, label %18, label %10
18: ; preds = %10, %1
%19 = load i32, i32* @stderr
%20 = call i32 @fflush(i32 %19)
%21 = load i32, i32* @SIG_DFL
%22 = call i32 @signal(i32 %0, i32 %21)
%23 = call i32 (...) @__gcov_flush()
%24 = call i32 (...) @getpid()
%25 = call i32 @kill(i32 %24, i32 %0)
ret void
}