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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
caddr_t _sbrk(int32_t incr) {
  extern uint8_t end asm("end");
  static uint8_t *heap_end;
  uint8_t *prev_heap_end;
  extern uint8_t _max_heap;
  if (heap_end == 0)
    heap_end = &end;
  prev_heap_end = heap_end;
  if (heap_end + incr > &_max_heap) {
    errno = ENOMEM;
    return (caddr_t)-1;
  }
  heap_end += incr;
  return (caddr_t)prev_heap_end;
}


x86 assembly (clang, -O0):
<_sbrk>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpq   $0x0,0x2f19(%rip)
jne    1123 <_sbrk+0x23>
mov    0x2ebc(%rip),%rax
mov    %rax,0x2f05(%rip)
mov    0x2efe(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2ef3(%rip),%rax
movslq -0x8(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    0x2e81(%rip),%rcx
cmp    %rcx,%rax
jbe    116e <_sbrk+0x6e>
mov    0x2e89(%rip),%rax
mov    (%rax),%ecx
mov    0x2e70(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1190 <_sbrk+0x90>
mov    -0x8(%rbp),%ecx
mov    0x2eb0(%rip),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,0x2e9f(%rip)
mov    -0x10(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sbrk>:
mov    0x2f21(%rip),%rax
test   %rax,%rax
jne    111a <_sbrk+0x1a>
mov    0x2ec5(%rip),%rax
mov    %rax,0x2f0e(%rip)
movslq %edi,%rcx
lea    (%rax,%rcx,4),%rcx
cmp    0x2ea0(%rip),%rcx
jbe    1142 <_sbrk+0x42>
mov    0x2eaf(%rip),%rax
mov    (%rax),%eax
mov    0x2e96(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret
mov    %rcx,0x2edf(%rip)
ret

LLVM IR (-O0):
@_sbrk.heap_end = internal global i32* null
@end = external global i32
@_max_heap = external global i32
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @_sbrk(i32 %0) {
%2 = alloca i32
%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* @end, i32** @_sbrk.heap_end
br label %8
8: ; preds = %7, %1
%9 = load i32*, i32** @_sbrk.heap_end
store i32* %9, i32** %4
%10 = load i32*, i32** @_sbrk.heap_end
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i32, i32* %10, i64 %12
%14 = icmp ugt i32* %13, @_max_heap
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = load i32, i32* @ENOMEM
store i32 %16, i32* @errno
store i32 -1, i32* %2
br label %24
17: ; preds = %8
%18 = load i32, i32* %3
%19 = load i32*, i32** @_sbrk.heap_end
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32* %21, i32** @_sbrk.heap_end
%22 = load i32*, i32** %4
%23 = ptrtoint i32* %22 to i32
store i32 %23, i32* %2
br label %24
24: ; preds = %17, %15
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@_sbrk.heap_end = internal unnamed_addr global i32* null
@end = external global i32
@_max_heap = external global i32
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @_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* @end, i32** @_sbrk.heap_end
br label %5
5: ; preds = %4, %1
%6 = phi i32* [ @end, %4 ], [ %2, %1 ]
%7 = sext i32 %0 to i64
%8 = getelementptr inbounds i32, i32* %6, i64 %7
%9 = icmp ugt i32* %8, @_max_heap
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = load i32, i32* @ENOMEM
store i32 %11, i32* @errno
br label %15
12: ; preds = %5
store i32* %8, i32** @_sbrk.heap_end
%13 = ptrtoint i32* %6 to i64
%14 = trunc i64 %13 to i32
br label %15
15: ; preds = %12, %10
%16 = phi i32 [ -1, %10 ], [ %14, %12 ]
ret i32 %16
}