#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 BUFSIZ;
long long atoll(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ isdigit(char);
int /*<<< orphan*/ printf(char *);
scalar_t__ strstr(char *, char *);
long long get_mem_size_meminfo(void) {
  long long mem_size = 0;
  char temp_string[BUFSIZ];
  FILE *info;
  int position = 0;
  info = fopen("/proc/meminfo", "r");
  if (info == NULL) {
    printf("Error opening /proc/meminfo!\n");
    return 0;
  }
  while (fgets(temp_string, BUFSIZ, info)) {
    position = 0;
    while (temp_string[position]) {
      if (isdigit(temp_string[position]))
        break;
      position++;
    }
    if (strstr(temp_string, "MemTotal"))
      mem_size = atoll(temp_string + position);
  }
  fclose(info);
  return mem_size / 1024ULL;
}


x86 assembly (clang, -O0):
<get_mem_size_meminfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x0,-0x10(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe46(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f5 <get_mem_size_meminfo+0x85>
lea    0xe34(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    12b0 <get_mem_size_meminfo+0x140>
jmp    11fa <get_mem_size_meminfo+0x8a>
mov    -0x38(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1294 <get_mem_size_meminfo+0x124>
movl   $0x0,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1261 <get_mem_size_meminfo+0xf1>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1253 <get_mem_size_meminfo+0xe3>
jmp    1261 <get_mem_size_meminfo+0xf1>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1221 <get_mem_size_meminfo+0xb1>
mov    -0x38(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     128f <get_mem_size_meminfo+0x11f>
mov    -0x38(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
call   1070 <atoll@plt>
mov    %rax,-0x10(%rbp)
jmp    11fa <get_mem_size_meminfo+0x8a>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mem_size_meminfo>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e5b(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe5f(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     124d <get_mem_size_meminfo+0xdd>
mov    %rax,%r14
mov    (%r12),%esi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1060 <fgets@plt>
xor    %r15d,%r15d
jmp    11df <get_mem_size_meminfo+0x6f>
nopl   (%rax)
mov    (%r12),%esi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     123f <get_mem_size_meminfo+0xcf>
mov    (%rbx),%al
mov    $0x0,%r13d
test   %al,%al
je     121b <get_mem_size_meminfo+0xab>
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    121b <get_mem_size_meminfo+0xab>
movzbl 0x1(%rbx,%r13,1),%eax
add    $0x1,%r13
test   %al,%al
jne    1200 <get_mem_size_meminfo+0x90>
mov    %rbx,%rdi
lea    0xdeb(%rip),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d0 <get_mem_size_meminfo+0x60>
mov    %r13d,%edi
add    %rbx,%rdi
call   1070 <atoll@plt>
mov    %rax,%r15
jmp    11d0 <get_mem_size_meminfo+0x60>
mov    %r14,%rdi
call   1050 <fclose@plt>
shr    $0xa,%r15
jmp    125c <get_mem_size_meminfo+0xec>
lea    0xdc5(%rip),%rdi
call   1040 <puts@plt>
xor    %r15d,%r15d
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"Error opening /proc/meminfo!\0A\00"
@.str.3 = [9 x i8] c"MemTotal\00"
define i64 @get_mem_size_meminfo() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i64 0, i64* %2
%8 = load i32, i32* @BUFSIZ
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %3
%11 = alloca i8, i64 %9
store i64 %9, i64* %4
store i32 0, i32* %6
%12 = 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* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %1
store i32 1, i32* %7
br label %55
17: ; preds = %0
br label %18
18: ; preds = %49, %17
%19 = load i32, i32* @BUFSIZ
%20 = load i32*, i32** %5
%21 = call i64 @fgets(i8* %11, i32 %19, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %50
23: ; preds = %18
store i32 0, i32* %6
br label %24
24: ; preds = %38, %23
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %11, i64 %26
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %24
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %11, i64 %32
%34 = load i8, i8* %33
%35 = call i64 @isdigit(i8 signext %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %41
38: ; preds = %30
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
br label %24
41: ; preds = %37, %24
%42 = call i64 @strstr(i8* %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %11, i64 %46
%48 = call i64 @atoll(i8* %47)
store i64 %48, i64* %2
br label %49
49: ; preds = %44, %41
br label %18
50: ; preds = %18
%51 = load i32*, i32** %5
%52 = call i32 @fclose(i32* %51)
%53 = load i64, i64* %2
%54 = udiv i64 %53, 1024
store i64 %54, i64* %1
store i32 1, i32* %7
br label %55
55: ; preds = %50, %15
%56 = load i8*, i8** %3
%57 = load i64, i64* %1
ret i64 %57
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"MemTotal\00"
@str = [29 x i8] c"Error opening /proc/meminfo!\00"
define i64 @get_mem_size_meminfo() {
%1 = load i32, i32* @BUFSIZ
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = 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))
%5 = icmp eq i32* %4, null
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = load i32, i32* @BUFSIZ
%8 = call i64 @fgets(i8* %3, i32 %7, i32* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %43, label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %47
12: ; preds = %6, %38
%13 = phi i64 [ %39, %38 ], [ 0, %6 ]
%14 = load i8, i8* %3
%15 = icmp eq i8 %14, 0
br i1 %15, label %30, label %16
16: ; preds = %12, %22
%17 = phi i64 [ %23, %22 ], [ 0, %12 ]
%18 = phi i8 [ %26, %22 ], [ %14, %12 ]
%19 = phi i32 [ %24, %22 ], [ 0, %12 ]
%20 = call i64 @isdigit(i8 signext %18)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = add nuw i64 %17, 1
%24 = add nuw nsw i32 %19, 1
%25 = getelementptr inbounds i8, i8* %3, i64 %23
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %30, label %16
28: ; preds = %16
%29 = trunc i64 %17 to i32
br label %30
30: ; preds = %22, %28, %12
%31 = phi i32 [ 0, %12 ], [ %29, %28 ], [ %24, %22 ]
%32 = call i64 @strstr(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = zext i32 %31 to i64
%36 = getelementptr inbounds i8, i8* %3, i64 %35
%37 = call i64 @atoll(i8* %36)
br label %38
38: ; preds = %34, %30
%39 = phi i64 [ %37, %34 ], [ %13, %30 ]
%40 = load i32, i32* @BUFSIZ
%41 = call i64 @fgets(i8* %3, i32 %40, i32* %4)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %12
43: ; preds = %38, %6
%44 = phi i64 [ 0, %6 ], [ %39, %38 ]
%45 = call i32 @fclose(i32* %4)
%46 = lshr i64 %44, 10
br label %47
47: ; preds = %43, %10
%48 = phi i64 [ 0, %10 ], [ %46, %43 ]
ret i64 %48
}