#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*/ EXIT_FAILURE;
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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 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) {
    printf("Map failed in allocating protected space\n");
    exit(EXIT_FAILURE);
  }
  status = mprotect(p, page, PROT_NONE);
  if (status != 0) {
    printf("Protection failed in allocating protected space\n");
    exit(EXIT_FAILURE);
  }
  status = mprotect(p + page + aligned_size, page, PROT_NONE);
  if (status != 0) {
    printf("Protection failed in allocating protected space\n");
    exit(EXIT_FAILURE);
  }
  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    0x2e2c(%rip),%rax
or     (%rax),%edx
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e12(%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    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <allocate_protected_space+0x98>
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
call   1050 <mprotect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1224 <allocate_protected_space+0xd4>
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%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    0x2da8(%rip),%rax
mov    (%rax),%edx
call   1050 <mprotect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     126e <allocate_protected_space+0x11e>
lea    0xdcf(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%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   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
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    0x2e38(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e45(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2dfb(%rip),%rax
cmp    (%rax),%rbx
je     1208 <allocate_protected_space+0xb8>
mov    0x2e27(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <mprotect@plt>
test   %eax,%eax
jne    11ff <allocate_protected_space+0xaf>
movslq %r15d,%rax
add    %rax,%rbx
movslq %r14d,%rdi
add    %rbx,%rdi
mov    (%r12),%edx
mov    %r15d,%esi
call   1050 <mprotect@plt>
test   %eax,%eax
jne    11ff <allocate_protected_space+0xaf>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdfa(%rip),%rdi
jmp    120f <allocate_protected_space+0xbf>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%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 = [42 x i8] c"Map failed in allocating protected space\0A\00"
@EXIT_FAILURE = global i32 0
@PROT_NONE = global i32 0
@.str.1 = [49 x i8] c"Protection failed in allocating protected space\0A\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 %34
30: ; preds = %1
%31 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %1
%35 = load i8*, i8** %6
%36 = load i32, i32* %3
%37 = load i32, i32* @PROT_NONE
%38 = call i32 @mprotect(i8* %35, i32 %36, i32 %37)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %34
%42 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %34
%46 = load i8*, i8** %6
%47 = load i32, i32* %3
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i32, i32* %3
%54 = load i32, i32* @PROT_NONE
%55 = call i32 @mprotect(i8* %52, i32 %53, i32 %54)
store i32 %55, i32* %4
%56 = load i32, i32* %4
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %45
%59 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %45
%63 = load i8*, i8** %6
%64 = load i32, i32* %3
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
ret i8* %66
}

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