#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 int64_t;
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 *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int64_t query_freemem(void) {
  FILE *fd = fopen("/proc/meminfo", "r");
  char buf[1024];
  int64_t result = -1;
  if (fd == NULL)
    return -1;
  if (fgets(buf, sizeof(buf), fd) == NULL)
    goto out;
  if (fscanf(fd, "MemFree: %ld", &result) <= 0)
    result = -1;
out:
  fclose(fd);
  return result;
}


x86 assembly (clang, -O0):
<query_freemem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xeae(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x414(%rbp)
ff ff ff
cmpq   $0x0,-0x10(%rbp)
jne    1183 <query_freemem+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <query_freemem+0xa9>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11a7 <query_freemem+0x67>
jmp    11d7 <query_freemem+0x97>
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
lea    -0x414(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x0,%rax
jg     11d2 <query_freemem+0x92>
movl   $0xffffffff,-0x414(%rbp)
ff ff ff
jmp    11d7 <query_freemem+0x97>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<query_freemem>:
push   %rbx
sub    $0x410,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1060 <fopen@plt>
movl   $0xffffffff,0xc(%rsp)
ff
test   %rax,%rax
je     11b1 <query_freemem+0x71>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11a3 <query_freemem+0x63>
lea    0xe87(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <fscanf@plt>
test   %rax,%rax
jg     11a3 <query_freemem+0x63>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    11b6 <query_freemem+0x76>
mov    $0xffffffff,%eax
add    $0x410,%rsp
pop    %rbx
ret

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

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