#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, long *, long *, long *,
                          long *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *get_memusage() {
  FILE *fd;
  long total, fre, buf, cache;
  double used;
  static char memstr[42];
  fd = fopen("/proc/meminfo", "r");
  fscanf(fd,
         "MemTotal: %ld kB\nMemFree: %ld kB\nBuffers: %ld kB\nCached: %ld kB\n",
         &total, &fre, &buf, &cache);
  fclose(fd);
  used = (double)(total - fre) / total;
  sprintf(memstr, "%.2lf%%", 100 * used);
  return memstr;
}


x86 assembly (clang, -O0):
<get_memusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb9(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeae(%rip),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%r8
lea    -0x28(%rbp),%r9
call   1040 <fscanf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
sub    -0x18(%rbp),%rax
cvtsi2sd %rax,%xmm0
cvtsi2sdq -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe54(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%edx
lea    0x2e94(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1060 <sprintf@plt>
lea    0x2e81(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_memusage>:
push   %rbx
sub    $0x20,%rsp
lea    0xebc(%rip),%rdi
lea    0xec3(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xeb6(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rax
cvtsi2sd %rax,%xmm0
sub    (%rsp),%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm0,%xmm1
mulsd  0xe5d(%rip),%xmm1
cvttsd2si %xmm1,%edx
lea    0x2ea2(%rip),%rbx
lea    0xea4(%rip),%rsi
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@get_memusage.memstr = internal global [42 x i8] zeroinitializer
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [65 x i8] c"MemTotal: %ld kB\0AMemFree: %ld kB\0ABuffers: %ld kB\0ACached: %ld kB\0A\00"
@.str.3 = [8 x i8] c"%.2lf%%\00"
define i8* @get_memusage() {
%1 = alloca i32*
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca double
%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** %1
%8 = load i32*, i32** %1
%9 = call i32 @fscanf(i32* %8, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i64* %2, i64* %3, i64* %4, i64* %5)
%10 = load i32*, i32** %1
%11 = call i32 @fclose(i32* %10)
%12 = load i64, i64* %2
%13 = load i64, i64* %3
%14 = sub nsw i64 %12, %13
%15 = sitofp i64 %14 to double
%16 = load i64, i64* %2
%17 = sitofp i64 %16 to double
%18 = fdiv double %15, %17
store double %18, double* %6
%19 = load double, double* %6
%20 = fmul double 1.000000e+02, %19
%21 = fptosi double %20 to i32
%22 = call i32 @sprintf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @get_memusage.memstr, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %21)
ret i8* getelementptr inbounds ([42 x i8], [42 x i8]* @get_memusage.memstr, i64 0, i64 0)
}

LLVM IR (-O2):
@get_memusage.memstr = internal global [42 x i8] zeroinitializer
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [65 x i8] c"MemTotal: %ld kB\0AMemFree: %ld kB\0ABuffers: %ld kB\0ACached: %ld kB\0A\00"
@.str.3 = [8 x i8] c"%.2lf%%\00"
define i8* @get_memusage() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %1 to i8*
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = 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))
%10 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i64* %1, i64* %2, i64* %3, i64* %4)
%11 = call i32 @fclose(i32* %9)
%12 = load i64, i64* %1
%13 = load i64, i64* %2
%14 = sub nsw i64 %12, %13
%15 = sitofp i64 %14 to double
%16 = sitofp i64 %12 to double
%17 = fdiv double %15, %16
%18 = fmul double %17, 1.000000e+02
%19 = fptosi double %18 to i32
%20 = call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @get_memusage.memstr, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %19)
ret i8* getelementptr inbounds ([42 x i8], [42 x i8]* @get_memusage.memstr, i64 0, i64 0)
}