#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 int /*<<< orphan*/ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
caddr_t _sbrk_r(struct _reent *r, int incr) {
  (void)r;
  extern char end asm("end");
  extern char HeapLimit asm("__HeapLimit");
  static char *heap_end;
  char *prev_heap_end;
  if (heap_end == NULL)
    heap_end = &end;
  prev_heap_end = heap_end;
  if (heap_end + incr > &HeapLimit) {
    errno = ENOMEM;
    return (caddr_t)-1;
  }
  heap_end += incr;
  return (caddr_t)prev_heap_end;
}


x86 assembly (clang, -O0):
<_sbrk_r>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,0x2f15(%rip)
jne    1127 <_sbrk_r+0x27>
mov    0x2eb0(%rip),%rax
mov    %rax,0x2f01(%rip)
mov    0x2efa(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2eef(%rip),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    0x2ea1(%rip),%rcx
cmp    %rcx,%rax
jbe    116e <_sbrk_r+0x6e>
mov    0x2e81(%rip),%rax
mov    (%rax),%ecx
mov    0x2e68(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    118c <_sbrk_r+0x8c>
mov    -0x14(%rbp),%ecx
mov    0x2eb0(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2ea3(%rip)
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sbrk_r>:
mov    0x2f21(%rip),%rax
test   %rax,%rax
jne    111a <_sbrk_r+0x1a>
mov    0x2ebd(%rip),%rax
mov    %rax,0x2f0e(%rip)
movslq %esi,%rcx
add    %rax,%rcx
cmp    0x2ec1(%rip),%rcx
jbe    1141 <_sbrk_r+0x41>
mov    0x2ea8(%rip),%rax
mov    (%rax),%eax
mov    0x2e8f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret
mov    %rcx,0x2ee0(%rip)
ret

LLVM IR (-O0):
%struct._reent = type { i32 }
@_sbrk_r.heap_end = internal global i8* null
@end = external global i8
@__HeapLimit = external global i8
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @_sbrk_r(%struct._reent* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct._reent*
%5 = alloca i32
%6 = alloca i8*
store %struct._reent* %0, %struct._reent** %4
store i32 %1, i32* %5
%7 = load %struct._reent*, %struct._reent** %4
%8 = load i8*, i8** @_sbrk_r.heap_end
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* @end, i8** @_sbrk_r.heap_end
br label %11
11: ; preds = %10, %2
%12 = load i8*, i8** @_sbrk_r.heap_end
store i8* %12, i8** %6
%13 = load i8*, i8** @_sbrk_r.heap_end
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = icmp ugt i8* %16, @__HeapLimit
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = load i32, i32* @ENOMEM
store i32 %19, i32* @errno
store i32 -1, i32* %3
br label %27
20: ; preds = %11
%21 = load i32, i32* %5
%22 = load i8*, i8** @_sbrk_r.heap_end
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8* %24, i8** @_sbrk_r.heap_end
%25 = load i8*, i8** %6
%26 = ptrtoint i8* %25 to i32
store i32 %26, i32* %3
br label %27
27: ; preds = %20, %18
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct._reent = type { i32 }
@_sbrk_r.heap_end = internal unnamed_addr global i8* null
@end = external global i8
@__HeapLimit = external global i8
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @_sbrk_r(%struct._reent* nocapture readnone %0, i32 %1) {
%3 = load i8*, i8** @_sbrk_r.heap_end
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %6
5: ; preds = %2
store i8* @end, i8** @_sbrk_r.heap_end
br label %6
6: ; preds = %5, %2
%7 = phi i8* [ @end, %5 ], [ %3, %2 ]
%8 = sext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %7, i64 %8
%10 = icmp ugt i8* %9, @__HeapLimit
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = load i32, i32* @ENOMEM
store i32 %12, i32* @errno
br label %16
13: ; preds = %6
store i8* %9, i8** @_sbrk_r.heap_end
%14 = ptrtoint i8* %7 to i64
%15 = trunc i64 %14 to i32
br label %16
16: ; preds = %13, %11
%17 = phi i32 [ -1, %11 ], [ %15, %13 ]
ret i32 %17
}