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

/* Variables and functions */
int /*<<< orphan*/ abort();
char *sp;
int /*<<< orphan*/ write(int, char const *, int);
char *_sbrk(int adj) {
  extern char end[];
  static char *heap = end;
  char *rv = heap;
  char *sp = (char *)&sp;
  if (heap + adj > sp) {
    const char msg[] = "Heap and stack collision\n";
    write(1, msg, sizeof(msg) - 1);
    abort();
  }
  heap += adj;
  return rv;
}


x86 assembly (clang, -O0):
<_sbrk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2efe(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2eeb(%rip),%rax
movslq -0x4(%rbp),%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    119c <_sbrk+0x7c>
mov    0xea3(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xea0(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe9d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe9a(%rip),%ax
mov    %ax,-0x28(%rbp)
lea    -0x40(%rbp),%rsi
mov    $0x1,%edi
mov    $0x19,%edx
call   1040 <write@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%ecx
mov    0x2e8a(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e7d(%rip)
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sbrk>:
sub    $0x38,%rsp
mov    0x2f05(%rip),%rax
lea    0x8(%rsp),%rdx
mov    %rdx,0x8(%rsp)
movslq %edi,%rcx
add    %rax,%rcx
cmp    %rdx,%rcx
ja     114c <_sbrk+0x2c>
mov    %rcx,0x2ee9(%rip)
add    $0x38,%rsp
ret
movups 0xeb7(%rip),%xmm0
movups %xmm0,0x1a(%rsp)
movaps 0xea1(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    $0x1,%edi
mov    $0x19,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@_sbrk.heap = internal global i8* getelementptr inbounds ([0 x i8], [0 x i8]* @end, i32 0, i32 0)
@end = external global [0 x i8]
@__const._sbrk.msg = [26 x i8] c"Heap and stack collision\0A\00"
@sp = global i8* null
define i8* @_sbrk(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [26 x i8]
store i32 %0, i32* %2
%6 = load i8*, i8** @_sbrk.heap
store i8* %6, i8** %3
%7 = bitcast i8** %4 to i8*
store i8* %7, i8** %4
%8 = load i8*, i8** @_sbrk.heap
%9 = load i32, i32* %2
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = load i8*, i8** %4
%13 = icmp ugt i8* %11, %12
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = bitcast [26 x i8]* %5 to i8*
%16 = getelementptr inbounds [26 x i8], [26 x i8]* %5, i64 0, i64 0
%17 = call i32 @write(i32 1, i8* %16, i32 25)
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %1
%20 = load i32, i32* %2
%21 = load i8*, i8** @_sbrk.heap
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8* %23, i8** @_sbrk.heap
%24 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
@_sbrk.heap = internal unnamed_addr global i8* getelementptr inbounds ([0 x i8], [0 x i8]* @end, i64 0, i64 0)
@end = external global [0 x i8]
@__const._sbrk.msg = [26 x i8] c"Heap and stack collision\0A\00"
@sp = global i8* null
define i8* @_sbrk(i32 %0) {
%2 = alloca i8*
%3 = alloca [26 x i8]
%4 = load i8*, i8** @_sbrk.heap
%5 = bitcast i8** %2 to i8*
%6 = bitcast i8** %2 to i8***
store i8** %2, i8*** %6
%7 = sext i32 %0 to i64
%8 = getelementptr inbounds i8, i8* %4, i64 %7
%9 = icmp ugt i8* %8, %5
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = getelementptr inbounds [26 x i8], [26 x i8]* %3, i64 0, i64 0
%12 = call i32 @write(i32 1, i8* %11, i32 25)
%13 = call i32 (...) @abort()
unreachable
14: ; preds = %1
store i8* %8, i8** @_sbrk.heap
ret i8* %4
}