#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 */
#define SEGV_ACCERR 129
#define SEGV_MAPERR 128
int /*<<< orphan*/ abort();
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ log_err(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:
    log_err("SEGV: address not mapped to object (%p)", info->si_addr);
    break;
  case SEGV_ACCERR:
    log_err("SEGV: invalid permissions for mapped object (%p)", info->si_addr);
    break;
  default:
    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) {
    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     11a9 <segv+0x69>
jmp    11c3 <segv+0x83>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1060 <log_err@plt>
jmp    11d8 <segv+0x98>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1060 <log_err@plt>
jmp    11d8 <segv+0x98>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1060 <log_err@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    1243 <segv+0x103>
mov    -0x1c(%rbp),%esi
mov    -0x828(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1060 <log_err@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1209 <segv+0xc9>
mov    $0x0,%al
call   1030 <abort@plt>

x86 assembly (clang, -O2):
<segv>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    (%rsi),%eax
mov    0x4(%rsi),%esi
cmp    $0x81,%eax
lea    0xec9(%rip),%rcx
lea    0xef3(%rip),%rdx
cmove  %rcx,%rdx
cmp    $0x80,%eax
lea    0xe8a(%rip),%rdi
cmovne %rdx,%rdi
xor    %eax,%eax
call   1060 <log_err@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    11c9 <segv+0x89>
mov    %rax,%r15
mov    %ebx,%r12d
lea    0xed1(%rip),%r14
xor    %ebx,%ebx
mov    (%r15,%rbx,8),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <log_err@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11b0 <segv+0x70>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@.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 %24 [
i32 128, label %14
i32 129, label %19
]
14: ; preds = %3
%15 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 (i8*, i32, ...) @log_err(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %29
19: ; preds = %3
%20 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 (i8*, i32, ...) @log_err(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %29
24: ; preds = %3
%25 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 (i8*, i32, ...) @log_err(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %24, %19, %14
%30 = getelementptr inbounds [256 x i8*], [256 x i8*]* %9, i64 0, i64 0
%31 = call i32 @backtrace(i8** %30, i32 2048)
store i32 %31, i32* %8
%32 = getelementptr inbounds [256 x i8*], [256 x i8*]* %9, i64 0, i64 0
%33 = load i32, i32* %8
%34 = call i8** @backtrace_symbols(i8** %32, i32 %33)
store i8** %34, i8*** %10
store i32 0, i32* %7
br label %35
35: ; preds = %47, %29
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %50
39: ; preds = %35
%40 = load i32, i32* %7
%41 = load i8**, i8*** %10
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = call i32 (i8*, i32, ...) @log_err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %40, i8* %45)
br label %47
47: ; preds = %39
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %35
50: ; preds = %35
%51 = call i32 (...) @abort()
unreachable
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@.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 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i32 (i8*, i32, ...) @log_err(i8* %11, i32 %13)
%15 = getelementptr inbounds [256 x i8*], [256 x i8*]* %4, i64 0, i64 0
%16 = call i32 @backtrace(i8** %15, i32 2048)
%17 = call i8** @backtrace_symbols(i8** %15, i32 %16)
%18 = icmp sgt i32 %16, 0
br i1 %18, label %19, label %29
19: ; preds = %3
%20 = zext i32 %16 to i64
br label %21
21: ; preds = %19, %21
%22 = phi i64 [ 0, %19 ], [ %27, %21 ]
%23 = getelementptr inbounds i8*, i8** %17, i64 %22
%24 = load i8*, i8** %23
%25 = trunc i64 %22 to i32
%26 = call i32 (i8*, i32, ...) @log_err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %25, i8* %24)
%27 = add nuw nsw i64 %22, 1
%28 = icmp eq i64 %27, %20
br i1 %28, label %29, label %21
29: ; preds = %21, %3
%30 = call i32 (...) @abort()
unreachable
}