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

/* Variables and functions */
long long atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ remove_all_chars(char *, char);
char *strstr(char *, char *);
long long int memory_size_kb(void) {
  char line[512], buffer[32];
  long long int column;
  FILE *meminfo;
  if (!(meminfo = fopen("/proc/meminfo", "r"))) {
    perror("/proc/meminfo: fopen");
    return -1;
  }
  while (fgets(line, sizeof(line), meminfo)) {
    if (strstr(line, "MemTotal")) {
      char *colStr;
      colStr = strstr(line, ":");
      remove_all_chars(colStr, ':');
      remove_all_chars(colStr, 'k');
      remove_all_chars(colStr, 'B');
      remove_all_chars(colStr, ' ');
      column = atoi(colStr);
      column = 1000 * column;
      fclose(meminfo);
      return column;
    }
  }
  fclose(meminfo);
  return -1;
}


x86 assembly (clang, -O0):
<memory_size_kb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x240(%rbp)
cmp    $0x0,%rax
jne    11b8 <memory_size_kb+0x48>
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12b4 <memory_size_kb+0x144>
jmp    11bd <memory_size_kb+0x4d>
lea    -0x210(%rbp),%rdi
mov    -0x240(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12a0 <memory_size_kb+0x130>
lea    -0x210(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     129b <memory_size_kb+0x12b>
lea    -0x210(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x248(%rbp)
mov    -0x248(%rbp),%rdi
mov    $0x3a,%esi
call   1080 <remove_all_chars@plt>
mov    -0x248(%rbp),%rdi
mov    $0x6b,%esi
call   1080 <remove_all_chars@plt>
mov    -0x248(%rbp),%rdi
mov    $0x42,%esi
call   1080 <remove_all_chars@plt>
mov    -0x248(%rbp),%rdi
mov    $0x20,%esi
call   1080 <remove_all_chars@plt>
mov    -0x248(%rbp),%rdi
call   1070 <atoi@plt>
mov    %rax,-0x238(%rbp)
imul   $0x3e8,-0x238(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    -0x240(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x238(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b4 <memory_size_kb+0x144>
jmp    11bd <memory_size_kb+0x4d>
mov    -0x240(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memory_size_kb>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
lea    0xe6d(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1261 <memory_size_kb+0xe1>
mov    %rax,%r14
mov    %rsp,%rbx
lea    0xe70(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1242 <memory_size_kb+0xc2>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11c0 <memory_size_kb+0x40>
mov    %rsp,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x3a,%esi
call   1090 <remove_all_chars@plt>
mov    %rbx,%rdi
mov    $0x6b,%esi
call   1090 <remove_all_chars@plt>
mov    %rbx,%rdi
mov    $0x42,%esi
call   1090 <remove_all_chars@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1090 <remove_all_chars@plt>
mov    %rbx,%rdi
call   1080 <atoi@plt>
imul   $0x3e8,%rax,%rbx
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1251 <memory_size_kb+0xd1>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xda8(%rip),%rdi
call   1070 <perror@plt>
jmp    124a <memory_size_kb+0xca>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"/proc/meminfo: fopen\00"
@.str.3 = [9 x i8] c"MemTotal\00"
@.str.4 = [2 x i8] c":\00"
define i64 @memory_size_kb() {
%1 = alloca i64
%2 = alloca [512 x i8]
%3 = alloca [32 x i8]
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
%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** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i64 -1, i64* %1
br label %43
11: ; preds = %0
br label %12
12: ; preds = %39, %11
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%14 = load i32*, i32** %5
%15 = call i64 @fgets(i8* %13, i32 512, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %12
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %39
21: ; preds = %17
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%23 = call i8* @strstr(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = call i32 @remove_all_chars(i8* %24, i8 signext 58)
%26 = load i8*, i8** %6
%27 = call i32 @remove_all_chars(i8* %26, i8 signext 107)
%28 = load i8*, i8** %6
%29 = call i32 @remove_all_chars(i8* %28, i8 signext 66)
%30 = load i8*, i8** %6
%31 = call i32 @remove_all_chars(i8* %30, i8 signext 32)
%32 = load i8*, i8** %6
%33 = call i64 @atoi(i8* %32)
store i64 %33, i64* %4
%34 = load i64, i64* %4
%35 = mul nsw i64 1000, %34
store i64 %35, i64* %4
%36 = load i32*, i32** %5
%37 = call i32 @fclose(i32* %36)
%38 = load i64, i64* %4
store i64 %38, i64* %1
br label %43
39: ; preds = %17
br label %12
40: ; preds = %12
%41 = load i32*, i32** %5
%42 = call i32 @fclose(i32* %41)
store i64 -1, i64* %1
br label %43
43: ; preds = %40, %21, %9
%44 = load i64, i64* %1
ret i64 %44
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"/proc/meminfo: fopen\00"
@.str.3 = [9 x i8] c"MemTotal\00"
define i64 @memory_size_kb() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %24
7: ; preds = %0, %10
%8 = call i64 @fgets(i8* %2, i32 512, i32* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %7
%11 = call i8* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %7, label %13
13: ; preds = %10
%14 = call i8* @strchr(i8* %2, i32 58)
%15 = call i32 @remove_all_chars(i8* %14, i8 signext 58)
%16 = call i32 @remove_all_chars(i8* %14, i8 signext 107)
%17 = call i32 @remove_all_chars(i8* %14, i8 signext 66)
%18 = call i32 @remove_all_chars(i8* %14, i8 signext 32)
%19 = call i64 @atoi(i8* %14)
%20 = mul nsw i64 %19, 1000
%21 = call i32 @fclose(i32* %3)
br label %24
22: ; preds = %7
%23 = call i32 @fclose(i32* %3)
br label %24
24: ; preds = %22, %13, %5
%25 = phi i64 [ %20, %13 ], [ -1, %22 ], [ -1, %5 ]
ret i64 %25
}