#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 backtrace(void **, int);
int /*<<< orphan*/ backtrace_symbols_fd(void **, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ *getMcontextEip(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
  ucontext_t *uc = (ucontext_t *)secret;
  fprintf(stderr, "Crashed with signal %i\n", sig);
  void *trace[100];
  int trace_size = 0;
  trace_size = backtrace(trace, 100);
  if (getMcontextEip(uc) != NULL)
    trace[1] = getMcontextEip(uc);
  backtrace_symbols_fd(trace, trace_size, 2);
}


x86 assembly (clang, -O0):
<sigsegvHandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe8f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x344(%rbp)
lea    -0x340(%rbp),%rdi
mov    $0x64,%esi
call   1040 <backtrace@plt>
mov    %eax,-0x344(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <getMcontextEip@plt>
cmp    $0x0,%rax
je     11ba <sigsegvHandler+0x7a>
mov    -0x20(%rbp),%rdi
call   1060 <getMcontextEip@plt>
mov    %rax,-0x338(%rbp)
lea    -0x340(%rbp),%rdi
mov    -0x344(%rbp),%esi
mov    $0x2,%edx
call   1030 <backtrace_symbols_fd@plt>
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigsegvHandler>:
push   %rbp
push   %rbx
sub    $0x328,%rsp
mov    %rdx,%rbx
mov    %edi,%edx
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
lea    0xea2(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rsp,%rdi
mov    $0x64,%esi
call   1040 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <getMcontextEip@plt>
test   %rax,%rax
je     118c <sigsegvHandler+0x4c>
mov    %rbx,%rdi
call   1060 <getMcontextEip@plt>
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
mov    %ebp,%esi
mov    $0x2,%edx
call   1030 <backtrace_symbols_fd@plt>
add    $0x328,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Crashed with signal %i\0A\00"
define void @sigsegvHandler(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [100 x i8*]
%9 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %7
%12 = load i32, i32* @stderr
%13 = load i32, i32* %4
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 0, i32* %9
%15 = getelementptr inbounds [100 x i8*], [100 x i8*]* %8, i64 0, i64 0
%16 = call i32 @backtrace(i8** %15, i32 100)
store i32 %16, i32* %9
%17 = load i32*, i32** %7
%18 = call i32* @getMcontextEip(i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i32*, i32** %7
%22 = call i32* @getMcontextEip(i32* %21)
%23 = bitcast i32* %22 to i8*
%24 = getelementptr inbounds [100 x i8*], [100 x i8*]* %8, i64 0, i64 1
store i8* %23, i8** %24
br label %25
25: ; preds = %20, %3
%26 = getelementptr inbounds [100 x i8*], [100 x i8*]* %8, i64 0, i64 0
%27 = load i32, i32* %9
%28 = call i32 @backtrace_symbols_fd(i8** %26, i32 %27, i32 2)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Crashed with signal %i\0A\00"
define void @sigsegvHandler(i32 %0, i32* nocapture readnone %1, i8* %2) {
%4 = alloca [100 x i8*]
%5 = bitcast i8* %2 to i32*
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = bitcast [100 x i8*]* %4 to i8*
%9 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 0
%10 = call i32 @backtrace(i8** %9, i32 100)
%11 = call i32* @getMcontextEip(i32* %5)
%12 = icmp eq i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %3
%14 = call i32* @getMcontextEip(i32* %5)
%15 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 1
%16 = bitcast i8** %15 to i32**
store i32* %14, i32** %16
br label %17
17: ; preds = %13, %3
%18 = call i32 @backtrace_symbols_fd(i8** %9, i32 %10, i32 2)
ret void
}