#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 */
struct sysinfo {
  int totalram;
  int freeram;
  int bufferram;
};

/* Variables and functions */
int meminfo(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, double);
char *smprintf(char *, double, char const);
scalar_t__ sysinfo(struct sysinfo *);
char *usedram() {
  struct sysinfo info;
  int cused = 0;
  const char unit[] = {'k', 'M', 'G', 'T'};
  if (sysinfo(&info) != 0)
    perror("sysinfo");
  printf("%d\n", meminfo("Cached: %d kB"));
  double used = ((info.totalram - info.freeram - info.bufferram) / 1024) -
                meminfo("Cached: %d kB");
  printf("used: %.2f\n", used);
  while (used > 1024) {
    used /= 1024;
    cused++;
  }
  return (smprintf("%.2f%c", used, unit[cused]));
}


x86 assembly (clang, -O0):
<usedram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x14(%rbp)
mov    0xea3(%rip),%eax
mov    %eax,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <sysinfo@plt>
cmp    $0x0,%rax
je     1187 <usedram+0x37>
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
lea    0xe8a(%rip),%rdi
call   1070 <meminfo@plt>
cvtsi2sd %eax,%xmm0
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
lea    0xe5a(%rip),%rdi
call   1070 <meminfo@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
movsd  -0x20(%rbp),%xmm0
movsd  0xe0f(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    121f <usedram+0xcf>
movsd  -0x20(%rbp),%xmm0
movsd  0xdf8(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e4 <usedram+0x94>
movsd  -0x20(%rbp),%xmm0
movslq -0x14(%rbp),%rax
lea    0xe03(%rip),%rdi
movsbl -0x18(%rbp,%rax,1),%esi
call   1030 <smprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usedram>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   1060 <sysinfo@plt>
test   %rax,%rax
jne    1223 <usedram+0xd3>
lea    0xeb0(%rip),%rbx
mov    %rbx,%rdi
call   1070 <meminfo@plt>
cvtsi2sd %eax,%xmm0
lea    0xe99(%rip),%rdi
call   1040 <printf@plt>
mov    0xc(%rsp),%eax
add    0x10(%rsp),%eax
mov    0x8(%rsp),%ecx
mov    %ecx,%edx
sub    %eax,%edx
neg    %eax
lea    (%rcx,%rax,1),%ebp
add    $0x3ff,%ebp
test   %edx,%edx
cmovns %edx,%ebp
sar    $0xa,%ebp
mov    %rbx,%rdi
call   1070 <meminfo@plt>
sub    %eax,%ebp
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
lea    0xe6b(%rip),%rdi
movsd  %xmm0,(%rsp)
call   1040 <printf@plt>
xor    %eax,%eax
cmp    $0x401,%ebp
jl     1200 <usedram+0xb0>
movsd  0xe21(%rip),%xmm2
movsd  0xe21(%rip),%xmm1
movsd  (%rsp),%xmm0
nopl   0x0(%rax)
mulsd  %xmm2,%xmm0
add    $0x1,%rax
ucomisd %xmm1,%xmm0
ja     11f0 <usedram+0xa0>
jmp    1205 <usedram+0xb5>
movsd  (%rsp),%xmm0
lea    0xe04(%rip),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xe23(%rip),%rdi
call   1030 <smprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdea(%rip),%rdi
call   1050 <perror@plt>
jmp    1169 <usedram+0x19>

LLVM IR (-O0):
%struct.sysinfo = type { i32, i32, i32 }
@__const.usedram.unit = [4 x i8] c"kMGT"
@.str = [8 x i8] c"sysinfo\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [14 x i8] c"Cached: %d kB\00"
@.str.3 = [12 x i8] c"used: %.2f\0A\00"
@.str.4 = [7 x i8] c"%.2f%c\00"
define i8* @usedram() {
%1 = alloca %struct.sysinfo
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca double
store i32 0, i32* %2
%5 = bitcast [4 x i8]* %3 to i8*
%6 = call i64 @sysinfo(%struct.sysinfo* %1)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = call i32 @meminfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%12 = sitofp i32 %11 to double
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double %12)
%14 = getelementptr inbounds %struct.sysinfo, %struct.sysinfo* %1, i32 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.sysinfo, %struct.sysinfo* %1, i32 0, i32 1
%17 = load i32, i32* %16
%18 = sub nsw i32 %15, %17
%19 = getelementptr inbounds %struct.sysinfo, %struct.sysinfo* %1, i32 0, i32 2
%20 = load i32, i32* %19
%21 = sub nsw i32 %18, %20
%22 = sdiv i32 %21, 1024
%23 = call i32 @meminfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%24 = sub nsw i32 %22, %23
%25 = sitofp i32 %24 to double
store double %25, double* %4
%26 = load double, double* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), double %26)
br label %28
28: ; preds = %31, %10
%29 = load double, double* %4
%30 = fcmp ogt double %29, 1.024000e+03
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load double, double* %4
%33 = fdiv double %32, 1.024000e+03
store double %33, double* %4
%34 = load i32, i32* %2
%35 = add nsw i32 %34, 1
store i32 %35, i32* %2
br label %28
36: ; preds = %28
%37 = load double, double* %4
%38 = load i32, i32* %2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = call i8* @smprintf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), double %37, i8 signext %41)
ret i8* %42
}

LLVM IR (-O2):
%struct.sysinfo = type { i32, i32, i32 }
@__const.usedram.unit = [4 x i8] c"kMGT"
@.str = [8 x i8] c"sysinfo\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [14 x i8] c"Cached: %d kB\00"
@.str.3 = [12 x i8] c"used: %.2f\0A\00"
@.str.4 = [7 x i8] c"%.2f%c\00"
define i8* @usedram() {
%1 = alloca %struct.sysinfo
%2 = bitcast %struct.sysinfo* %1 to i8*
%3 = call i64 @sysinfo(%struct.sysinfo* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = call i32 @meminfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%9 = sitofp i32 %8 to double
%10 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double %9)
%11 = getelementptr inbounds %struct.sysinfo, %struct.sysinfo* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.sysinfo, %struct.sysinfo* %1, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.sysinfo, %struct.sysinfo* %1, i64 0, i32 2
%16 = load i32, i32* %15
%17 = add i32 %14, %16
%18 = sub i32 %12, %17
%19 = sdiv i32 %18, 1024
%20 = call i32 @meminfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%21 = sub nsw i32 %19, %20
%22 = sitofp i32 %21 to double
%23 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), double %22)
%24 = icmp sgt i32 %21, 1024
br i1 %24, label %25, label %33
25: ; preds = %7, %25
%26 = phi double [ %28, %25 ], [ %22, %7 ]
%27 = phi i32 [ %29, %25 ], [ 0, %7 ]
%28 = fmul double %26, 0x3F50000000000000
%29 = add nuw nsw i32 %27, 1
%30 = fcmp ogt double %28, 1.024000e+03
br i1 %30, label %25, label %31
31: ; preds = %25
%32 = zext i32 %29 to i64
br label %33
33: ; preds = %31, %7
%34 = phi i64 [ 0, %7 ], [ %32, %31 ]
%35 = phi double [ %22, %7 ], [ %28, %31 ]
%36 = getelementptr inbounds [4 x i8], [4 x i8]* @__const.usedram.unit, i64 0, i64 %34
%37 = load i8, i8* %36
%38 = call i8* @smprintf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), double %35, i8 signext %37)
ret i8* %38
}