#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 PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
scalar_t__ munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  void *a1;
  if ((a1 = mmap((void *)0x200000, 4096, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0)) ==
      MAP_FAILED) {
    perror("mmap1");
    exit(1);
  }
  if (munmap(a1, 4096) < 0) {
    perror("munmap1");
    exit(1);
  }
  if (mmap((void *)0x100000, 4096, PROT_READ | PROT_WRITE,
           MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
    perror("mmap2");
    exit(1);
  }
  if (mmap((void *)0x100000, 4096, PROT_READ | PROT_WRITE,
           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
    perror("mmap2");
    exit(1);
  }
  printf("success\n");
  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    0x2e8b(%rip),%rax
mov    (%rax),%edx
mov    0x2e5a(%rip),%rax
or     (%rax),%edx
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
mov    0x2e38(%rip),%rax
or     (%rax),%ecx
mov    0x2e4f(%rip),%rax
or     (%rax),%ecx
mov    $0x200000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11d5 <main+0x85>
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
cmp    $0x0,%rax
jge    1203 <main+0xb3>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    0x2dbd(%rip),%rax
or     (%rax),%edx
mov    0x2dac(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9b(%rip),%rax
or     (%rax),%ecx
mov    0x2db2(%rip),%rax
or     (%rax),%ecx
mov    $0x100000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%rax
jne    126e <main+0x11e>
lea    0xdaf(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
mov    0x2d52(%rip),%rax
or     (%rax),%edx
mov    0x2d41(%rip),%rax
mov    (%rax),%ecx
mov    0x2d50(%rip),%rax
or     (%rax),%ecx
mov    $0x100000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%rax
jne    12d0 <main+0x180>
lea    0xd4d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xd3d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e96(%rip),%r14
mov    0x2e67(%rip),%r12
mov    (%r12),%edx
or     (%r14),%edx
mov    0x2e51(%rip),%r15
mov    0x2e42(%rip),%rbp
mov    0x0(%rbp),%ecx
or     (%r15),%ecx
mov    0x2e55(%rip),%rbx
or     (%rbx),%ecx
mov    $0x200000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2e0c(%rip),%r13
cmp    0x0(%r13),%rax
jne    11bb <main+0x6b>
lea    0xe47(%rip),%rdi
jmp    1234 <main+0xe4>
mov    %rax,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
test   %rax,%rax
jns    11d6 <main+0x86>
lea    0xe32(%rip),%rdi
jmp    1234 <main+0xe4>
mov    (%r12),%edx
or     (%r14),%edx
mov    0x0(%rbp),%ecx
or     (%r15),%ecx
or     (%rbx),%ecx
mov    $0x100000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    0x0(%r13),%rax
je     122d <main+0xdd>
mov    (%r12),%edx
or     (%r14),%edx
mov    (%rbx),%ecx
or     (%r15),%ecx
mov    $0x100000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    0x0(%r13),%rax
jne    1243 <main+0xf3>
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

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