#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);
long getdefaulthugesize();
char *mmap(int /*<<< orphan*/ *, long, int, int, int, int /*<<< orphan*/);
int mprotect(char *, long, int);
int /*<<< orphan*/ perror(char *);
int main(void) {
  char *ptr;
  int rc;
  long length = getdefaulthugesize();
  ptr = mmap(NULL, length, PROT_READ | PROT_WRITE,
             MAP_SHARED | MAP_ANONYMOUS | 0x40000, -1, 0);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  rc = mprotect(ptr, length, 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    $0x0,%al
call   1040 <getdefaulthugesize@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
mov    0x2e42(%rip),%rax
or     (%rax),%edx
mov    0x2e61(%rip),%rax
mov    (%rax),%ecx
mov    0x2e48(%rip),%rax
or     (%rax),%ecx
or     $0x40000,%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <main+0x88>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
call   1050 <mprotect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    121a <main+0xca>
lea    0xdfa(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getdefaulthugesize@plt>
mov    %rax,%rbx
mov    0x2e93(%rip),%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
or     (%r14),%edx
mov    0x2e70(%rip),%rax
mov    (%rax),%ecx
mov    0x2e57(%rip),%rax
or     (%rax),%ecx
or     $0x40000,%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    11b1 <main+0x61>
lea    0xe51(%rip),%rdi
jmp    11d3 <main+0x83>
mov    0x2e18(%rip),%rcx
mov    (%rcx),%edx
or     (%r14),%edx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <mprotect@plt>
test   %eax,%eax
jns    11e2 <main+0x92>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <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
%4 = alloca i64
store i32 0, i32* %1
%5 = call i64 (...) @getdefaulthugesize()
store i64 %5, i64* %4
%6 = load i64, i64* %4
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_SHARED
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = or i32 %10, %11
%13 = or i32 %12, 262144
%14 = call i8* @mmap(i32* null, i64 %6, i32 %9, i32 %13, i32 -1, i32 0)
store i8* %14, i8** %2
%15 = load i8*, i8** %2
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = load i8*, i8** %2
%23 = load i64, i64* %4
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_EXEC
%26 = or i32 %24, %25
%27 = call i32 @mprotect(i8* %22, i64 %23, i32 %26)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %21
%34 = 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 = tail call i64 (...) @getdefaulthugesize()
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_SHARED
%6 = load i32, i32* @MAP_ANONYMOUS
%7 = or i32 %5, %6
%8 = or i32 %7, 262144
%9 = tail call i8* @mmap(i32* null, i64 %1, i32 %4, 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, i64 %1, 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
}