#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_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int mprotect(char *, int, int);
int /*<<< orphan*/ perror(char *);
int main(void) {
  char *ptr;
  int rc;
  ptr = mmap(NULL, (2 * 1024 * 1024), PROT_READ | PROT_WRITE,
             MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  rc = mprotect(ptr, (2 * 1024 * 1024), PROT_READ | PROT_EXEC);
  if (rc < 0) {
    perror("mprotect");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%edx
mov    0x2e61(%rip),%rax
or     (%rax),%edx
mov    0x2e50(%rip),%rax
mov    (%rax),%ecx
mov    0x2e67(%rip),%rax
or     (%rax),%ecx
mov    0x2e6e(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x200000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <main+0x81>
lea    0xe4e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    0x2dfb(%rip),%rax
or     (%rax),%edx
mov    $0x200000,%esi
call   1040 <mprotect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1204 <main+0xc4>
lea    0xe10(%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   %rbx
mov    0x2eb0(%rip),%rbx
mov    0x2e71(%rip),%rax
mov    (%rax),%edx
or     (%rbx),%edx
mov    0x2e5e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e7c(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x200000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
jne    1198 <main+0x58>
lea    0xe6a(%rip),%rdi
jmp    11bb <main+0x7b>
mov    0x2e31(%rip),%rcx
mov    (%rcx),%edx
or     (%rbx),%edx
mov    %rax,%rdi
mov    $0x200000,%esi
call   1040 <mprotect@plt>
test   %eax,%eax
jns    11ca <main+0x8a>
lea    0xe4a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@PROT_EXEC = global i32 0
@.str.1 = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %4, %5
%7 = load i32, i32* @MAP_PRIVATE
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_HUGETLB
%11 = or i32 %9, %10
%12 = call i8* @mmap(i32* null, i32 2097152, i32 %6, i32 %11, i32 -1, i32 0)
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i8*, i8** %2
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_EXEC
%23 = or i32 %21, %22
%24 = call i32 @mprotect(i8* %20, i32 2097152, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = 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_ANONYMOUS = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@PROT_EXEC = global i32 0
@.str.1 = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_PRIVATE
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_HUGETLB
%8 = or i32 %6, %7
%9 = tail call i8* @mmap(i32* null, i32 2097152, i32 %3, i32 %8, i32 -1, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %9, %10
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_EXEC
%18 = or i32 %17, %16
%19 = tail call i32 @mprotect(i8* %9, i32 2097152, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = tail call i32 @exit(i32 0)
unreachable
}