#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 */
struct mallinfo {
  unsigned int fordblks;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
struct mallinfo mallinfo();
char *strchr(char *, char);
char *strstr(char *, char *);
unsigned int get_free_mem() {
  struct mallinfo mi;
  FILE *f;
  int n;
  char *start, *end;
  char buffer[0x1000];
  mi = mallinfo();
  f = fopen("/proc/meminfo", "r");
  if (!f)
    return 0;
  n = fread(buffer, 1, 0x1000, f);
  if (n < 1) {
    fclose(f);
    return 0;
  }
  fclose(f);
  buffer[0x1000 - 1] = '\0';
  start = strstr(buffer, "MemFree:");
  if (start == NULL)
    return 0;
  end = strchr(start, '\n');
  if (end == NULL)
    return 0;
  *end = '\0';
  return (unsigned int)atoi(start + 8) * 1024 + mi.fordblks;
}


x86 assembly (clang, -O0):
<get_free_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    $0x0,%al
call   1060 <mallinfo@plt>
mov    %eax,-0x1038(%rbp)
mov    -0x1038(%rbp),%eax
mov    %eax,-0x8(%rbp)
lea    0xe68(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bf <get_free_mem+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    127c <get_free_mem+0x10c>
lea    -0x1030(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11fb <get_free_mem+0x8b>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    127c <get_free_mem+0x10c>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movb   $0x0,-0x31(%rbp)
lea    -0x1030(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1236 <get_free_mem+0xc6>
movl   $0x0,-0x4(%rbp)
jmp    127c <get_free_mem+0x10c>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    125f <get_free_mem+0xef>
movl   $0x0,-0x4(%rbp)
jmp    127c <get_free_mem+0x10c>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
add    $0x8,%rdi
call   1080 <atoi@plt>
shl    $0xa,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_free_mem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <mallinfo@plt>
mov    %eax,%r14d
lea    0xe6f(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1212 <get_free_mem+0xa2>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %ebx,%ebx
jle    1212 <get_free_mem+0xa2>
movb   $0x0,0xfff(%rsp)
lea    0xe39(%rip),%rsi
mov    %rsp,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     1212 <get_free_mem+0xa2>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1212 <get_free_mem+0xa2>
movb   $0x0,(%rax)
add    $0x8,%rbp
mov    %rbp,%rdi
call   1080 <atoi@plt>
mov    %rax,%r15
shl    $0xa,%r15d
add    %r14d,%r15d
mov    %r15d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mallinfo = type { i32 }
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"MemFree:\00"
define i32 @get_free_mem() {
%1 = alloca i32
%2 = alloca %struct.mallinfo
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [4096 x i8]
%8 = alloca %struct.mallinfo
%9 = call i32 (...) @mallinfo()
%10 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %8, i32 0, i32 0
store i32 %9, i32* %10
%11 = bitcast %struct.mallinfo* %2 to i8*
%12 = bitcast %struct.mallinfo* %8 to i8*
%13 = 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* %13, i32** %3
%14 = load i32*, i32** %3
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %0
store i32 0, i32* %1
br label %51
17: ; preds = %0
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %3
%20 = call i32 @fread(i8* %18, i32 1, i32 4096, i32* %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32*, i32** %3
%25 = call i32 @fclose(i32* %24)
store i32 0, i32* %1
br label %51
26: ; preds = %17
%27 = load i32*, i32** %3
%28 = call i32 @fclose(i32* %27)
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 4095
store i8 0, i8* %29
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = call i8* @strstr(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %26
store i32 0, i32* %1
br label %51
35: ; preds = %26
%36 = load i8*, i8** %5
%37 = call i8* @strchr(i8* %36, i8 signext 10)
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 0, i32* %1
br label %51
41: ; preds = %35
%42 = load i8*, i8** %6
store i8 0, i8* %42
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 8
%45 = call i64 @atoi(i8* %44)
%46 = trunc i64 %45 to i32
%47 = mul i32 %46, 1024
%48 = getelementptr inbounds %struct.mallinfo, %struct.mallinfo* %2, i32 0, i32 0
%49 = load i32, i32* %48
%50 = add i32 %47, %49
store i32 %50, i32* %1
br label %51
51: ; preds = %41, %40, %34, %23, %16
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"MemFree:\00"
define i32 @get_free_mem() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @mallinfo()
%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 %23, label %6
6: ; preds = %0
%7 = call i32 @fread(i8* %2, i32 1, i32 4096, i32* %4)
%8 = icmp slt i32 %7, 1
%9 = tail call i32 @fclose(i32* %4)
br i1 %8, label %23, label %10
10: ; preds = %6
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 4095
store i8 0, i8* %11
%12 = call i8* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = call i8* @strchr(i8* %12, i8 signext 10)
%16 = icmp eq i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %14
store i8 0, i8* %15
%18 = getelementptr inbounds i8, i8* %12, i64 8
%19 = call i64 @atoi(i8* %18)
%20 = trunc i64 %19 to i32
%21 = shl i32 %20, 10
%22 = add i32 %21, %3
br label %23
23: ; preds = %6, %14, %10, %0, %17
%24 = phi i32 [ %22, %17 ], [ 0, %0 ], [ 0, %10 ], [ 0, %14 ], [ 0, %6 ]
ret i32 %24
}