#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 /*<<< orphan*/ si_addr;
};
typedef TYPE_1__ siginfo_t;
typedef int /*<<< orphan*/ pointers;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
#define SEGV_ACCERR 129
#define SEGV_MAPERR 128
int /*<<< orphan*/ abort();
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int, ...);
void segv(int sig, siginfo_t *info, void *data) {
  int i;
  int frame_count = 0;
  void *pointers[256];
  switch (info->si_code) {
  case SEGV_MAPERR:
    syslog(LOG_ERR, "SEGV: address not mapped to object (%p)", info->si_addr);
    break;
  case SEGV_ACCERR:
    syslog(LOG_ERR, "SEGV: invalid permissions for mapped object (%p)",
           info->si_addr);
    break;
  default:
    syslog(LOG_ERR, "segmentation violation at address %p\n", info->si_addr);
  }
  frame_count = backtrace(pointers, sizeof(pointers));
  char **frames = backtrace_symbols(pointers, frame_count);
  for (i = 0; i < frame_count; ++i) {
    syslog(LOG_ERR, "frame(%03d): %s", i, frames[i]);
  }
  abort();
}


x86 assembly (clang, -O0):
<segv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x82c(%rbp)
sub    $0x80,%eax
je     118f <segv+0x4f>
jmp    1179 <segv+0x39>
mov    -0x82c(%rbp),%eax
sub    $0x81,%eax
je     11b2 <segv+0x72>
jmp    11d5 <segv+0x95>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    11f3 <segv+0xb3>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    11f3 <segv+0xb3>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
lea    -0x820(%rbp),%rdi
mov    $0x800,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x20(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x828(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1267 <segv+0x127>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x828(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1224 <segv+0xe4>
mov    $0x0,%al
call   1030 <abort@plt>

x86 assembly (clang, -O2):
<segv>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    (%rsi),%eax
mov    0x4(%rsi),%edx
cmp    $0x81,%eax
lea    0xec7(%rip),%rcx
lea    0xef1(%rip),%rdi
cmove  %rcx,%rdi
cmp    $0x80,%eax
lea    0xe88(%rip),%rsi
cmovne %rdi,%rsi
mov    0x2e55(%rip),%r12
mov    (%r12),%edi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x800,%esi
call   1050 <backtrace@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <backtrace_symbols@plt>
test   %ebx,%ebx
jle    11dd <segv+0x9d>
mov    %rax,%r15
mov    %ebx,%r13d
lea    0xec4(%rip),%r14
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r12),%edi
mov    (%r15,%rbx,8),%rcx
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <syslog@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11c0 <segv+0x80>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@LOG_ERR = global i32 0
@.str = [40 x i8] c"SEGV: address not mapped to object (%p)\00"
@.str.1 = [49 x i8] c"SEGV: invalid permissions for mapped object (%p)\00"
@.str.2 = [38 x i8] c"segmentation violation at address %p\0A\00"
@.str.3 = [16 x i8] c"frame(%03d): %s\00"
define void @segv(i32 %0, %struct.TYPE_3__* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8*]
%10 = alloca i8**
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
store i32 0, i32* %8
%11 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %11, i32 0, i32 0
%13 = load i32, i32* %12
switch i32 %13, label %26 [
i32 128, label %14
i32 129, label %20
]
14: ; preds = %3
%15 = load i32, i32* @LOG_ERR
%16 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 (i32, i8*, i32, ...) @syslog(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %32
20: ; preds = %3
%21 = load i32, i32* @LOG_ERR
%22 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 (i32, i8*, i32, ...) @syslog(i32 %21, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %32
26: ; preds = %3
%27 = load i32, i32* @LOG_ERR
%28 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 (i32, i8*, i32, ...) @syslog(i32 %27, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %26, %20, %14
%33 = getelementptr inbounds [256 x i8*], [256 x i8*]* %9, i64 0, i64 0
%34 = call i32 @backtrace(i8** %33, i32 2048)
store i32 %34, i32* %8
%35 = getelementptr inbounds [256 x i8*], [256 x i8*]* %9, i64 0, i64 0
%36 = load i32, i32* %8
%37 = call i8** @backtrace_symbols(i8** %35, i32 %36)
store i8** %37, i8*** %10
store i32 0, i32* %7
br label %38
38: ; preds = %51, %32
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %54
42: ; preds = %38
%43 = load i32, i32* @LOG_ERR
%44 = load i32, i32* %7
%45 = load i8**, i8*** %10
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %45, i64 %47
%49 = load i8*, i8** %48
%50 = call i32 (i32, i8*, i32, ...) @syslog(i32 %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %44, i8* %49)
br label %51
51: ; preds = %42
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %38
54: ; preds = %38
%55 = call i32 (...) @abort()
unreachable
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@LOG_ERR = global i32 0
@.str = [40 x i8] c"SEGV: address not mapped to object (%p)\00"
@.str.1 = [49 x i8] c"SEGV: invalid permissions for mapped object (%p)\00"
@.str.2 = [38 x i8] c"segmentation violation at address %p\0A\00"
@.str.3 = [16 x i8] c"frame(%03d): %s\00"
define void @segv(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture readnone %2) {
%4 = alloca [256 x i8*]
%5 = bitcast [256 x i8*]* %4 to i8*
%6 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%7 = load i32, i32* %6
%8 = icmp eq i32 %7, 129
%9 = select i1 %8, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0)
%10 = icmp eq i32 %7, 128
%11 = select i1 %10, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %9
%12 = load i32, i32* @LOG_ERR
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%14 = load i32, i32* %13
%15 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %12, i8* %11, i32 %14)
%16 = getelementptr inbounds [256 x i8*], [256 x i8*]* %4, i64 0, i64 0
%17 = call i32 @backtrace(i8** %16, i32 2048)
%18 = call i8** @backtrace_symbols(i8** %16, i32 %17)
%19 = icmp sgt i32 %17, 0
br i1 %19, label %20, label %31
20: ; preds = %3
%21 = zext i32 %17 to i64
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ 0, %20 ], [ %29, %22 ]
%24 = load i32, i32* @LOG_ERR
%25 = getelementptr inbounds i8*, i8** %18, i64 %23
%26 = load i8*, i8** %25
%27 = trunc i64 %23 to i32
%28 = call i32 (i32, i8*, i32, ...) @syslog(i32 %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %27, i8* %26)
%29 = add nuw nsw i64 %23, 1
%30 = icmp eq i64 %29, %21
br i1 %30, label %31, label %22
31: ; preds = %22, %3
%32 = call i32 (...) @abort()
unreachable
}