#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_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int atoi(char *);
int atol(char *);
int /*<<< orphan*/ check_bytes(void *);
int /*<<< orphan*/ exit(int);
void *mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
scalar_t__ munmap(void *, unsigned long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_bytes(void *);
int /*<<< orphan*/ write_bytes(void *);
int main(int argc, char **argv) {
  void *addr;
  int ret;
  size_t length = (256UL * 1024 * 1024);
  int flags = (MAP_PRIVATE | MAP_ANONYMOUS | 0x40000);
  int shift = 0;
  if (argc > 1)
    length = atol(argv[1]) << 20;
  if (argc > 2) {
    shift = atoi(argv[2]);
    if (shift)
      flags |= (shift & 0x3f) << 26;
  }
  if (shift)
    printf("%u kB hugepages\n", 1 << shift);
  else
    printf("Default size hugepages\n");
  printf("Mapping %lu Mbytes\n", (unsigned long)length >> 20);
  addr = mmap((void *)(0x0UL), length, (PROT_READ | PROT_WRITE), flags, -1, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  printf("Returned address is %p\n", addr);
  check_bytes(addr);
  write_bytes(addr);
  ret = read_bytes(addr);
  if (munmap(addr, (256UL * 1024 * 1024))) {
    perror("munmap");
    exit(1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x10000000,-0x28(%rbp)
10
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    0x2e12(%rip),%rcx
or     (%rcx),%eax
or     $0x40000,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ff <main+0x5f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atol@plt>
shl    $0x14,%eax
cltq
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1237 <main+0x97>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1232 <main+0x92>
mov    -0x30(%rbp),%eax
and    $0x3f,%eax
shl    $0x1a,%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1237 <main+0x97>
cmpl   $0x0,-0x30(%rbp)
je     125e <main+0xbe>
mov    -0x30(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126c <main+0xcc>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
shr    $0x14,%rsi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    0x2d6b(%rip),%rax
mov    (%rax),%edx
mov    0x2d3a(%rip),%rax
or     (%rax),%edx
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d04(%rip),%rcx
cmp    (%rcx),%rax
jne    12db <main+0x13b>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <check_bytes@plt>
mov    -0x18(%rbp),%rdi
call   1060 <write_bytes@plt>
mov    -0x18(%rbp),%rdi
call   1030 <read_bytes@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x10000000,%esi
call   1070 <munmap@plt>
cmp    $0x0,%rax
je     1339 <main+0x199>
lea    0xd30(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e0a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e19(%rip),%rax
or     (%rax),%ebx
or     $0x40000,%ebx
mov    $0x10000000,%r14d
cmp    $0x2,%edi
jl     1220 <main+0x70>
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1090 <atol@plt>
shl    $0x14,%eax
movslq %eax,%r14
cmp    $0x2,%ebp
je     1220 <main+0x70>
mov    0x10(%r15),%rdi
call   10c0 <atoi@plt>
test   %eax,%eax
je     1220 <main+0x70>
mov    %eax,%ecx
shl    $0x1a,%ecx
or     %ecx,%ebx
mov    $0x1,%esi
mov    %eax,%ecx
shl    %cl,%esi
lea    0xde9(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    122c <main+0x7c>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rsi
shr    $0x14,%rsi
lea    0xdd7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2db0(%rip),%rax
mov    0x2d81(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebx,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d4e(%rip),%rax
cmp    (%rax),%rbx
je     12c1 <main+0x111>
lea    0xdac(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10a0 <check_bytes@plt>
mov    %rbx,%rdi
call   1070 <write_bytes@plt>
mov    %rbx,%rdi
call   1040 <read_bytes@plt>
mov    %eax,%ebp
mov    $0x10000000,%esi
mov    %rbx,%rdi
call   1080 <munmap@plt>
test   %rax,%rax
jne    12ca <main+0x11a>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5d(%rip),%rdi
jmp    12d1 <main+0x121>
lea    0xd71(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [17 x i8] c"%u kB hugepages\0A\00"
@.str.1 = [24 x i8] c"Default size hugepages\0A\00"
@.str.2 = [20 x i8] c"Mapping %lu Mbytes\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [24 x i8] c"Returned address is %p\0A\00"
@.str.5 = [7 x i8] c"munmap\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 268435456, i64* %8
%11 = load i32, i32* @MAP_PRIVATE
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = or i32 %11, %12
%14 = or i32 %13, 262144
store i32 %14, i32* %9
store i32 0, i32* %10
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atol(i8* %20)
%22 = shl i32 %21, 20
%23 = sext i32 %22 to i64
store i64 %23, i64* %8
br label %24
24: ; preds = %17, %2
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 2
br i1 %26, label %27, label %41
27: ; preds = %24
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %27
%35 = load i32, i32* %10
%36 = and i32 %35, 63
%37 = shl i32 %36, 26
%38 = load i32, i32* %9
%39 = or i32 %38, %37
store i32 %39, i32* %9
br label %40
40: ; preds = %34, %27
br label %41
41: ; preds = %40, %24
%42 = load i32, i32* %10
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* %10
%46 = shl i32 1, %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %46)
br label %50
48: ; preds = %41
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = load i64, i64* %8
%52 = lshr i64 %51, 20
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 %52)
%54 = load i64, i64* %8
%55 = load i32, i32* @PROT_READ
%56 = load i32, i32* @PROT_WRITE
%57 = or i32 %55, %56
%58 = load i32, i32* %9
%59 = call i8* @mmap(i8* null, i64 %54, i32 %57, i32 %58, i32 -1, i32 0)
store i8* %59, i8** %6
%60 = load i8*, i8** %6
%61 = load i8*, i8** @MAP_FAILED
%62 = icmp eq i8* %60, %61
br i1 %62, label %63, label %66
63: ; preds = %50
%64 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %50
%67 = load i8*, i8** %6
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %6
%70 = call i32 @check_bytes(i8* %69)
%71 = load i8*, i8** %6
%72 = call i32 @write_bytes(i8* %71)
%73 = load i8*, i8** %6
%74 = call i32 @read_bytes(i8* %73)
store i32 %74, i32* %7
%75 = load i8*, i8** %6
%76 = call i64 @munmap(i8* %75, i64 268435456)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %66
%79 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%80 = call i32 @exit(i32 1)
unreachable
81: ; preds = %66
%82 = load i32, i32* %7
ret i32 %82
}

LLVM IR (-O2):
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [17 x i8] c"%u kB hugepages\0A\00"
@.str.2 = [20 x i8] c"Mapping %lu Mbytes\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [24 x i8] c"Returned address is %p\0A\00"
@.str.5 = [7 x i8] c"munmap\00"
@str = [23 x i8] c"Default size hugepages\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @MAP_PRIVATE
%4 = load i32, i32* @MAP_ANONYMOUS
%5 = or i32 %3, %4
%6 = or i32 %5, 262144
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %25
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atol(i8* %10)
%12 = shl i32 %11, 20
%13 = sext i32 %12 to i64
%14 = icmp eq i32 %0, 2
br i1 %14, label %25, label %15
15: ; preds = %8
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = shl i32 %18, 26
%22 = or i32 %21, %6
%23 = shl nuw i32 1, %18
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %28
25: ; preds = %2, %15, %8
%26 = phi i64 [ %13, %8 ], [ %13, %15 ], [ 268435456, %2 ]
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %25, %20
%29 = phi i32 [ %6, %25 ], [ %22, %20 ]
%30 = phi i64 [ %26, %25 ], [ %13, %20 ]
%31 = lshr i64 %30, 20
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 %31)
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = tail call i8* @mmap(i8* null, i64 %30, i32 %35, i32 %29, i32 -1, i32 0)
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %42
39: ; preds = %28
%40 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %28
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%44 = tail call i32 @check_bytes(i8* %36)
%45 = tail call i32 @write_bytes(i8* %36)
%46 = tail call i32 @read_bytes(i8* %36)
%47 = tail call i64 @munmap(i8* %36, i64 268435456)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %42
%50 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%51 = tail call i32 @exit(i32 1)
unreachable
52: ; preds = %42
ret i32 %46
}