#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);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ *getMcontextEip(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
  ucontext_t *uc = (ucontext_t *)secret;
  void *trace[100];
  char **messages = NULL;
  int i, trace_size = 0;
  trace_size = backtrace(trace, 100);
  if (getMcontextEip(uc) != NULL) {
    trace[1] = getMcontextEip(uc);
  }
  messages = backtrace_symbols(trace, trace_size);
  for (i = 1; i < trace_size; ++i)
    printf("%s\n", messages[i]);
}


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)
movq   $0x0,-0x348(%rbp)
movl   $0x0,-0x350(%rbp)
lea    -0x340(%rbp),%rdi
mov    $0x64,%esi
call   1040 <backtrace@plt>
mov    %eax,-0x350(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <getMcontextEip@plt>
cmp    $0x0,%rax
je     11ad <sigsegvHandler+0x6d>
mov    -0x20(%rbp),%rdi
call   1060 <getMcontextEip@plt>
mov    %rax,-0x338(%rbp)
lea    -0x340(%rbp),%rdi
mov    -0x350(%rbp),%esi
call   1030 <backtrace_symbols@plt>
mov    %rax,-0x348(%rbp)
movl   $0x1,-0x34c(%rbp)
mov    -0x34c(%rbp),%eax
cmp    -0x350(%rbp),%eax
jge    1214 <sigsegvHandler+0xd4>
mov    -0x348(%rbp),%rax
movslq -0x34c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe05(%rip),%rdi
call   1050 <printf@plt>
mov    -0x34c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34c(%rbp)
jmp    11d0 <sigsegvHandler+0x90>
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigsegvHandler>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x328,%rsp
mov    %rdx,%rbx
mov    %rsp,%rdi
mov    $0x64,%esi
call   1040 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <getMcontextEip@plt>
test   %rax,%rax
je     1179 <sigsegvHandler+0x39>
mov    %rbx,%rdi
call   1060 <getMcontextEip@plt>
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
mov    %ebp,%esi
call   1030 <backtrace_symbols@plt>
cmp    $0x2,%ebp
jl     11b5 <sigsegvHandler+0x75>
mov    %rax,%r15
mov    %ebp,%ebp
mov    $0x1,%ebx
lea    0xe67(%rip),%r14
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rsi
mov    %r14,%rdi
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11a0 <sigsegvHandler+0x60>
add    $0x328,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\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 i8**
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %7
store i8** null, i8*** %9
store i32 0, i32* %11
%14 = getelementptr inbounds [100 x i8*], [100 x i8*]* %8, i64 0, i64 0
%15 = call i32 @backtrace(i8** %14, i32 100)
store i32 %15, i32* %11
%16 = load i32*, i32** %7
%17 = call i32* @getMcontextEip(i32* %16)
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %3
%20 = load i32*, i32** %7
%21 = call i32* @getMcontextEip(i32* %20)
%22 = bitcast i32* %21 to i8*
%23 = getelementptr inbounds [100 x i8*], [100 x i8*]* %8, i64 0, i64 1
store i8* %22, i8** %23
br label %24
24: ; preds = %19, %3
%25 = getelementptr inbounds [100 x i8*], [100 x i8*]* %8, i64 0, i64 0
%26 = load i32, i32* %11
%27 = call i8** @backtrace_symbols(i8** %25, i32 %26)
store i8** %27, i8*** %9
store i32 1, i32* %10
br label %28
28: ; preds = %39, %24
%29 = load i32, i32* %10
%30 = load i32, i32* %11
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %42
32: ; preds = %28
%33 = load i8**, i8*** %9
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %32
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
store i32 %41, i32* %10
br label %28
42: ; preds = %28
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\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 = bitcast [100 x i8*]* %4 to i8*
%7 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 0
%8 = call i32 @backtrace(i8** %7, i32 100)
%9 = call i32* @getMcontextEip(i32* %5)
%10 = icmp eq i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = call i32* @getMcontextEip(i32* %5)
%13 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 1
%14 = bitcast i8** %13 to i32**
store i32* %12, i32** %14
br label %15
15: ; preds = %11, %3
%16 = call i8** @backtrace_symbols(i8** %7, i32 %8)
%17 = icmp sgt i32 %8, 1
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = zext i32 %8 to i64
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 1, %18 ], [ %25, %20 ]
%22 = getelementptr inbounds i8*, i8** %16, i64 %21
%23 = load i8*, i8** %22
%24 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = add nuw nsw i64 %21, 1
%26 = icmp eq i64 %25, %19
br i1 %26, label %27, label %20
27: ; preds = %20, %15
ret void
}