#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_SHARED;
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, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1,
             0);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  rc = mprotect(ptr, 4096, 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    0x2e80(%rip),%rax
mov    (%rax),%ecx
mov    0x2e67(%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,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <main+0x78>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1040 <mprotect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <main+0xbb>
lea    0xe19(%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    0x2e8e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <main+0x4f>
lea    0xe73(%rip),%rdi
jmp    11b2 <main+0x72>
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%edx
or     (%rbx),%edx
mov    %rax,%rdi
mov    $0x1000,%esi
call   1040 <mprotect@plt>
test   %eax,%eax
jns    11c1 <main+0x81>
lea    0xe53(%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_SHARED = global i32 0
@MAP_ANONYMOUS = 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_SHARED
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %7, %8
%10 = call i8* @mmap(i32* null, i32 4096, i32 %6, i32 %9, i32 -1, i32 0)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i8*, i8** %2
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_EXEC
%21 = or i32 %19, %20
%22 = call i32 @mprotect(i8* %18, i32 4096, i32 %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = 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_SHARED
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i32* 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 = %0
%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 = %0
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_EXEC
%16 = or i32 %15, %14
%17 = tail call i32 @mprotect(i8* %7, i32 4096, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = tail call i32 @exit(i32 0)
unreachable
}