#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*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, long *);
int mem_size_mb(void) {
  FILE *f;
  char buf[4096];
  long int memsize = -1;
  f = fopen("/proc/meminfo", "r");
  if (f == NULL)
    return -1;
  while (fgets(buf, sizeof(buf), f) != NULL) {
    long int value;
    if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
      memsize = value / 1024;
      break;
    }
  }
  fclose(f);
  return memsize;
}


x86 assembly (clang, -O0):
<mem_size_mb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movq   $0xffffffffffffffff,-0x1018(%rbp)
ff ff ff ff
lea    0xea3(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <mem_size_mb+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <mem_size_mb+0xc0>
jmp    1189 <mem_size_mb+0x49>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11ed <mem_size_mb+0xad>
lea    -0x1010(%rbp),%rdi
lea    0xe5a(%rip),%rsi
lea    -0x1020(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11e8 <mem_size_mb+0xa8>
mov    -0x1020(%rbp),%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,-0x1018(%rbp)
jmp    11ed <mem_size_mb+0xad>
jmp    1189 <mem_size_mb+0x49>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1018(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_size_mb>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1010,%rsp
lea    0xea9(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ca <mem_size_mb+0x8a>
mov    %rax,%r12
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11e9 <mem_size_mb+0xa9>
lea    0xe81(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     11d2 <mem_size_mb+0x92>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11a0 <mem_size_mb+0x60>
jmp    11e9 <mem_size_mb+0xa9>
mov    $0xffffffff,%r14d
jmp    11f1 <mem_size_mb+0xb1>
mov    0x8(%rsp),%rax
lea    0x3ff(%rax),%r14
test   %rax,%rax
cmovns %rax,%r14
shr    $0xa,%r14
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r14d,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"MemTotal: %ld kB\00"
define i32 @mem_size_mb() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [4096 x i8]
%4 = alloca i64
%5 = alloca i64
store i64 -1, 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** %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 %29
10: ; preds = %0
br label %11
11: ; preds = %23, %10
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32* @fgets(i8* %12, i32 4096, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64* %5)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i64, i64* %5
%22 = sdiv i64 %21, 1024
store i64 %22, i64* %4
br label %24
23: ; preds = %16
br label %11
24: ; preds = %20, %11
%25 = load i32*, i32** %2
%26 = call i32 @fclose(i32* %25)
%27 = load i64, i64* %4
%28 = trunc i64 %27 to i32
store i32 %28, i32* %1
br label %29
29: ; preds = %24, %9
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"MemTotal: %ld kB\00"
define i32 @mem_size_mb() {
%1 = alloca [4096 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%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 %24, label %6
6: ; preds = %0
%7 = call i32* @fgets(i8* %3, i32 4096, i32* %4)
%8 = icmp eq i32* %7, null
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = bitcast i64* %2 to i8*
br label %11
11: ; preds = %9, %18
%12 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i64, i64* %2
%16 = sdiv i64 %15, 1024
%17 = trunc i64 %16 to i32
br label %21
18: ; preds = %11
%19 = call i32* @fgets(i8* %3, i32 4096, i32* %4)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %11
21: ; preds = %18, %6, %14
%22 = phi i32 [ %17, %14 ], [ -1, %6 ], [ -1, %18 ]
%23 = call i32 @fclose(i32* %4)
br label %24
24: ; preds = %0, %21
%25 = phi i32 [ %22, %21 ], [ -1, %0 ]
ret i32 %25
}