#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*/ ptrdiff_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
void *_sbrk(ptrdiff_t size) {
  extern const char _HEAP_START;
  extern const char _HEAP_MAX;
  static const char *heap_ptr;
  const char *old_heap_ptr;
  static unsigned char init_sbrk = 0;
  if (init_sbrk == 0) {
    heap_ptr = &_HEAP_START;
    init_sbrk = 1;
  }
  old_heap_ptr = heap_ptr;
  if ((heap_ptr + size) > &_HEAP_MAX) {
    errno = ENOMEM;
    return (void *)-1;
  }
  heap_ptr += size;
  return (void *)old_heap_ptr;
}


x86 assembly (clang, -O0):
<_sbrk>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
movzbl 0x2f22(%rip),%eax
cmp    $0x0,%eax
jne    112c <_sbrk+0x2c>
mov    0x2eca(%rip),%rax
mov    %rax,0x2f03(%rip)
movb   $0x1,0x2f04(%rip)
mov    0x2ef5(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2eea(%rip),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    0x2e7c(%rip),%rcx
cmp    %rcx,%rax
jbe    1177 <_sbrk+0x77>
mov    0x2e7c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6b(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1196 <_sbrk+0x96>
mov    -0xc(%rbp),%ecx
mov    0x2ea7(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e9a(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sbrk>:
cmpb   $0x1,0x2f29(%rip)
jne    1112 <_sbrk+0x12>
mov    0x2f18(%rip),%rax
jmp    1127 <_sbrk+0x27>
mov    0x2ecf(%rip),%rax
mov    %rax,0x2f08(%rip)
movb   $0x1,0x2f09(%rip)
movslq %edi,%rcx
add    %rax,%rcx
cmp    0x2e94(%rip),%rcx
jbe    1150 <_sbrk+0x50>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    0x2e8a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
ret
mov    %rcx,0x2ed1(%rip)
ret

LLVM IR (-O0):
@_sbrk.heap_ptr = internal global i8* null
@_sbrk.init_sbrk = internal global i8 0
@_HEAP_START = external constant i8
@_HEAP_MAX = external constant 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* @_sbrk.init_sbrk
%6 = zext i8 %5 to i32
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* @_HEAP_START, i8** @_sbrk.heap_ptr
store i8 1, i8* @_sbrk.init_sbrk
br label %9
9: ; preds = %8, %1
%10 = load i8*, i8** @_sbrk.heap_ptr
store i8* %10, i8** %4
%11 = load i8*, i8** @_sbrk.heap_ptr
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = icmp ugt i8* %14, @_HEAP_MAX
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = load i32, i32* @ENOMEM
store i32 %17, i32* @errno
store i8* inttoptr (i64 -1 to i8*), i8** %2
br label %24
18: ; preds = %9
%19 = load i32, i32* %3
%20 = load i8*, i8** @_sbrk.heap_ptr
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** @_sbrk.heap_ptr
%23 = load i8*, i8** %4
store i8* %23, i8** %2
br label %24
24: ; preds = %18, %16
%25 = load i8*, i8** %2
ret i8* %25
}

LLVM IR (-O2):
@_sbrk.heap_ptr = internal unnamed_addr global i8* null
@_sbrk.init_sbrk = internal unnamed_addr global i1 false
@_HEAP_START = external constant i8
@_HEAP_MAX = external constant i8
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @_sbrk(i32 %0) {
%2 = load i1, i1* @_sbrk.init_sbrk
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i8*, i8** @_sbrk.heap_ptr
br label %6
5: ; preds = %1
store i8* @_HEAP_START, i8** @_sbrk.heap_ptr
store i1 true, i1* @_sbrk.init_sbrk
br label %6
6: ; preds = %3, %5
%7 = phi i8* [ %4, %3 ], [ @_HEAP_START, %5 ]
%8 = sext i32 %0 to i64
%9 = getelementptr inbounds i8, i8* %7, i64 %8
%10 = icmp ugt i8* %9, @_HEAP_MAX
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = load i32, i32* @ENOMEM
store i32 %12, i32* @errno
br label %14
13: ; preds = %6
store i8* %9, i8** @_sbrk.heap_ptr
br label %14
14: ; preds = %13, %11
%15 = phi i8* [ inttoptr (i64 -1 to i8*), %11 ], [ %7, %13 ]
ret i8* %15
}