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

/* Variables and functions */
void *sbrk(int incr) {
  extern uint8_t __HeapStart;
  extern uint8_t __StackLimit;
  static uint8_t *heap_end = 0;
  if (heap_end == 0) {
    heap_end = &__HeapStart;
  }
  if (incr < 0) {
    return (void *)-1;
  }
  uint8_t *prev_heap_end = heap_end;
  if (heap_end + incr > &__StackLimit) {
    return (void *)-1;
  }
  heap_end += incr;
  return (void *)prev_heap_end;
}


x86 assembly (clang, -O0):
<sbrk>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
cmpq   $0x0,0x2f19(%rip)
jne    1123 <sbrk+0x23>
mov    0x2ec4(%rip),%rax
mov    %rax,0x2f05(%rip)
cmpl   $0x0,-0xc(%rbp)
jge    113d <sbrk+0x3d>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    119d <sbrk+0x9d>
mov    0x2ee4(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ed9(%rip),%rax
movslq -0xc(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    0x2e77(%rip),%rcx
cmp    %rcx,%rax
jbe    117a <sbrk+0x7a>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    119d <sbrk+0x9d>
mov    -0xc(%rbp),%ecx
mov    0x2ea4(%rip),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,0x2e93(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<sbrk>:
mov    0x2f21(%rip),%rcx
test   %rcx,%rcx
jne    111a <sbrk+0x1a>
mov    0x2ecd(%rip),%rcx
mov    %rcx,0x2f0e(%rip)
mov    $0xffffffffffffffff,%rax
test   %edi,%edi
js     113e <sbrk+0x3e>
mov    %edi,%edx
lea    (%rcx,%rdx,4),%rdx
cmp    0x2ea6(%rip),%rdx
ja     113e <sbrk+0x3e>
mov    %rdx,0x2eed(%rip)
mov    %rcx,%rax
ret

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

LLVM IR (-O2):
@sbrk.heap_end = internal unnamed_addr global i32* null
@__HeapStart = external global i32
@__StackLimit = external global i32
define i8* @sbrk(i32 %0) {
%2 = load i32*, i32** @sbrk.heap_end
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %5
4: ; preds = %1
store i32* @__HeapStart, i32** @sbrk.heap_end
br label %5
5: ; preds = %4, %1
%6 = phi i32* [ @__HeapStart, %4 ], [ %2, %1 ]
%7 = icmp slt i32 %0, 0
br i1 %7, label %14, label %8
8: ; preds = %5
%9 = zext i32 %0 to i64
%10 = getelementptr inbounds i32, i32* %6, i64 %9
%11 = icmp ugt i32* %10, @__StackLimit
br i1 %11, label %14, label %12
12: ; preds = %8
store i32* %10, i32** @sbrk.heap_end
%13 = bitcast i32* %6 to i8*
br label %14
14: ; preds = %12, %8, %5
%15 = phi i8* [ inttoptr (i64 -1 to i8*), %5 ], [ %13, %12 ], [ inttoptr (i64 -1 to i8*), %8 ]
ret i8* %15
}