#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 */
struct _reent {
  int dummy;
};
typedef char *caddr_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
__attribute__((used, weak)) caddr_t _sbrk_r(struct _reent *reent, size_t size) {
  extern char __heap_start[];
  extern char __heap_end[];
  static char *heap = __heap_start;
  char *base;
  (void)reent;
  if (heap + size <= __heap_end) {
    base = heap;
    heap += size;
  } else {
    errno = ENOMEM;
    base = (caddr_t)-1;
  }
  return base;
}


x86 assembly (clang, -O0):
<_sbrk_r>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2f0d(%rip),%rax
add    -0x10(%rbp),%rax
mov    0x2eca(%rip),%rcx
cmp    %rcx,%rax
ja     1149 <_sbrk_r+0x49>
mov    0x2ef2(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    0x2ee3(%rip),%rax
mov    %rax,0x2edc(%rip)
jmp    1166 <_sbrk_r+0x66>
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
mov    0x2e77(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sbrk_r>:
mov    0x2f19(%rip),%rax
add    %rax,%rsi
cmp    0x2ed7(%rip),%rsi
jbe    112d <_sbrk_r+0x2d>
mov    0x2ebe(%rip),%rax
mov    (%rax),%eax
mov    0x2ead(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
ret
mov    %rsi,0x2eec(%rip)
ret

LLVM IR (-O0):
%struct._reent = type { i32 }
@_sbrk_r.heap = internal global i8* getelementptr inbounds ([0 x i8], [0 x i8]* @__heap_start, i32 0, i32 0)
@__heap_start = external global [0 x i8]
@__heap_end = external global [0 x i8]
@ENOMEM = global i32 0
@errno = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i8* (%struct._reent*, i64)* @_sbrk_r to i8*)], section "llvm.metadata"
define weak i8* @_sbrk_r(%struct._reent* %0, i64 %1) {
%3 = alloca %struct._reent*
%4 = alloca i64
%5 = alloca i8*
store %struct._reent* %0, %struct._reent** %3
store i64 %1, i64* %4
%6 = load %struct._reent*, %struct._reent** %3
%7 = load i8*, i8** @_sbrk_r.heap
%8 = load i64, i64* %4
%9 = getelementptr inbounds i8, i8* %7, i64 %8
%10 = icmp ule i8* %9, getelementptr inbounds ([0 x i8], [0 x i8]* @__heap_end, i64 0, i64 0)
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8*, i8** @_sbrk_r.heap
store i8* %12, i8** %5
%13 = load i64, i64* %4
%14 = load i8*, i8** @_sbrk_r.heap
%15 = getelementptr inbounds i8, i8* %14, i64 %13
store i8* %15, i8** @_sbrk_r.heap
br label %18
16: ; preds = %2
%17 = load i32, i32* @ENOMEM
store i32 %17, i32* @errno
store i8* inttoptr (i64 -1 to i8*), i8** %5
br label %18
18: ; preds = %16, %11
%19 = load i8*, i8** %5
ret i8* %19
}

LLVM IR (-O2):
%struct._reent = type { i32 }
@_sbrk_r.heap = internal unnamed_addr global i8* getelementptr inbounds ([0 x i8], [0 x i8]* @__heap_start, i64 0, i64 0)
@__heap_start = external global [0 x i8]
@__heap_end = external global [0 x i8]
@ENOMEM = global i32 0
@errno = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i8* (%struct._reent*, i64)* @_sbrk_r to i8*)], section "llvm.metadata"
define weak i8* @_sbrk_r(%struct._reent* %0, i64 %1) {
%3 = load i8*, i8** @_sbrk_r.heap
%4 = getelementptr inbounds i8, i8* %3, i64 %1
%5 = icmp ugt i8* %4, getelementptr inbounds ([0 x i8], [0 x i8]* @__heap_end, i64 0, i64 0)
br i1 %5, label %7, label %6
6: ; preds = %2
store i8* %4, i8** @_sbrk_r.heap
br label %9
7: ; preds = %2
%8 = load i32, i32* @ENOMEM
store i32 %8, i32* @errno
br label %9
9: ; preds = %7, %6
%10 = phi i8* [ %3, %6 ], [ inttoptr (i64 -1 to i8*), %7 ]
ret i8* %10
}