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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char const *, int *);
long local_read_meminfo(const char *tag) {
  unsigned long val = 0;
  char buff[256];
  int memsize;
  FILE *meminfo = fopen("/proc/meminfo", "r");
  if (meminfo == NULL)
    exit(-1);
  while (fgets(buff, sizeof(buff), meminfo)) {
    if (sscanf(buff, tag, &memsize) == 1)
      val = memsize;
  }
  if (fclose(meminfo) != 0)
    exit(-1);
  return val;
}


x86 assembly (clang, -O0):
<local_read_meminfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe92(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    1199 <local_read_meminfo+0x49>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    119e <local_read_meminfo+0x4e>
lea    -0x110(%rbp),%rdi
mov    -0x120(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11f0 <local_read_meminfo+0xa0>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    -0x114(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11eb <local_read_meminfo+0x9b>
movslq -0x114(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    119e <local_read_meminfo+0x4e>
mov    -0x120(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1210 <local_read_meminfo+0xc0>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local_read_meminfo>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rdi,%r14
lea    0xe96(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1200 <local_read_meminfo+0xb0>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11df <local_read_meminfo+0x8f>
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
movslq 0xc(%rsp),%rax
cmove  %rax,%rbx
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11b0 <local_read_meminfo+0x60>
mov    %r15,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jne    1200 <local_read_meminfo+0xb0>
mov    %rbx,%rax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    $0xffffffff,%edi
call   1070 <exit@plt>

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

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