#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int si_code;
  void *si_addr;
};
typedef TYPE_1__ siginfo_t;

/* Variables and functions */
int SEGV_ACCERR;
int SEGV_MAPERR;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ designate_modified(void *);
int /*<<< orphan*/ printf(char *, ...);
void fault_handler(int sn, siginfo_t *si, void *ctx) {
  void *p = si->si_addr;
  int c = si->si_code;
  if (si->si_code != SEGV_ACCERR) {
    if (c == SEGV_MAPERR) {
      printf("SIGSEGV MAPERR si_code %i fault on addr %p\n", c, p);
    }
    if (c == 0) {
      printf("Signal as SI_USER (from debugger?) - ignoring\n");
      return;
    }
    if (c == 128) {
      if (0) {
        printf("Signal as SI_KERNEL - ignoring\n");
      }
      return;
    }
    abort();
  }
  if (!designate_modified(p)) {
    if (si->si_code == SEGV_ACCERR) {
      printf("SIGSEGV SEGV_ACCERR SI_CODE %i fault on %p\n", c, p);
    } else {
      printf("SIGSEGV ???? SI_CODE %i fault on %p\n", c, p);
    }
    abort();
  }
}


x86 assembly (clang, -O0):
<fault_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%eax
je     11ca <fault_handler+0x9a>
mov    -0x24(%rbp),%eax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%eax
jne    1194 <fault_handler+0x64>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
jne    11b1 <fault_handler+0x81>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1227 <fault_handler+0xf7>
cmpl   $0x80,-0x24(%rbp)
jne    11c3 <fault_handler+0x93>
jmp    1227 <fault_handler+0xf7>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rdi
call   1050 <designate_modified@plt>
cmp    $0x0,%eax
jne    1227 <fault_handler+0xf7>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%eax
jne    120b <fault_handler+0xdb>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <fault_handler+0xf0>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fault_handler>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%r14
mov    (%rsi),%ebp
mov    0x2e94(%rip),%r15
cmp    (%r15),%ebp
jne    1192 <fault_handler+0x52>
mov    %rsi,%rbx
mov    %r14,%rdi
call   1060 <designate_modified@plt>
test   %eax,%eax
jne    11d2 <fault_handler+0x92>
mov    (%rbx),%eax
cmp    (%r15),%eax
lea    0xeb8(%rip),%rax
lea    0xedd(%rip),%rdi
cmove  %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2e3f(%rip),%rax
cmp    (%rax),%ebp
jne    11b0 <fault_handler+0x70>
lea    0xe5c(%rip),%rdi
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x80,%ebp
je     11d2 <fault_handler+0x92>
test   %ebp,%ebp
jne    11dd <fault_handler+0x9d>
lea    0xeba(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i8* }
@SEGV_ACCERR = global i32 0
@SEGV_MAPERR = global i32 0
@.str = [44 x i8] c"SIGSEGV MAPERR si_code %i fault on addr %p\0A\00"
@.str.1 = [47 x i8] c"Signal as SI_USER (from debugger?) - ignoring\0A\00"
@.str.2 = [44 x i8] c"SIGSEGV SEGV_ACCERR SI_CODE %i fault on %p\0A\00"
@.str.3 = [37 x i8] c"SIGSEGV ???? SI_CODE %i fault on %p\0A\00"
define void @fault_handler(i32 %0, %struct.TYPE_3__* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
%9 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i32 0, i32 1
%11 = load i8*, i8** %10
store i8* %11, i8** %7
%12 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i32 0, i32 0
%14 = load i32, i32* %13
store i32 %14, i32* %8
%15 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @SEGV_ACCERR
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %39
20: ; preds = %3
%21 = load i32, i32* %8
%22 = load i32, i32* @SEGV_MAPERR
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* %8
%26 = load i8*, i8** %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %25, i8* %26)
br label %28
28: ; preds = %24, %20
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %59
33: ; preds = %28
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 128
br i1 %35, label %36, label %37
36: ; preds = %33
br label %59
37: ; preds = %33
%38 = call i32 (...) @abort()
unreachable
39: ; preds = %3
%40 = load i8*, i8** %7
%41 = call i32 @designate_modified(i8* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %59, label %43
43: ; preds = %39
%44 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%45 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32, i32* @SEGV_ACCERR
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %43
%50 = load i32, i32* %8
%51 = load i8*, i8** %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %50, i8* %51)
br label %57
53: ; preds = %43
%54 = load i32, i32* %8
%55 = load i8*, i8** %7
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %54, i8* %55)
br label %57
57: ; preds = %53, %49
%58 = call i32 (...) @abort()
unreachable
59: ; preds = %31, %36, %39
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i8* }
@SEGV_ACCERR = global i32 0
@SEGV_MAPERR = global i32 0
@.str = [44 x i8] c"SIGSEGV MAPERR si_code %i fault on addr %p\0A\00"
@.str.2 = [44 x i8] c"SIGSEGV SEGV_ACCERR SI_CODE %i fault on %p\0A\00"
@.str.3 = [37 x i8] c"SIGSEGV ???? SI_CODE %i fault on %p\0A\00"
@str = [46 x i8] c"Signal as SI_USER (from debugger?) - ignoring\00"
define void @fault_handler(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture readnone %2) {
%4 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%5 = load i8*, i8** %4
%6 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* @SEGV_ACCERR
%9 = icmp eq i32 %7, %8
br i1 %9, label %20, label %10
10: ; preds = %3
%11 = load i32, i32* @SEGV_MAPERR
%12 = icmp eq i32 %7, %11
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %5)
br label %15
15: ; preds = %13, %10
switch i32 %7, label %18 [
i32 0, label %16
i32 128, label %30
]
16: ; preds = %15
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %30
18: ; preds = %15
%19 = tail call i32 (...) @abort()
unreachable
20: ; preds = %3
%21 = tail call i32 @designate_modified(i8* %5)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i32, i32* %6
%25 = load i32, i32* @SEGV_ACCERR
%26 = icmp eq i32 %24, %25
%27 = select i1 %26, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %27, i32 %7, i8* %5)
%29 = tail call i32 (...) @abort()
unreachable
30: ; preds = %20, %15, %16
ret void
}