#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;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(void *);
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
void *stack_alloc(int size) {
  size_t sp;
  void *q = NULL;
  void *p;
  p = mmap(q, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
  assert(p);
  sp = (size_t)p + size;
  return (void *)sp;
}


x86 assembly (clang, -O0):
<stack_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    0x2eb7(%rip),%rax
mov    (%rax),%edx
mov    0x2e8e(%rip),%rax
or     (%rax),%edx
mov    0x2e7d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6c(%rip),%rax
or     (%rax),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rax
movslq -0x4(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stack_alloc>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2ecc(%rip),%rax
mov    0x2ea5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e92(%rip),%rax
mov    0x2e83(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <assert@plt>
movslq %ebp,%rax
add    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @stack_alloc(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %2
store i8* null, i8** %4
%6 = load i8*, i8** %4
%7 = load i32, i32* %2
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_ANON
%12 = load i32, i32* @MAP_PRIVATE
%13 = or i32 %11, %12
%14 = call i8* @mmap(i8* %6, i32 %7, i32 %10, i32 %13, i32 -1, i32 0)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = call i32 @assert(i8* %15)
%17 = load i8*, i8** %5
%18 = ptrtoint i8* %17 to i64
%19 = load i32, i32* %2
%20 = sext i32 %19 to i64
%21 = add i64 %18, %20
store i64 %21, i64* %3
%22 = load i64, i64* %3
%23 = inttoptr i64 %22 to i8*
ret i8* %23
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @stack_alloc(i32 %0) {
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_ANON
%6 = load i32, i32* @MAP_PRIVATE
%7 = or i32 %6, %5
%8 = tail call i8* @mmap(i8* null, i32 %0, i32 %4, i32 %7, i32 -1, i32 0)
%9 = tail call i32 @assert(i8* %8)
%10 = ptrtoint i8* %8 to i64
%11 = sext i32 %0 to i64
%12 = add i64 %10, %11
%13 = inttoptr i64 %12 to i8*
ret i8* %13
}