#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
float ramusage(void) {
  FILE *fd;
  char *buf;
  int total, active, buflen;
  total = active = -1;
  buflen = 512;
  fd = fopen("/proc/meminfo", "r");
  if (fd == NULL) {
    fprintf(stderr, "Error opening /proc/meminfo.\n");
    return -1;
  }
  buf = (char *)malloc(sizeof(char) * buflen);
  while (total == -1) {
    fgets(buf, buflen, fd);
    sscanf(buf, "MemTotal: %d kB\n", &total);
  }
  while (active == -1) {
    fgets(buf, buflen, fd);
    sscanf(buf, "Active: %d kB\n", &active);
  }
  fclose(fd);
  free(buf);
  return ((float)active / (float)total) * 100.0;
}


x86 assembly (clang, -O0):
<ramusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0x20(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x200,-0x24(%rbp)
lea    0xe78(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d6 <ramusage+0x66>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1060 <fprintf@plt>
movss  0xe3c(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    128d <ramusage+0x11d>
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    121c <ramusage+0xac>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
mov    -0x18(%rbp),%rdi
lea    0xe2c(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <sscanf@plt>
jmp    11e9 <ramusage+0x79>
jmp    1221 <ramusage+0xb1>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1254 <ramusage+0xe4>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
mov    -0x18(%rbp),%rdi
lea    0xe05(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <sscanf@plt>
jmp    1221 <ramusage+0xb1>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cvtsi2ssl -0x20(%rbp),%xmm0
cvtsi2ssl -0x1c(%rbp),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd80(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ramusage>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
movl   $0xffffffff,(%rsp)
movl   $0xffffffff,0x4(%rsp)
ff
lea    0xe7a(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     123d <ramusage+0xcd>
mov    %rax,%r14
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe7c(%rip),%r15
lea    0x4(%rsp),%r12
nop
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <sscanf@plt>
cmpl   $0xffffffff,0x4(%rsp)
je     11c0 <ramusage+0x50>
lea    0xe5b(%rip),%r15
mov    %rsp,%r12
nop
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <sscanf@plt>
cmpl   $0xffffffff,(%rsp)
je     11f0 <ramusage+0x80>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
cvtsi2ssl (%rsp),%xmm0
cvtsi2ssl 0x4(%rsp),%xmm1
divss  %xmm1,%xmm0
mulss  0xdc5(%rip),%xmm0
jmp    125a <ramusage+0xea>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1060 <fprintf@plt>
movss  0xdaa(%rip),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Error opening /proc/meminfo.\0A\00"
@.str.3 = [17 x i8] c"MemTotal: %d kB\0A\00"
@.str.4 = [15 x i8] c"Active: %d kB\0A\00"
define float @ramusage() {
%1 = alloca float
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 -1, i32* %5
store i32 -1, i32* %4
store i32 512, i32* %6
%7 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %54
13: ; preds = %0
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = mul i64 1, %15
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %3
br label %20
20: ; preds = %23, %13
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = load i32, i32* %6
%26 = load i32*, i32** %2
%27 = call i32 @fgets(i8* %24, i32 %25, i32* %26)
%28 = load i8*, i8** %3
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32* %4)
br label %20
30: ; preds = %20
br label %31
31: ; preds = %34, %30
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i8*, i8** %3
%36 = load i32, i32* %6
%37 = load i32*, i32** %2
%38 = call i32 @fgets(i8* %35, i32 %36, i32* %37)
%39 = load i8*, i8** %3
%40 = call i32 @sscanf(i8* %39, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32* %5)
br label %31
41: ; preds = %31
%42 = load i32*, i32** %2
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %3
%45 = call i32 @free(i8* %44)
%46 = load i32, i32* %5
%47 = sitofp i32 %46 to float
%48 = load i32, i32* %4
%49 = sitofp i32 %48 to float
%50 = fdiv float %47, %49
%51 = fpext float %50 to double
%52 = fmul double %51, 1.000000e+02
%53 = fptrunc double %52 to float
store float %53, float* %1
br label %54
54: ; preds = %41, %10
%55 = load float, float* %1
ret float %55
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Error opening /proc/meminfo.\0A\00"
@.str.3 = [17 x i8] c"MemTotal: %d kB\0A\00"
@.str.4 = [15 x i8] c"Active: %d kB\0A\00"
define float @ramusage() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
store i32 -1, i32* %1
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %32
10: ; preds = %0
%11 = tail call i64 @malloc(i32 512)
%12 = inttoptr i64 %11 to i8*
br label %13
13: ; preds = %10, %13
%14 = call i32 @fgets(i8* %12, i32 512, i32* %5)
%15 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%16 = load i32, i32* %1
%17 = icmp eq i32 %16, -1
br i1 %17, label %13, label %18
18: ; preds = %13, %18
%19 = call i32 @fgets(i8* %12, i32 512, i32* %5)
%20 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%21 = load i32, i32* %2
%22 = icmp eq i32 %21, -1
br i1 %22, label %18, label %23
23: ; preds = %18
%24 = call i32 @fclose(i32* %5)
%25 = call i32 @free(i8* %12)
%26 = load i32, i32* %2
%27 = sitofp i32 %26 to float
%28 = load i32, i32* %1
%29 = sitofp i32 %28 to float
%30 = fdiv float %27, %29
%31 = fmul float %30, 1.000000e+02
br label %32
32: ; preds = %23, %7
%33 = phi float [ -1.000000e+00, %7 ], [ %31, %23 ]
ret float %33
}