#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;
  int si_errno;
  int si_pid;
  void *si_uid;
  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 *, int, void *);
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("SIGSEGV SI_USER SI_ERRNO %i fault on addr %p\n", si->si_errno, p);
      printf("pid %i uid %i\n", si->si_pid, si->si_uid);
      return;
    }
    if (c == 128) {
      printf("SIGSEGV SI_KERNEL SI_ERRNO %i fault on addr %p\n", si->si_errno,
             p);
    }
    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    0x18(%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     11fe <fault_handler+0xce>
mov    -0x24(%rbp),%eax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%eax
jne    1192 <fault_handler+0x62>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe73(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
jne    11d3 <fault_handler+0xa3>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rdx
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe91(%rip),%rdi
call   1040 <printf@plt>
jmp    1257 <fault_handler+0x127>
cmpl   $0x80,-0x24(%rbp)
jne    11f7 <fault_handler+0xc7>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rdx
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rdi
call   1050 <designate_modified@plt>
cmp    $0x0,%eax
jne    1257 <fault_handler+0x127>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%eax
jne    123d <fault_handler+0x10d>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
jmp    1250 <fault_handler+0x120>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe7a(%rip),%rdi
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    %rsi,%rbx
mov    0x18(%rsi),%r14
mov    (%rsi),%ebp
mov    0x2ea1(%rip),%r15
cmp    (%r15),%ebp
jne    1163 <fault_handler+0x33>
mov    %r14,%rdi
call   1050 <designate_modified@plt>
test   %eax,%eax
je     11d3 <fault_handler+0xa3>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e6e(%rip),%rax
cmp    (%rax),%ebp
jne    117f <fault_handler+0x4f>
lea    0xe8b(%rip),%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1040 <printf@plt>
test   %ebp,%ebp
jne    11b2 <fault_handler+0x82>
mov    0x4(%rbx),%esi
lea    0xe9f(%rip),%rdi
mov    %r14,%rdx
call   1040 <printf@plt>
mov    0x8(%rbx),%esi
mov    0x10(%rbx),%rdx
lea    0xeb7(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
cmp    $0x80,%ebp
jne    11cc <fault_handler+0x9c>
mov    0x4(%rbx),%esi
lea    0xea5(%rip),%rdi
mov    %r14,%rdx
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    (%rbx),%eax
cmp    (%r15),%eax
lea    0xeba(%rip),%rax
lea    0xedf(%rip),%rdi
cmove  %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32, i32, i8*, 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 = [46 x i8] c"SIGSEGV SI_USER SI_ERRNO %i fault on addr %p\0A\00"
@.str.2 = [15 x i8] c"pid %i uid %i\0A\00"
@.str.3 = [48 x i8] c"SIGSEGV SI_KERNEL SI_ERRNO %i fault on addr %p\0A\00"
@.str.4 = [44 x i8] c"SIGSEGV SEGV_ACCERR SI_CODE %i fault on %p\0A\00"
@.str.5 = [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 4
%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 %55
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 @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 %44
31: ; preds = %28
%32 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%33 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load i8*, i8** %7
%36 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %34, i8* %35)
%37 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%38 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %37, i32 0, i32 2
%39 = load i32, i32* %38
%40 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%41 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %40, i32 0, i32 3
%42 = load i8*, i8** %41
%43 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %39, i8* %42)
br label %75
44: ; preds = %28
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, 128
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%49 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load i8*, i8** %7
%52 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %50, i8* %51)
br label %53
53: ; preds = %47, %44
%54 = call i32 (...) @abort()
unreachable
55: ; preds = %3
%56 = load i8*, i8** %7
%57 = call i32 @designate_modified(i8* %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %75, label %59
59: ; preds = %55
%60 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%61 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = load i32, i32* @SEGV_ACCERR
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %69
65: ; preds = %59
%66 = load i32, i32* %8
%67 = load i8*, i8** %7
%68 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %66, i8* %67)
br label %73
69: ; preds = %59
%70 = load i32, i32* %8
%71 = load i8*, i8** %7
%72 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %70, i8* %71)
br label %73
73: ; preds = %69, %65
%74 = call i32 (...) @abort()
unreachable
75: ; preds = %31, %55
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32, i32, i8*, 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 = [46 x i8] c"SIGSEGV SI_USER SI_ERRNO %i fault on addr %p\0A\00"
@.str.2 = [15 x i8] c"pid %i uid %i\0A\00"
@.str.3 = [48 x i8] c"SIGSEGV SI_KERNEL SI_ERRNO %i fault on addr %p\0A\00"
@.str.4 = [44 x i8] c"SIGSEGV SEGV_ACCERR SI_CODE %i fault on %p\0A\00"
@.str.5 = [37 x i8] c"SIGSEGV ???? SI_CODE %i fault on %p\0A\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 4
%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 %31, 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 @printf(i8* 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 %29 [
i32 0, label %16
i32 128, label %25
]
16: ; preds = %15
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8* %5)
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 2
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 3
%23 = load i8*, i8** %22
%24 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %21, i8* %23)
br label %41
25: ; preds = %15
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%27 = load i32, i32* %26
%28 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %27, i8* %5)
br label %29
29: ; preds = %15, %25
%30 = tail call i32 (...) @abort()
unreachable
31: ; preds = %3
%32 = tail call i32 @designate_modified(i8* %5)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i32, i32* %6
%36 = load i32, i32* @SEGV_ACCERR
%37 = icmp eq i32 %35, %36
%38 = select i1 %37, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0)
%39 = tail call i32 @printf(i8* %38, i32 %7, i8* %5)
%40 = tail call i32 (...) @abort()
unreachable
41: ; preds = %31, %16
ret void
}