#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(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
void *reduce(void *, int);
int main() {
  malloc(1);
  int size = (1 * (1024 * 1024 * 1024));
  while (size > (1 * 1024)) {
    void *ptr = malloc(size);
    ptr = reduce(ptr, size / 2);
    free(ptr);
    size /= 2;
  }
  printf("Memory was allocated, used, and freed!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
movl   $0x40000000,-0x8(%rbp)
cmpl   $0x400,-0x8(%rbp)
jle    11af <main+0x6f>
mov    -0x8(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1040 <reduce@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    1160 <main+0x20>
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x40000000,%ebx
mov    $0x40000000,%ebp
nopl   (%rax)
mov    %ebx,%edi
call   1060 <malloc@plt>
shr    %ebp
mov    %rax,%rdi
mov    %ebp,%esi
call   1050 <reduce@plt>
mov    %rax,%rdi
call   1030 <free@plt>
cmp    $0x801,%ebx
mov    %ebp,%ebx
ja     1150 <main+0x10>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Memory was allocated, used, and freed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i8* @malloc(i32 1)
store i32 1073741824, i32* %2
br label %5
5: ; preds = %8, %0
%6 = load i32, i32* %2
%7 = icmp sgt i32 %6, 1024
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = load i32, i32* %2
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = load i32, i32* %2
%13 = sdiv i32 %12, 2
%14 = call i8* @reduce(i8* %11, i32 %13)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = call i32 @free(i8* %15)
%17 = load i32, i32* %2
%18 = sdiv i32 %17, 2
store i32 %18, i32* %2
br label %5
19: ; preds = %5
%20 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@str = [39 x i8] c"Memory was allocated, used, and freed!\00"
define i32 @main() {
br label %1
1: ; preds = %0, %1
%2 = phi i32 [ 1073741824, %0 ], [ %4, %1 ]
%3 = tail call i8* @malloc(i32 %2)
%4 = lshr i32 %2, 1
%5 = tail call i8* @reduce(i8* %3, i32 %4)
%6 = tail call i32 @free(i8* %5)
%7 = icmp ugt i32 %2, 2049
br i1 %7, label %1, label %8
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
ret i32 0
}