#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned long atoll(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize) {
  char buffer[256] = {0};
  char *cmd = "cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'";
  FILE *cmdfile = popen(cmd, "r");
  if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
    perror("Failed to read meminfo\n");
    return -1;
  }
  pclose(cmdfile);
  *memfree = atoll(buffer);
  cmd = "cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'";
  cmdfile = popen(cmd, "r");
  if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
    perror("Failed to read meminfo\n");
    return -1;
  }
  pclose(cmdfile);
  *hugepagesize = atoll(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<read_memory_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    0xe73(%rip),%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x130(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    11e7 <read_memory_info+0x87>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <read_memory_info+0x133>
mov    -0x130(%rbp),%rdi
call   1030 <pclose@plt>
lea    -0x120(%rbp),%rdi
call   1060 <atoll@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
lea    0xe41(%rip),%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x130(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    126a <read_memory_info+0x10a>
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <read_memory_info+0x133>
mov    -0x130(%rbp),%rdi
call   1030 <pclose@plt>
lea    -0x120(%rbp),%rdi
call   1060 <atoll@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_memory_info>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe2b(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     125c <read_memory_info+0x10c>
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    %rsp,%r12
mov    %r12,%rdi
call   1050 <atoll@plt>
mov    %rax,(%r15)
lea    0xe3c(%rip),%rdi
lea    0xe1b(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     125c <read_memory_info+0x10c>
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    %rsp,%rdi
call   1050 <atoll@plt>
mov    %rax,(%r14)
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdd6(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    124d <read_memory_info+0xfd>

LLVM IR (-O0):
@.str = [55 x i8] c"cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Failed to read meminfo\0A\00"
@.str.3 = [60 x i8] c"cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'\00"
define i32 @read_memory_info(i64* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i32*
store i64* %0, i64** %4
store i64* %1, i64** %5
%9 = bitcast [256 x i8]* %6 to i8*
store i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8** %7
%10 = load i8*, i8** %7
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %8
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = load i32*, i32** %8
%14 = call i32 @fgets(i8* %12, i32 256, i32* %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
18: ; preds = %2
%19 = load i32*, i32** %8
%20 = call i32 @pclose(i32* %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = call i64 @atoll(i8* %21)
%23 = load i64*, i64** %4
store i64 %22, i64* %23
store i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i8** %7
%24 = load i8*, i8** %7
%25 = call i32* @popen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %8
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = load i32*, i32** %8
%28 = call i32 @fgets(i8* %26, i32 256, i32* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
32: ; preds = %18
%33 = load i32*, i32** %8
%34 = call i32 @pclose(i32* %33)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%36 = call i64 @atoll(i8* %35)
%37 = load i64*, i64** %5
store i64 %36, i64* %37
store i32 0, i32* %3
br label %38
38: ; preds = %32, %30, %16
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [55 x i8] c"cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Failed to read meminfo\0A\00"
@.str.3 = [60 x i8] c"cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'\00"
define i32 @read_memory_info(i64* nocapture writeonly %0, i64* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @fgets(i8* %4, i32 256, i32* %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %21
10: ; preds = %2
%11 = call i32 @pclose(i32* %5)
%12 = call i64 @atoll(i8* %4)
store i64 %12, i64* %0
%13 = call i32* @popen(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @fgets(i8* %4, i32 256, i32* %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %21
18: ; preds = %10
%19 = call i32 @pclose(i32* %13)
%20 = call i64 @atoll(i8* %4)
store i64 %20, i64* %1
br label %21
21: ; preds = %18, %16, %8
%22 = phi i32 [ 0, %18 ], [ -1, %16 ], [ -1, %8 ]
ret i32 %22
}