#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 */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_NONE;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ _watch_address(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int getpagesize();
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
void test_segfault_protector() {
  char *memory;
  int fd, alloc_size;
  alloc_size = getpagesize();
  fd = open("/dev/zero", O_RDONLY);
  memory = mmap(NULL, alloc_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
  assert(memory != NULL);
  close(fd);
  memory[0] = 0;
  _watch_address(memory, alloc_size, PROT_NONE);
  char *ref = &memory[0];
  munmap(memory, alloc_size);
}


x86 assembly (clang, -O0):
<test_segfault_protector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    0x2e1b(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
call   1030 <_watch_address@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1070 <munmap@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_segfault_protector>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %eax,%r14d
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    %ebp,%edi
call   1050 <close@plt>
movb   $0x0,(%rbx)
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <_watch_address@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <munmap@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_NONE = global i32 0
define void @test_segfault_protector() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = call i32 (...) @getpagesize()
store i32 %5, i32* %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %3
%9 = load i32, i32* @PROT_WRITE
%10 = load i32, i32* @MAP_PRIVATE
%11 = load i32, i32* %2
%12 = call i8* @mmap(i32* null, i32 %8, i32 %9, i32 %10, i32 %11, i32 0)
store i8* %12, i8** %1
%13 = load i8*, i8** %1
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
%19 = load i8*, i8** %1
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 0, i8* %20
%21 = load i8*, i8** %1
%22 = load i32, i32* %3
%23 = load i32, i32* @PROT_NONE
%24 = call i32 @_watch_address(i8* %21, i32 %22, i32 %23)
%25 = load i8*, i8** %1
%26 = getelementptr inbounds i8, i8* %25, i64 0
store i8* %26, i8** %4
%27 = load i8*, i8** %1
%28 = load i32, i32* %3
%29 = call i32 @munmap(i8* %27, i32 %28)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_NONE = global i32 0
define void @test_segfault_protector() {
%1 = tail call i32 (...) @getpagesize()
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @PROT_WRITE
%5 = load i32, i32* @MAP_PRIVATE
%6 = tail call i8* @mmap(i32* null, i32 %1, i32 %4, i32 %5, i32 %3, i32 0)
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32 @close(i32 %3)
store i8 0, i8* %6
%11 = load i32, i32* @PROT_NONE
%12 = tail call i32 @_watch_address(i8* %6, i32 %1, i32 %11)
%13 = tail call i32 @munmap(i8* %6, i32 %1)
ret void
}