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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
lea    0xe28(%rip),%rdi
mov    $0x1ed,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1205 <main+0x55>
lea    0xe1c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
mov    0x2dab(%rip),%rax
or     (%rax),%edx
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10000000,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%rax
jne    126f <main+0xbf>
lea    0xdca(%rip),%rdi
call   10c0 <perror@plt>
lea    0xda0(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xda9(%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    -0x10(%rbp),%rdi
call   1040 <read_bytes@plt>
mov    -0x10(%rbp),%rdi
mov    $0x10000000,%esi
call   1090 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xd49(%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    0x2e3e(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe34(%rip),%rdi
mov    $0x1ed,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     126c <main+0xbc>
mov    %eax,%ebp
mov    0x2e09(%rip),%rax
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    $0x10000000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2da1(%rip),%rax
cmp    (%rax),%rbx
je     1282 <main+0xd2>
lea    0xe00(%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    %rbx,%rdi
call   1040 <read_bytes@plt>
mov    $0x10000000,%esi
mov    %rbx,%rdi
call   1090 <munmap@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xda2(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd95(%rip),%rdi
call   10c0 <perror@plt>
lea    0xd6b(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"/mnt/hugepagefile\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 ([18 x i8], [18 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 268435456, 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 ([18 x i8], [18 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 = load i8*, i8** %2
%35 = call i32 @read_bytes(i8* %34)
%36 = load i8*, i8** %2
%37 = call i32 @munmap(i8* %36, i64 268435456)
%38 = load i32, i32* %3
%39 = call i32 @close(i32 %38)
%40 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"/mnt/hugepagefile\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 ([18 x i8], [18 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 268435456, 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 ([18 x i8], [18 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 @read_bytes(i8* %14)
%26 = tail call i32 @munmap(i8* %14, i64 268435456)
%27 = tail call i32 @close(i32 %4)
%28 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
ret i32 0
}