#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*/ getchar();
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, unsigned long);
int main() {
  int i;
  int tb = 128;
  for (i = 0; i < ((unsigned long)tb << 40) / (1 << 12); ++i) {
    void *mem = malloc((1 << 12));
    if (!mem)
      break;
    memset(mem, 0, 1);
  }
  printf("Allocated %lu MB\n", ((unsigned long)i * (1 << 12)) >> 20);
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x80,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
movslq -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x28,%rcx
shr    $0xc,%rcx
cmp    %rcx,%rax
jae    11b2 <main+0x72>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <main+0x54>
jmp    11b2 <main+0x72>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    115d <main+0x1d>
movslq -0x8(%rbp),%rsi
shl    $0xc,%rsi
shr    $0x14,%rsi
lea    0xe3b(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x1000,%edi
call   1060 <malloc@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1184 <main+0x44>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
add    $0x1,%ebx
mov    $0x1000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    1160 <main+0x20>
shr    $0x8,%ebx
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Allocated %lu MB\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
store i32 128, i32* %3
store i32 0, i32* %2
br label %5
5: ; preds = %21, %0
%6 = load i32, i32* %2
%7 = sext i32 %6 to i64
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = shl i64 %9, 40
%11 = udiv i64 %10, 4096
%12 = icmp ult i64 %7, %11
br i1 %12, label %13, label %24
13: ; preds = %5
%14 = call i8* @malloc(i32 4096)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %13
br label %24
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32 @memset(i8* %19, i32 0, i32 1)
br label %21
21: ; preds = %18
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %5
24: ; preds = %17, %5
%25 = load i32, i32* %2
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 4096
%28 = lshr i64 %27, 20
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %28)
%30 = call i32 (...) @getchar()
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"Allocated %lu MB\0A\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%2 = icmp eq i8* %1, null
br i1 %2, label %13, label %3
3: ; preds = %0, %3
%4 = phi i8* [ %8, %3 ], [ %1, %0 ]
%5 = phi i32 [ %7, %3 ], [ 0, %0 ]
%6 = tail call i32 @memset(i8* %4, i32 0, i32 1)
%7 = add nuw nsw i32 %5, 1
%8 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %3
10: ; preds = %3
%11 = lshr i32 %7, 8
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %10, %0
%14 = phi i64 [ 0, %0 ], [ %12, %10 ]
%15 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %14)
%16 = tail call i32 (...) @getchar()
ret i32 0
}