#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 read_bytes(void *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write_bytes(void *);
int main(void) {
  void *addr;
  int fd, ret;
  fd = open("huge/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("huge/hugepagefile");
    exit(1);
  }
  printf("Returned address is %p\n", addr);
  check_bytes(addr);
  write_bytes(addr);
  ret = read_bytes(addr);
  munmap(addr, (256UL * 1024 * 1024));
  close(fd);
  unlink("huge/hugepagefile");
  return ret;
}


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    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x10000000,%esi
call   1090 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xd46(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e3d(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe33(%rip),%rdi
mov    $0x1ed,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     126f <main+0xbf>
mov    %eax,%r14d
mov    0x2e07(%rip),%rax
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de5(%rip),%rax
mov    (%rax),%ecx
mov    $0x10000000,%esi
xor    %edi,%edi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d9f(%rip),%rax
cmp    (%rax),%rbx
je     1285 <main+0xd5>
lea    0xdfe(%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    %eax,%ebp
mov    $0x10000000,%esi
mov    %rbx,%rdi
call   1090 <munmap@plt>
mov    %r14d,%edi
call   1070 <close@plt>
lea    0xd9d(%rip),%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd9c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd92(%rip),%rdi
call   10c0 <perror@plt>
lea    0xd68(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

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

LLVM IR (-O2):
@.str = [18 x i8] c"huge/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 %25
}