#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*/ FILE;

/* Variables and functions */
size_t atoll(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
size_t getline(char **, size_t *, int /*<<< orphan*/ *);
int sscanf(char *, char *, char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
void procfs_getmem(size_t *memtotal, size_t *memfree, size_t *memavailable) {
  FILE *meminfo;
  int n;
  size_t nread, len;
  char *line = NULL;
  char head[64], val[64], unit[64];
  *memtotal = -1;
  *memfree = -1;
  *memavailable = -1;
  meminfo = fopen("/proc/meminfo", "r");
  if (meminfo) {
    while ((nread = getline(&line, &len, meminfo)) != -1) {
      n = sscanf(line, "%60s %60s %60s", head, val, unit);
      if (n == 3) {
        if (!strcmp(head, "MemTotal:"))
          *memtotal = atoll(val);
        else if (!strcmp(head, "MemFree:"))
          *memfree = atoll(val);
        else if (!strcmp(head, "MemAvailable:"))
          *memavailable = atoll(val);
      }
    }
    free(line);
  }
  fclose(meminfo);
}


x86 assembly (clang, -O0):
<procfs_getmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
mov    -0x10(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
mov    -0x18(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
lea    0xe49(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12d9 <procfs_getmem+0x169>
jmp    11d7 <procfs_getmem+0x67>
mov    -0x20(%rbp),%rdx
lea    -0x40(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1090 <getline@plt>
mov    %rax,-0x30(%rbp)
cmp    $0xffffffffffffffff,%rax
je     12d0 <procfs_getmem+0x160>
mov    -0x40(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    -0xc0(%rbp),%rcx
lea    -0x100(%rbp),%r8
lea    0xdfd(%rip),%rsi
call   1070 <sscanf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jne    12cb <procfs_getmem+0x15b>
lea    -0x80(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1259 <procfs_getmem+0xe9>
lea    -0xc0(%rbp),%rdi
call   1050 <atoll@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    12c6 <procfs_getmem+0x156>
lea    -0x80(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    128d <procfs_getmem+0x11d>
lea    -0xc0(%rbp),%rdi
call   1050 <atoll@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    12c1 <procfs_getmem+0x151>
lea    -0x80(%rbp),%rdi
lea    0xd9a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12bc <procfs_getmem+0x14c>
lea    -0xc0(%rbp),%rdi
call   1050 <atoll@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    12c1 <procfs_getmem+0x151>
jmp    12c6 <procfs_getmem+0x156>
jmp    12cb <procfs_getmem+0x15b>
jmp    11d7 <procfs_getmem+0x67>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procfs_getmem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf8,%rsp
movq   $0x0,0x8(%rsp)
mov    %rdi,0x20(%rsp)
movq   $0xffffffffffffffff,(%rdi)
mov    %rsi,0x18(%rsp)
movq   $0xffffffffffffffff,(%rsi)
mov    %rdx,0x10(%rsp)
movq   $0xffffffffffffffff,(%rdx)
lea    0xe5b(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12d0 <procfs_getmem+0x170>
lea    0x8(%rsp),%rdi
lea    0x28(%rsp),%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    $0xffffffffffffffff,%rax
je     12c6 <procfs_getmem+0x166>
lea    0xb0(%rsp),%r14
lea    0x70(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0x28(%rsp),%r13
jmp    1224 <procfs_getmem+0xc4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1050 <atoll@plt>
mov    %rax,0x0(%rbp)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    $0xffffffffffffffff,%rax
je     12c6 <procfs_getmem+0x166>
mov    0x8(%rsp),%rdi
lea    0xde0(%rip),%rsi
lea    0x30(%rsp),%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1060 <sscanf@plt>
cmp    $0x3,%eax
jne    120c <procfs_getmem+0xac>
mov    0x30(%rsp),%rax
movabs $0x6c61746f546d654d,%rcx
74 61 6c
xor    %rcx,%rax
movzwl 0x38(%rsp),%ecx
xor    $0x3a,%rcx
or     %rax,%rcx
mov    0x20(%rsp),%rbp
je     1200 <procfs_getmem+0xa0>
mov    0x30(%rsp),%rax
movabs $0x3a656572466d654d,%rcx
65 65 3a
xor    %rcx,%rax
movzbl 0x38(%rsp),%ecx
or     %rax,%rcx
mov    0x18(%rsp),%rbp
je     1200 <procfs_getmem+0xa0>
mov    0x30(%rsp),%rax
movabs $0x6c696176416d654d,%rcx
61 69 6c
xor    %rcx,%rax
mov    0x36(%rsp),%rcx
movabs $0x3a656c62616c69,%rdx
xor    %rdx,%rcx
or     %rax,%rcx
mov    0x10(%rsp),%rbp
jne    120c <procfs_getmem+0xac>
jmp    1200 <procfs_getmem+0xa0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0xf8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"%60s %60s %60s\00"
@.str.3 = [10 x i8] c"MemTotal:\00"
@.str.4 = [9 x i8] c"MemFree:\00"
@.str.5 = [14 x i8] c"MemAvailable:\00"
define void @procfs_getmem(i64* %0, i64* %1, i64* %2) {
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca [64 x i8]
%13 = alloca [64 x i8]
%14 = alloca [64 x i8]
store i64* %0, i64** %4
store i64* %1, i64** %5
store i64* %2, i64** %6
store i8* null, i8** %11
%15 = load i64*, i64** %4
store i64 -1, i64* %15
%16 = load i64*, i64** %5
store i64 -1, i64* %16
%17 = load i64*, i64** %6
store i64 -1, i64* %17
%18 = 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* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %65
21: ; preds = %3
br label %22
22: ; preds = %61, %21
%23 = load i32*, i32** %7
%24 = call i64 @getline(i8** %11, i64* %10, i32* %23)
store i64 %24, i64* %9
%25 = icmp ne i64 %24, -1
br i1 %25, label %26, label %62
26: ; preds = %22
%27 = load i8*, i8** %11
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%31 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %28, i8* %29, i8* %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, 3
br i1 %33, label %34, label %61
34: ; preds = %26
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%40 = call i64 @atoll(i8* %39)
%41 = load i64*, i64** %4
store i64 %40, i64* %41
br label %60
42: ; preds = %34
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%44 = call i32 @strcmp(i8* %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp ne i32 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %42
%47 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%48 = call i64 @atoll(i8* %47)
%49 = load i64*, i64** %5
store i64 %48, i64* %49
br label %59
50: ; preds = %42
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%52 = call i32 @strcmp(i8* %51, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%53 = icmp ne i32 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%56 = call i64 @atoll(i8* %55)
%57 = load i64*, i64** %6
store i64 %56, i64* %57
br label %58
58: ; preds = %54, %50
br label %59
59: ; preds = %58, %46
br label %60
60: ; preds = %59, %38
br label %61
61: ; preds = %60, %26
br label %22
62: ; preds = %22
%63 = load i8*, i8** %11
%64 = call i32 @free(i8* %63)
br label %65
65: ; preds = %62, %3
%66 = load i32*, i32** %7
%67 = call i32 @fclose(i32* %66)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"%60s %60s %60s\00"
@.str.3 = [10 x i8] c"MemTotal:\00"
@.str.4 = [9 x i8] c"MemFree:\00"
@.str.5 = [14 x i8] c"MemAvailable:\00"
define void @procfs_getmem(i64* nocapture writeonly %0, i64* nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca [64 x i8]
%7 = alloca [64 x i8]
%8 = alloca [64 x i8]
%9 = bitcast i64* %4 to i8*
%10 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
store i64 -1, i64* %0
store i64 -1, i64* %1
store i64 -1, i64* %2
%14 = 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))
%15 = icmp eq i32* %14, null
br i1 %15, label %41, label %16
16: ; preds = %3
%17 = call i64 @getline(i8** %5, i64* %4, i32* %14)
%18 = icmp eq i64 %17, -1
br i1 %18, label %38, label %19
19: ; preds = %16, %35
%20 = load i8*, i8** %5
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %11, i8* %12, i8* %13)
%22 = icmp eq i32 %21, 3
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = call i32 @bcmp(i8* dereferenceable(10) %11, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 10)
%25 = icmp eq i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = call i32 @bcmp(i8* dereferenceable(9) %11, i8* dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i64 9)
%28 = icmp eq i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @bcmp(i8* dereferenceable(14) %11, i8* dereferenceable(14) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i64 14)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29, %26, %23
%33 = phi i64* [ %0, %23 ], [ %1, %26 ], [ %2, %29 ]
%34 = call i64 @atoll(i8* %12)
store i64 %34, i64* %33
br label %35
35: ; preds = %32, %29, %19
%36 = call i64 @getline(i8** %5, i64* %4, i32* %14)
%37 = icmp eq i64 %36, -1
br i1 %37, label %38, label %19
38: ; preds = %35, %16
%39 = load i8*, i8** %5
%40 = call i32 @free(i8* %39)
br label %41
41: ; preds = %38, %3
%42 = call i32 @fclose(i32* %14)
ret void
}