#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 */
char *MAP_FAILED;
int MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, unsigned long, int, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, unsigned long);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  printf("Shared Huge pages example program\n");
  int fd = open(argv[1], O_CREAT | O_RDWR, 777);
  unsigned long BUFF_SIZE = 1024 * 1024 * 5;
  int FLAGS = MAP_SHARED;
  char *buffer;
  buffer = mmap(NULL, BUFF_SIZE, PROT_READ | PROT_WRITE, FLAGS, fd, 0);
  close(fd);
  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);
  munmap(buffer, BUFF_SIZE);
  printf("Program exiting...\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
mov    $0x309,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
movq   $0x500000,-0x20(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rsi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rax
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <main+0xcf>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1290 <main+0x110>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1268 <main+0xe8>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <munmap@plt>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
mov    0x2e38(%rip),%rax
mov    0x2e09(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x309,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
mov    0x2e09(%rip),%rax
mov    0x2dca(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    $0x500000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d9b(%rip),%rax
cmp    (%rax),%rbx
je     1283 <main+0xe3>
lea    0xde4(%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    0xdd9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x64,%esi
call   1080 <fgets@plt>
mov    $0x500000,%esi
mov    %rbx,%rdi
call   1090 <munmap@plt>
lea    0xde3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd76(%rip),%rdi
call   10b0 <perror@plt>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"Shared Huge pages example program\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@MAP_SHARED = 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 i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_RDWR
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %15, i32 %18, i32 777)
store i32 %19, i32* %6
store i64 5242880, i64* %7
%20 = load i32, i32* @MAP_SHARED
store i32 %20, i32* %8
%21 = load i64, i64* %7
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = call i8* @mmap(i32* null, i64 %21, i32 %24, i32 %25, i32 %26, i32 0)
store i8* %27, i8** %9
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %9
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %37
33: ; preds = %2
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %2
%38 = load i8*, i8** %9
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %38)
store i32 0, i32* %10
br label %40
40: ; preds = %50, %37
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = load i64, i64* %7
%44 = icmp ult i64 %42, %43
br i1 %44, label %45, label %53
45: ; preds = %40
%46 = load i8*, i8** %9
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8 97, i8* %49
br label %50
50: ; preds = %45
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
br label %40
53: ; preds = %40
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%55 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%56 = load i32, i32* @stdin
%57 = call i32 @fgets(i8* %55, i32 100, i32 %56)
%58 = load i8*, i8** %9
%59 = load i64, i64* %7
%60 = call i32 @munmap(i8* %58, i64 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@MAP_SHARED = 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 = [34 x i8] c"Shared 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 %1) {
%3 = alloca [100 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %6, i32 %9, i32 777)
%11 = load i32, i32* @MAP_SHARED
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = tail call i8* @mmap(i32* null, i64 5242880, i32 %14, i32 %11, i32 %10, i32 0)
%16 = tail call i32 @close(i32 %10)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %15, %17
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @stdin
%28 = call i32 @fgets(i8* %25, i32 100, i32 %27)
%29 = call i32 @munmap(i8* %15, i64 5242880)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
ret i32 0
}