#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ *malloc(unsigned long);
int /*<<< orphan*/ printf(char *, unsigned long);
int main(int argc, char *argv[]) {
  unsigned long block_size;
  unsigned long total_size;
  uint8_t *pData;
  block_size = (1 * 1024 * 1024);
  while (1) {
    pData = malloc(block_size);
    if (pData == NULL) {
      if (block_size <= 2) {
        break;
      } else {
        printf("block size: %08X\n", block_size);
        block_size >>= 1;
      }
    } else {
      total_size += block_size;
    }
  }
  printf("total size: %08X\n", total_size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x100000,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1187 <main+0x67>
cmpq   $0x2,-0x18(%rbp)
ja     1166 <main+0x46>
jmp    1198 <main+0x78>
mov    -0x18(%rbp),%rsi
lea    0xe8f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1193 <main+0x73>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113e <main+0x1e>
mov    -0x20(%rbp),%rsi
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x100000,%ebx
lea    0xecf(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
jne    1163 <main+0x43>
cmp    $0x3,%rbx
jb     1168 <main+0x48>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
shr    %rbx
jmp    1140 <main+0x20>
add    %rbx,%r14
jmp    1140 <main+0x20>
lea    0xea3(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"block size: %08X\0A\00"
@.str.1 = [18 x i8] c"total size: %08X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 1048576, i64* %6
br label %9
9: ; preds = %2, %28
%10 = load i64, i64* %6
%11 = call i32* @malloc(i64 %10)
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = load i64, i64* %6
%16 = icmp ule i64 %15, 2
br i1 %16, label %17, label %18
17: ; preds = %14
br label %29
18: ; preds = %14
%19 = load i64, i64* %6
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %19)
%21 = load i64, i64* %6
%22 = lshr i64 %21, 1
store i64 %22, i64* %6
br label %23
23: ; preds = %18
br label %28
24: ; preds = %9
%25 = load i64, i64* %6
%26 = load i64, i64* %7
%27 = add i64 %26, %25
store i64 %27, i64* %7
br label %28
28: ; preds = %24, %23
br label %9
29: ; preds = %17
%30 = load i64, i64* %7
%31 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"block size: %08X\0A\00"
@.str.1 = [18 x i8] c"total size: %08X\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
br label %3
3: ; preds = %15, %2
%4 = phi i64 [ %16, %15 ], [ undef, %2 ]
%5 = phi i64 [ %7, %15 ], [ 1048576, %2 ]
br label %6
6: ; preds = %3, %12
%7 = phi i64 [ %14, %12 ], [ %5, %3 ]
%8 = tail call i32* @malloc(i64 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = icmp ult i64 %7, 3
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %7)
%14 = lshr i64 %7, 1
br label %6
15: ; preds = %6
%16 = add i64 %7, %4
br label %3
17: ; preds = %10
%18 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %4)
ret i32 0
}