#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 */
typedef size_t ptrdiff_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
void *sbrk(size_t);
void *na_reserve_mem(size_t size) {
  void *current = sbrk(0);
  if ((intptr_t)current == -1) {
    goto fail;
  }
  ptrdiff_t diff =
      ((void *)(((intptr_t)current + ((1 << 4) - 1)) & (~0 << 4))) - current;
  if (diff) {
    current = sbrk(diff);
    if ((intptr_t)current == -1) {
      goto fail;
    }
    assert((!((intptr_t)current & ~(~0 << 4))) == 0);
  }
  void *start = sbrk(size);
  if ((intptr_t)start == -1) {
    goto fail;
  }
  return start;
fail:
  return NULL;
}


x86 assembly (clang, -O0):
<na_reserve_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1030 <sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    $0xffffffffffffffff,%rax
jne    114c <na_reserve_mem+0x2c>
jmp    11e1 <na_reserve_mem+0xc1>
mov    -0x18(%rbp),%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b4 <na_reserve_mem+0x94>
mov    -0x20(%rbp),%rdi
call   1030 <sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    $0xffffffffffffffff,%rax
jne    118e <na_reserve_mem+0x6e>
jmp    11e1 <na_reserve_mem+0xc1>
mov    -0x18(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <sbrk@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    $0xffffffffffffffff,%rax
jne    11d4 <na_reserve_mem+0xb4>
jmp    11e1 <na_reserve_mem+0xc1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <na_reserve_mem+0xc9>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<na_reserve_mem>:
push   %rbx
mov    %rdi,%rbx
xor    %edi,%edi
call   1030 <sbrk@plt>
cmp    $0xffffffffffffffff,%rax
je     1166 <na_reserve_mem+0x46>
lea    0xf(%rax),%rdi
and    $0xfffffffffffffff0,%rdi
sub    %rax,%rdi
je     1156 <na_reserve_mem+0x36>
call   1030 <sbrk@plt>
cmp    $0xffffffffffffffff,%rax
je     1166 <na_reserve_mem+0x46>
xor    %edi,%edi
test   $0xf,%al
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <sbrk@plt>
cmp    $0xffffffffffffffff,%rax
je     1166 <na_reserve_mem+0x46>
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @na_reserve_mem(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %3
%7 = call i8* @sbrk(i64 0)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = ptrtoint i8* %8 to i64
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
br label %50
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = ptrtoint i8* %13 to i64
%15 = add nsw i64 %14, 15
%16 = and i64 %15, -16
%17 = inttoptr i64 %16 to i8*
%18 = load i8*, i8** %4
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %12
%25 = load i64, i64* %5
%26 = call i8* @sbrk(i64 %25)
store i8* %26, i8** %4
%27 = load i8*, i8** %4
%28 = ptrtoint i8* %27 to i64
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %24
br label %50
31: ; preds = %24
%32 = load i8*, i8** %4
%33 = ptrtoint i8* %32 to i64
%34 = and i64 %33, 15
%35 = icmp ne i64 %34, 0
%36 = xor i1 %35, true
%37 = zext i1 %36 to i32
%38 = icmp eq i32 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
br label %41
41: ; preds = %31, %12
%42 = load i64, i64* %3
%43 = call i8* @sbrk(i64 %42)
store i8* %43, i8** %6
%44 = load i8*, i8** %6
%45 = ptrtoint i8* %44 to i64
%46 = icmp eq i64 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %41
br label %50
48: ; preds = %41
%49 = load i8*, i8** %6
store i8* %49, i8** %2
br label %51
50: ; preds = %47, %30, %11
store i8* null, i8** %2
br label %51
51: ; preds = %50, %48
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
define i8* @na_reserve_mem(i64 %0) {
%2 = tail call i8* @sbrk(i64 0)
%3 = ptrtoint i8* %2 to i64
%4 = icmp eq i8* %2, inttoptr (i64 -1 to i8*)
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = add nsw i64 %3, 15
%7 = and i64 %6, -16
%8 = sub i64 %7, %3
%9 = icmp eq i64 %8, 0
br i1 %9, label %19, label %10
10: ; preds = %5
%11 = tail call i8* @sbrk(i64 %8)
%12 = icmp eq i8* %11, inttoptr (i64 -1 to i8*)
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = ptrtoint i8* %11 to i64
%15 = and i64 %14, 15
%16 = icmp ne i64 %15, 0
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
br label %19
19: ; preds = %13, %5
%20 = tail call i8* @sbrk(i64 %0)
%21 = icmp eq i8* %20, inttoptr (i64 -1 to i8*)
br i1 %21, label %22, label %23
22: ; preds = %19, %10, %1
br label %23
23: ; preds = %19, %22
%24 = phi i8* [ null, %22 ], [ %20, %19 ]
ret i8* %24
}