#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 *);
int getmem() {
  FILE *fd;
  long total, free, available;
  int used;
  fd = fopen("/proc/meminfo", "r");
  fscanf(fd, "MemTotal: %ld kB\n", &total);
  fscanf(fd, "MemFree: %ld kB\n", &free);
  fscanf(fd, "MemAvailable: %ld kB\n", &available);
  fclose(fd);
  used = 100 * (total - available) / total;
  return used;
}


x86 assembly (clang, -O0):
<getmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rdi
lea    0xec8(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb6(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x8(%rbp),%rdi
lea    0xeb4(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x8(%rbp),%rdi
lea    0xeb1(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
imul   $0x64,%rax,%rax
cqto
idivq  -0x10(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getmem>:
push   %rbx
sub    $0x20,%rsp
lea    0xec4(%rip),%rdi
lea    0xecb(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xebe(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
lea    0xebc(%rip),%rsi
lea    0x18(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <fscanf@plt>
lea    0xeb9(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rcx
mov    %rcx,%rax
sub    0x8(%rsp),%rax
imul   $0x64,%rax,%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     11b3 <getmem+0x83>
cqto
idiv   %rcx
jmp    11b7 <getmem+0x87>
xor    %edx,%edx
div    %ecx
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"MemTotal: %ld kB\0A\00"
@.str.3 = [17 x i8] c"MemFree: %ld kB\0A\00"
@.str.4 = [22 x i8] c"MemAvailable: %ld kB\0A\00"
define i32 @getmem() {
%1 = alloca i32*
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = 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* %6, i32** %1
%7 = load i32*, i32** %1
%8 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%9 = load i32*, i32** %1
%10 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i64* %3)
%11 = load i32*, i32** %1
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64* %4)
%13 = load i32*, i32** %1
%14 = call i32 @fclose(i32* %13)
%15 = load i64, i64* %2
%16 = load i64, i64* %4
%17 = sub nsw i64 %15, %16
%18 = mul nsw i64 100, %17
%19 = load i64, i64* %2
%20 = sdiv i64 %18, %19
%21 = trunc i64 %20 to i32
store i32 %21, i32* %5
%22 = load i32, i32* %5
ret i32 %22
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"MemTotal: %ld kB\0A\00"
@.str.3 = [17 x i8] c"MemFree: %ld kB\0A\00"
@.str.4 = [22 x i8] c"MemAvailable: %ld kB\0A\00"
define i32 @getmem() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %1 to i8*
%5 = bitcast i64* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = 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))
%8 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%9 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i64* %2)
%10 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64* %3)
%11 = call i32 @fclose(i32* %7)
%12 = load i64, i64* %1
%13 = load i64, i64* %3
%14 = sub nsw i64 %12, %13
%15 = mul nsw i64 %14, 100
%16 = sdiv i64 %15, %12
%17 = trunc i64 %16 to i32
ret i32 %17
}