#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_EXEC;
int PROT_NONE;
int PROT_READ;
int /*<<< orphan*/ assert(int);
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
int mprotect(void *, int, int);
int /*<<< orphan*/ printf(char *);
int main() {
  int ret;
  printf("main()\n");
  void *alloc = mmap((void *)0x80000, 4096, PROT_READ,
                     MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  assert(alloc != MAP_FAILED);
  ret = mprotect(alloc, 4096, PROT_READ);
  assert(ret == 0);
  ret = mprotect(alloc, 4096, PROT_READ | PROT_EXEC);
  assert(ret == 0);
  ret = mprotect(alloc, 4096, PROT_NONE);
  assert(ret == 0);
  void *large_alloc = mmap((void *)0x80000 + 4096, 4096 * 10, PROT_READ,
                           MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  assert(large_alloc != MAP_FAILED);
  ret = mprotect(alloc, 4096, PROT_READ | PROT_EXEC);
  assert(ret == 0);
  ret = mprotect(alloc + 4096 * 2, 4096, PROT_READ | PROT_EXEC);
  assert(ret == 0);
  ret = mprotect(alloc + 4096 * 4, 4096, PROT_READ | PROT_EXEC);
  assert(ret == 0);
  ret = mprotect(alloc, 4096 * 6, PROT_READ | PROT_EXEC);
  assert(ret == 0);
  ret = mprotect(alloc, 4096, PROT_EXEC);
  assert(ret == 0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e9d(%rip),%rax
mov    (%rax),%edx
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6b(%rip),%rax
or     (%rax),%ecx
mov    0x2e42(%rip),%rax
or     (%rax),%ecx
mov    $0x80000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e11(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
mov    0x2d77(%rip),%rax
mov    (%rax),%ecx
mov    0x2d86(%rip),%rax
or     (%rax),%ecx
mov    0x2d5d(%rip),%rax
or     (%rax),%ecx
mov    $0x80000,%edi
add    $0x1000,%rdi
mov    $0xa000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
mov    0x2d21(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
add    $0x2000,%rdi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
mov    0x2ce6(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
add    $0x4000,%rdi
mov    0x2cdc(%rip),%rax
mov    (%rax),%edx
mov    0x2cab(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
mov    0x2c77(%rip),%rax
or     (%rax),%edx
mov    $0x6000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2c4c(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeae(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e9a(%rip),%r14
mov    0x2e5b(%rip),%rax
mov    0x2e6c(%rip),%r13
mov    0x0(%r13),%ecx
or     (%rax),%ecx
mov    (%r14),%edx
mov    0x2e3c(%rip),%rbp
or     0x0(%rbp),%ecx
mov    $0x80000,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2e0f(%rip),%r12
xor    %edi,%edi
cmp    (%r12),%rax
setne  %dil
call   1050 <assert@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x2dfc(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    (%r14),%edx
mov    0x0(%r13),%ecx
mov    0x2d9b(%rip),%rax
or     (%rax),%ecx
or     0x0(%rbp),%ecx
mov    $0x81000,%edi
mov    $0xa000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
xor    %edi,%edi
cmp    (%r12),%rax
setne  %dil
call   1050 <assert@plt>
mov    (%r15),%edx
or     (%r14),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
lea    0x2000(%rbx),%rdi
mov    (%r15),%edx
or     (%r14),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
lea    0x4000(%rbx),%rdi
mov    (%r15),%edx
or     (%r14),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    (%r15),%edx
or     (%r14),%edx
mov    %rbx,%rdi
mov    $0x6000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <mprotect@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"main()\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@PROT_EXEC = global i32 0
@PROT_NONE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @MAP_PRIVATE
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = or i32 %6, %7
%9 = load i32, i32* @MAP_FIXED
%10 = or i32 %8, %9
%11 = call i8* @mmap(i8* inttoptr (i64 524288 to i8*), i32 4096, i32 %5, i32 %10, i32 -1, i32 0)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp ne i8* %12, %13
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %2
%18 = load i32, i32* @PROT_READ
%19 = call i32 @mprotect(i8* %17, i32 4096, i32 %18)
store i32 %19, i32* %1
%20 = load i32, i32* %1
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i8*, i8** %2
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_EXEC
%27 = or i32 %25, %26
%28 = call i32 @mprotect(i8* %24, i32 4096, i32 %27)
store i32 %28, i32* %1
%29 = load i32, i32* %1
%30 = icmp eq i32 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i8*, i8** %2
%34 = load i32, i32* @PROT_NONE
%35 = call i32 @mprotect(i8* %33, i32 4096, i32 %34)
store i32 %35, i32* %1
%36 = load i32, i32* %1
%37 = icmp eq i32 %36, 0
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_PRIVATE
%42 = load i32, i32* @MAP_ANONYMOUS
%43 = or i32 %41, %42
%44 = load i32, i32* @MAP_FIXED
%45 = or i32 %43, %44
%46 = call i8* @mmap(i8* getelementptr (i8, i8* inttoptr (i64 524288 to i8*), i64 4096), i32 40960, i32 %40, i32 %45, i32 -1, i32 0)
store i8* %46, i8** %3
%47 = load i8*, i8** %3
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp ne i8* %47, %48
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i8*, i8** %2
%53 = load i32, i32* @PROT_READ
%54 = load i32, i32* @PROT_EXEC
%55 = or i32 %53, %54
%56 = call i32 @mprotect(i8* %52, i32 4096, i32 %55)
store i32 %56, i32* %1
%57 = load i32, i32* %1
%58 = icmp eq i32 %57, 0
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = load i8*, i8** %2
%62 = getelementptr i8, i8* %61, i64 8192
%63 = load i32, i32* @PROT_READ
%64 = load i32, i32* @PROT_EXEC
%65 = or i32 %63, %64
%66 = call i32 @mprotect(i8* %62, i32 4096, i32 %65)
store i32 %66, i32* %1
%67 = load i32, i32* %1
%68 = icmp eq i32 %67, 0
%69 = zext i1 %68 to i32
%70 = call i32 @assert(i32 %69)
%71 = load i8*, i8** %2
%72 = getelementptr i8, i8* %71, i64 16384
%73 = load i32, i32* @PROT_READ
%74 = load i32, i32* @PROT_EXEC
%75 = or i32 %73, %74
%76 = call i32 @mprotect(i8* %72, i32 4096, i32 %75)
store i32 %76, i32* %1
%77 = load i32, i32* %1
%78 = icmp eq i32 %77, 0
%79 = zext i1 %78 to i32
%80 = call i32 @assert(i32 %79)
%81 = load i8*, i8** %2
%82 = load i32, i32* @PROT_READ
%83 = load i32, i32* @PROT_EXEC
%84 = or i32 %82, %83
%85 = call i32 @mprotect(i8* %81, i32 24576, i32 %84)
store i32 %85, i32* %1
%86 = load i32, i32* %1
%87 = icmp eq i32 %86, 0
%88 = zext i1 %87 to i32
%89 = call i32 @assert(i32 %88)
%90 = load i8*, i8** %2
%91 = load i32, i32* @PROT_EXEC
%92 = call i32 @mprotect(i8* %90, i32 4096, i32 %91)
store i32 %92, i32* %1
%93 = load i32, i32* %1
%94 = icmp eq i32 %93, 0
%95 = zext i1 %94 to i32
%96 = call i32 @assert(i32 %95)
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@PROT_EXEC = global i32 0
@PROT_NONE = global i32 0
@str = [7 x i8] c"main()\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @MAP_PRIVATE
%4 = load i32, i32* @MAP_ANONYMOUS
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_FIXED
%7 = or i32 %5, %6
%8 = tail call i8* @mmap(i8* inttoptr (i64 524288 to i8*), i32 4096, i32 %2, i32 %7, i32 -1, i32 0)
%9 = load i8*, i8** @MAP_FAILED
%10 = icmp ne i8* %8, %9
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = load i32, i32* @PROT_READ
%14 = tail call i32 @mprotect(i8* %8, i32 4096, i32 %13)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_EXEC
%20 = or i32 %19, %18
%21 = tail call i32 @mprotect(i8* %8, i32 4096, i32 %20)
%22 = icmp eq i32 %21, 0
%23 = zext i1 %22 to i32
%24 = tail call i32 @assert(i32 %23)
%25 = load i32, i32* @PROT_NONE
%26 = tail call i32 @mprotect(i8* %8, i32 4096, i32 %25)
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i32
%29 = tail call i32 @assert(i32 %28)
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @MAP_PRIVATE
%32 = load i32, i32* @MAP_ANONYMOUS
%33 = or i32 %32, %31
%34 = load i32, i32* @MAP_FIXED
%35 = or i32 %33, %34
%36 = tail call i8* @mmap(i8* inttoptr (i64 528384 to i8*), i32 40960, i32 %30, i32 %35, i32 -1, i32 0)
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp ne i8* %36, %37
%39 = zext i1 %38 to i32
%40 = tail call i32 @assert(i32 %39)
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_EXEC
%43 = or i32 %42, %41
%44 = tail call i32 @mprotect(i8* %8, i32 4096, i32 %43)
%45 = icmp eq i32 %44, 0
%46 = zext i1 %45 to i32
%47 = tail call i32 @assert(i32 %46)
%48 = getelementptr i8, i8* %8, i64 8192
%49 = load i32, i32* @PROT_READ
%50 = load i32, i32* @PROT_EXEC
%51 = or i32 %50, %49
%52 = tail call i32 @mprotect(i8* %48, i32 4096, i32 %51)
%53 = icmp eq i32 %52, 0
%54 = zext i1 %53 to i32
%55 = tail call i32 @assert(i32 %54)
%56 = getelementptr i8, i8* %8, i64 16384
%57 = load i32, i32* @PROT_READ
%58 = load i32, i32* @PROT_EXEC
%59 = or i32 %58, %57
%60 = tail call i32 @mprotect(i8* %56, i32 4096, i32 %59)
%61 = icmp eq i32 %60, 0
%62 = zext i1 %61 to i32
%63 = tail call i32 @assert(i32 %62)
%64 = load i32, i32* @PROT_READ
%65 = load i32, i32* @PROT_EXEC
%66 = or i32 %65, %64
%67 = tail call i32 @mprotect(i8* %8, i32 24576, i32 %66)
%68 = icmp eq i32 %67, 0
%69 = zext i1 %68 to i32
%70 = tail call i32 @assert(i32 %69)
%71 = load i32, i32* @PROT_EXEC
%72 = tail call i32 @mprotect(i8* %8, i32 4096, i32 %71)
%73 = icmp eq i32 %72, 0
%74 = zext i1 %73 to i32
%75 = tail call i32 @assert(i32 %74)
ret i32 0
}