#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*/ STDERR_FILENO;
scalar_t__ __get_MSP();
int /*<<< orphan*/ _write(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ errno;
caddr_t _sbrk(int incr) {
  extern char _ebss;
  static char *heap_end = NULL;
  char *prev_heap_end;
  if (heap_end == NULL) {
    heap_end = &_ebss;
  }
  prev_heap_end = heap_end;
  char *stack = (char *)__get_MSP();
  if (heap_end + incr > stack) {
    _write(STDERR_FILENO, "Heap and stack collision\n", 25);
    while (1)
      ;
    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
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpq   $0x0,0x2f05(%rip)
jne    1147 <_sbrk+0x27>
mov    0x2ea8(%rip),%rax
mov    %rax,0x2ef1(%rip)
mov    0x2eea(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <__get_MSP@plt>
mov    %rax,-0x18(%rbp)
mov    0x2ed4(%rip),%rax
movslq -0x4(%rbp),%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    1194 <_sbrk+0x74>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    $0x19,%edx
call   1040 <_write@plt>
jmp    118f <_sbrk+0x6f>
mov    -0x4(%rbp),%ecx
mov    0x2e9a(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e8d(%rip)
mov    -0x10(%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_MSP@plt>
movslq %ebp,%rcx
add    0x2ee8(%rip),%rcx
cmp    %rax,%rcx
jbe    1172 <_sbrk+0x52>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
mov    $0x19,%edx
call   1040 <_write@plt>
nop
jmp    1170 <_sbrk+0x50>
mov    %rcx,0x2ebf(%rip)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@_sbrk.heap_end = internal global i8* null
@_ebss = 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 i32 @_sbrk(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 %0, i32* %2
%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* @_ebss, i8** @_sbrk.heap_end
br label %8
8: ; preds = %7, %1
%9 = load i8*, i8** @_sbrk.heap_end
store i8* %9, i8** %3
%10 = call i64 (...) @__get_MSP()
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %4
%12 = load i8*, i8** @_sbrk.heap_end
%13 = load i32, i32* %2
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8*, i8** %4
%17 = icmp ugt i8* %15, %16
br i1 %17, label %18, label %22
18: ; preds = %8
%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)
br label %21
21: ; preds = %18, %21
br label %21
22: ; preds = %8
%23 = load i32, i32* %2
%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** %3
%28 = ptrtoint i8* %27 to i32
ret i32 %28
}

LLVM IR (-O2):
@_sbrk.heap_end = internal unnamed_addr global i8* null
@_ebss = 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 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* @_ebss, i8** @_sbrk.heap_end
br label %5
5: ; preds = %4, %1
%6 = phi i8* [ @_ebss, %4 ], [ %2, %1 ]
%7 = tail call i64 (...) @__get_MSP()
%8 = inttoptr i64 %7 to i8*
%9 = load i8*, i8** @_sbrk.heap_end
%10 = sext i32 %0 to i64
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = icmp ugt i8* %11, %8
br i1 %12, label %13, label %17
13: ; preds = %5
%14 = load i32, i32* @STDERR_FILENO
%15 = tail call i32 @_write(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 25)
br label %16
16: ; preds = %16, %13
br label %16
17: ; preds = %5
store i8* %11, i8** @_sbrk.heap_end
%18 = ptrtoint i8* %6 to i64
%19 = trunc i64 %18 to i32
ret i32 %19
}