#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
void *sbrk(int);
int main() {
  printf("testing sbrk\n");
  void *addr, *addr_new;
  addr = sbrk(0);
  addr_new = sbrk(0x1000);
  if (addr != addr_new) {
    printf("sbrk failed on increment\n");
    return 1;
  }
  addr_new = sbrk(0);
  if ((unsigned int)addr + 0x1000 != (unsigned int)addr_new) {
    printf("sbrk failed on increment a PAGE_SIZE\n");
    return 1;
  }
  sbrk(-0x1000);
  addr_new = sbrk(0);
  if (addr != addr_new) {
    printf("sbrk failed on decrement a PAGE_SIZE\n");
    return 1;
  }
  printf("Test passed OK\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <sbrk@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1000,%edi
call   1040 <sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     118a <main+0x5a>
lea    0xe95(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1210 <main+0xe0>
xor    %edi,%edi
call   1040 <sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1000,%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
je     11c2 <main+0x92>
lea    0xe77(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1210 <main+0xe0>
mov    $0xfffff000,%edi
call   1040 <sbrk@plt>
xor    %edi,%edi
call   1040 <sbrk@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11fd <main+0xcd>
lea    0xe62(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1210 <main+0xe0>
lea    0xe70(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xec8(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1040 <sbrk@plt>
mov    %rax,%rbx
mov    $0x1000,%edi
call   1040 <sbrk@plt>
cmp    %rax,%rbx
je     115f <main+0x2f>
lea    0xf09(%rip),%rdi
jmp    1196 <main+0x66>
xor    %edi,%edi
call   1040 <sbrk@plt>
lea    0x1000(%rbx),%ecx
cmp    %eax,%ecx
jne    118f <main+0x5f>
mov    $0xfffff000,%edi
call   1040 <sbrk@plt>
xor    %edi,%edi
call   1040 <sbrk@plt>
cmp    %rax,%rbx
je     11a2 <main+0x72>
lea    0xe8f(%rip),%rdi
jmp    1196 <main+0x66>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"testing sbrk\0A\00"
@.str.1 = [26 x i8] c"sbrk failed on increment\0A\00"
@.str.2 = [38 x i8] c"sbrk failed on increment a PAGE_SIZE\0A\00"
@.str.3 = [38 x i8] c"sbrk failed on decrement a PAGE_SIZE\0A\00"
@.str.4 = [16 x i8] c"Test passed OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = call i8* @sbrk(i32 0)
store i8* %5, i8** %2
%6 = call i8* @sbrk(i32 4096)
store i8* %6, i8** %3
%7 = load i8*, i8** %2
%8 = load i8*, i8** %3
%9 = icmp ne i8* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %33
12: ; preds = %0
%13 = call i8* @sbrk(i32 0)
store i8* %13, i8** %3
%14 = load i8*, i8** %2
%15 = ptrtoint i8* %14 to i32
%16 = add i32 %15, 4096
%17 = load i8*, i8** %3
%18 = ptrtoint i8* %17 to i32
%19 = icmp ne i32 %16, %18
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %33
22: ; preds = %12
%23 = call i8* @sbrk(i32 -4096)
%24 = call i8* @sbrk(i32 0)
store i8* %24, i8** %3
%25 = load i8*, i8** %2
%26 = load i8*, i8** %3
%27 = icmp ne i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %33
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %28, %20, %10
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@str = [13 x i8] c"testing sbrk\00"
@str.5 = [15 x i8] c"Test passed OK\00"
@str.6 = [37 x i8] c"sbrk failed on decrement a PAGE_SIZE\00"
@str.7 = [37 x i8] c"sbrk failed on increment a PAGE_SIZE\00"
@str.8 = [25 x i8] c"sbrk failed on increment\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%2 = tail call i8* @sbrk(i32 0)
%3 = tail call i8* @sbrk(i32 4096)
%4 = icmp eq i8* %2, %3
br i1 %4, label %5, label %20
5: ; preds = %0
%6 = tail call i8* @sbrk(i32 0)
%7 = ptrtoint i8* %2 to i64
%8 = trunc i64 %7 to i32
%9 = add i32 %8, 4096
%10 = ptrtoint i8* %6 to i64
%11 = trunc i64 %10 to i32
%12 = icmp eq i32 %9, %11
br i1 %12, label %13, label %20
13: ; preds = %5
%14 = tail call i8* @sbrk(i32 -4096)
%15 = tail call i8* @sbrk(i32 0)
%16 = icmp eq i8* %2, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %13, %5, %0
%21 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0), %0 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.7, i64 0, i64 0), %5 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0), %13 ]
%22 = tail call i32 @puts(i8* dereferenceable(1) %21)
ret i32 1
}