#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;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int);
void *mmap(void *, int, int /*<<< orphan*/, int, int, int /*<<< orphan*/);
scalar_t__ munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int main(int argc, char **argv) {
  void *p1;
  void *p2;
  if ((p1 = mmap(0, 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  if (munmap(p1, 4096) != 0) {
    perror("munmap");
    exit(1);
  }
  if ((p2 = mmap(p1 + 1, 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1,
                 0)) == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  if (munmap(p2, 4096) != 0) {
    perror("munmap");
    exit(1);
  }
  if ((p2 = mmap(p1 + 1, 4096, PROT_READ,
                 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  if (munmap(p2, 4096) != 0) {
    perror("munmap");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e71(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <main+0x72>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
cmp    $0x0,%rax
je     11e0 <main+0xa0>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddf(%rip),%rax
or     (%rax),%ecx
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
jne    1240 <main+0x100>
lea    0xdcf(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
cmp    $0x0,%rax
je     126e <main+0x12e>
lea    0xda6(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d51(%rip),%rax
or     (%rax),%ecx
mov    0x2d30(%rip),%rax
or     (%rax),%ecx
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d08(%rip),%rcx
cmp    (%rcx),%rax
jne    12d7 <main+0x197>
lea    0xd38(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
cmp    $0x0,%rax
je     1305 <main+0x1c5>
lea    0xd0f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    0x2ea8(%rip),%r14
mov    (%r14),%edx
mov    0x2e76(%rip),%r15
mov    0x2e7f(%rip),%r13
mov    0x0(%r13),%ecx
or     (%r15),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e3c(%rip),%r12
cmp    (%r12),%rax
jne    1196 <main+0x56>
lea    0xe6f(%rip),%rdi
jmp    123e <main+0xfe>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
test   %rax,%rax
jne    1237 <main+0xf7>
add    $0x1,%rbx
mov    (%r14),%edx
mov    0x0(%r13),%ecx
or     (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    (%r12),%rax
je     118a <main+0x4a>
mov    %rax,%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
test   %rax,%rax
jne    1237 <main+0xf7>
mov    0x0(%r13),%ecx
or     (%r15),%ecx
mov    (%r14),%edx
mov    0x2dcc(%rip),%rax
or     (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    (%r12),%rax
je     118a <main+0x4a>
mov    %rax,%rdi
mov    $0x1000,%esi
call   1040 <munmap@plt>
test   %rax,%rax
jne    1237 <main+0xf7>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xdc7(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [7 x i8] c"munmap\00"
@MAP_FIXED = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @MAP_PRIVATE
%10 = load i32, i32* @MAP_ANONYMOUS
%11 = or i32 %9, %10
%12 = call i8* @mmap(i8* null, i32 4096, i32 %8, i32 %11, i32 -1, i32 0)
store i8* %12, i8** %6
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = call i64 @munmap(i8* %19, i32 4096)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = load i8*, i8** %6
%27 = getelementptr i8, i8* %26, i64 1
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @MAP_PRIVATE
%30 = load i32, i32* @MAP_ANONYMOUS
%31 = or i32 %29, %30
%32 = call i8* @mmap(i8* %27, i32 4096, i32 %28, i32 %31, i32 -1, i32 0)
store i8* %32, i8** %7
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %25
%39 = load i8*, i8** %7
%40 = call i64 @munmap(i8* %39, i32 4096)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %38
%46 = load i8*, i8** %6
%47 = getelementptr i8, i8* %46, i64 1
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @MAP_PRIVATE
%50 = load i32, i32* @MAP_ANONYMOUS
%51 = or i32 %49, %50
%52 = load i32, i32* @MAP_FIXED
%53 = or i32 %51, %52
%54 = call i8* @mmap(i8* %47, i32 4096, i32 %48, i32 %53, i32 -1, i32 0)
store i8* %54, i8** %7
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %60
57: ; preds = %45
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %45
%61 = load i8*, i8** %7
%62 = call i64 @munmap(i8* %61, i32 4096)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %60
%68 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [7 x i8] c"munmap\00"
@MAP_FIXED = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @MAP_PRIVATE
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i8* null, i32 4096, i32 %3, i32 %6, i32 -1, i32 0)
%8 = load i8*, i8** @MAP_FAILED
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i64 @munmap(i8* %7, i32 4096)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = getelementptr i8, i8* %7, i64 1
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_PRIVATE
%23 = load i32, i32* @MAP_ANONYMOUS
%24 = or i32 %23, %22
%25 = tail call i8* @mmap(i8* %20, i32 4096, i32 %21, i32 %24, i32 -1, i32 0)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = tail call i64 @munmap(i8* %25, i32 4096)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @MAP_PRIVATE
%40 = load i32, i32* @MAP_ANONYMOUS
%41 = or i32 %40, %39
%42 = load i32, i32* @MAP_FIXED
%43 = or i32 %41, %42
%44 = tail call i8* @mmap(i8* %20, i32 4096, i32 %38, i32 %43, i32 -1, i32 0)
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %44, %45
br i1 %46, label %47, label %50
47: ; preds = %37
%48 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %37
%51 = tail call i64 @munmap(i8* %44, i32 4096)
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %50
%54 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%55 = tail call i32 @exit(i32 1)
unreachable
56: ; preds = %50
%57 = tail call i32 @exit(i32 0)
unreachable
}