#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;
char *MAP_FAILED;
int MAP_HUGETLB;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, unsigned long, int, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  printf("Huge pages example program\n");
  unsigned long BUFF_SIZE = 1024 * 1024 * 5;
  int FLAGS = MAP_PRIVATE | MAP_HUGETLB | MAP_ANONYMOUS;
  char *buffer;
  buffer = mmap(NULL, BUFF_SIZE, PROT_READ | PROT_WRITE, FLAGS, -1, 0);
  if (buffer == MAP_FAILED) {
    perror("mmap");
    printf("FATAL ERROR\n");
    exit(1);
  }
  printf("Returned address is %p\n", buffer);
  for (int i = 0; i < BUFF_SIZE; i++) {
    buffer[i] = 'a';
  }
  char str[100];
  printf("Hit [enter] to terminate");
  fgets(str, 100, stdin);
  printf("Program exiting...\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x500000,-0x18(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    0x2e59(%rip),%rcx
or     (%rcx),%eax
mov    0x2e38(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    0x2e0f(%rip),%rax
or     (%rax),%edx
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <main+0xb4>
lea    0xe35(%rip),%rdi
call   1060 <perror@plt>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1245 <main+0xf5>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    121d <main+0xcd>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1050 <fgets@plt>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x70,%rsp
lea    0xeba(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e30(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e36(%rip),%rax
or     (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    0x2e16(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    $0x500000,%esi
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2de3(%rip),%rax
cmp    (%rax),%rbx
je     122d <main+0xbd>
lea    0xe2c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x500000,%edx
mov    %rbx,%rdi
mov    $0x61,%esi
call   1060 <memset@plt>
lea    0xe21(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
ret
lea    0xdcc(%rip),%rdi
call   1080 <perror@plt>
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Huge pages example program\0A\00"
@MAP_PRIVATE = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_ANONYMOUS = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [13 x i8] c"FATAL ERROR\0A\00"
@.str.3 = [24 x i8] c"Returned address is %p\0A\00"
@.str.4 = [25 x i8] c"Hit [enter] to terminate\00"
@stdin = global i32 0
@.str.5 = [20 x i8] c"Program exiting...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i64 5242880, i64* %6
%12 = load i32, i32* @MAP_PRIVATE
%13 = load i32, i32* @MAP_HUGETLB
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_ANONYMOUS
%16 = or i32 %14, %15
store i32 %16, i32* %7
%17 = load i64, i64* %6
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* %7
%22 = call i8* @mmap(i32* null, i64 %17, i32 %20, i32 %21, i32 -1, i32 0)
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %30
26: ; preds = %2
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %2
%31 = load i8*, i8** %8
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %31)
store i32 0, i32* %9
br label %33
33: ; preds = %43, %30
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = load i64, i64* %6
%37 = icmp ult i64 %35, %36
br i1 %37, label %38, label %46
38: ; preds = %33
%39 = load i8*, i8** %8
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 97, i8* %42
br label %43
43: ; preds = %38
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
br label %33
46: ; preds = %33
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%49 = load i32, i32* @stdin
%50 = call i32 @fgets(i8* %48, i32 100, i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@MAP_PRIVATE = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_ANONYMOUS = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.3 = [24 x i8] c"Returned address is %p\0A\00"
@.str.4 = [25 x i8] c"Hit [enter] to terminate\00"
@stdin = global i32 0
@str = [27 x i8] c"Huge pages example program\00"
@str.6 = [19 x i8] c"Program exiting...\00"
@str.7 = [12 x i8] c"FATAL ERROR\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%5 = load i32, i32* @MAP_PRIVATE
%6 = load i32, i32* @MAP_HUGETLB
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %7, %8
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = tail call i8* @mmap(i32* null, i64 5242880, i32 %12, i32 %9, i32 -1, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %22, i32 100, i32 %24)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
ret i32 0
}