#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 fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int mem_used() {
  FILE *file;
  int ret;
  int total, available;
  file = fopen("/proc/meminfo", "r");
  if (file == NULL)
    return -1;
  ret = fscanf(file, "MemTotal: %d kB\nMemFree: %*d kB\nMemAvailable: %d kB",
               &total, &available);
  fclose(file);
  if (ret != 2)
    return -1;
  return total - available;
}


x86 assembly (clang, -O0):
<mem_used>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
lea    0xec8(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1166 <mem_used+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <mem_used+0x79>
mov    -0x10(%rbp),%rdi
lea    0xe9f(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1040 <fscanf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x2,-0x14(%rbp)
je     11a0 <mem_used+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <mem_used+0x79>
mov    -0x18(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_used>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
lea    0xec8(%rip),%rsi
call   1050 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     118a <mem_used+0x5a>
mov    %rax,%rbx
lea    0xeb1(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x2,%r14d
jne    118a <mem_used+0x5a>
mov    0xc(%rsp),%ebp
sub    0x8(%rsp),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [52 x i8] c"MemTotal: %d kB\0AMemFree: %*d kB\0AMemAvailable: %d kB\00"
define i32 @mem_used() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%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** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %22
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %5)
store i32 %12, i32* %3
%13 = load i32*, i32** %2
%14 = call i32 @fclose(i32* %13)
%15 = load i32, i32* %3
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %18
17: ; preds = %10
store i32 -1, i32* %1
br label %22
18: ; preds = %10
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = sub nsw i32 %19, %20
store i32 %21, i32* %1
br label %22
22: ; preds = %18, %17, %9
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [52 x i8] c"MemTotal: %d kB\0AMemFree: %*d kB\0AMemAvailable: %d kB\00"
define i32 @mem_used() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%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 %15, label %7
7: ; preds = %0
%8 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32* %1, i32* %2)
%9 = call i32 @fclose(i32* %5)
%10 = icmp eq i32 %8, 2
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* %1
%13 = load i32, i32* %2
%14 = sub nsw i32 %12, %13
br label %15
15: ; preds = %7, %0, %11
%16 = phi i32 [ %14, %11 ], [ -1, %0 ], [ -1, %7 ]
ret i32 %16
}