#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(int /*<<< orphan*/);
void *malloc(size_t);
int /*<<< orphan*/ malloc_and_break(void *, int, size_t);
int /*<<< orphan*/ memset(void *, int, size_t);
int /*<<< orphan*/ printf(char *);
int main() {
  malloc(1);
  size_t len = (1024 * 1024 * 1024);
  while (len > (128 * 1024 * 1024)) {
    void *mem = malloc(len);
    memset(mem, 0xff, len);
    free(malloc_and_break(mem, 0xff, len));
    len /= 3;
  }
  printf("Memory was allocated and freed!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
call   1070 <malloc@plt>
movq   $0x40000000,-0x10(%rbp)
40
cmpq   $0x8000000,-0x10(%rbp)
08
jbe    11ce <main+0x7e>
mov    -0x10(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xff,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xff,%esi
call   1060 <malloc_and_break@plt>
mov    %eax,%edi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x10(%rbp)
jmp    1171 <main+0x21>
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x40000000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    $0x40000000,%edx
mov    %rax,%rdi
mov    $0xff,%esi
call   1050 <memset@plt>
mov    $0x40000000,%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1060 <malloc_and_break@plt>
mov    %eax,%edi
call   1030 <free@plt>
mov    $0x15555555,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    $0x15555555,%edx
mov    %rax,%rdi
mov    $0xff,%esi
call   1050 <memset@plt>
mov    $0x15555555,%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1060 <malloc_and_break@plt>
mov    %eax,%edi
call   1030 <free@plt>
lea    0xe38(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@str = [32 x i8] c"Memory was allocated and freed!\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(1073741824) i8* @malloc(i64 1073741824)
%2 = tail call i32 @memset(i8* %1, i32 255, i64 1073741824)
%3 = tail call i32 @malloc_and_break(i8* %1, i32 255, i64 1073741824)
%4 = tail call i32 @free(i32 %3)
%5 = tail call dereferenceable_or_null(357913941) i8* @malloc(i64 357913941)
%6 = tail call i32 @memset(i8* %5, i32 255, i64 357913941)
%7 = tail call i32 @malloc_and_break(i8* %5, i32 255, i64 357913941)
%8 = tail call i32 @free(i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
ret i32 0
}