#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *, unsigned long *, char *);
scalar_t__ strcmp(char *, char *);
double getRAMUsage(void) {
  static char statusfile[] = "/proc/self/status";
  static char memfile[] = "/proc/meminfo";
  unsigned long VmRSS = 0, value = 0, total = 0;
  char title[32] = "";
  char units[32] = "";
  int ret = 0;
  FILE *fp = NULL;
  fp = fopen(statusfile, "r");
  if (fp) {
    while (ret != EOF) {
      ret = fscanf(fp, "%31s %lu %s\n", title, &value, units);
      if (strcmp(title, "VmRSS:") == 0) {
        VmRSS = value * 1024;
        break;
      }
    }
    fclose(fp);
  }
  fp = fopen(memfile, "r");
  if (fp) {
    while (ret != EOF) {
      ret = fscanf(fp, "%31s %lu %s\n", title, &value, units);
      if (strcmp(title, "MemTotal:") == 0) {
        total = value * 1024;
        break;
      }
    }
    fclose(fp);
  }
  if (VmRSS > 0 && total > 0) {
    return ((double)VmRSS * 100) / (double)total;
  } else {
    return 0.0;
  }
}


x86 assembly (clang, -O0):
<getRAMUsage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movl   $0x0,-0x64(%rbp)
movq   $0x0,-0x70(%rbp)
lea    0x2eaa(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     1230 <getRAMUsage+0xe0>
jmp    11c6 <getRAMUsage+0x76>
mov    -0x64(%rbp),%eax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
je     1227 <getRAMUsage+0xd7>
mov    -0x70(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%r8
lea    0xe3f(%rip),%rsi
lea    -0x18(%rbp),%rcx
call   1050 <fscanf@plt>
mov    %eax,-0x64(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1222 <getRAMUsage+0xd2>
mov    -0x18(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x10(%rbp)
jmp    1227 <getRAMUsage+0xd7>
jmp    11c6 <getRAMUsage+0x76>
mov    -0x70(%rbp),%rdi
call   1030 <fclose@plt>
lea    0x2e2b(%rip),%rdi
lea    0xdea(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     12c1 <getRAMUsage+0x171>
jmp    1257 <getRAMUsage+0x107>
mov    -0x64(%rbp),%eax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%eax
je     12b8 <getRAMUsage+0x168>
mov    -0x70(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%r8
lea    0xdae(%rip),%rsi
lea    -0x18(%rbp),%rcx
call   1050 <fscanf@plt>
mov    %eax,-0x64(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12b3 <getRAMUsage+0x163>
mov    -0x18(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x20(%rbp)
jmp    12b8 <getRAMUsage+0x168>
jmp    1257 <getRAMUsage+0x107>
mov    -0x70(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x10(%rbp)
jbe    1330 <getRAMUsage+0x1e0>
cmpq   $0x0,-0x20(%rbp)
jbe    1330 <getRAMUsage+0x1e0>
movq   -0x10(%rbp),%xmm0
movaps 0xd1d(%rip),%xmm3
punpckldq %xmm3,%xmm0
movapd 0xd21(%rip),%xmm2
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xd1a(%rip),%xmm1
mulsd  %xmm1,%xmm0
movq   -0x20(%rbp),%xmm1
punpckldq %xmm3,%xmm1
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1338 <getRAMUsage+0x1e8>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRAMUsage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
movq   $0x0,0x8(%rsp)
xorpd  %xmm0,%xmm0
movapd %xmm0,0x40(%rsp)
movapd %xmm0,0x30(%rsp)
movapd %xmm0,0x20(%rsp)
movapd %xmm0,0x10(%rsp)
lea    0x2ed6(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11fc <getRAMUsage+0xbc>
mov    %rax,%r14
xor    %eax,%eax
mov    %rax,(%rsp)
lea    0x30(%rsp),%r12
lea    0x8(%rsp),%r13
lea    0x10(%rsp),%rbp
lea    0xe8d(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x2e31(%rip),%rax
cmp    (%rax),%ebx
je     11f2 <getRAMUsage+0xb2>
mov    %r14,%rdi
lea    0xe65(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
call   1040 <fscanf@plt>
mov    %eax,%ebx
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11b0 <getRAMUsage+0x70>
mov    0x8(%rsp),%rax
shl    $0xa,%rax
mov    %rax,(%rsp)
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1204 <getRAMUsage+0xc4>
xor    %eax,%eax
mov    %rax,(%rsp)
xor    %ebx,%ebx
lea    0x2e57(%rip),%rdi
lea    0xe16(%rip),%rsi
call   1060 <fopen@plt>
xorpd  %xmm0,%xmm0
test   %rax,%rax
je     12f4 <getRAMUsage+0x1b4>
mov    %rax,%r14
lea    0x30(%rsp),%r12
lea    0x8(%rsp),%r13
lea    0x10(%rsp),%rbp
lea    0xe01(%rip),%r15
nopl   (%rax)
mov    0x2da1(%rip),%rax
cmp    (%rax),%ebx
je     1284 <getRAMUsage+0x144>
mov    %r14,%rdi
lea    0xdd5(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
call   1040 <fscanf@plt>
mov    %eax,%ebx
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1240 <getRAMUsage+0x100>
mov    0x8(%rsp),%rbp
shl    $0xa,%rbp
mov    (%rsp),%rbx
jmp    128a <getRAMUsage+0x14a>
mov    (%rsp),%rbx
xor    %ebp,%ebp
mov    %r14,%rdi
call   1030 <fclose@plt>
test   %rbx,%rbx
je     12f0 <getRAMUsage+0x1b0>
test   %rbp,%rbp
xorpd  %xmm0,%xmm0
je     12f4 <getRAMUsage+0x1b4>
movq   %rbx,%xmm3
movdqa 0xd53(%rip),%xmm1
punpckldq %xmm1,%xmm3
movapd 0xd57(%rip),%xmm2
subpd  %xmm2,%xmm3
movapd %xmm3,%xmm0
unpckhpd %xmm3,%xmm0
addsd  %xmm3,%xmm0
mulsd  0xd4f(%rip),%xmm0
movq   %rbp,%xmm3
punpckldq %xmm1,%xmm3
subpd  %xmm2,%xmm3
movapd %xmm3,%xmm1
unpckhpd %xmm3,%xmm1
addsd  %xmm3,%xmm1
divsd  %xmm1,%xmm0
jmp    12f4 <getRAMUsage+0x1b4>
xorpd  %xmm0,%xmm0
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@getRAMUsage.statusfile = internal global [18 x i8] c"/proc/self/status\00"
@getRAMUsage.memfile = internal global [14 x i8] c"/proc/meminfo\00"
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [13 x i8] c"%31s %lu %s\0A\00"
@.str.2 = [7 x i8] c"VmRSS:\00"
@.str.3 = [10 x i8] c"MemTotal:\00"
define double @getRAMUsage() {
%1 = alloca double
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca [32 x i8]
%6 = alloca [32 x i8]
%7 = alloca i32
%8 = alloca i32*
store i64 0, i64* %2
store i64 0, i64* %3
store i64 0, i64* %4
%9 = bitcast [32 x i8]* %5 to i8*
%10 = bitcast [32 x i8]* %6 to i8*
store i32 0, i32* %7
store i32* null, i32** %8
%11 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @getRAMUsage.statusfile, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %34
14: ; preds = %0
br label %15
15: ; preds = %30, %14
%16 = load i32, i32* %7
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %31
19: ; preds = %15
%20 = load i32*, i32** %8
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%23 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %21, i64* %3, i8* %22)
store i32 %23, i32* %7
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i64, i64* %3
%29 = mul i64 %28, 1024
store i64 %29, i64* %2
br label %31
30: ; preds = %19
br label %15
31: ; preds = %27, %15
%32 = load i32*, i32** %8
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %31, %0
%35 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @getRAMUsage.memfile, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %35, i32** %8
%36 = load i32*, i32** %8
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %58
38: ; preds = %34
br label %39
39: ; preds = %54, %38
%40 = load i32, i32* %7
%41 = load i32, i32* @EOF
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %55
43: ; preds = %39
%44 = load i32*, i32** %8
%45 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%47 = call i32 @fscanf(i32* %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %45, i64* %3, i8* %46)
store i32 %47, i32* %7
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %43
%52 = load i64, i64* %3
%53 = mul i64 %52, 1024
store i64 %53, i64* %4
br label %55
54: ; preds = %43
br label %39
55: ; preds = %51, %39
%56 = load i32*, i32** %8
%57 = call i32 @fclose(i32* %56)
br label %58
58: ; preds = %55, %34
%59 = load i64, i64* %2
%60 = icmp ugt i64 %59, 0
br i1 %60, label %61, label %71
61: ; preds = %58
%62 = load i64, i64* %4
%63 = icmp ugt i64 %62, 0
br i1 %63, label %64, label %71
64: ; preds = %61
%65 = load i64, i64* %2
%66 = uitofp i64 %65 to double
%67 = fmul double %66, 1.000000e+02
%68 = load i64, i64* %4
%69 = uitofp i64 %68 to double
%70 = fdiv double %67, %69
store double %70, double* %1
br label %72
71: ; preds = %61, %58
store double 0.000000e+00, double* %1
br label %72
72: ; preds = %71, %64
%73 = load double, double* %1
ret double %73
}

LLVM IR (-O2):
@getRAMUsage.statusfile = internal global [18 x i8] c"/proc/self/status\00"
@getRAMUsage.memfile = internal global [14 x i8] c"/proc/meminfo\00"
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [13 x i8] c"%31s %lu %s\0A\00"
@.str.2 = [7 x i8] c"VmRSS:\00"
@.str.3 = [10 x i8] c"MemTotal:\00"
define double @getRAMUsage() {
%1 = alloca i64
%2 = alloca [32 x i8]
%3 = alloca [32 x i8]
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @getRAMUsage.statusfile, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %24, label %9
9: ; preds = %0, %13
%10 = phi i32 [ %14, %13 ], [ 0, %0 ]
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %20, label %13
13: ; preds = %9
%14 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %5, i64* %1, i8* %6)
%15 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %9
17: ; preds = %13
%18 = load i64, i64* %1
%19 = shl i64 %18, 10
br label %20
20: ; preds = %9, %17
%21 = phi i64 [ %19, %17 ], [ 0, %9 ]
%22 = phi i32 [ %14, %17 ], [ %10, %9 ]
%23 = call i32 @fclose(i32* %7)
br label %24
24: ; preds = %20, %0
%25 = phi i64 [ %21, %20 ], [ 0, %0 ]
%26 = phi i32 [ %22, %20 ], [ 0, %0 ]
%27 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @getRAMUsage.memfile, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %51, label %29
29: ; preds = %24, %33
%30 = phi i32 [ %34, %33 ], [ %26, %24 ]
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %40, label %33
33: ; preds = %29
%34 = call i32 @fscanf(i32* %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %5, i64* %1, i8* %6)
%35 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %29
37: ; preds = %33
%38 = load i64, i64* %1
%39 = shl i64 %38, 10
br label %40
40: ; preds = %29, %37
%41 = phi i64 [ %39, %37 ], [ 0, %29 ]
%42 = call i32 @fclose(i32* %27)
%43 = icmp ne i64 %25, 0
%44 = icmp ne i64 %41, 0
%45 = select i1 %43, i1 %44, i1 false
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = uitofp i64 %25 to double
%48 = fmul double %47, 1.000000e+02
%49 = uitofp i64 %41 to double
%50 = fdiv double %48, %49
br label %51
51: ; preds = %24, %40, %46
%52 = phi double [ %50, %46 ], [ 0.000000e+00, %40 ], [ 0.000000e+00, %24 ]
ret double %52
}