#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*/ ENOMEM;
int /*<<< orphan*/ errno;
void *_sbrk(int nbytes) {
  extern int end;
  extern void *stack;
  static void *heap_ptr = (void *)&end;
  if ((stack - (heap_ptr + nbytes)) > 65536) {
    void *base = heap_ptr;
    heap_ptr += nbytes;
    return base;
  } else {
    errno = ENOMEM;
    return (void *)-1;
  }
}


x86 assembly (clang, -O0):
<_sbrk>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
mov    0x2eda(%rip),%rax
mov    (%rax),%rax
mov    0x2f08(%rip),%rcx
movslq -0xc(%rbp),%rdx
add    %rdx,%rcx
sub    %rcx,%rax
cmp    $0x10000,%rax
jle    115d <_sbrk+0x5d>
mov    0x2eeb(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
mov    0x2edd(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2ed0(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    117a <_sbrk+0x7a>
mov    0x2e74(%rip),%rax
mov    (%rax),%ecx
mov    0x2e63(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sbrk>:
mov    0x2ee1(%rip),%rax
mov    (%rax),%rdx
mov    0x2f0f(%rip),%rax
movslq %edi,%rcx
add    %rax,%rcx
sub    %rcx,%rdx
cmp    $0x10001,%rdx
jl     112b <_sbrk+0x2b>
mov    %rcx,0x2ef6(%rip)
ret
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    0x2e95(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
ret

LLVM IR (-O0):
@_sbrk.heap_ptr = internal global i8* bitcast (i32* @end to i8*)
@end = external global i32
@stack = external global i8*
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @_sbrk(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i8*, i8** @stack
%6 = load i8*, i8** @_sbrk.heap_ptr
%7 = load i32, i32* %3
%8 = sext i32 %7 to i64
%9 = getelementptr i8, i8* %6, i64 %8
%10 = ptrtoint i8* %5 to i64
%11 = ptrtoint i8* %9 to i64
%12 = sub i64 %10, %11
%13 = icmp sgt i64 %12, 65536
br i1 %13, label %14, label %21
14: ; preds = %1
%15 = load i8*, i8** @_sbrk.heap_ptr
store i8* %15, i8** %4
%16 = load i32, i32* %3
%17 = load i8*, i8** @_sbrk.heap_ptr
%18 = sext i32 %16 to i64
%19 = getelementptr i8, i8* %17, i64 %18
store i8* %19, i8** @_sbrk.heap_ptr
%20 = load i8*, i8** %4
store i8* %20, i8** %2
br label %23
21: ; preds = %1
%22 = load i32, i32* @ENOMEM
store i32 %22, i32* @errno
store i8* inttoptr (i64 -1 to i8*), i8** %2
br label %23
23: ; preds = %21, %14
%24 = load i8*, i8** %2
ret i8* %24
}

LLVM IR (-O2):
@_sbrk.heap_ptr = internal unnamed_addr global i8* bitcast (i32* @end to i8*)
@end = external global i32
@stack = external global i8*
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @_sbrk(i32 %0) {
%2 = load i8*, i8** @stack
%3 = load i8*, i8** @_sbrk.heap_ptr
%4 = sext i32 %0 to i64
%5 = getelementptr i8, i8* %3, i64 %4
%6 = ptrtoint i8* %2 to i64
%7 = ptrtoint i8* %5 to i64
%8 = sub i64 %6, %7
%9 = icmp sgt i64 %8, 65536
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* %5, i8** @_sbrk.heap_ptr
br label %13
11: ; preds = %1
%12 = load i32, i32* @ENOMEM
store i32 %12, i32* @errno
br label %13
13: ; preds = %11, %10
%14 = phi i8* [ %3, %10 ], [ inttoptr (i64 -1 to i8*), %11 ]
ret i8* %14
}