#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*/);
scalar_t__ 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) {
    if (munmap(addr, (16 * 1024 * 1024))) {
      perror("munmap");
      return 1;
    }
  }
  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     11e2 <test_body+0xa2>
mov    -0x10(%rbp),%rdi
mov    $0x1000000,%esi
call   1050 <munmap@plt>
cmp    $0x0,%rax
je     11dd <test_body+0x9d>
lea    0xe34(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1279 <test_body+0x139>
jmp    11e2 <test_body+0xa2>
mov    -0x10(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
mov    0x2dd9(%rip),%rax
mov    (%rax),%ecx
mov    0x2db8(%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    0x2d8c(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <test_body+0x11d>
mov    -0x10(%rbp),%rsi
lea    0xdc7(%rip),%rdi
call   1040 <printf@plt>
lea    0xdd0(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1279 <test_body+0x139>
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     11b3 <test_body+0x73>
mov    $0xa0000000,%edi
mov    $0x1000000,%esi
call   1050 <munmap@plt>
test   %rax,%rax
jne    1217 <test_body+0xd7>
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     11fd <test_body+0xbd>
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    0xe03(%rip),%rdi
mov    $0xa0000000,%esi
call   1040 <printf@plt>
lea    0xe07(%rip),%rdi
jmp    121e <test_body+0xde>
lea    0xde2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
jmp    11f1 <test_body+0xb1>

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 = [7 x i8] c"munmap\00"
@.str.1 = [21 x i8] c"Mapping failed @ %p\0A\00"
@.str.2 = [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 %24
17: ; preds = %0
%18 = load i8*, i8** %2
%19 = call i64 @munmap(i8* %18, i32 16777216)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %44
23: ; preds = %17
br label %24
24: ; preds = %23, %0
%25 = load i8*, i8** %2
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_ANONYMOUS
%30 = load i32, i32* @MAP_PRIVATE
%31 = or i32 %29, %30
%32 = call i8* @mmap(i8* %25, i32 16777216, i32 %28, i32 %31, i32 -1, i32 0)
store i8* %32, i8** %3
%33 = load i8*, i8** %3
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %40
36: ; preds = %24
%37 = load i8*, i8** %2
%38 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %44
40: ; preds = %24
%41 = load i8*, i8** %3
store i8 15, i8* %41
%42 = load i8*, i8** %2
%43 = call i64 @munmap(i8* %42, i32 16777216)
store i32 0, i32* %1
br label %44
44: ; preds = %40, %36, %21
%45 = load i32, i32* %1
ret i32 %45
}

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 = [7 x i8] c"munmap\00"
@.str.1 = [21 x i8] c"Mapping failed @ %p\0A\00"
@.str.2 = [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 %17, label %12
12: ; preds = %0
%13 = tail call i64 @munmap(i8* inttoptr (i64 2684354560 to i8*), i32 16777216)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %32
17: ; preds = %12, %0
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_ANONYMOUS
%22 = load i32, i32* @MAP_PRIVATE
%23 = or i32 %22, %21
%24 = tail call i8* @mmap(i8* inttoptr (i64 2684354560 to i8*), i32 16777216, i32 %20, i32 %23, i32 -1, i32 0)
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* inttoptr (i64 2684354560 to i8*))
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %32
30: ; preds = %17
store i8 15, i8* %24
%31 = tail call i64 @munmap(i8* inttoptr (i64 2684354560 to i8*), i32 16777216)
br label %32
32: ; preds = %30, %27, %15
%33 = phi i32 [ 1, %15 ], [ 1, %27 ], [ 0, %30 ]
ret i32 %33
}