#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 const);
int get_free_memory() {
  const int chunk_size = 65536, chunks = 1024;
  void *mem_reserv[chunks];
  int total_mem = 0, i;
  for (i = 0; i < chunks; i++)
    mem_reserv[i] = NULL;
  for (i = 0; i < chunks; i++) {
    if (!(mem_reserv[i] = malloc(chunk_size)))
      break;
    total_mem += chunk_size;
  }
  for (i = 0; i < chunks; i++) {
    if (!mem_reserv[i])
      break;
    free(mem_reserv[i]);
  }
  return total_mem;
}


x86 assembly (clang, -O0):
<get_free_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
movl   $0x10000,-0x4(%rbp)
movl   $0x400,-0x8(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x400,-0x18(%rbp)
jge    1179 <get_free_memory+0x59>
movslq -0x18(%rbp),%rax
movq   $0x0,-0x2020(%rbp,%rax,8)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114e <get_free_memory+0x2e>
movl   $0x0,-0x18(%rbp)
cmpl   $0x400,-0x18(%rbp)
jge    11cb <get_free_memory+0xab>
mov    $0x10000,%edi
call   1040 <malloc@plt>
movslq -0x18(%rbp),%rcx
mov    %rax,-0x2020(%rbp,%rcx,8)
ff
cmp    $0x0,%rax
jne    11b2 <get_free_memory+0x92>
jmp    11cb <get_free_memory+0xab>
mov    -0x14(%rbp),%eax
add    $0x10000,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1180 <get_free_memory+0x60>
movl   $0x0,-0x18(%rbp)
cmpl   $0x400,-0x18(%rbp)
jge    1216 <get_free_memory+0xf6>
movslq -0x18(%rbp),%rax
cmpq   $0x0,-0x2020(%rbp,%rax,8)
jne    11f7 <get_free_memory+0xd7>
jmp    1216 <get_free_memory+0xf6>
movslq -0x18(%rbp),%rax
mov    -0x2020(%rbp,%rax,8),%rdi
ff
call   1030 <free@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11d2 <get_free_memory+0xb2>
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_free_memory>:
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    $0x2000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
nop
mov    $0x10000,%edi
call   1050 <malloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     117c <get_free_memory+0x4c>
add    $0x8,%rbx
add    $0x10000,%r14
cmp    $0x4000000,%r14
jne    1150 <get_free_memory+0x20>
mov    $0x4000000,%r14d
xor    %ebx,%ebx
xchg   %ax,%ax
mov    (%rsp,%rbx,8),%rdi
test   %rdi,%rdi
je     119b <get_free_memory+0x6b>
call   1030 <free@plt>
add    $0x1,%rbx
cmp    $0x400,%rbx
jne    1180 <get_free_memory+0x50>
mov    %r14d,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @get_free_memory() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 65536, i32* %1
store i32 1024, i32* %2
%6 = call i8* @llvm.stacksave()
store i8* %6, i8** %3
%7 = alloca i8*, i64 1024
store i32 0, i32* %4
store i32 0, i32* %5
br label %8
8: ; preds = %15, %0
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 1024
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8*, i8** %7, i64 %13
store i8* null, i8** %14
br label %15
15: ; preds = %11
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 1
store i32 %17, i32* %5
br label %8
18: ; preds = %8
store i32 0, i32* %5
br label %19
19: ; preds = %32, %18
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 1024
br i1 %21, label %22, label %35
22: ; preds = %19
%23 = call i8* @malloc(i32 65536)
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %7, i64 %25
store i8* %23, i8** %26
%27 = icmp ne i8* %23, null
br i1 %27, label %29, label %28
28: ; preds = %22
br label %35
29: ; preds = %22
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 65536
store i32 %31, i32* %4
br label %32
32: ; preds = %29
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %19
35: ; preds = %28, %19
store i32 0, i32* %5
br label %36
36: ; preds = %52, %35
%37 = load i32, i32* %5
%38 = icmp slt i32 %37, 1024
br i1 %38, label %39, label %55
39: ; preds = %36
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %7, i64 %41
%43 = load i8*, i8** %42
%44 = icmp ne i8* %43, null
br i1 %44, label %46, label %45
45: ; preds = %39
br label %55
46: ; preds = %39
%47 = load i32, i32* %5
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %7, i64 %48
%50 = load i8*, i8** %49
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %46
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %36
55: ; preds = %45, %36
%56 = load i32, i32* %4
%57 = load i8*, i8** %3
ret i32 %56
}

LLVM IR (-O2):
define i32 @get_free_memory() {
%1 = alloca [1024 x i8*]
%2 = bitcast [1024 x i8*]* %1 to i8*
br label %3
3: ; preds = %0, %9
%4 = phi i64 [ 0, %0 ], [ %11, %9 ]
%5 = phi i32 [ 0, %0 ], [ %10, %9 ]
%6 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%7 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %1, i64 0, i64 %4
store i8* %6, i8** %7
%8 = icmp eq i8* %6, null
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = add nuw nsw i32 %5, 65536
%11 = add nuw nsw i64 %4, 1
%12 = icmp eq i64 %11, 1024
br i1 %12, label %13, label %3
13: ; preds = %3, %9
%14 = phi i32 [ %5, %3 ], [ 67108864, %9 ]
br label %15
15: ; preds = %13, %20
%16 = phi i64 [ 0, %13 ], [ %22, %20 ]
%17 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %1, i64 0, i64 %16
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = tail call i32 @free(i8* %18)
%22 = add nuw nsw i64 %16, 1
%23 = icmp eq i64 %22, 1024
br i1 %23, label %24, label %15
24: ; preds = %15, %20
ret i32 %14
}