#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*/ free(char *);
char *malloc(size_t);
unsigned maxavail(void) {
  char *ptr;
  unsigned low, high, size;
  low = 0;
  high = 256e6;
  while ((high - low) > 4) {
    size = (high + low) / 2;
    if ((ptr = malloc((size_t)size)) == NULL)
      high = size;
    else {
      free(ptr);
      low = size;
    }
  }
  return low;
}


x86 assembly (clang, -O0):
<maxavail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0xc(%rbp)
movl   $0xf424000,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
cmp    $0x4,%eax
jbe    1188 <maxavail+0x68>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    1174 <maxavail+0x54>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1183 <maxavail+0x63>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1136 <maxavail+0x16>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maxavail>:
push   %r15
push   %r14
push   %rbx
mov    $0xf424000,%r15d
xor    %r14d,%r14d
jmp    1146 <maxavail+0x26>
mov    %rax,%rdi
call   1030 <free@plt>
mov    %ebx,%r14d
mov    %r15d,%eax
sub    %r14d,%eax
cmp    $0x4,%eax
jbe    115e <maxavail+0x3e>
lea    (%r14,%r15,1),%ebx
shr    %ebx
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
jne    1130 <maxavail+0x10>
mov    %ebx,%r15d
jmp    113b <maxavail+0x1b>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @maxavail() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %2
store i32 256000000, i32* %3
br label %5
5: ; preds = %25, %0
%6 = load i32, i32* %3
%7 = load i32, i32* %2
%8 = sub i32 %6, %7
%9 = icmp ugt i32 %8, 4
br i1 %9, label %10, label %26
10: ; preds = %5
%11 = load i32, i32* %3
%12 = load i32, i32* %2
%13 = add i32 %11, %12
%14 = udiv i32 %13, 2
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = zext i32 %15 to i64
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %1
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = load i32, i32* %4
store i32 %20, i32* %3
br label %25
21: ; preds = %10
%22 = load i8*, i8** %1
%23 = call i32 @free(i8* %22)
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %25
25: ; preds = %21, %19
br label %5
26: ; preds = %5
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
define i32 @maxavail() {
br label %1
1: ; preds = %0, %11
%2 = phi i32 [ 0, %0 ], [ %13, %11 ]
%3 = phi i32 [ 256000000, %0 ], [ %12, %11 ]
%4 = add i32 %2, %3
%5 = lshr i32 %4, 1
%6 = zext i32 %5 to i64
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = tail call i32 @free(i8* %7)
br label %11
11: ; preds = %1, %9
%12 = phi i32 [ %3, %9 ], [ %5, %1 ]
%13 = phi i32 [ %5, %9 ], [ %2, %1 ]
%14 = sub i32 %12, %13
%15 = icmp ugt i32 %14, 4
br i1 %15, label %1, label %16
16: ; preds = %11
ret i32 %13
}