#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_PRIVATE;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int getpagesize();
char *mmap(int /*<<< orphan*/, int, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int mprotect(char *, int, int /*<<< orphan*/);
char *allocate_protected_space(int size) {
  int page = getpagesize();
  int status;
  int aligned_size = ((size + page - 1) / page) * page;
  char *p = mmap(0, aligned_size + 2 * page, PROT_READ | PROT_WRITE,
                 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  if (p == MAP_FAILED) {
    exit(1);
  }
  status = mprotect(p, page, PROT_NONE);
  if (status != 0) {
    exit(1);
  }
  status = mprotect(p + page + aligned_size, page, PROT_NONE);
  if (status != 0) {
    exit(1);
  }
  return (p + page);
}


x86 assembly (clang, -O0):
<allocate_protected_space>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
add    -0x8(%rbp),%eax
sub    $0x1,%eax
cltd
idivl  -0x8(%rbp)
imul   -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
add    %eax,%esi
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2a(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    %r9d,%edi
mov    %r9d,%r8d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <allocate_protected_space+0x88>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
call   1040 <mprotect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11f4 <allocate_protected_space+0xb4>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
movslq -0x8(%rbp),%rax
add    %rax,%rdi
movslq -0x10(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
call   1040 <mprotect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     122e <allocate_protected_space+0xee>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
add    %rcx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_protected_space>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %eax,%r15d
lea    (%rbx,%r15,1),%r14d
add    $0xffffffff,%r14d
mov    %r14d,%eax
cltd
idiv   %r15d
sub    %edx,%r14d
lea    (%r14,%r15,2),%esi
mov    0x2e87(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e55(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e13(%rip),%rax
cmp    (%rax),%rbx
je     11ef <allocate_protected_space+0xaf>
mov    0x2e37(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <mprotect@plt>
test   %eax,%eax
jne    11ef <allocate_protected_space+0xaf>
movslq %r15d,%rax
add    %rax,%rbx
movslq %r14d,%rdi
add    %rbx,%rdi
mov    (%r12),%edx
mov    %r15d,%esi
call   1040 <mprotect@plt>
test   %eax,%eax
jne    11ef <allocate_protected_space+0xaf>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_NONE = global i32 0
define i8* @allocate_protected_space(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
%7 = call i32 (...) @getpagesize()
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = load i32, i32* %3
%10 = add nsw i32 %8, %9
%11 = sub nsw i32 %10, 1
%12 = load i32, i32* %3
%13 = sdiv i32 %11, %12
%14 = load i32, i32* %3
%15 = mul nsw i32 %13, %14
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = load i32, i32* %3
%18 = mul nsw i32 2, %17
%19 = add nsw i32 %16, %18
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %20, %21
%23 = load i32, i32* @MAP_ANONYMOUS
%24 = load i32, i32* @MAP_PRIVATE
%25 = or i32 %23, %24
%26 = call i8* @mmap(i32 0, i32 %19, i32 %22, i32 %25, i32 0, i32 0)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %1
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %1
%33 = load i8*, i8** %6
%34 = load i32, i32* %3
%35 = load i32, i32* @PROT_NONE
%36 = call i32 @mprotect(i8* %33, i32 %34, i32 %35)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32
%42 = load i8*, i8** %6
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i32, i32* %5
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %3
%50 = load i32, i32* @PROT_NONE
%51 = call i32 @mprotect(i8* %48, i32 %49, i32 %50)
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %41
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %41
%57 = load i8*, i8** %6
%58 = load i32, i32* %3
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
ret i8* %60
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_NONE = global i32 0
define i8* @allocate_protected_space(i32 %0) {
%2 = tail call i32 (...) @getpagesize()
%3 = add i32 %0, -1
%4 = add i32 %3, %2
%5 = srem i32 %4, %2
%6 = sub i32 %4, %5
%7 = shl nsw i32 %2, 1
%8 = add nsw i32 %6, %7
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = load i32, i32* @MAP_PRIVATE
%14 = or i32 %13, %12
%15 = tail call i8* @mmap(i32 0, i32 %8, i32 %11, i32 %14, i32 0, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = load i32, i32* @PROT_NONE
%22 = tail call i32 @mprotect(i8* %15, i32 %2, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %20
%27 = sext i32 %2 to i64
%28 = getelementptr inbounds i8, i8* %15, i64 %27
%29 = sext i32 %6 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i32, i32* @PROT_NONE
%32 = tail call i32 @mprotect(i8* %30, i32 %2, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %26
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %26
ret i8* %28
}