#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*/);
int /*<<< orphan*/ perror(char *);
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) {
    perror("map");
    exit(1);
  }
  status = mprotect(p, page, PROT_NONE);
  if (status != 0) {
    perror("mprotect");
    exit(status);
  }
  status = mprotect(p + page + aligned_size, page, PROT_NONE);
  if (status != 0) {
    perror("mprotect");
    exit(status);
  }
  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   1060 <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    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    0x2e34(%rip),%rax
or     (%rax),%edx
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1a(%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    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    11e4 <allocate_protected_space+0x94>
lea    0xe2b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
call   1040 <mprotect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     121a <allocate_protected_space+0xca>
lea    0xdf7(%rip),%rdi
call   1050 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <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    0x2db2(%rip),%rax
mov    (%rax),%edx
call   1040 <mprotect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     125e <allocate_protected_space+0x10e>
lea    0xdb3(%rip),%rdi
call   1050 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <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   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <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    0x2e77(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e45(%rip),%rax
mov    0x2e26(%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    0x2e03(%rip),%rax
cmp    (%rax),%rbx
je     1211 <allocate_protected_space+0xc1>
mov    0x2e27(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <mprotect@plt>
test   %eax,%eax
jne    11fc <allocate_protected_space+0xac>
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    11fc <allocate_protected_space+0xac>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xdff(%rip),%rdi
call   1050 <perror@plt>
mov    %ebp,%edi
call   1070 <exit@plt>
lea    0xde8(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <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
@.str = [4 x i8] c"map\00"
@PROT_NONE = global i32 0
@.str.1 = [9 x i8] c"mprotect\00"
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 %33
30: ; preds = %1
%31 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %1
%34 = load i8*, i8** %6
%35 = load i32, i32* %3
%36 = load i32, i32* @PROT_NONE
%37 = call i32 @mprotect(i8* %34, i32 %35, i32 %36)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* %4
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %33
%45 = load i8*, i8** %6
%46 = load i32, i32* %3
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i32, i32* %3
%53 = load i32, i32* @PROT_NONE
%54 = call i32 @mprotect(i8* %51, i32 %52, i32 %53)
store i32 %54, i32* %4
%55 = load i32, i32* %4
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %44
%58 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%59 = load i32, i32* %4
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %44
%62 = load i8*, i8** %6
%63 = load i32, i32* %3
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
ret i8* %65
}

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
@.str = [4 x i8] c"map\00"
@PROT_NONE = global i32 0
@.str.1 = [9 x i8] c"mprotect\00"
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 %21
18: ; preds = %1
%19 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = load i32, i32* @PROT_NONE
%23 = tail call i32 @mprotect(i8* %15, i32 %2, i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @exit(i32 %23)
unreachable
28: ; preds = %21
%29 = sext i32 %2 to i64
%30 = getelementptr inbounds i8, i8* %15, i64 %29
%31 = sext i32 %6 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i32, i32* @PROT_NONE
%34 = tail call i32 @mprotect(i8* %32, i32 %2, i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %28
%37 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%38 = tail call i32 @exit(i32 %34)
unreachable
39: ; preds = %28
ret i8* %30
}