#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ check_bytes(void *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
void *mmap(void *, unsigned long, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, unsigned long);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, void *);
int /*<<< orphan*/ read_bytes(void *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write_bytes(void *);
int main(void) {
  void *addr;
  int fd;
  fd = open("/root/huge", O_CREAT | O_RDWR, 0755);
  if (fd < 0) {
    perror("Open failed");
    exit(1);
  }
  addr = mmap((void *)(0x0UL), (128UL * 1024 * 1024), (PROT_READ | PROT_WRITE),
              (MAP_SHARED), fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    unlink("/root/huge");
    exit(1);
  }
  printf("Returned address is %p\n", addr);
  check_bytes(addr);
  write_bytes(addr);
  sleep(1000);
  read_bytes(addr);
  munmap(addr, (128UL * 1024 * 1024));
  close(fd);
  unlink("/root/huge");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
lea    0xe18(%rip),%rdi
mov    $0x1ed,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1215 <main+0x55>
lea    0xe05(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    0x2d9b(%rip),%rax
or     (%rax),%edx
mov    0x2db2(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x8000000,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%rax
jne    127f <main+0xbf>
lea    0xdb3(%rip),%rdi
call   10c0 <perror@plt>
lea    0xd90(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xd92(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <check_bytes@plt>
mov    -0x10(%rbp),%rdi
call   1080 <write_bytes@plt>
mov    $0x3e8,%edi
call   10e0 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1040 <read_bytes@plt>
mov    -0x10(%rbp),%rdi
mov    $0x8000000,%esi
call   1090 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xd2f(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e2e(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe24(%rip),%rdi
mov    $0x1ed,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1286 <main+0xc6>
mov    %eax,%ebp
mov    0x2df9(%rip),%rax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd7(%rip),%rax
mov    (%rax),%ecx
mov    $0x8000000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d91(%rip),%rax
cmp    (%rax),%rbx
je     129c <main+0xdc>
lea    0xde9(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10b0 <check_bytes@plt>
mov    %rbx,%rdi
call   1080 <write_bytes@plt>
mov    $0x3e8,%edi
call   10e0 <sleep@plt>
mov    %rbx,%rdi
call   1040 <read_bytes@plt>
mov    $0x8000000,%esi
mov    %rbx,%rdi
call   1090 <munmap@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xd88(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd7e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd74(%rip),%rdi
call   10c0 <perror@plt>
lea    0xd51(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/root/huge\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"Open failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [24 x i8] c"Returned address is %p\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6, i32 493)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_SHARED
%18 = load i32, i32* %3
%19 = call i8* @mmap(i8* null, i64 134217728, i32 %16, i32 %17, i32 %18, i32 0)
store i8* %19, i8** %2
%20 = load i8*, i8** %2
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %13
%28 = load i8*, i8** %2
%29 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %2
%31 = call i32 @check_bytes(i8* %30)
%32 = load i8*, i8** %2
%33 = call i32 @write_bytes(i8* %32)
%34 = call i32 @sleep(i32 1000)
%35 = load i8*, i8** %2
%36 = call i32 @read_bytes(i8* %35)
%37 = load i8*, i8** %2
%38 = call i32 @munmap(i8* %37, i64 134217728)
%39 = load i32, i32* %3
%40 = call i32 @close(i32 %39)
%41 = call i32 @unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/root/huge\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"Open failed\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [24 x i8] c"Returned address is %p\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3, i32 493)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i8* @mmap(i8* null, i64 134217728, i32 %12, i32 %13, i32 %4, i32 0)
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %14, %15
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %9
%22 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%23 = tail call i32 @check_bytes(i8* %14)
%24 = tail call i32 @write_bytes(i8* %14)
%25 = tail call i32 @sleep(i32 1000)
%26 = tail call i32 @read_bytes(i8* %14)
%27 = tail call i32 @munmap(i8* %14, i64 134217728)
%28 = tail call i32 @close(i32 %4)
%29 = tail call i32 @unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
ret i32 0
}