#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 PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ mprotect(void *, size_t, int);
scalar_t__ posix_memalign(void **, size_t, size_t);
size_t sysconf(int /*<<< orphan*/);
void *xalloc(size_t size) {
  void *p;
  size_t pagesize = sysconf(_SC_PAGESIZE);
  size = (size + pagesize - 1) & ~(pagesize - 1);
  if (posix_memalign(&p, pagesize, size))
    return NULL;
  mprotect(p, size, PROT_READ | PROT_WRITE | PROT_EXEC);
  return p;
}


x86 assembly (clang, -O0):
<xalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
call   1050 <posix_memalign@plt>
cmp    $0x0,%rax
je     1195 <xalloc+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <xalloc+0x95>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    0x2e1b(%rip),%rax
or     (%rax),%edx
mov    0x2e22(%rip),%rax
or     (%rax),%edx
call   1030 <mprotect@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xalloc>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
lea    (%rbx,%rax,1),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rax,%rbx
neg    %rbx
and    %rcx,%rbx
lea    0x8(%rsp),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1050 <posix_memalign@plt>
test   %rax,%rax
je     1170 <xalloc+0x40>
xor    %eax,%eax
jmp    119d <xalloc+0x6d>
mov    0x2e81(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x8(%rsp),%rdi
mov    0x2e4a(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rsi
call   1030 <mprotect@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define i8* @xalloc(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
store i64 %0, i64* %3
%6 = load i32, i32* @_SC_PAGESIZE
%7 = call i64 @sysconf(i32 %6)
store i64 %7, i64* %5
%8 = load i64, i64* %3
%9 = load i64, i64* %5
%10 = add i64 %8, %9
%11 = sub i64 %10, 1
%12 = load i64, i64* %5
%13 = sub i64 %12, 1
%14 = xor i64 %13, -1
%15 = and i64 %11, %14
store i64 %15, i64* %3
%16 = load i64, i64* %5
%17 = load i64, i64* %3
%18 = call i64 @posix_memalign(i8** %4, i64 %16, i64 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %1
store i8* null, i8** %2
br label %31
21: ; preds = %1
%22 = load i8*, i8** %4
%23 = load i64, i64* %3
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @PROT_EXEC
%28 = or i32 %26, %27
%29 = call i32 @mprotect(i8* %22, i64 %23, i32 %28)
%30 = load i8*, i8** %4
store i8* %30, i8** %2
br label %31
31: ; preds = %21, %20
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
define i8* @xalloc(i64 %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = load i32, i32* @_SC_PAGESIZE
%5 = tail call i64 @sysconf(i32 %4)
%6 = add i64 %0, -1
%7 = add i64 %6, %5
%8 = sub i64 0, %5
%9 = and i64 %7, %8
%10 = call i64 @posix_memalign(i8** %2, i64 %5, i64 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @PROT_EXEC
%18 = or i32 %16, %17
%19 = call i32 @mprotect(i8* %13, i64 %9, i32 %18)
%20 = load i8*, i8** %2
br label %21
21: ; preds = %1, %12
%22 = phi i8* [ %20, %12 ], [ null, %1 ]
ret i8* %22
}