#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;
char *mmap(void *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, void *);
int test_body(void) {
  void *addr;
  char *p;
  addr = (void *)0xa0000000;
  p = mmap(addr, (16 * 1024 * 1024), PROT_READ | PROT_WRITE,
           MAP_HUGETLB | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (p != MAP_FAILED) {
    munmap(addr, (16 * 1024 * 1024));
  }
  p = mmap(addr, (16 * 1024 * 1024), PROT_READ | PROT_WRITE,
           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (p == MAP_FAILED) {
    printf("Mapping failed @ %p\n", addr);
    perror("mmap");
    return 1;
  }
  *p = 0xf;
  munmap(addr, (16 * 1024 * 1024));
  return 0;
}


x86 assembly (clang, -O0):
<test_body>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0xa0000000,%eax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9c(%rip),%rax
mov    (%rax),%edx
mov    0x2e63(%rip),%rax
or     (%rax),%edx
mov    0x2e7a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e61(%rip),%rax
or     (%rax),%ecx
mov    0x2e40(%rip),%rax
or     (%rax),%ecx
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
je     11bb <test_body+0x7b>
mov    -0x10(%rbp),%rdi
mov    $0x1000000,%esi
call   1050 <munmap@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    0x2df9(%rip),%rax
or     (%rax),%edx
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddf(%rip),%rax
or     (%rax),%ecx
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    1236 <test_body+0xf6>
mov    -0x10(%rbp),%rsi
lea    0xde7(%rip),%rdi
call   1040 <printf@plt>
lea    0xdf0(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1252 <test_body+0x112>
mov    -0x18(%rbp),%rax
movb   $0xf,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1000000,%esi
call   1050 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_body>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    0x2ea8(%rip),%r14
mov    0x2e71(%rip),%r12
mov    (%r12),%edx
or     (%r14),%edx
mov    0x2e83(%rip),%rax
mov    0x2e6c(%rip),%r15
mov    (%r15),%ecx
or     (%rax),%ecx
mov    0x2e48(%rip),%rbx
or     (%rbx),%ecx
mov    $0xa0000000,%edi
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e1f(%rip),%r13
cmp    0x0(%r13),%rax
je     11ae <test_body+0x6e>
mov    $0xa0000000,%edi
mov    $0x1000000,%esi
call   1050 <munmap@plt>
mov    (%r12),%edx
or     (%r14),%edx
mov    (%rbx),%ecx
or     (%r15),%ecx
xor    %ebx,%ebx
mov    $0xa0000000,%edi
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    0x0(%r13),%rax
je     11f8 <test_body+0xb8>
movb   $0xf,(%rax)
mov    $0xa0000000,%edi
mov    $0x1000000,%esi
call   1050 <munmap@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe01(%rip),%rdi
mov    $0xa0000000,%esi
call   1040 <printf@plt>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
jmp    11ec <test_body+0xac>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [21 x i8] c"Mapping failed @ %p\0A\00"
@.str.1 = [5 x i8] c"mmap\00"
define i32 @test_body() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i8* inttoptr (i64 2684354560 to i8*), i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %5, %6
%8 = load i32, i32* @MAP_HUGETLB
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_PRIVATE
%12 = or i32 %10, %11
%13 = call i8* @mmap(i8* %4, i32 16777216, i32 %7, i32 %12, i32 -1, i32 0)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp ne i8* %14, %15
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = load i8*, i8** %2
%19 = call i32 @munmap(i8* %18, i32 16777216)
br label %20
20: ; preds = %17, %0
%21 = load i8*, i8** %2
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @MAP_ANONYMOUS
%26 = load i32, i32* @MAP_PRIVATE
%27 = or i32 %25, %26
%28 = call i8* @mmap(i8* %21, i32 16777216, i32 %24, i32 %27, i32 -1, i32 0)
store i8* %28, i8** %3
%29 = load i8*, i8** %3
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %36
32: ; preds = %20
%33 = load i8*, i8** %2
%34 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %33)
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %40
36: ; preds = %20
%37 = load i8*, i8** %3
store i8 15, i8* %37
%38 = load i8*, i8** %2
%39 = call i32 @munmap(i8* %38, i32 16777216)
store i32 0, i32* %1
br label %40
40: ; preds = %36, %32
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [21 x i8] c"Mapping failed @ %p\0A\00"
@.str.1 = [5 x i8] c"mmap\00"
define i32 @test_body() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_HUGETLB
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_PRIVATE
%8 = or i32 %6, %7
%9 = tail call i8* @mmap(i8* inttoptr (i64 2684354560 to i8*), i32 16777216, i32 %3, i32 %8, i32 -1, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %9, %10
br i1 %11, label %14, label %12
12: ; preds = %0
%13 = tail call i32 @munmap(i8* inttoptr (i64 2684354560 to i8*), i32 16777216)
br label %14
14: ; preds = %12, %0
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_ANONYMOUS
%19 = load i32, i32* @MAP_PRIVATE
%20 = or i32 %19, %18
%21 = tail call i8* @mmap(i8* inttoptr (i64 2684354560 to i8*), i32 16777216, i32 %17, i32 %20, i32 -1, i32 0)
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* inttoptr (i64 2684354560 to i8*))
%26 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
27: ; preds = %14
store i8 15, i8* %21
%28 = tail call i32 @munmap(i8* inttoptr (i64 2684354560 to i8*), i32 16777216)
br label %29
29: ; preds = %27, %24
%30 = phi i32 [ 1, %24 ], [ 0, %27 ]
ret i32 %30
}