#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 MAP_ANONYMOUS;
scalar_t__ MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
scalar_t__ mmap(int /*<<< orphan*/ *, size_t, int, int, int /*<<< orphan*/,
                int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, size_t);
int main() {
  size_t bytesCount = 0;
  size_t unit = -1;
  while (unit > 0) {
    if (MAP_FAILED == mmap(NULL, unit, PROT_READ | PROT_WRITE,
                           MAP_PRIVATE | MAP_ANONYMOUS, 0, 0))
      unit /= 2;
    else
      bytesCount += unit;
  }
  printf("Max mem size = %ld bytes.\n", bytesCount);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0xffffffffffffffff,-0x18(%rbp)
ff
cmpq   $0x0,-0x18(%rbp)
jbe    11c1 <main+0xa1>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2e95(%rip),%rax
mov    (%rax),%edx
mov    0x2e64(%rip),%rax
or     (%rax),%edx
mov    0x2e53(%rip),%rax
mov    (%rax),%ecx
mov    0x2e62(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jne    11b0 <main+0x90>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11bc <main+0x9c>
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    113f <main+0x1f>
mov    -0x10(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffffffffffff,%rbx
mov    0x2e97(%rip),%rbp
mov    0x2e88(%rip),%r15
mov    0x2e99(%rip),%r12
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edx
mov    0x2e9e(%rip),%rax
or     (%rax),%edx
mov    0x2e5d(%rip),%rax
mov    (%rax),%r13
mov    (%r12),%ecx
or     (%r15),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    %rax,%r13
sete   %cl
mov    %rbx,%rax
mov    $0x0,%edx
cmove  %rdx,%rax
add    %rax,%r14
shr    %cl,%rbx
test   %rbx,%rbx
jne    1150 <main+0x30>
lea    0xe5f(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAP_FAILED = global i64 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [27 x i8] c"Max mem size = %ld bytes.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
store i64 0, i64* %2
store i64 -1, i64* %3
br label %4
4: ; preds = %25, %0
%5 = load i64, i64* %3
%6 = icmp ugt i64 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %4
%8 = load i64, i64* @MAP_FAILED
%9 = load i64, i64* %3
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* @MAP_ANONYMOUS
%15 = or i32 %13, %14
%16 = call i64 @mmap(i32* null, i64 %9, i32 %12, i32 %15, i32 0, i32 0)
%17 = icmp eq i64 %8, %16
br i1 %17, label %18, label %21
18: ; preds = %7
%19 = load i64, i64* %3
%20 = udiv i64 %19, 2
store i64 %20, i64* %3
br label %25
21: ; preds = %7
%22 = load i64, i64* %3
%23 = load i64, i64* %2
%24 = add i64 %23, %22
store i64 %24, i64* %2
br label %25
25: ; preds = %21, %18
br label %4
26: ; preds = %4
%27 = load i64, i64* %2
%28 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %27)
ret i32 0
}

LLVM IR (-O2):
@MAP_FAILED = global i64 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [27 x i8] c"Max mem size = %ld bytes.\0A\00"
define i32 @main() {
br label %1
1: ; preds = %0, %1
%2 = phi i64 [ -1, %0 ], [ %16, %1 ]
%3 = phi i64 [ 0, %0 ], [ %14, %1 ]
%4 = load i64, i64* @MAP_FAILED
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_PRIVATE
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %9, %8
%11 = tail call i64 @mmap(i32* null, i64 %2, i32 %7, i32 %10, i32 0, i32 0)
%12 = icmp eq i64 %4, %11
%13 = select i1 %12, i64 0, i64 %2
%14 = add i64 %13, %3
%15 = zext i1 %12 to i64
%16 = lshr i64 %2, %15
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %1
18: ; preds = %1
%19 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %14)
ret i32 0
}