#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;
  char buff[256];
  FILE *meminfo = fopen("/proc/meminfo", "r");
  if (meminfo == NULL) {
    exit(-1);
  }
  while (fgets(buff, sizeof(buff), meminfo)) {
    int memsize;
    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)
lea    0xe9a(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    1191 <local_read_meminfo+0x41>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    1196 <local_read_meminfo+0x46>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11e8 <local_read_meminfo+0x98>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    -0x11c(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11e3 <local_read_meminfo+0x93>
movslq -0x11c(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1196 <local_read_meminfo+0x46>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1208 <local_read_meminfo+0xb8>
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     11f2 <local_read_meminfo+0xa2>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11d1 <local_read_meminfo+0x81>
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
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    11a0 <local_read_meminfo+0x50>
jmp    11d1 <local_read_meminfo+0x81>
mov    %r15,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jne    11f2 <local_read_meminfo+0xa2>
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
%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 = load i32*, i32** %5
%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** %5
%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* %6)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %6
%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** %5
%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 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = call i64 @fgets(i8* %4, i32 256, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = bitcast i32* %3 to i8*
br label %14
12: ; preds = %1
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %10, %14
%15 = phi i64 [ undef, %10 ], [ %20, %14 ]
%16 = call i32 @sscanf(i8* %4, i8* %0, i32* %3)
%17 = icmp eq i32 %16, 1
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = select i1 %17, i64 %19, i64 %15
%21 = call i64 @fgets(i8* %4, i32 256, i32* %5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %14
23: ; preds = %14, %7
%24 = phi i64 [ undef, %7 ], [ %20, %14 ]
%25 = call i64 @fclose(i32* %5)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %23
ret i64 %24
}