#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);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int sbrk(int /*<<< orphan*/);
int main() {
  int i;
  int inc_dec;
  int delta;
  intptr_t brk_stat;
  for (inc_dec = 1; inc_dec >= -1; inc_dec -= 2) {
    for (delta = 1; delta <= 400; delta += 399) {
      if (0)
        printf("initial brk value for inc_dec %d delta %d: %p\n", inc_dec,
               delta, sbrk(0));
      for (i = 0; i < 20000; i++) {
        brk_stat = (intptr_t)brk(sbrk(0) + inc_dec * delta);
        if (brk_stat == -1) {
          printf("brk value at failure: %p\n", sbrk(0));
          perror("brk() failed!\n");
          return 0;
        }
      }
      if (0)
        printf("resulting brk value for inc_dec %d delta %d: %p\n", inc_dec,
               delta, sbrk(0));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jl     1218 <main+0xd8>
movl   $0x1,-0x10(%rbp)
cmpl   $0x190,-0x10(%rbp)
jg     1205 <main+0xc5>
movl   $0x0,-0x8(%rbp)
cmpl   $0x4e20,-0x8(%rbp)
jge    11f0 <main+0xb0>
xor    %edi,%edi
call   1050 <sbrk@plt>
mov    %eax,%edi
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
add    %eax,%edi
call   1040 <brk@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11dd <main+0x9d>
xor    %edi,%edi
call   1050 <sbrk@plt>
mov    %eax,%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    121f <main+0xdf>
jmp    11e2 <main+0xa2>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    117b <main+0x3b>
jmp    11f5 <main+0xb5>
mov    -0x10(%rbp),%eax
add    $0x18f,%eax
mov    %eax,-0x10(%rbp)
jmp    1167 <main+0x27>
jmp    120a <main+0xca>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0xc(%rbp)
jmp    1156 <main+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x4e20,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    0x1(%rax),%edi
call   1040 <brk@plt>
cmp    $0xffffffffffffffff,%rax
je     11df <main+0x9f>
add    $0xffffffff,%ebx
jne    1150 <main+0x10>
mov    $0x4e20,%ebx
nop
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    0x190(%rax),%edi
call   1040 <brk@plt>
cmp    $0xffffffffffffffff,%rax
je     11df <main+0x9f>
add    $0xffffffff,%ebx
jne    1170 <main+0x30>
mov    $0x4e20,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    -0x1(%rax),%edi
call   1040 <brk@plt>
cmp    $0xffffffffffffffff,%rax
je     11df <main+0x9f>
add    $0xffffffff,%ebx
jne    11a0 <main+0x60>
mov    $0x4e20,%ebx
nop
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    -0x190(%rax),%edi
call   1040 <brk@plt>
cmp    $0xffffffffffffffff,%rax
je     11df <main+0x9f>
add    $0xffffffff,%ebx
jne    11c0 <main+0x80>
jmp    1202 <main+0xc2>
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"brk value at failure: %p\0A\00"
@.str.1 = [15 x i8] c"brk() failed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 0, i32* %1
store i32 1, i32* %3
br label %6
6: ; preds = %39, %0
%7 = load i32, i32* %3
%8 = icmp sge i32 %7, -1
br i1 %8, label %9, label %42
9: ; preds = %6
store i32 1, i32* %4
br label %10
10: ; preds = %35, %9
%11 = load i32, i32* %4
%12 = icmp sle i32 %11, 400
br i1 %12, label %13, label %38
13: ; preds = %10
store i32 0, i32* %2
br label %14
14: ; preds = %31, %13
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 20000
br i1 %16, label %17, label %34
17: ; preds = %14
%18 = call i32 @sbrk(i32 0)
%19 = load i32, i32* %3
%20 = load i32, i32* %4
%21 = mul nsw i32 %19, %20
%22 = add nsw i32 %18, %21
%23 = call i64 @brk(i32 %22)
store i64 %23, i64* %5
%24 = load i64, i64* %5
%25 = icmp eq i64 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = call i32 @sbrk(i32 0)
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %43
30: ; preds = %17
br label %31
31: ; preds = %30
%32 = load i32, i32* %2
%33 = add nsw i32 %32, 1
store i32 %33, i32* %2
br label %14
34: ; preds = %14
br label %35
35: ; preds = %34
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 399
store i32 %37, i32* %4
br label %10
38: ; preds = %10
br label %39
39: ; preds = %38
%40 = load i32, i32* %3
%41 = sub nsw i32 %40, 2
store i32 %41, i32* %3
br label %6
42: ; preds = %6
store i32 0, i32* %1
br label %43
43: ; preds = %42, %26
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [26 x i8] c"brk value at failure: %p\0A\00"
@.str.1 = [15 x i8] c"brk() failed!\0A\00"
define i32 @main() {
br label %4
1: ; preds = %4
%2 = add nuw nsw i32 %5, 1
%3 = icmp eq i32 %2, 20000
br i1 %3, label %14, label %4
4: ; preds = %0, %1
%5 = phi i32 [ 0, %0 ], [ %2, %1 ]
%6 = tail call i32 @sbrk(i32 0)
%7 = add nsw i32 %6, 1
%8 = tail call i64 @brk(i32 %7)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %1
10: ; preds = %4, %14, %23, %32
%11 = tail call i32 @sbrk(i32 0)
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %41
14: ; preds = %1, %20
%15 = phi i32 [ %21, %20 ], [ 0, %1 ]
%16 = tail call i32 @sbrk(i32 0)
%17 = add nsw i32 %16, 400
%18 = tail call i64 @brk(i32 %17)
%19 = icmp eq i64 %18, -1
br i1 %19, label %10, label %20
20: ; preds = %14
%21 = add nuw nsw i32 %15, 1
%22 = icmp eq i32 %21, 20000
br i1 %22, label %23, label %14
23: ; preds = %20, %29
%24 = phi i32 [ %30, %29 ], [ 0, %20 ]
%25 = tail call i32 @sbrk(i32 0)
%26 = add nsw i32 %25, -1
%27 = tail call i64 @brk(i32 %26)
%28 = icmp eq i64 %27, -1
br i1 %28, label %10, label %29
29: ; preds = %23
%30 = add nuw nsw i32 %24, 1
%31 = icmp eq i32 %30, 20000
br i1 %31, label %32, label %23
32: ; preds = %29, %38
%33 = phi i32 [ %39, %38 ], [ 0, %29 ]
%34 = tail call i32 @sbrk(i32 0)
%35 = add nsw i32 %34, -400
%36 = tail call i64 @brk(i32 %35)
%37 = icmp eq i64 %36, -1
br i1 %37, label %10, label %38
38: ; preds = %32
%39 = add nuw nsw i32 %33, 1
%40 = icmp eq i32 %39, 20000
br i1 %40, label %41, label %32
41: ; preds = %38, %10
ret i32 0
}