#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 /*<<< orphan*/ M_MMAP_THRESHOLD;
int PROT_EXEC;
int PROT_READ;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpagesize();
int mallopt(int /*<<< orphan*/, int);
int mprotect(void *, int, int);
int /*<<< orphan*/ perror(char *);
int posix_memalign(void **, int, int);
int /*<<< orphan*/ stderr;
int main(void) {
  void *ptr;
  int rc;
  int pagesize = getpagesize();
  rc = mallopt(M_MMAP_THRESHOLD, 512 * 1024);
  if (rc != 1) {
    fprintf(stderr, "mallopt failed: %d\n", rc);
    exit(1);
  }
  rc = posix_memalign(&ptr, pagesize, pagesize);
  if (rc) {
    fprintf(stderr, "posix_memalign failed: %d\n", rc);
    exit(1);
  }
  rc = mprotect(ptr, pagesize, 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   1070 <getpagesize@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    $0x80000,%esi
call   1030 <mallopt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
je     11cb <main+0x5b>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe44(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    -0x10(%rbp),%rdi
call   1090 <posix_memalign@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1209 <main+0x99>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe1a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
call   1050 <mprotect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124a <main+0xda>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %eax,%ebx
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    $0x80000,%esi
call   1030 <mallopt@plt>
cmp    $0x1,%eax
jne    11e0 <main+0x70>
lea    0x8(%rsp),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
call   1090 <posix_memalign@plt>
test   %eax,%eax
jne    11f2 <main+0x82>
mov    0x8(%rsp),%rdi
mov    0x2e44(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %ebx,%esi
call   1050 <mprotect@plt>
test   %eax,%eax
jns    1213 <main+0xa3>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edi
lea    0xe10(%rip),%rsi
jmp    1202 <main+0x92>
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%edi
lea    0xe12(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@M_MMAP_THRESHOLD = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"mallopt failed: %d\0A\00"
@.str.1 = [27 x i8] c"posix_memalign failed: %d\0A\00"
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@.str.2 = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @getpagesize()
store i32 %5, i32* %4
%6 = load i32, i32* @M_MMAP_THRESHOLD
%7 = call i32 @mallopt(i32 %6, i32 524288)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 1
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* %3
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %4
%17 = load i32, i32* %4
%18 = call i32 @posix_memalign(i8** %2, i32 %16, i32 %17)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = load i32, i32* %3
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = load i8*, i8** %2
%28 = load i32, i32* %4
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_EXEC
%31 = or i32 %29, %30
%32 = call i32 @mprotect(i8* %27, i32 %28, i32 %31)
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %26
%39 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@M_MMAP_THRESHOLD = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"mallopt failed: %d\0A\00"
@.str.1 = [27 x i8] c"posix_memalign failed: %d\0A\00"
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@.str.2 = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call i32 (...) @getpagesize()
%4 = load i32, i32* @M_MMAP_THRESHOLD
%5 = tail call i32 @mallopt(i32 %4, i32 524288)
%6 = icmp eq i32 %5, 1
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @posix_memalign(i8** %1, i32 %3, i32 %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i8*, i8** %1
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_EXEC
%22 = or i32 %21, %20
%23 = call i32 @mprotect(i8* %19, i32 %3, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = call i32 @exit(i32 0)
unreachable
}