#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*/ _exit(int);
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ stderr;
void ert_process_backtrace_handler(int sig) {
  void *array[50];
  fprintf(stderr, "Error: signal %d:\n", sig);
  int size = backtrace(array, 50);
  char **strings = backtrace_symbols(array, size);
  fprintf(stderr, "Obtained %d stack frames:\n", size);
  for (int i = 0; i < size; i++) {
    fprintf(stderr, "  %s\n", strings[i]);
  }
  free(strings);
  _exit(1);
}


x86 assembly (clang, -O0):
<ert_process_backtrace_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x1a0(%rbp),%rdi
mov    $0x32,%esi
call   1060 <backtrace@plt>
mov    %eax,-0x1a4(%rbp)
lea    -0x1a0(%rbp),%rdi
mov    -0x1a4(%rbp),%esi
call   1050 <backtrace_symbols@plt>
mov    %rax,-0x1b0(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x1a4(%rbp),%edx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x1b4(%rbp)
mov    -0x1b4(%rbp),%eax
cmp    -0x1a4(%rbp),%eax
jge    121e <ert_process_backtrace_handler+0xce>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x1b0(%rbp),%rax
movslq -0x1b4(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b4(%rbp)
jmp    11cf <ert_process_backtrace_handler+0x7f>
mov    -0x1b0(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>

x86 assembly (clang, -O2):
<ert_process_backtrace_handler>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x190,%rsp
mov    %edi,%edx
mov    0x2e78(%rip),%r14
mov    (%r14),%edi
lea    0xe8e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x32,%esi
call   1060 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <backtrace_symbols@plt>
mov    %rax,%r12
mov    (%r14),%edi
lea    0xe71(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %ebp,%ebp
jle    11da <ert_process_backtrace_handler+0x8a>
mov    %ebp,%ebp
lea    0xe76(%rip),%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    (%r12,%rbx,8),%rdx
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11c0 <ert_process_backtrace_handler+0x70>
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Error: signal %d:\0A\00"
@.str.1 = [27 x i8] c"Obtained %d stack frames:\0A\00"
@.str.2 = [6 x i8] c" %s\0A\00"
define void @ert_process_backtrace_handler(i32 %0) {
%2 = alloca i32
%3 = alloca [50 x i8*]
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* %2
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [50 x i8*], [50 x i8*]* %3, i64 0, i64 0
%11 = call i32 @backtrace(i8** %10, i32 50)
store i32 %11, i32* %4
%12 = getelementptr inbounds [50 x i8*], [50 x i8*]* %3, i64 0, i64 0
%13 = load i32, i32* %4
%14 = call i8** @backtrace_symbols(i8** %12, i32 %13)
store i8** %14, i8*** %5
%15 = load i32, i32* @stderr
%16 = load i32, i32* %4
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 0, i32* %6
br label %18
18: ; preds = %30, %1
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i8**, i8*** %5
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %22
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
br label %18
33: ; preds = %18
%34 = load i8**, i8*** %5
%35 = call i32 @free(i8** %34)
%36 = call i32 @_exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Error: signal %d:\0A\00"
@.str.1 = [27 x i8] c"Obtained %d stack frames:\0A\00"
@.str.2 = [6 x i8] c" %s\0A\00"
define void @ert_process_backtrace_handler(i32 %0) {
%2 = alloca [50 x i8*]
%3 = bitcast [50 x i8*]* %2 to i8*
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = getelementptr inbounds [50 x i8*], [50 x i8*]* %2, i64 0, i64 0
%7 = call i32 @backtrace(i8** %6, i32 50)
%8 = call i8** @backtrace_symbols(i8** %6, i32 %7)
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%11 = icmp sgt i32 %7, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = zext i32 %7 to i64
br label %17
14: ; preds = %17, %1
%15 = call i32 @free(i8** %8)
%16 = call i32 @_exit(i32 1)
unreachable
17: ; preds = %12, %17
%18 = phi i64 [ 0, %12 ], [ %23, %17 ]
%19 = load i32, i32* @stderr
%20 = getelementptr inbounds i8*, i8** %8, i64 %18
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = add nuw nsw i64 %18, 1
%24 = icmp eq i64 %23, %13
br i1 %24, label %14, label %17
}