#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 */
scalar_t__ brk;
int /*<<< orphan*/ exit(int);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
void *sbrk(int i) {
  void *p;
  static brk = 0;
  if (!i)
    return (void *)brk;
  if (i < 0) {
    printf("sbrk(i<0) not implemented\n");
    exit(-1);
  }
  if (p = malloc(i)) {
    memset(p, 0, i);
    brk += i;
    return p;
  }
  return (void *)-1;
}


x86 assembly (clang, -O0):
<sbrk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1165 <sbrk+0x25>
movslq 0x2eec(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11d0 <sbrk+0x90>
cmpl   $0x0,-0xc(%rbp)
jge    1185 <sbrk+0x45>
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11c5 <sbrk+0x85>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0xc(%rbp),%eax
add    0x2e96(%rip),%eax
mov    %eax,0x2e90(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d0 <sbrk+0x90>
mov    $0xffffffffffffffff,%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
test   %edi,%edi
je     116c <sbrk+0x2c>
js     1186 <sbrk+0x46>
mov    %edi,%ebp
call   1050 <malloc@plt>
test   %rax,%rax
je     1175 <sbrk+0x35>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
add    %ebp,0x2ede(%rip)
jmp    117c <sbrk+0x3c>
movslq 0x2ed5(%rip),%rbx
jmp    117c <sbrk+0x3c>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@sbrk.brk = internal global i32 0
@.str = [27 x i8] c"sbrk(i<0) not implemented\0A\00"
@brk = global i64 0
define i8* @sbrk(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp ne i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32, i32* @sbrk.brk
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %2
br label %30
11: ; preds = %1
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %11
%18 = load i32, i32* %3
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = load i32, i32* %3
%24 = call i32 @memset(i8* %22, i32 0, i32 %23)
%25 = load i32, i32* %3
%26 = load i32, i32* @sbrk.brk
%27 = add nsw i32 %26, %25
store i32 %27, i32* @sbrk.brk
%28 = load i8*, i8** %4
store i8* %28, i8** %2
br label %30
29: ; preds = %17
store i8* inttoptr (i64 -1 to i8*), i8** %2
br label %30
30: ; preds = %29, %21, %7
%31 = load i8*, i8** %2
ret i8* %31
}

LLVM IR (-O2):
@sbrk.brk = internal unnamed_addr global i32 0
@brk = global i64 0
@str = [26 x i8] c"sbrk(i<0) not implemented\00"
define i8* @sbrk(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %7
3: ; preds = %1
%4 = load i32, i32* @sbrk.brk
%5 = sext i32 %4 to i64
%6 = inttoptr i64 %5 to i8*
br label %19
7: ; preds = %1
%8 = icmp slt i32 %0, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %7
%13 = tail call i8* @malloc(i32 %0)
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i32 @memset(i8* %13, i32 0, i32 %0)
%17 = load i32, i32* @sbrk.brk
%18 = add nsw i32 %17, %0
store i32 %18, i32* @sbrk.brk
br label %19
19: ; preds = %12, %15, %3
%20 = phi i8* [ %13, %15 ], [ %6, %3 ], [ inttoptr (i64 -1 to i8*), %12 ]
ret i8* %20
}