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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, char *, long *);
int /*<<< orphan*/ strcmp(char *, char *);
long memtotal() {
  char buf[1024];
  char name[256];
  long value, sum = 0;
  FILE *f = fopen("/proc/meminfo", "r");
  while (fgets(buf, sizeof(buf), f)) {
    sscanf(buf, "%s%ld", name, &value);
    if (!strcmp(name, "MemAvailable:") || !strcmp(name, "SwapFree:"))
      sum += value;
  }
  fclose(f);
  return sum * 1024;
}


x86 assembly (clang, -O0):
<memtotal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x520,%rsp
movq   $0x0,-0x510(%rbp)
lea    0xe93(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x518(%rbp)
lea    -0x400(%rbp),%rdi
mov    -0x518(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1215 <memtotal+0xc5>
lea    -0x400(%rbp),%rdi
lea    -0x500(%rbp),%rdx
lea    0xe59(%rip),%rsi
lea    -0x508(%rbp),%rcx
call   1060 <sscanf@plt>
lea    -0x500(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     11fb <memtotal+0xab>
lea    -0x500(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1210 <memtotal+0xc0>
mov    -0x508(%rbp),%rax
add    -0x510(%rbp),%rax
mov    %rax,-0x510(%rbp)
jmp    1180 <memtotal+0x30>
mov    -0x518(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x510(%rbp),%rax
shl    $0xa,%rax
add    $0x520,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memtotal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x518,%rsp
lea    0xea8(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0x110(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1218 <memtotal+0xd8>
lea    0x110(%rsp),%r12
lea    0x8(%rsp),%rbp
movabs $0x6c696176416d654d,%r15
61 69 6c
movabs $0x3a656c62616c69,%r13
jmp    11ca <memtotal+0x8a>
nopw   0x0(%rax,%rax,1)
add    0x8(%rsp),%rbx
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1218 <memtotal+0xd8>
mov    %r12,%rdi
lea    0xe3c(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rbp,%rcx
call   1050 <sscanf@plt>
mov    0x10(%rsp),%rax
xor    %r15,%rax
mov    0x16(%rsp),%rcx
xor    %r13,%rcx
or     %rax,%rcx
je     11b0 <memtotal+0x70>
mov    0x10(%rsp),%rax
movabs $0x6565724670617753,%rcx
72 65 65
xor    %rcx,%rax
movzwl 0x18(%rsp),%ecx
xor    $0x3a,%rcx
or     %rax,%rcx
jne    11b5 <memtotal+0x75>
jmp    11b0 <memtotal+0x70>
mov    %r14,%rdi
call   1030 <fclose@plt>
shl    $0xa,%rbx
mov    %rbx,%rax
add    $0x518,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"%s%ld\00"
@.str.3 = [14 x i8] c"MemAvailable:\00"
@.str.4 = [10 x i8] c"SwapFree:\00"
define i64 @memtotal() {
%1 = alloca [1024 x i8]
%2 = alloca [256 x i8]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
store i64 0, i64* %4
%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** %5
br label %7
7: ; preds = %27, %0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%9 = load i32*, i32** %5
%10 = call i64 @fgets(i8* %8, i32 1024, i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %7
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%15 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %14, i64* %3)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%17 = call i32 @strcmp(i8* %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%21 = call i32 @strcmp(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%22 = icmp ne i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19, %12
%24 = load i64, i64* %3
%25 = load i64, i64* %4
%26 = add nsw i64 %25, %24
store i64 %26, i64* %4
br label %27
27: ; preds = %23, %19
br label %7
28: ; preds = %7
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
%31 = load i64, i64* %4
%32 = mul nsw i64 %31, 1024
ret i64 %32
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"%s%ld\00"
@.str.3 = [14 x i8] c"MemAvailable:\00"
@.str.4 = [10 x i8] c"SwapFree:\00"
define i64 @memtotal() {
%1 = alloca [1024 x i8]
%2 = alloca [256 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%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 i64 @fgets(i8* %4, i32 1024, i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %0, %21
%11 = phi i64 [ %22, %21 ], [ 0, %0 ]
%12 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %5, i64* %3)
%13 = call i32 @bcmp(i8* dereferenceable(14) %5, i8* dereferenceable(14) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i64 14)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = call i32 @bcmp(i8* dereferenceable(10) %5, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 10)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15, %10
%19 = load i64, i64* %3
%20 = add nsw i64 %19, %11
br label %21
21: ; preds = %18, %15
%22 = phi i64 [ %11, %15 ], [ %20, %18 ]
%23 = call i64 @fgets(i8* %4, i32 1024, i32* %7)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %10
25: ; preds = %21, %0
%26 = phi i64 [ 0, %0 ], [ %22, %21 ]
%27 = call i32 @fclose(i32* %7)
%28 = shl nsw i64 %26, 10
ret i64 %28
}