#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__ {
  char *si_addr;
};
typedef TYPE_1__ siginfo_t;
typedef int pid_t;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ SIGKILL;
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ raise(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ system(char *);
void sigsegv_handler(int sig, siginfo_t *si, void *unused) {
  {
    char *touched_addr = si->si_addr;
    printf("Detected segfault at: %p\n", touched_addr);
    printf("Dumping process memory map\n");
    printf("==========================\n");
    char cmd[255];
    pid_t pid = getpid();
    snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", pid);
    system(cmd);
    printf("==========================\n");
    raise(SIGKILL);
  }
}


x86 assembly (clang, -O0):
<sigsegv_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x124(%rbp),%ecx
mov    $0xff,%esi
lea    0xe8d(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1050 <system@plt>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigsegv_handler>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    (%rsi),%rsi
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xeaa(%rip),%rdi
call   1040 <puts@plt>
lea    0xeb9(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe76(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <system@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i8* }
@.str = [26 x i8] c"Detected segfault at: %p\0A\00"
@.str.1 = [28 x i8] c"Dumping process memory map\0A\00"
@.str.2 = [28 x i8] c"==========================\0A\00"
@.str.3 = [18 x i8] c"cat /proc/%d/maps\00"
@SIGKILL = global i32 0
define void @sigsegv_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 [255 x i8]
%9 = alloca i32
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
%10 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %10, i32 0, i32 0
%12 = load i8*, i8** %11
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (...) @getpid()
store i32 %17, i32* %9
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%19 = load i32, i32* %9
%20 = call i32 @snprintf(i8* %18, i32 255, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%22 = call i32 @system(i8* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @SIGKILL
%25 = call i32 @raise(i32 %24)
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i8* }
@.str = [26 x i8] c"Detected segfault at: %p\0A\00"
@.str.3 = [18 x i8] c"cat /proc/%d/maps\00"
@SIGKILL = global i32 0
@str = [27 x i8] c"Dumping process memory map\00"
@str.5 = [27 x i8] c"==========================\00"
define void @sigsegv_handler(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture readnone %2) {
%4 = alloca [255 x i8]
%5 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%6 = load i8*, i8** %5
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%11 = tail call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %10, i32 255, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = call i32 @system(i8* %10)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%15 = load i32, i32* @SIGKILL
%16 = call i32 @raise(i32 %15)
ret void
}