#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 */
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, float, char *);
char *mem_avail(unsigned long memory, unsigned long base) {
  char *size_names[5] = {"B", "kB", "mB", "gB", "tB"};
  float total = (float)(memory * base);
  int high;
  for (high = 0; total > 1024; high++, total /= 1024)
    ;
  char *memsz = malloc(sizeof(char) * 16);
  snprintf(memsz, 16, "%.2f %s\n", total, size_names[high]);
  return memsz;
}


x86 assembly (clang, -O0):
<mem_avail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x30(%rbp)
movaps %xmm0,-0x40(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xec1(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xeaf(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xea7(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe9f(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
imul   %rcx,%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x58(%rbp)
test   %rax,%rax
movss  %xmm0,-0x54(%rbp)
js     11bf <mem_avail+0x9f>
movss  -0x58(%rbp),%xmm0
movss  %xmm0,-0x54(%rbp)
movss  -0x54(%rbp),%xmm0
movss  %xmm0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movss  -0x44(%rbp),%xmm0
movss  0xe23(%rip),%xmm1
ucomiss %xmm1,%xmm0
jbe    120f <mem_avail+0xef>
jmp    11eb <mem_avail+0xcb>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
movss  -0x44(%rbp),%xmm0
movss  0xdff(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x44(%rbp)
jmp    11d0 <mem_avail+0xb0>
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
movss  -0x44(%rbp),%xmm0
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rcx
mov    $0x10,%esi
lea    0xdd5(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x50(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_avail>:
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0xeda(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xed1(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xec8(%rip),%rax
mov    %rax,0x28(%rsp)
lea    0xebf(%rip),%rax
mov    %rax,0x30(%rsp)
imul   %rsi,%rdi
test   %rdi,%rdi
js     117d <mem_avail+0x5d>
cvtsi2ss %rdi,%xmm2
cmp    $0x401,%rdi
jae    119d <mem_avail+0x7d>
movss  %xmm2,0xc(%rsp)
lea    0xe94(%rip),%r14
jmp    11d6 <mem_avail+0xb6>
mov    %rdi,%rax
shr    %rax
mov    %edi,%ecx
and    $0x1,%ecx
or     %rax,%rcx
cvtsi2ss %rcx,%xmm2
addss  %xmm2,%xmm2
cmp    $0x401,%rdi
jb     116e <mem_avail+0x4e>
lea    0x10(%rsp),%rax
movss  0xe56(%rip),%xmm0
movss  0xe52(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mulss  %xmm0,%xmm2
add    $0x8,%rax
ucomiss %xmm1,%xmm2
ja     11c0 <mem_avail+0xa0>
movss  %xmm2,0xc(%rsp)
mov    (%rax),%r14
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
lea    0xe2a(%rip),%rdx
mov    %rax,%rdi
mov    $0x10,%esi
movss  0xc(%rsp),%xmm0
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"B\00"
@.str.1 = [3 x i8] c"kB\00"
@.str.2 = [3 x i8] c"mB\00"
@.str.3 = [3 x i8] c"gB\00"
@.str.4 = [3 x i8] c"tB\00"
@.str.5 = [9 x i8] c"%.2f %s\0A\00"
define i8* @mem_avail(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca [5 x i8*]
%6 = alloca float
%7 = alloca i32
%8 = alloca i8*
store i64 %0, i64* %3
store i64 %1, i64* %4
%9 = bitcast [5 x i8*]* %5 to i8*
%10 = bitcast i8* %9 to [5 x i8*]*
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i32 0, i32 0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i32 0, i32 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i32 0, i32 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i32 0, i32 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i32 0, i32 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8** %15
%16 = load i64, i64* %3
%17 = load i64, i64* %4
%18 = mul i64 %16, %17
%19 = uitofp i64 %18 to float
store float %19, float* %6
store i32 0, i32* %7
br label %20
20: ; preds = %24, %2
%21 = load float, float* %6
%22 = fcmp ogt float %21, 1.024000e+03
br i1 %22, label %23, label %29
23: ; preds = %20
br label %24
24: ; preds = %23
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
%27 = load float, float* %6
%28 = fdiv float %27, 1.024000e+03
store float %28, float* %6
br label %20
29: ; preds = %20
%30 = call i8* @malloc(i32 16)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = load float, float* %6
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @snprintf(i8* %31, i32 16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), float %32, i8* %36)
%38 = load i8*, i8** %8
ret i8* %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"B\00"
@.str.1 = [3 x i8] c"kB\00"
@.str.2 = [3 x i8] c"mB\00"
@.str.3 = [3 x i8] c"gB\00"
@.str.4 = [3 x i8] c"tB\00"
@.str.5 = [9 x i8] c"%.2f %s\0A\00"
define i8* @mem_avail(i64 %0, i64 %1) {
%3 = alloca [5 x i8*]
%4 = bitcast [5 x i8*]* %3 to i8*
%5 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %8
%9 = mul i64 %1, %0
%10 = uitofp i64 %9 to float
%11 = icmp ugt i64 %9, 1024
br i1 %11, label %12, label %22
12: ; preds = %2, %12
%13 = phi float [ %16, %12 ], [ %10, %2 ]
%14 = phi i32 [ %15, %12 ], [ 0, %2 ]
%15 = add nuw nsw i32 %14, 1
%16 = fmul float %13, 0x3F50000000000000
%17 = fcmp ogt float %16, 1.024000e+03
br i1 %17, label %12, label %18
18: ; preds = %12
%19 = zext i32 %15 to i64
%20 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %19
%21 = load i8*, i8** %20
br label %22
22: ; preds = %2, %18
%23 = phi i8* [ %21, %18 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%24 = phi float [ %16, %18 ], [ %10, %2 ]
%25 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%26 = tail call i32 @snprintf(i8* %25, i32 16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), float %24, i8* %23)
ret i8* %25
}