#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 *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_bytes(void *, size_t);
int /*<<< orphan*/ write_bytes(void *, size_t);
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 - 10));
  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, length);
  ret = read_bytes(addr, length);
  if (munmap(addr, length)) {
    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     1261 <main+0xc1>
mov    -0x30(%rbp),%ecx
sub    $0xa,%ecx
mov    $0x1,%esi
shl    %cl,%esi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126f <main+0xcf>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
shr    $0x14,%rsi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
mov    0x2d37(%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    0x2d01(%rip),%rcx
cmp    (%rcx),%rax
jne    12de <main+0x13e>
lea    0xd6e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <check_bytes@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <write_bytes@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <read_bytes@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <munmap@plt>
cmp    $0x0,%rax
je     1343 <main+0x1a3>
lea    0xd26(%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),%ebp
mov    0x2e19(%rip),%rax
or     (%rax),%ebp
or     $0x40000,%ebp
mov    $0x10000000,%r15d
cmp    $0x2,%edi
jl     1221 <main+0x71>
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
call   1090 <atol@plt>
shl    $0x14,%eax
movslq %eax,%r15
cmp    $0x2,%ebx
je     1221 <main+0x71>
mov    0x10(%r14),%rdi
call   10c0 <atoi@plt>
test   %eax,%eax
je     1221 <main+0x71>
mov    %eax,%ecx
shl    $0x1a,%ecx
or     %ecx,%ebp
lea    -0xa(%rax),%ecx
mov    $0x1,%esi
shl    %cl,%esi
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    122d <main+0x7d>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rsi
shr    $0x14,%rsi
lea    0xdd6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2daf(%rip),%rax
mov    0x2d80(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
mov    0x2d4d(%rip),%rax
cmp    (%rax),%rbp
je     12c6 <main+0x116>
lea    0xdab(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   10a0 <check_bytes@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <write_bytes@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <read_bytes@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <munmap@plt>
test   %rax,%rax
jne    12cf <main+0x11f>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd58(%rip),%rdi
jmp    12d6 <main+0x126>
lea    0xd6c(%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 %49
44: ; preds = %41
%45 = load i32, i32* %10
%46 = sub nsw i32 %45, 10
%47 = shl i32 1, %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %47)
br label %51
49: ; preds = %41
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %44
%52 = load i64, i64* %8
%53 = lshr i64 %52, 20
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 %53)
%55 = load i64, i64* %8
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @PROT_WRITE
%58 = or i32 %56, %57
%59 = load i32, i32* %9
%60 = call i8* @mmap(i8* null, i64 %55, i32 %58, i32 %59, i32 -1, i32 0)
store i8* %60, i8** %6
%61 = load i8*, i8** %6
%62 = load i8*, i8** @MAP_FAILED
%63 = icmp eq i8* %61, %62
br i1 %63, label %64, label %67
64: ; preds = %51
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %51
%68 = load i8*, i8** %6
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %68)
%70 = load i8*, i8** %6
%71 = call i32 @check_bytes(i8* %70)
%72 = load i8*, i8** %6
%73 = load i64, i64* %8
%74 = call i32 @write_bytes(i8* %72, i64 %73)
%75 = load i8*, i8** %6
%76 = load i64, i64* %8
%77 = call i32 @read_bytes(i8* %75, i64 %76)
store i32 %77, i32* %7
%78 = load i8*, i8** %6
%79 = load i64, i64* %8
%80 = call i64 @munmap(i8* %78, i64 %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %67
%83 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %67
%86 = load i32, i32* %7
ret i32 %86
}

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 %26
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 %26, 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 %26, label %20
20: ; preds = %15
%21 = shl i32 %18, 26
%22 = or i32 %21, %6
%23 = add nsw i32 %18, -10
%24 = shl nuw i32 1, %23
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %29
26: ; preds = %2, %15, %8
%27 = phi i64 [ %13, %8 ], [ %13, %15 ], [ 268435456, %2 ]
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %26, %20
%30 = phi i32 [ %6, %26 ], [ %22, %20 ]
%31 = phi i64 [ %27, %26 ], [ %13, %20 ]
%32 = lshr i64 %31, 20
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 %32)
%34 = load i32, i32* @PROT_READ
%35 = load i32, i32* @PROT_WRITE
%36 = or i32 %35, %34
%37 = tail call i8* @mmap(i8* null, i64 %31, i32 %36, i32 %30, i32 -1, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %43
40: ; preds = %29
%41 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %29
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %37)
%45 = tail call i32 @check_bytes(i8* %37)
%46 = tail call i32 @write_bytes(i8* %37, i64 %31)
%47 = tail call i32 @read_bytes(i8* %37, i64 %31)
%48 = tail call i64 @munmap(i8* %37, i64 %31)
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %43
%51 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %43
ret i32 %47
}