#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 */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int sscanf(char *, char *, int *);
int meminfo(char *param) {
  int mem;
  char line[256];
  FILE *meminfo = fopen("/proc/meminfo", "r");
  printf("%s\n", param);
  while (fgets(line, sizeof(line), meminfo)) {
    if (sscanf(line, param, &mem) == 1) {
      fclose(meminfo);
      return mem;
    }
  }
  fclose(meminfo);
  return -1;
}


x86 assembly (clang, -O0):
<meminfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x128(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe8c(%rip),%rdi
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x128(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11e4 <meminfo+0x94>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11df <meminfo+0x8f>
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <meminfo+0xa7>
jmp    1189 <meminfo+0x39>
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<meminfo>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
lea    0xe98(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
lea    0xe92(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r12
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11c6 <meminfo+0x76>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1190 <meminfo+0x40>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    11d3 <meminfo+0x83>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
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 = [4 x i8] c"%s\0A\00"
define i32 @meminfo(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i32*
store i8* %0, i8** %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 i8*, i8** %3
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %24, %1
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%12 = load i32*, i32** %6
%13 = call i64 @fgets(i8* %11, i32 256, i32* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = call i32 @sscanf(i8* %16, i8* %17, i32* %4)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* %4
store i32 %23, i32* %2
br label %28
24: ; preds = %15
br label %10
25: ; preds = %10
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
store i32 -1, i32* %2
br label %28
28: ; preds = %25, %20
%29 = load i32, i32* %2
ret i32 %29
}

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