#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 mem_size();
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ safe_free(void *);
void *safe_malloc(int);
int main(void) {
  double size;
  void *ptr;
  size = mem_size() / (1024 * 1024);
  printf("Initial memory use = %.2f MB\n", size);
  ptr = safe_malloc(4096 * 1024);
  size = mem_size() / (1024 * 1024);
  printf("Memory use after malloc = %.2f MB\n", size);
  safe_free(ptr);
  size = mem_size() / (1024 * 1024);
  printf("Memory use after free = %.2f MB\n", size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <mem_size@plt>
mov    $0x100000,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
lea    0xe8d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x400000,%edi
call   1030 <safe_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <mem_size@plt>
mov    $0x100000,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <safe_free@plt>
mov    $0x0,%al
call   1050 <mem_size@plt>
mov    $0x100000,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1050 <mem_size@plt>
lea    0xfffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x14,%ecx
cvtsi2sd %ecx,%xmm0
lea    0xe9f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x400000,%edi
call   1030 <safe_malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <mem_size@plt>
lea    0xfffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x14,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
lea    0xe88(%rip),%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <safe_free@plt>
xor    %eax,%eax
call   1050 <mem_size@plt>
lea    0xfffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x14,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Initial memory use = %.2f MB\0A\00"
@.str.1 = [35 x i8] c"Memory use after malloc = %.2f MB\0A\00"
@.str.2 = [33 x i8] c"Memory use after free = %.2f MB\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 (...) @mem_size()
%5 = sdiv i32 %4, 1048576
%6 = sitofp i32 %5 to double
store double %6, double* %2
%7 = load double, double* %2
%8 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), double %7)
%9 = call i8* @safe_malloc(i32 4194304)
store i8* %9, i8** %3
%10 = call i32 (...) @mem_size()
%11 = sdiv i32 %10, 1048576
%12 = sitofp i32 %11 to double
store double %12, double* %2
%13 = load double, double* %2
%14 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), double %13)
%15 = load i8*, i8** %3
%16 = call i32 @safe_free(i8* %15)
%17 = call i32 (...) @mem_size()
%18 = sdiv i32 %17, 1048576
%19 = sitofp i32 %18 to double
store double %19, double* %2
%20 = load double, double* %2
%21 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), double %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"Initial memory use = %.2f MB\0A\00"
@.str.1 = [35 x i8] c"Memory use after malloc = %.2f MB\0A\00"
@.str.2 = [33 x i8] c"Memory use after free = %.2f MB\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @mem_size()
%2 = sdiv i32 %1, 1048576
%3 = sitofp i32 %2 to double
%4 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), double %3)
%5 = tail call i8* @safe_malloc(i32 4194304)
%6 = tail call i32 (...) @mem_size()
%7 = sdiv i32 %6, 1048576
%8 = sitofp i32 %7 to double
%9 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), double %8)
%10 = tail call i32 @safe_free(i8* %5)
%11 = tail call i32 (...) @mem_size()
%12 = sdiv i32 %11, 1048576
%13 = sitofp i32 %12 to double
%14 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), double %13)
ret i32 0
}