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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *);
int sscanf(char *, char *, unsigned long long *);
int /*<<< orphan*/ stderr;
int get_lowmem(unsigned long long *mem) {
  FILE *fd;
  char str[128];
  if ((fd = fopen("/proc/meminfo", "r")) == NULL) {
    logger(-1, errno,
           "Cannot open "
           "/proc/meminfo");
    return -1;
  }
  *mem = 0;
  while (fgets(str, sizeof(str), fd)) {
    if (sscanf(str, "LowTotal: %llu", mem) == 1)
      break;
    sscanf(str, "MemTotal: %llu", mem);
  }
  fclose(fd);
  if (*mem == 0) {
    fprintf(stderr, "Neither LowTotal nor MemTotal found in the "
                    "/proc/meminfo"
                    "\n");
    return -1;
  }
  *mem *= 1024;
  return 0;
}


x86 assembly (clang, -O0):
<get_lowmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe98(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b6 <get_lowmem+0x56>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
lea    0xe5d(%rip),%rdx
call   1060 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <get_lowmem+0x10e>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1221 <get_lowmem+0xc1>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe2a(%rip),%rsi
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1205 <get_lowmem+0xa5>
jmp    1221 <get_lowmem+0xc1>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe14(%rip),%rsi
call   1070 <sscanf@plt>
jmp    11c1 <get_lowmem+0x61>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1259 <get_lowmem+0xf9>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <get_lowmem+0x10e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
shl    $0xa,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_lowmem>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rdi,%r12
lea    0xe94(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1213 <get_lowmem+0xb3>
mov    %rax,%r14
movq   $0x0,(%r12)
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11f6 <get_lowmem+0x96>
lea    0xe66(%rip),%r15
mov    %rsp,%rbx
lea    0xe6b(%rip),%r13
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     11f6 <get_lowmem+0x96>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1070 <sscanf@plt>
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11c0 <get_lowmem+0x60>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    (%r12),%rax
test   %rax,%rax
je     1234 <get_lowmem+0xd4>
shl    $0xa,%rax
mov    %rax,(%r12)
xor    %ebx,%ebx
jmp    124e <get_lowmem+0xee>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    0xddf(%rip),%rdx
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
call   1060 <logger@plt>
jmp    124e <get_lowmem+0xee>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Cannot open /proc/meminfo\00"
@.str.3 = [15 x i8] c"LowTotal: %llu\00"
@.str.4 = [15 x i8] c"MemTotal: %llu\00"
@stderr = global i32 0
@.str.5 = [58 x i8] c"Neither LowTotal nor MemTotal found in the /proc/meminfo\0A\00"
define i32 @get_lowmem(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca i32*
%5 = alloca [128 x i8]
store i64* %0, i64** %3
%6 = 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* %6, i32** %4
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = call i32 @logger(i32 -1, i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
11: ; preds = %1
%12 = load i64*, i64** %3
store i64 0, i64* %12
br label %13
13: ; preds = %24, %11
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i64 @fgets(i8* %14, i32 128, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %13
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%20 = load i64*, i64** %3
%21 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64* %20)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %18
br label %28
24: ; preds = %18
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%26 = load i64*, i64** %3
%27 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64* %26)
br label %13
28: ; preds = %23, %13
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load i64*, i64** %3
%32 = load i64, i64* %31
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
37: ; preds = %28
%38 = load i64*, i64** %3
%39 = load i64, i64* %38
%40 = mul i64 %39, 1024
store i64 %40, i64* %38
store i32 0, i32* %2
br label %41
41: ; preds = %37, %34, %8
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Cannot open /proc/meminfo\00"
@.str.3 = [15 x i8] c"LowTotal: %llu\00"
@.str.4 = [15 x i8] c"MemTotal: %llu\00"
@stderr = global i32 0
@.str.5 = [58 x i8] c"Neither LowTotal nor MemTotal found in the /proc/meminfo\0A\00"
define i32 @get_lowmem(i64* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%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 %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i32 @logger(i32 -1, i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %28
9: ; preds = %1
store i64 0, i64* %0
%10 = call i64 @fgets(i8* %3, i32 128, i32* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9, %15
%13 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64* %0)
%14 = icmp eq i32 %13, 1
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64* %0)
%17 = call i64 @fgets(i8* %3, i32 128, i32* %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %12
19: ; preds = %15, %12, %9
%20 = call i32 @fclose(i32* %4)
%21 = load i64, i64* %0
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
br label %28
26: ; preds = %19
%27 = shl i64 %21, 10
store i64 %27, i64* %0
br label %28
28: ; preds = %26, %23, %6
%29 = phi i32 [ -1, %6 ], [ -1, %23 ], [ 0, %26 ]
ret i32 %29
}