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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ _write(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ errno;
char *get_stackpointer();
void *_sbrk(int incr) {
  extern char __HeapLimit;
  static char *heap_end = 0;
  char *prev_heap_end;
  char *stack;
  if (heap_end == 0) {
    heap_end = &__HeapLimit;
  }
  prev_heap_end = heap_end;
  stack = get_stackpointer();
  if (heap_end + incr > stack) {
    _write(STDERR_FILENO, "Heap and stack collision\n", 25);
    errno = ENOMEM;
    return (void *)-1;
  }
  heap_end += incr;
  return (void *)prev_heap_end;
}


x86 assembly (clang, -O0):
<_sbrk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpq   $0x0,0x2f05(%rip)
jne    1147 <_sbrk+0x27>
mov    0x2ea8(%rip),%rax
mov    %rax,0x2ef1(%rip)
mov    0x2eea(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <get_stackpointer@plt>
mov    %rax,-0x20(%rbp)
mov    0x2ed4(%rip),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
cmp    -0x20(%rbp),%rax
jbe    11b1 <_sbrk+0x91>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    $0x19,%edx
call   1040 <_write@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%ecx
mov    0x2e31(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11d0 <_sbrk+0xb0>
mov    -0xc(%rbp),%ecx
mov    0x2e7d(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e70(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sbrk>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2f0c(%rip),%rbx
test   %rbx,%rbx
jne    113f <_sbrk+0x1f>
mov    0x2eb0(%rip),%rbx
mov    %rbx,0x2ef9(%rip)
xor    %eax,%eax
call   1030 <get_stackpointer@plt>
movslq %ebp,%rcx
add    0x2ee8(%rip),%rcx
cmp    %rax,%rcx
jbe    118a <_sbrk+0x6a>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
mov    $0x19,%edx
call   1040 <_write@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rbx
jmp    1191 <_sbrk+0x71>
mov    %rcx,0x2ea7(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@_sbrk.heap_end = internal global i8* null
@__HeapLimit = external global i8
@STDERR_FILENO = global i32 0
@.str = [26 x i8] c"Heap and stack collision\0A\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @_sbrk(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %3
%6 = load i8*, i8** @_sbrk.heap_end
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* @__HeapLimit, i8** @_sbrk.heap_end
br label %9
9: ; preds = %8, %1
%10 = load i8*, i8** @_sbrk.heap_end
store i8* %10, i8** %4
%11 = call i8* (...) @get_stackpointer()
store i8* %11, i8** %5
%12 = load i8*, i8** @_sbrk.heap_end
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8*, i8** %5
%17 = icmp ugt i8* %15, %16
br i1 %17, label %18, label %22
18: ; preds = %9
%19 = load i32, i32* @STDERR_FILENO
%20 = call i32 @_write(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 25)
%21 = load i32, i32* @ENOMEM
store i32 %21, i32* @errno
store i8* inttoptr (i64 -1 to i8*), i8** %2
br label %28
22: ; preds = %9
%23 = load i32, i32* %3
%24 = load i8*, i8** @_sbrk.heap_end
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** @_sbrk.heap_end
%27 = load i8*, i8** %4
store i8* %27, i8** %2
br label %28
28: ; preds = %22, %18
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@_sbrk.heap_end = internal unnamed_addr global i8* null
@__HeapLimit = external global i8
@STDERR_FILENO = global i32 0
@.str = [26 x i8] c"Heap and stack collision\0A\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @_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* @__HeapLimit, i8** @_sbrk.heap_end
br label %5
5: ; preds = %4, %1
%6 = phi i8* [ @__HeapLimit, %4 ], [ %2, %1 ]
%7 = tail call i8* (...) @get_stackpointer()
%8 = load i8*, i8** @_sbrk.heap_end
%9 = sext i32 %0 to i64
%10 = getelementptr inbounds i8, i8* %8, i64 %9
%11 = icmp ugt i8* %10, %7
br i1 %11, label %12, label %16
12: ; preds = %5
%13 = load i32, i32* @STDERR_FILENO
%14 = tail call i32 @_write(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 25)
%15 = load i32, i32* @ENOMEM
store i32 %15, i32* @errno
br label %17
16: ; preds = %5
store i8* %10, i8** @_sbrk.heap_end
br label %17
17: ; preds = %16, %12
%18 = phi i8* [ inttoptr (i64 -1 to i8*), %12 ], [ %6, %16 ]
ret i8* %18
}