#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 MAP_ANON;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
size_t getpagesize();
void *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
void make_segv() {
  volatile int *p;
  int i;
  size_t pagesize = getpagesize();
  void *ptr = mmap(0, 2 * pagesize, (PROT_READ | PROT_WRITE),
                   (MAP_PRIVATE | MAP_ANON), -1, 0);
  char *unmap = ((char *)ptr) + pagesize + 0x20;
  munmap((char *)ptr + pagesize, pagesize);
  p = (volatile int *)unmap;
  i = *p;
}


x86 assembly (clang, -O0):
<make_segv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
mov    0x2ea4(%rip),%rax
mov    (%rax),%edx
mov    0x2e7b(%rip),%rax
or     (%rax),%edx
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e61(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <munmap@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_segv>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %rax,%r14
lea    (%rax,%rax,1),%esi
mov    0x2eb0(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
lea    (%rax,%r14,1),%rdi
mov    %r14,%rsi
call   1040 <munmap@plt>
mov    0x20(%rbx,%r14,1),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
define void @make_segv() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = call i64 (...) @getpagesize()
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = mul i64 2, %7
%9 = trunc i64 %8 to i32
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* @MAP_ANON
%15 = or i32 %13, %14
%16 = call i8* @mmap(i32 0, i32 %9, i32 %12, i32 %15, i32 -1, i32 0)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = load i64, i64* %3
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 32
store i8* %20, i8** %5
%21 = load i8*, i8** %4
%22 = load i64, i64* %3
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i64, i64* %3
%25 = call i32 @munmap(i8* %23, i64 %24)
%26 = load i8*, i8** %5
%27 = bitcast i8* %26 to i32*
store i32* %27, i32** %1
%28 = load i32*, i32** %1
%29 = load volatile i32, i32* %28
store i32 %29, i32* %2
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
define void @make_segv() {
%1 = tail call i64 (...) @getpagesize()
%2 = trunc i64 %1 to i32
%3 = shl i32 %2, 1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_PRIVATE
%8 = load i32, i32* @MAP_ANON
%9 = or i32 %8, %7
%10 = tail call i8* @mmap(i32 0, i32 %3, i32 %6, i32 %9, i32 -1, i32 0)
%11 = getelementptr inbounds i8, i8* %10, i64 %1
%12 = getelementptr inbounds i8, i8* %11, i64 32
%13 = tail call i32 @munmap(i8* %11, i64 %1)
%14 = bitcast i8* %12 to i32*
%15 = load volatile i32, i32* %14
ret void
}