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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
extern caddr_t _sbrk(int incr) {
  extern unsigned char end asm("end");
  static unsigned char *heap_end;
  unsigned char *prev_heap_end;
  extern unsigned char _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
add    %rcx,%rax
mov    0x2e85(%rip),%rcx
cmp    %rcx,%rax
jbe    116a <_sbrk+0x6a>
mov    0x2e8d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e74(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <_sbrk+0x88>
mov    -0x8(%rbp),%ecx
mov    0x2eb4(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2ea7(%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
add    %rax,%rcx
cmp    0x2ea1(%rip),%rcx
jbe    1141 <_sbrk+0x41>
mov    0x2eb0(%rip),%rax
mov    (%rax),%eax
mov    0x2e97(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret
mov    %rcx,0x2ee0(%rip)
ret

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