#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 int /*<<< orphan*/ ucontext_t;
typedef int /*<<< orphan*/ siginfo_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ backtrace_symbols_detail(void **, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
void *get_uc_mcontext_pc(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int);
char *strsignal(int);
void backtrace_handler(int sig_num, siginfo_t *info, void *ucontext) {
  void *array[100];
  int i, size = 0;
  char **strings = NULL;
  void *caller_addr = get_uc_mcontext_pc((ucontext_t *)ucontext);
  fprintf(stderr, "Program received signal %s.\n", strsignal(sig_num));
  fprintf(stderr, "%d:%s [%p]\n", sig_num, strerror(sig_num), caller_addr);
  size = backtrace(array, 100);
  array[1] = caller_addr;
  strings = backtrace_symbols(array, size);
  for (i = 0; i < size; ++i) {
    fprintf(stderr, "#%d  %s\n", i, strings[i]);
  }
  backtrace_symbols_detail(array, size);
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<backtrace_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x370,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x348(%rbp)
movq   $0x0,-0x350(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <get_uc_mcontext_pc@plt>
mov    %rax,-0x358(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x364(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <strsignal@plt>
mov    -0x364(%rbp),%edi
mov    %rax,%rdx
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x360(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x35c(%rbp)
mov    -0x4(%rbp),%edi
call   10a0 <strerror@plt>
mov    -0x360(%rbp),%edi
mov    -0x35c(%rbp),%edx
mov    %rax,%rcx
mov    -0x358(%rbp),%r8
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    -0x340(%rbp),%rdi
mov    $0x64,%esi
call   1040 <backtrace@plt>
mov    %eax,-0x348(%rbp)
mov    -0x358(%rbp),%rax
mov    %rax,-0x338(%rbp)
lea    -0x340(%rbp),%rdi
mov    -0x348(%rbp),%esi
call   1030 <backtrace_symbols@plt>
mov    %rax,-0x350(%rbp)
movl   $0x0,-0x344(%rbp)
mov    -0x344(%rbp),%eax
cmp    -0x348(%rbp),%eax
jge    12ca <backtrace_handler+0x14a>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    -0x344(%rbp),%edx
mov    -0x350(%rbp),%rax
movslq -0x344(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x344(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x344(%rbp)
jmp    1275 <backtrace_handler+0xf5>
lea    -0x340(%rbp),%rdi
mov    -0x348(%rbp),%esi
call   1080 <backtrace_symbols_detail@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<backtrace_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x328,%rsp
mov    %edi,%ebx
mov    %rdx,%rdi
call   1050 <get_uc_mcontext_pc@plt>
mov    %rax,%r14
mov    0x2e3b(%rip),%r12
mov    (%r12),%ebp
mov    %ebx,%edi
call   1070 <strsignal@plt>
lea    0xe49(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r12),%ebp
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xe48(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <backtrace@plt>
mov    %eax,%r15d
mov    %r14,0x8(%rsp)
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <backtrace_symbols@plt>
test   %r15d,%r15d
jle    123e <backtrace_handler+0xbe>
mov    %rax,%r13
mov    %r15d,%ebx
lea    0xe0f(%rip),%r14
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    (%r12),%edi
mov    0x0(%r13,%rbp,8),%rcx
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1220 <backtrace_handler+0xa0>
mov    %rsp,%rdi
mov    %r15d,%esi
call   1080 <backtrace_symbols_detail@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Program received signal %s.\0A\00"
@.str.1 = [12 x i8] c"%d:%s [%p]\0A\00"
@.str.2 = [9 x i8] c"#%d %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @backtrace_handler(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca [100 x i8*]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8**
%11 = alloca i8*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
store i32 0, i32* %9
store i8** null, i8*** %10
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to i32*
%14 = call i8* @get_uc_mcontext_pc(i32* %13)
store i8* %14, i8** %11
%15 = load i32, i32* @stderr
%16 = load i32, i32* %4
%17 = call i8* @strsignal(i32 %16)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @stderr
%20 = load i32, i32* %4
%21 = load i32, i32* %4
%22 = call i8* @strerror(i32 %21)
%23 = load i8*, i8** %11
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %22, i8* %23)
%25 = getelementptr inbounds [100 x i8*], [100 x i8*]* %7, i64 0, i64 0
%26 = call i32 @backtrace(i8** %25, i32 100)
store i32 %26, i32* %9
%27 = load i8*, i8** %11
%28 = getelementptr inbounds [100 x i8*], [100 x i8*]* %7, i64 0, i64 1
store i8* %27, i8** %28
%29 = getelementptr inbounds [100 x i8*], [100 x i8*]* %7, i64 0, i64 0
%30 = load i32, i32* %9
%31 = call i8** @backtrace_symbols(i8** %29, i32 %30)
store i8** %31, i8*** %10
store i32 0, i32* %8
br label %32
32: ; preds = %45, %3
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %48
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i32, i32* %8
%39 = load i8**, i8*** %10
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %38, i8* %43)
br label %45
45: ; preds = %36
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %32
48: ; preds = %32
%49 = getelementptr inbounds [100 x i8*], [100 x i8*]* %7, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @backtrace_symbols_detail(i8** %49, i32 %50)
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Program received signal %s.\0A\00"
@.str.1 = [12 x i8] c"%d:%s [%p]\0A\00"
@.str.2 = [9 x i8] c"#%d %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @backtrace_handler(i32 %0, i32* nocapture readnone %1, i8* %2) {
%4 = alloca [100 x i8*]
%5 = bitcast [100 x i8*]* %4 to i8*
%6 = bitcast i8* %2 to i32*
%7 = tail call i8* @get_uc_mcontext_pc(i32* %6)
%8 = load i32, i32* @stderr
%9 = tail call i8* @strsignal(i32 %0)
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @stderr
%12 = tail call i8* @strerror(i32 %0)
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %12, i8* %7)
%14 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 0
%15 = call i32 @backtrace(i8** %14, i32 100)
%16 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 1
store i8* %7, i8** %16
%17 = call i8** @backtrace_symbols(i8** %14, i32 %15)
%18 = icmp sgt i32 %15, 0
br i1 %18, label %19, label %30
19: ; preds = %3
%20 = zext i32 %15 to i64
br label %21
21: ; preds = %19, %21
%22 = phi i64 [ 0, %19 ], [ %28, %21 ]
%23 = load i32, i32* @stderr
%24 = getelementptr inbounds i8*, i8** %17, i64 %22
%25 = load i8*, i8** %24
%26 = trunc i64 %22 to i32
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %26, i8* %25)
%28 = add nuw nsw i64 %22, 1
%29 = icmp eq i64 %28, %20
br i1 %29, label %30, label %21
30: ; preds = %21, %3
%31 = call i32 @backtrace_symbols_detail(i8** %14, i32 %15)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
}