#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 */
#define SIGBUS 130
#define SIGSEGV 129
#define SIGUSR1 128
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void signal_handler(int sig, siginfo_t *siginfo, ucontext_t *ucxt) {
  fprintf(stderr, "signal_handler: sig=%d\n", sig);
  switch (sig) {
  case SIGBUS:
  case SIGSEGV: {
    fprintf(stderr, "Got SIGSEGV\n");
    break;
  }
  case SIGUSR1: {
    fprintf(stderr, "Got SIGUSR1\n");
    break;
  }
  default:
    assert(0);
  }
}


x86 assembly (clang, -O0):
<signal_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xeba(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     1195 <signal_handler+0x75>
jmp    1163 <signal_handler+0x43>
mov    -0x1c(%rbp),%eax
add    $0xffffff7f,%eax
sub    $0x1,%eax
ja     11b1 <signal_handler+0x91>
jmp    1179 <signal_handler+0x59>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11b8 <signal_handler+0x98>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11b8 <signal_handler+0x98>
xor    %edi,%edi
call   1030 <assert@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_handler>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2eb3(%rip),%r14
mov    (%r14),%edi
lea    0xec9(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    -0x81(%rbx),%eax
cmp    $0x2,%eax
jae    1157 <signal_handler+0x37>
mov    (%r14),%edi
lea    0xec3(%rip),%rsi
jmp    1169 <signal_handler+0x49>
cmp    $0x80,%ebx
jne    1177 <signal_handler+0x57>
mov    (%r14),%edi
lea    0xebc(%rip),%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <fprintf@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <assert@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"signal_handler: sig=%d\0A\00"
@.str.1 = [13 x i8] c"Got SIGSEGV\0A\00"
@.str.2 = [13 x i8] c"Got SIGUSR1\0A\00"
define void @signal_handler(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%7 = load i32, i32* @stderr
%8 = load i32, i32* %4
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %4
switch i32 %10, label %17 [
i32 130, label %11
i32 129, label %11
i32 128, label %14
]
11: ; preds = %3, %3
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %19
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %19
17: ; preds = %3
%18 = call i32 @assert(i32 0)
br label %19
19: ; preds = %17, %14, %11
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"signal_handler: sig=%d\0A\00"
@.str.1 = [13 x i8] c"Got SIGSEGV\0A\00"
@.str.2 = [13 x i8] c"Got SIGUSR1\0A\00"
define void @signal_handler(i32 %0, i32* nocapture readnone %1, i32* nocapture readnone %2) {
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
switch i32 %0, label %12 [
i32 130, label %6
i32 129, label %6
i32 128, label %9
]
6: ; preds = %3, %3
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %14
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %14
12: ; preds = %3
%13 = tail call i32 @assert(i32 0)
br label %14
14: ; preds = %12, %9, %6
ret void
}