#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_ANON;
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*/ printf(char *);
char *allocate_protected_space(int size) {
  int i;
  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_ANON | MAP_PRIVATE, 0, 0);
  if (p == MAP_FAILED) {
    printf("Could not map memory!\n");
    exit(1);
  }
  status = mprotect(p, page, PROT_NONE);
  if (status != 0) {
    printf("First protection page failed to initialize!\n");
    exit(1);
  }
  status = mprotect(p + page + aligned_size, page, PROT_NONE);
  if (status != 0) {
    printf("Second protection page failed to initialize!\n");
    exit(1);
  }
  for (i = 0; i < size / 4; i++) {
    *(p + page + i * 4 + 0) = 0xDE;
    *(p + page + i * 4 + 1) = 0xAD;
    *(p + page + i * 4 + 2) = 0xBE;
    *(p + page + i * 4 + 3) = 0xEF;
  }
  return (p + page);
}


x86 assembly (clang, -O0):
<allocate_protected_space>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
add    -0xc(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0xc(%rbp)
imul   -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
add    %eax,%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    0x2e3c(%rip),%rax
or     (%rax),%edx
mov    0x2e2b(%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,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    11e4 <allocate_protected_space+0x94>
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
call   1050 <mprotect@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     121c <allocate_protected_space+0xcc>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0xc(%rbp),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
call   1050 <mprotect@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1262 <allocate_protected_space+0x112>
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    12f8 <allocate_protected_space+0x1a8>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    -0x8(%rbp),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
movb   $0xde,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    -0x8(%rbp),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
movb   $0xad,0x1(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    -0x8(%rbp),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
movb   $0xbe,0x2(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    -0x8(%rbp),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
movb   $0xef,0x3(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1269 <allocate_protected_space+0x119>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_protected_space>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %eax,%ebx
lea    (%r15,%rbx,1),%r13d
add    $0x1,%r13d
mov    %r13d,%eax
cltd
idiv   %ebx
sub    %edx,%r13d
lea    0x0(%r13,%rbx,2),%esi
mov    0x2e74(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e32(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
mov    0x2e00(%rip),%rax
cmp    (%rax),%r12
je     125f <allocate_protected_space+0x10f>
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %ebx,%esi
call   1050 <mprotect@plt>
test   %eax,%eax
jne    1268 <allocate_protected_space+0x118>
movslq %ebx,%rbp
lea    (%r12,%rbp,1),%r14
movslq %r13d,%rdi
add    %r14,%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%esi
call   1050 <mprotect@plt>
test   %eax,%eax
jne    1271 <allocate_protected_space+0x121>
cmp    $0x4,%r15d
jl     124d <allocate_protected_space+0xfd>
shr    $0x2,%r15d
cmp    $0x1,%r15d
jne    1211 <allocate_protected_space+0xc1>
xor    %eax,%eax
jmp    123f <allocate_protected_space+0xef>
mov    %r15d,%ecx
and    $0xfffffffe,%ecx
lea    (%r12,%rbp,1),%rdx
add    $0x7,%rdx
xor    %eax,%eax
movabs $0xefbeaddeefbeadde,%rsi
ad be ef
nopl   0x0(%rax,%rax,1)
mov    %rsi,-0x7(%rdx,%rax,1)
add    $0x8,%rax
add    $0xfffffffffffffffe,%rcx
jne    1230 <allocate_protected_space+0xe0>
test   $0x1,%r15b
je     124d <allocate_protected_space+0xfd>
movl   $0xefbeadde,(%r14,%rax,1)
ef
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf3(%rip),%rdi
jmp    1278 <allocate_protected_space+0x128>
lea    0xdbe(%rip),%rdi
jmp    1278 <allocate_protected_space+0x128>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [23 x i8] c"Could not map memory!\0A\00"
@PROT_NONE = global i32 0
@.str.1 = [45 x i8] c"First protection page failed to initialize!\0A\00"
@.str.2 = [46 x i8] c"Second protection page failed to initialize!\0A\00"
define i8* @allocate_protected_space(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %2
%8 = call i32 (...) @getpagesize()
store i32 %8, i32* %4
%9 = load i32, i32* %2
%10 = load i32, i32* %4
%11 = add nsw i32 %9, %10
%12 = add nsw i32 %11, 1
%13 = load i32, i32* %4
%14 = sdiv i32 %12, %13
%15 = load i32, i32* %4
%16 = mul nsw i32 %14, %15
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = load i32, i32* %4
%19 = mul nsw i32 2, %18
%20 = add nsw i32 %17, %19
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_ANON
%25 = load i32, i32* @MAP_PRIVATE
%26 = or i32 %24, %25
%27 = call i8* @mmap(i32 0, i32 %20, i32 %23, i32 %26, i32 0, i32 0)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %34
31: ; preds = %1
%32 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %1
%35 = load i8*, i8** %7
%36 = load i32, i32* %4
%37 = load i32, i32* @PROT_NONE
%38 = call i32 @mprotect(i8* %35, i32 %36, i32 %37)
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = load i8*, i8** %7
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i32, i32* %4
%53 = load i32, i32* @PROT_NONE
%54 = call i32 @mprotect(i8* %51, i32 %52, i32 %53)
store i32 %54, i32* %5
%55 = load i32, i32* %5
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %44
%58 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %44
store i32 0, i32* %3
br label %61
61: ; preds = %103, %60
%62 = load i32, i32* %3
%63 = load i32, i32* %2
%64 = sdiv i32 %63, 4
%65 = icmp slt i32 %62, %64
br i1 %65, label %66, label %106
66: ; preds = %61
%67 = load i8*, i8** %7
%68 = load i32, i32* %4
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
%71 = load i32, i32* %3
%72 = mul nsw i32 %71, 4
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
%75 = getelementptr inbounds i8, i8* %74, i64 0
store i8 -34, i8* %75
%76 = load i8*, i8** %7
%77 = load i32, i32* %4
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = load i32, i32* %3
%81 = mul nsw i32 %80, 4
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %79, i64 %82
%84 = getelementptr inbounds i8, i8* %83, i64 1
store i8 -83, i8* %84
%85 = load i8*, i8** %7
%86 = load i32, i32* %4
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %85, i64 %87
%89 = load i32, i32* %3
%90 = mul nsw i32 %89, 4
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %88, i64 %91
%93 = getelementptr inbounds i8, i8* %92, i64 2
store i8 -66, i8* %93
%94 = load i8*, i8** %7
%95 = load i32, i32* %4
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %94, i64 %96
%98 = load i32, i32* %3
%99 = mul nsw i32 %98, 4
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %97, i64 %100
%102 = getelementptr inbounds i8, i8* %101, i64 3
store i8 -17, i8* %102
br label %103
103: ; preds = %66
%104 = load i32, i32* %3
%105 = add nsw i32 %104, 1
store i32 %105, i32* %3
br label %61
106: ; preds = %61
%107 = load i8*, i8** %7
%108 = load i32, i32* %4
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i8, i8* %107, i64 %109
ret i8* %110
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@PROT_NONE = global i32 0
@str = [45 x i8] c"Second protection page failed to initialize!\00"
@str.3 = [44 x i8] c"First protection page failed to initialize!\00"
@str.4 = [22 x i8] c"Could not map memory!\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_ANON
%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 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.4, 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.3, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
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 %36, label %45
36: ; preds = %28
%37 = icmp sgt i32 %0, 3
br i1 %37, label %38, label %75
38: ; preds = %36
%39 = lshr i32 %0, 2
%40 = zext i32 %39 to i64
%41 = and i64 %40, 1
%42 = icmp eq i32 %39, 1
br i1 %42, label %67, label %43
43: ; preds = %38
%44 = and i64 %40, 1073741822
br label %48
45: ; preds = %28
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %48, %43
%49 = phi i64 [ 0, %43 ], [ %62, %48 ]
%50 = phi i64 [ 0, %43 ], [ %63, %48 ]
%51 = shl nsw i64 %49, 2
%52 = getelementptr inbounds i8, i8* %30, i64 %51
store i8 -34, i8* %52
%53 = getelementptr inbounds i8, i8* %52, i64 1
store i8 -83, i8* %53
%54 = getelementptr inbounds i8, i8* %52, i64 2
store i8 -66, i8* %54
%55 = getelementptr inbounds i8, i8* %52, i64 3
store i8 -17, i8* %55
%56 = shl i64 %49, 2
%57 = or i64 %56, 4
%58 = getelementptr inbounds i8, i8* %30, i64 %57
store i8 -34, i8* %58
%59 = getelementptr inbounds i8, i8* %58, i64 1
store i8 -83, i8* %59
%60 = getelementptr inbounds i8, i8* %58, i64 2
store i8 -66, i8* %60
%61 = getelementptr inbounds i8, i8* %58, i64 3
store i8 -17, i8* %61
%62 = add nuw nsw i64 %49, 2
%63 = add i64 %50, 2
%64 = icmp eq i64 %63, %44
br i1 %64, label %65, label %48
65: ; preds = %48
%66 = shl i64 %62, 2
br label %67
67: ; preds = %65, %38
%68 = phi i64 [ 0, %38 ], [ %66, %65 ]
%69 = icmp eq i64 %41, 0
br i1 %69, label %75, label %70
70: ; preds = %67
%71 = getelementptr inbounds i8, i8* %30, i64 %68
store i8 -34, i8* %71
%72 = getelementptr inbounds i8, i8* %71, i64 1
store i8 -83, i8* %72
%73 = getelementptr inbounds i8, i8* %71, i64 2
store i8 -66, i8* %73
%74 = getelementptr inbounds i8, i8* %71, i64 3
store i8 -17, i8* %74
br label %75
75: ; preds = %70, %67, %36
ret i8* %30
}