#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;
caddr_t _sbrk(int incr) {
  extern char _Heap_Begin;
  extern char _Heap_Limit;
  static char *current_heap_end;
  char *current_block_address;
  if (current_heap_end == 0) {
    current_heap_end = &_Heap_Begin;
  }
  current_block_address = current_heap_end;
  incr = (incr + 3) & (~3);
  if (current_heap_end + incr > &_Heap_Limit) {
    errno = ENOMEM;
    return (caddr_t)-1;
  }
  current_heap_end += incr;
  return (caddr_t)current_block_address;
}


x86 assembly (clang, -O0):
<_sbrk>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpq   $0x0,0x2f19(%rip)
jne    1123 <_sbrk+0x23>
mov    0x2eac(%rip),%rax
mov    %rax,0x2f05(%rip)
mov    0x2efe(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x3,%eax
and    $0xfffffffc,%eax
mov    %eax,-0x8(%rbp)
mov    0x2ee7(%rip),%rax
movslq -0x8(%rbp),%rcx
add    %rcx,%rax
mov    0x2e89(%rip),%rcx
cmp    %rcx,%rax
jbe    1176 <_sbrk+0x76>
mov    0x2e81(%rip),%rax
mov    (%rax),%ecx
mov    0x2e68(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <_sbrk+0x94>
mov    -0x8(%rbp),%ecx
mov    0x2ea8(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e9b(%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    0x2eb5(%rip),%rax
mov    %rax,0x2f0e(%rip)
add    $0x3,%edi
and    $0xfffffffc,%edi
movslq %edi,%rcx
add    %rax,%rcx
cmp    0x2eab(%rip),%rcx
jbe    1147 <_sbrk+0x47>
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    0x2e91(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret
mov    %rcx,0x2eda(%rip)
ret

LLVM IR (-O0):
@_sbrk.current_heap_end = internal global i8* null
@_Heap_Begin = external global i8
@_Heap_Limit = 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.current_heap_end
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
store i8* @_Heap_Begin, i8** @_sbrk.current_heap_end
br label %8
8: ; preds = %7, %1
%9 = load i8*, i8** @_sbrk.current_heap_end
store i8* %9, i8** %4
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 3
%12 = and i32 %11, -4
store i32 %12, i32* %3
%13 = load i8*, i8** @_sbrk.current_heap_end
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = icmp ugt i8* %16, @_Heap_Limit
br i1 %17, label %18, label %20
18: ; preds = %8
%19 = load i32, i32* @ENOMEM
store i32 %19, i32* @errno
store i32 -1, i32* %2
br label %27
20: ; preds = %8
%21 = load i32, i32* %3
%22 = load i8*, i8** @_sbrk.current_heap_end
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8* %24, i8** @_sbrk.current_heap_end
%25 = load i8*, i8** %4
%26 = ptrtoint i8* %25 to i32
store i32 %26, i32* %2
br label %27
27: ; preds = %20, %18
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@_sbrk.current_heap_end = internal unnamed_addr global i8* null
@_Heap_Begin = external global i8
@_Heap_Limit = external global i8
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @_sbrk(i32 %0) {
%2 = load i8*, i8** @_sbrk.current_heap_end
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %5
4: ; preds = %1
store i8* @_Heap_Begin, i8** @_sbrk.current_heap_end
br label %5
5: ; preds = %4, %1
%6 = phi i8* [ @_Heap_Begin, %4 ], [ %2, %1 ]
%7 = add nsw i32 %0, 3
%8 = and i32 %7, -4
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %6, i64 %9
%11 = icmp ugt i8* %10, @_Heap_Limit
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = load i32, i32* @ENOMEM
store i32 %13, i32* @errno
br label %17
14: ; preds = %5
store i8* %10, i8** @_sbrk.current_heap_end
%15 = ptrtoint i8* %6 to i64
%16 = trunc i64 %15 to i32
br label %17
17: ; preds = %14, %12
%18 = phi i32 [ -1, %12 ], [ %16, %14 ]
ret i32 %18
}