#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*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ exit(int);
unsigned char *malloc(int);
long sysconf(int /*<<< orphan*/);
unsigned char *alloc_execmem(int len) {
  unsigned char *execmem;
  long pagesz = sysconf(_SC_PAGE_SIZE);
  execmem = malloc(len + pagesz);
  size_t e = (size_t)execmem;
  e += (e % pagesz == 0) ? (size_t)pagesz : pagesz - (e % pagesz);
  execmem = (void *)e;
  if (execmem == NULL)
    exit(17);
  return execmem;
}


x86 assembly (clang, -O0):
<alloc_execmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x18(%rbp)
movslq -0x4(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    $0x0,%rdx
jne    118b <alloc_execmem+0x5b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11aa <alloc_execmem+0x7a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    -0x30(%rbp),%rax
sub    %rdx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d3 <alloc_execmem+0xa3>
mov    $0x11,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_execmem>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,%rbx
add    %ebx,%ebp
mov    %ebp,%edi
call   1030 <malloc@plt>
mov    %rax,%rcx
or     %rbx,%rax
shr    $0x20,%rax
je     1165 <alloc_execmem+0x35>
mov    %rcx,%rax
xor    %edx,%edx
div    %rbx
jmp    116b <alloc_execmem+0x3b>
mov    %ecx,%eax
xor    %edx,%edx
div    %ebx
sub    %rdx,%rbx
add    %rcx,%rbx
je     117d <alloc_execmem+0x4d>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x11,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
define i8* @alloc_execmem(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %2
%6 = load i32, i32* @_SC_PAGE_SIZE
%7 = call i64 @sysconf(i32 %6)
store i64 %7, i64* %4
%8 = load i32, i32* %2
%9 = sext i32 %8 to i64
%10 = load i64, i64* %4
%11 = add nsw i64 %9, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = ptrtoint i8* %14 to i64
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = load i64, i64* %4
%18 = urem i64 %16, %17
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = load i64, i64* %4
br label %28
22: ; preds = %1
%23 = load i64, i64* %4
%24 = load i64, i64* %5
%25 = load i64, i64* %4
%26 = urem i64 %24, %25
%27 = sub i64 %23, %26
br label %28
28: ; preds = %22, %20
%29 = phi i64 [ %21, %20 ], [ %27, %22 ]
%30 = load i64, i64* %5
%31 = add i64 %30, %29
store i64 %31, i64* %5
%32 = load i64, i64* %5
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %3
%34 = load i8*, i8** %3
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @exit(i32 17)
unreachable
38: ; preds = %28
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
define i8* @alloc_execmem(i32 %0) {
%2 = load i32, i32* @_SC_PAGE_SIZE
%3 = tail call i64 @sysconf(i32 %2)
%4 = trunc i64 %3 to i32
%5 = add i32 %4, %0
%6 = tail call i8* @malloc(i32 %5)
%7 = ptrtoint i8* %6 to i64
%8 = urem i64 %7, %3
%9 = sub i64 %3, %8
%10 = add i64 %9, %7
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @exit(i32 17)
unreachable
14: ; preds = %1
%15 = inttoptr i64 %10 to i8*
ret i8* %15
}