#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;
void *MAP_FAILED;
int MAP_PRIVATE;
int MAP_STACK;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
scalar_t__ mprotect(void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
void *cio_stack_alloc(size_t size, size_t guard_size) {
  void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
  if (ptr == MAP_FAILED)
    return NULL;
  if (mprotect(ptr, guard_size, PROT_NONE) < 0) {
    munmap(ptr, size);
    return NULL;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<cio_stack_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2ead(%rip),%rax
mov    (%rax),%edx
mov    0x2e74(%rip),%rax
or     (%rax),%edx
mov    0x2e63(%rip),%rax
mov    (%rax),%ecx
mov    0x2e72(%rip),%rax
or     (%rax),%ecx
mov    0x2e39(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
jne    11a8 <cio_stack_alloc+0x78>
movq   $0x0,-0x8(%rbp)
jmp    11ea <cio_stack_alloc+0xba>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1050 <mprotect@plt>
cmp    $0x0,%rax
jge    11e2 <cio_stack_alloc+0xb2>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <munmap@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ea <cio_stack_alloc+0xba>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cio_stack_alloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2eb9(%rip),%rax
mov    0x2e82(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6f(%rip),%rax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e3f(%rip),%rax
or     (%rax),%ecx
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r15,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e2d(%rip),%rax
cmp    (%rax),%rbx
je     11bc <cio_stack_alloc+0x8c>
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <mprotect@plt>
test   %rax,%rax
js     11ae <cio_stack_alloc+0x7e>
mov    %rbx,%r12
jmp    11bc <cio_stack_alloc+0x8c>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <munmap@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_STACK = global i32 0
@MAP_FAILED = global i8* null
@PROT_NONE = global i32 0
define i8* @cio_stack_alloc(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %4
store i64 %1, i64* %5
%7 = load i64, i64* %4
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_PRIVATE
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_STACK
%15 = or i32 %13, %14
%16 = call i8* @mmap(i32* null, i64 %7, i32 %10, i32 %15, i32 -1, i32 0)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %21
20: ; preds = %2
store i8* null, i8** %3
br label %33
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = load i64, i64* %5
%24 = load i32, i32* @PROT_NONE
%25 = call i64 @mprotect(i8* %22, i64 %23, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i8*, i8** %6
%29 = load i64, i64* %4
%30 = call i32 @munmap(i8* %28, i64 %29)
store i8* null, i8** %3
br label %33
31: ; preds = %21
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %27, %20
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_STACK = global i32 0
@MAP_FAILED = global i8* null
@PROT_NONE = global i32 0
define i8* @cio_stack_alloc(i64 %0, i64 %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_PRIVATE
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_STACK
%10 = or i32 %8, %9
%11 = tail call i8* @mmap(i32* null, i64 %0, i32 %5, i32 %10, i32 -1, i32 0)
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i32, i32* @PROT_NONE
%16 = tail call i64 @mprotect(i8* %11, i64 %1, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @munmap(i8* %11, i64 %0)
br label %20
20: ; preds = %14, %2, %18
%21 = phi i8* [ null, %18 ], [ null, %2 ], [ %11, %14 ]
ret i8* %21
}