#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 /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ unlink(char *);
int main() {
  int fd;
  char *addr;
  int i, j, count = 0;
  fd = open("/mnt/huge/test", O_CREAT | O_RDWR, 0755);
  if (fd < 0) {
    perror("Err: ");
    return -1;
  }
  addr = mmap(0, (2 * 1024 * 1024), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
    perror("Err: ");
    close(fd);
    unlink("/mnt/huge/test");
    return -1;
  }
  for (i = 0; i < (2 * 1024 * 1024); i++) {
    addr[i] = 'A';
  }
  for (j = 0; j < (2 * 1024 * 1024); j++)
    if (addr[j] == 'A')
      count++;
  if (count == j)
    printf("hugetlbfs read sucess.\n");
  else
    printf("hugetlbfs read failed.\n");
  munmap(addr, (2 * 1024 * 1024));
  close(fd);
  unlink("/mnt/huge/test");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
lea    0xe61(%rip),%rdi
mov    $0x1ed,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ce <main+0x5e>
lea    0xe52(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <main+0x196>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x200000,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%rax
jne    1241 <main+0xd1>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xdd0(%rip),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <main+0x196>
movl   $0x0,-0x14(%rbp)
cmpl   $0x200000,-0x14(%rbp)
jge    126f <main+0xff>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x41,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1248 <main+0xd8>
movl   $0x0,-0x18(%rbp)
cmpl   $0x200000,-0x18(%rbp)
jge    12b4 <main+0x144>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x41,%eax
jne    12a1 <main+0x131>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12a6 <main+0x136>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1276 <main+0x106>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    12d1 <main+0x161>
lea    0xd4e(%rip),%rdi
call   1050 <printf@plt>
jmp    12dd <main+0x16d>
lea    0xd55(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x200000,%esi
call   1070 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xd06(%rip),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe94(%rip),%rdi
mov    $0x1ed,%edx
call   1090 <open@plt>
test   %eax,%eax
js     136e <main+0x1ee>
mov    %eax,%ebp
mov    0x2e39(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x200000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2dd1(%rip),%rax
cmp    (%rax),%rbx
je     1381 <main+0x201>
mov    $0x200000,%edx
mov    %rbx,%rdi
mov    $0x41,%esi
call   1060 <memset@plt>
pxor   %xmm4,%xmm4
movdqa 0xdf2(%rip),%xmm5
mov    $0xd,%eax
movdqa 0xdf5(%rip),%xmm0
movdqa 0xdfd(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movd   -0xc(%rbx,%rax,1),%xmm3
movd   -0x8(%rbx,%rax,1),%xmm2
pcmpeqb %xmm0,%xmm3
punpcklbw %xmm3,%xmm3
punpcklwd %xmm3,%xmm3
pand   %xmm1,%xmm3
paddd  %xmm5,%xmm3
pcmpeqb %xmm0,%xmm2
punpcklbw %xmm2,%xmm2
punpcklwd %xmm2,%xmm2
pand   %xmm1,%xmm2
paddd  %xmm4,%xmm2
cmp    $0x1ffffd,%rax
je     12ad <main+0x12d>
movd   -0x4(%rbx,%rax,1),%xmm4
movd   (%rbx,%rax,1),%xmm5
pcmpeqb %xmm0,%xmm4
punpcklbw %xmm4,%xmm4
punpcklwd %xmm4,%xmm4
pand   %xmm1,%xmm4
pcmpeqb %xmm0,%xmm5
punpcklbw %xmm5,%xmm5
punpcklwd %xmm5,%xmm5
pand   %xmm1,%xmm5
paddd  %xmm4,%xmm3
paddd  %xmm5,%xmm2
add    $0x10,%rax
movdqa %xmm3,%xmm5
movdqa %xmm2,%xmm4
jmp    1230 <main+0xb0>
paddd  %xmm3,%xmm2
pshufd $0xee,%xmm2,%xmm0
paddd  %xmm2,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
xor    %eax,%eax
cmpb   $0x41,0x1ffff9(%rbx)
movd   %xmm1,%ecx
sete   %al
add    %ecx,%eax
xor    %ecx,%ecx
cmpb   $0x41,0x1ffffa(%rbx)
sete   %cl
add    %eax,%ecx
xor    %eax,%eax
cmpb   $0x41,0x1ffffb(%rbx)
sete   %al
add    %ecx,%eax
xor    %ecx,%ecx
cmpb   $0x41,0x1ffffc(%rbx)
sete   %cl
add    %eax,%ecx
xor    %eax,%eax
cmpb   $0x41,0x1ffffd(%rbx)
sete   %al
add    %ecx,%eax
xor    %ecx,%ecx
cmpb   $0x41,0x1ffffe(%rbx)
sete   %cl
add    %eax,%ecx
xor    %eax,%eax
cmpb   $0x41,0x1fffff(%rbx)
sete   %al
add    %ecx,%eax
cmp    $0x200000,%eax
lea    0xd27(%rip),%rax
lea    0xd09(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    $0x200000,%esi
call   1080 <munmap@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xcd0(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xcca(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1367 <main+0x1e7>
lea    0xcb7(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xc95(%rip),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%eax
jmp    1367 <main+0x1e7>

LLVM IR (-O0):
@.str = [15 x i8] c"/mnt/huge/test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"Err: \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [24 x i8] c"hugetlbfs read sucess.\0A\00"
@.str.3 = [24 x i8] c"hugetlbfs read failed.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9, i32 493)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
15: ; preds = %0
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_SHARED
%20 = load i32, i32* %2
%21 = call i8* @mmap(i32 0, i32 2097152, i32 %18, i32 %19, i32 %20, i32 0)
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %30
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
%29 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %75
30: ; preds = %15
store i32 0, i32* %4
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 2097152
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i8*, i8** %3
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 65, i8* %38
br label %39
39: ; preds = %34
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %31
42: ; preds = %31
store i32 0, i32* %5
br label %43
43: ; preds = %58, %42
%44 = load i32, i32* %5
%45 = icmp slt i32 %44, 2097152
br i1 %45, label %46, label %61
46: ; preds = %43
%47 = load i8*, i8** %3
%48 = load i32, i32* %5
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 65
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %57
57: ; preds = %54, %46
br label %58
58: ; preds = %57
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %43
61: ; preds = %43
%62 = load i32, i32* %6
%63 = load i32, i32* %5
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %69
67: ; preds = %61
%68 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %69
69: ; preds = %67, %65
%70 = load i8*, i8** %3
%71 = call i32 @munmap(i8* %70, i32 2097152)
%72 = load i32, i32* %2
%73 = call i32 @close(i32 %72)
%74 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %75
75: ; preds = %69, %25, %13
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
@.str = [15 x i8] c"/mnt/huge/test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"Err: \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@str = [23 x i8] c"hugetlbfs read failed.\00"
@str.4 = [23 x i8] c"hugetlbfs read sucess.\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 ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3, i32 493)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %98
8: ; preds = %0
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i8* @mmap(i32 0, i32 2097152, i32 %11, i32 %12, i32 %4, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %50, label %16
16: ; preds = %8
br label %17
17: ; preds = %35, %16
%18 = phi i64 [ 0, %16 ], [ %49, %35 ]
%19 = phi <4 x i32> [ <i32 1, i32 0, i32 0, i32 0>, %16 ], [ %47, %35 ]
%20 = phi <4 x i32> [ zeroinitializer, %16 ], [ %48, %35 ]
%21 = or i64 %18, 1
%22 = getelementptr inbounds i8, i8* %13, i64 %21
%23 = bitcast i8* %22 to <4 x i8>*
%24 = load <4 x i8>, <4 x i8>* %23
%25 = getelementptr inbounds i8, i8* %22, i64 4
%26 = bitcast i8* %25 to <4 x i8>*
%27 = load <4 x i8>, <4 x i8>* %26
%28 = icmp eq <4 x i8> %24, <i8 65, i8 65, i8 65, i8 65>
%29 = icmp eq <4 x i8> %27, <i8 65, i8 65, i8 65, i8 65>
%30 = zext <4 x i1> %28 to <4 x i32>
%31 = zext <4 x i1> %29 to <4 x i32>
%32 = add <4 x i32> %19, %30
%33 = add <4 x i32> %20, %31
%34 = icmp eq i64 %18, 2097136
br i1 %34, label %54, label %35
35: ; preds = %17
%36 = or i64 %18, 9
%37 = getelementptr inbounds i8, i8* %13, i64 %36
%38 = bitcast i8* %37 to <4 x i8>*
%39 = load <4 x i8>, <4 x i8>* %38
%40 = getelementptr inbounds i8, i8* %37, i64 4
%41 = bitcast i8* %40 to <4 x i8>*
%42 = load <4 x i8>, <4 x i8>* %41
%43 = icmp eq <4 x i8> %39, <i8 65, i8 65, i8 65, i8 65>
%44 = icmp eq <4 x i8> %42, <i8 65, i8 65, i8 65, i8 65>
%45 = zext <4 x i1> %43 to <4 x i32>
%46 = zext <4 x i1> %44 to <4 x i32>
%47 = add <4 x i32> %32, %45
%48 = add <4 x i32> %33, %46
%49 = add nuw nsw i64 %18, 16
br label %17
50: ; preds = %8
%51 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%52 = tail call i32 @close(i32 %4)
%53 = tail call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %98
54: ; preds = %17
%55 = add <4 x i32> %33, %32
%56 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %55)
%57 = getelementptr inbounds i8, i8* %13, i64 2097145
%58 = load i8, i8* %57
%59 = icmp eq i8 %58, 65
%60 = zext i1 %59 to i32
%61 = add nuw nsw i32 %56, %60
%62 = getelementptr inbounds i8, i8* %13, i64 2097146
%63 = load i8, i8* %62
%64 = icmp eq i8 %63, 65
%65 = zext i1 %64 to i32
%66 = add nuw nsw i32 %61, %65
%67 = getelementptr inbounds i8, i8* %13, i64 2097147
%68 = load i8, i8* %67
%69 = icmp eq i8 %68, 65
%70 = zext i1 %69 to i32
%71 = add nuw nsw i32 %66, %70
%72 = getelementptr inbounds i8, i8* %13, i64 2097148
%73 = load i8, i8* %72
%74 = icmp eq i8 %73, 65
%75 = zext i1 %74 to i32
%76 = add nuw nsw i32 %71, %75
%77 = getelementptr inbounds i8, i8* %13, i64 2097149
%78 = load i8, i8* %77
%79 = icmp eq i8 %78, 65
%80 = zext i1 %79 to i32
%81 = add nuw nsw i32 %76, %80
%82 = getelementptr inbounds i8, i8* %13, i64 2097150
%83 = load i8, i8* %82
%84 = icmp eq i8 %83, 65
%85 = zext i1 %84 to i32
%86 = add nuw nsw i32 %81, %85
%87 = getelementptr inbounds i8, i8* %13, i64 2097151
%88 = load i8, i8* %87
%89 = icmp eq i8 %88, 65
%90 = zext i1 %89 to i32
%91 = add nuw nsw i32 %86, %90
%92 = icmp eq i32 %91, 2097152
%93 = select i1 %92, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0)
%94 = tail call i32 @puts(i8* dereferenceable(1) %93)
%95 = tail call i32 @munmap(i8* %13, i32 2097152)
%96 = tail call i32 @close(i32 %4)
%97 = tail call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %98
98: ; preds = %54, %50, %6
%99 = phi i32 [ -1, %6 ], [ -1, %50 ], [ 0, %54 ]
ret i32 %99
}