#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;
int MAP_PRIVATE;
int PAGE_SIZE;
int PROT_EXEC;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
void *mmap(int /*<<< orphan*/, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ mprotect(void *, int, int /*<<< orphan*/);
void *create_isolated_section_rwx(size_t len) {
  assert((len & ~(PAGE_SIZE - 1)) == len);
  size_t len_ = len + PAGE_SIZE * 2;
  void *map = mmap(0, len_, PROT_READ | PROT_WRITE | PROT_EXEC,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  mprotect(map, PAGE_SIZE, PROT_NONE);
  mprotect(map + len + PAGE_SIZE, PAGE_SIZE, PROT_NONE);
  void *ret = map + PAGE_SIZE;
  memset(ret, 0, len);
  return ret;
}


x86 assembly (clang, -O0):
<create_isolated_section_rwx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e81(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
movslq %ecx,%rcx
and    %rcx,%rax
cmp    -0x8(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    0x2e14(%rip),%rax
or     (%rax),%edx
mov    0x2e1b(%rip),%rax
or     (%rax),%edx
mov    0x2dfa(%rip),%rax
mov    (%rax),%ecx
mov    0x2e11(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
call   1060 <mprotect@plt>
mov    -0x18(%rbp),%rdi
add    -0x8(%rbp),%rdi
mov    0x2dd8(%rip),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
call   1060 <mprotect@plt>
mov    -0x18(%rbp),%rax
mov    0x2db0(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_isolated_section_rwx>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e86(%rip),%r12
xor    %eax,%eax
sub    (%r12),%eax
cltq
not    %rax
xor    %edi,%edi
test   %rax,%r14
sete   %dil
call   1050 <assert@plt>
movslq (%r12),%rax
lea    (%r14,%rax,2),%rsi
mov    0x2e7e(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3c(%rip),%rax
or     (%rax),%edx
mov    0x2e1b(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    (%r12),%esi
mov    0x2e2a(%rip),%rbx
mov    (%rbx),%edx
mov    %rax,%rdi
call   1060 <mprotect@plt>
lea    (%r15,%r14,1),%rdi
movslq (%r12),%rsi
add    %rsi,%rdi
mov    (%rbx),%edx
call   1060 <mprotect@plt>
movslq (%r12),%rbx
add    %r15,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@PROT_NONE = global i32 0
define i8* @create_isolated_section_rwx(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = load i32, i32* @PAGE_SIZE
%8 = sub nsw i32 %7, 1
%9 = xor i32 %8, -1
%10 = sext i32 %9 to i64
%11 = and i64 %6, %10
%12 = load i64, i64* %2
%13 = icmp eq i64 %11, %12
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i64, i64* %2
%17 = load i32, i32* @PAGE_SIZE
%18 = mul nsw i32 %17, 2
%19 = sext i32 %18 to i64
%20 = add i64 %16, %19
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @PROT_EXEC
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_PRIVATE
%28 = load i32, i32* @MAP_ANONYMOUS
%29 = or i32 %27, %28
%30 = call i8* @mmap(i32 0, i64 %21, i32 %26, i32 %29, i32 -1, i32 0)
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = load i32, i32* @PAGE_SIZE
%33 = load i32, i32* @PROT_NONE
%34 = call i32 @mprotect(i8* %31, i32 %32, i32 %33)
%35 = load i8*, i8** %4
%36 = load i64, i64* %2
%37 = getelementptr i8, i8* %35, i64 %36
%38 = load i32, i32* @PAGE_SIZE
%39 = sext i32 %38 to i64
%40 = getelementptr i8, i8* %37, i64 %39
%41 = load i32, i32* @PAGE_SIZE
%42 = load i32, i32* @PROT_NONE
%43 = call i32 @mprotect(i8* %40, i32 %41, i32 %42)
%44 = load i8*, i8** %4
%45 = load i32, i32* @PAGE_SIZE
%46 = sext i32 %45 to i64
%47 = getelementptr i8, i8* %44, i64 %46
store i8* %47, i8** %5
%48 = load i8*, i8** %5
%49 = load i64, i64* %2
%50 = call i32 @memset(i8* %48, i32 0, i64 %49)
%51 = load i8*, i8** %5
ret i8* %51
}

LLVM IR (-O2):
@PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@PROT_NONE = global i32 0
define i8* @create_isolated_section_rwx(i64 %0) {
%2 = load i32, i32* @PAGE_SIZE
%3 = sub i32 0, %2
%4 = sext i32 %3 to i64
%5 = and i64 %4, %0
%6 = icmp eq i64 %5, %0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* @PAGE_SIZE
%10 = shl nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = add i64 %11, %0
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @PROT_EXEC
%17 = or i32 %15, %16
%18 = load i32, i32* @MAP_PRIVATE
%19 = load i32, i32* @MAP_ANONYMOUS
%20 = or i32 %19, %18
%21 = tail call i8* @mmap(i32 0, i64 %12, i32 %17, i32 %20, i32 -1, i32 0)
%22 = load i32, i32* @PAGE_SIZE
%23 = load i32, i32* @PROT_NONE
%24 = tail call i32 @mprotect(i8* %21, i32 %22, i32 %23)
%25 = getelementptr i8, i8* %21, i64 %0
%26 = load i32, i32* @PAGE_SIZE
%27 = sext i32 %26 to i64
%28 = getelementptr i8, i8* %25, i64 %27
%29 = load i32, i32* @PROT_NONE
%30 = tail call i32 @mprotect(i8* %28, i32 %26, i32 %29)
%31 = load i32, i32* @PAGE_SIZE
%32 = sext i32 %31 to i64
%33 = getelementptr i8, i8* %21, i64 %32
%34 = tail call i32 @memset(i8* %33, i32 0, i64 %0)
ret i8* %33
}