#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*/ siginfo_t;

/* Variables and functions */
#define SIGABRT 132
#define SIGBUS 131
#define SIGFPE 130
#define SIGILL 129
#define SIGSEGV 128
int errno;
int /*<<< orphan*/ forensics_report_crash(char const *);
void signal_handler(int sig, siginfo_t *info, void *context) {
  int saved_errno = errno;
  const char *message = NULL;
  switch (sig) {
  case SIGABRT:
    message = "got signal: SIGABRT";
    break;
  case SIGBUS:
    message = "got signal: SIGBUS";
    break;
  case SIGFPE:
    message = "got signal: SIGFPE";
    break;
  case SIGILL:
    message = "got signal: SIGILL";
    break;
  case SIGSEGV:
    message = "got signal: SIGSEGV";
    break;
  default:
    message = "got unexpected signal";
    break;
  }
  forensics_report_crash(message);
  errno = saved_errno;
}


x86 assembly (clang, -O0):
<signal_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x4,%eax
ja     11b0 <signal_handler+0xa0>
mov    -0x30(%rbp),%rax
lea    0xea9(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xead(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11bb <signal_handler+0xab>
lea    0xeb1(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11bb <signal_handler+0xab>
lea    0xeb4(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11bb <signal_handler+0xab>
lea    0xeb7(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11bb <signal_handler+0xab>
lea    0xeba(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11bb <signal_handler+0xab>
lea    0xebe(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <forensics_report_crash@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2e12(%rip),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_handler>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ec6(%rip),%rbx
mov    (%rbx),%ebp
add    $0xffffff80,%edi
cmp    $0x4,%edi
ja     1137 <signal_handler+0x27>
movslq %edi,%rax
lea    0xf4a(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    113e <signal_handler+0x2e>
lea    0xf23(%rip),%rdi
call   1030 <forensics_report_crash@plt>
mov    %ebp,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [20 x i8] c"got signal: SIGABRT\00"
@.str.1 = [19 x i8] c"got signal: SIGBUS\00"
@.str.2 = [19 x i8] c"got signal: SIGFPE\00"
@.str.3 = [19 x i8] c"got signal: SIGILL\00"
@.str.4 = [20 x i8] c"got signal: SIGSEGV\00"
@.str.5 = [22 x i8] c"got unexpected signal\00"
define void @signal_handler(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%9 = load i32, i32* @errno
store i32 %9, i32* %7
store i8* null, i8** %8
%10 = load i32, i32* %4
switch i32 %10, label %16 [
i32 132, label %11
i32 131, label %12
i32 130, label %13
i32 129, label %14
i32 128, label %15
]
11: ; preds = %3
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %8
br label %17
12: ; preds = %3
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8** %8
br label %17
13: ; preds = %3
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %17
14: ; preds = %3
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8** %8
br label %17
15: ; preds = %3
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8** %8
br label %17
16: ; preds = %3
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8** %8
br label %17
17: ; preds = %16, %15, %14, %13, %12, %11
%18 = load i8*, i8** %8
%19 = call i32 @forensics_report_crash(i8* %18)
%20 = load i32, i32* %7
store i32 %20, i32* @errno
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [20 x i8] c"got signal: SIGABRT\00"
@.str.1 = [19 x i8] c"got signal: SIGBUS\00"
@.str.2 = [19 x i8] c"got signal: SIGFPE\00"
@.str.3 = [19 x i8] c"got signal: SIGILL\00"
@.str.4 = [20 x i8] c"got signal: SIGSEGV\00"
@.str.5 = [22 x i8] c"got unexpected signal\00"
@reltable.signal_handler = [5 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([20 x i8]* @.str.4 to i64), i64 ptrtoint ([5 x i32]* @reltable.signal_handler to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([19 x i8]* @.str.3 to i64), i64 ptrtoint ([5 x i32]* @reltable.signal_handler to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([19 x i8]* @.str.2 to i64), i64 ptrtoint ([5 x i32]* @reltable.signal_handler to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([19 x i8]* @.str.1 to i64), i64 ptrtoint ([5 x i32]* @reltable.signal_handler to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([20 x i8]* @.str to i64), i64 ptrtoint ([5 x i32]* @reltable.signal_handler to i64)) to i32)]
define void @signal_handler(i32 %0, i32* nocapture readnone %1, i8* nocapture readnone %2) {
%4 = load i32, i32* @errno
%5 = add i32 %0, -128
%6 = icmp ult i32 %5, 5
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = sext i32 %5 to i64
%9 = shl i64 %8, 2
%10 = call i8* @llvm.load.relative.i64(i8* bitcast ([5 x i32]* @reltable.signal_handler to i8*), i64 %9)
br label %11
11: ; preds = %3, %7
%12 = phi i8* [ %10, %7 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), %3 ]
%13 = tail call i32 @forensics_report_crash(i8* %12)
store i32 %4, i32* @errno
ret void
}