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

/* Variables and functions */
int fscanf(int /*<<< orphan*/ *, char *, long long *);
int /*<<< orphan*/ msg(int, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int64_t getsystemtotalmem(int explain) {
  long long res;
  FILE *p = popen("grep MemTotal /proc/meminfo", "r");
  if (p && fscanf(p, "MemTotal: %lld kB", &res) == 1) {
    if (explain)
      msg(-1, 0, "%lld KB total memory according to /proc/meminfo", res);
    res <<= 10;
  } else {
    res = ((long long)12) << 30;
    ;
    if (explain)
      msg(-1, 0, "assuming compiled in memory size of %d GB", 12);
  }
  if (p)
    pclose(p);
  return (int64_t)res;
}


x86 assembly (clang, -O0):
<getsystemtotalmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeae(%rip),%rdi
lea    0xec3(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11be <getsystemtotalmem+0x7e>
mov    -0x18(%rbp),%rdi
lea    0xea6(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    11be <getsystemtotalmem+0x7e>
cmpl   $0x0,-0x4(%rbp)
je     11ad <getsystemtotalmem+0x6d>
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0xffffffff,%edi
xor    %esi,%esi
lea    0xe88(%rip),%rdx
call   1060 <msg@plt>
mov    -0x10(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x10(%rbp)
jmp    11f3 <getsystemtotalmem+0xb3>
movabs $0x300000000,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11ee <getsystemtotalmem+0xae>
mov    $0xffffffff,%edi
xor    %esi,%esi
lea    0xe7c(%rip),%rdx
mov    $0xc,%ecx
call   1060 <msg@plt>
jmp    11f3 <getsystemtotalmem+0xb3>
cmpq   $0x0,-0x18(%rbp)
je     1207 <getsystemtotalmem+0xc7>
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsystemtotalmem>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xeb4(%rip),%rdi
lea    0xec9(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1198 <getsystemtotalmem+0x58>
lea    0xeb7(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    1198 <getsystemtotalmem+0x58>
test   %ebp,%ebp
je     1191 <getsystemtotalmem+0x51>
mov    (%rsp),%ecx
lea    0xeab(%rip),%rdx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <msg@plt>
shlq   $0xa,(%rsp)
jmp    11c7 <getsystemtotalmem+0x87>
movabs $0x300000000,%rax
mov    %rax,(%rsp)
test   %ebp,%ebp
je     11c2 <getsystemtotalmem+0x82>
lea    0xeaf(%rip),%rdx
mov    $0xffffffff,%edi
xor    %esi,%esi
mov    $0xc,%ecx
call   1060 <msg@plt>
test   %rbx,%rbx
je     11cf <getsystemtotalmem+0x8f>
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"grep MemTotal /proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"MemTotal: %lld kB\00"
@.str.3 = [48 x i8] c"%lld KB total memory according to /proc/meminfo\00"
@.str.4 = [42 x i8] c"assuming compiled in memory size of %d GB\00"
define i32 @getsystemtotalmem(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32*
store i32 %0, i32* %2
%5 = call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 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** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %8, label %22
8: ; preds = %1
%9 = load i32*, i32** %4
%10 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = load i32, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i64, i64* %3
%17 = trunc i64 %16 to i32
%18 = call i32 @msg(i32 -1, i32 0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %12
%20 = load i64, i64* %3
%21 = shl i64 %20, 10
store i64 %21, i64* %3
br label %28
22: ; preds = %8, %1
store i64 12884901888, i64* %3
%23 = load i32, i32* %2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @msg(i32 -1, i32 0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 12)
br label %27
27: ; preds = %25, %22
br label %28
28: ; preds = %27, %19
%29 = load i32*, i32** %4
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32*, i32** %4
%33 = call i32 @pclose(i32* %32)
br label %34
34: ; preds = %31, %28
%35 = load i64, i64* %3
%36 = trunc i64 %35 to i32
ret i32 %36
}

LLVM IR (-O2):
@.str = [28 x i8] c"grep MemTotal /proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"MemTotal: %lld kB\00"
@.str.3 = [48 x i8] c"%lld KB total memory according to /proc/meminfo\00"
@.str.4 = [42 x i8] c"assuming compiled in memory size of %d GB\00"
define i32 @getsystemtotalmem(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 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 %18, label %6
6: ; preds = %1
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %18
9: ; preds = %6
%10 = icmp eq i32 %0, 0
br i1 %10, label %15, label %11
11: ; preds = %9
%12 = load i64, i64* %2
%13 = trunc i64 %12 to i32
%14 = call i32 @msg(i32 -1, i32 0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %9, %11
%16 = load i64, i64* %2
%17 = shl i64 %16, 10
store i64 %17, i64* %2
br label %23
18: ; preds = %6, %1
store i64 12884901888, i64* %2
%19 = icmp eq i32 %0, 0
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = call i32 @msg(i32 -1, i32 0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 12)
br label %22
22: ; preds = %18, %20
br i1 %5, label %25, label %23
23: ; preds = %15, %22
%24 = call i32 @pclose(i32* %4)
br label %25
25: ; preds = %23, %22
%26 = load i64, i64* %2
%27 = trunc i64 %26 to i32
ret i32 %27
}