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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, char *, long *);
void dump_mem_stats(void) {
  FILE *f = fopen("/proc/meminfo", "r");
  if (!f)
    return;
  printf("  \"mem\":\n  {\n");
  bool terminate_prev_line = false;
  while (!feof(f)) {
    char line[256];
    char key[32];
    long value = 0;
    fgets(line, sizeof(line), f);
    if (sscanf(line, "%s %lu %*s", key, &value) < 2)
      continue;
    if (terminate_prev_line)
      printf(",\n");
    terminate_prev_line = true;
    printf("    \"%s\": %lu", key, value);
  }
  fclose(f);
  printf("\n  }");
}


x86 assembly (clang, -O0):
<dump_mem_stats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
lea    0xe8e(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1192 <dump_mem_stats+0x32>
jmp    126b <dump_mem_stats+0x10b>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c5 <dump_mem_stats+0x65>
jmp    1254 <dump_mem_stats+0xf4>
movq   $0x0,-0x138(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
lea    -0x110(%rbp),%rdi
lea    -0x130(%rbp),%rdx
lea    0xe24(%rip),%rsi
lea    -0x138(%rbp),%rcx
call   1070 <sscanf@plt>
cmp    $0x2,%eax
jge    1214 <dump_mem_stats+0xb4>
jmp    11a7 <dump_mem_stats+0x47>
cmpl   $0x0,-0xc(%rbp)
je     122c <dump_mem_stats+0xcc>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
lea    -0x130(%rbp),%rsi
mov    -0x138(%rbp),%rdx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a7 <dump_mem_stats+0x47>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_mem_stats>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ca <dump_mem_stats+0x5a>
mov    %rax,%rbx
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11dc <dump_mem_stats+0x6c>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x30(%rsp),%r14
lea    0xe28(%rip),%r15
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
xor    %ebp,%ebp
jmp    1227 <dump_mem_stats+0xb7>
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%rdx
lea    0xe0f(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11b4 <dump_mem_stats+0x44>
movq   $0x0,0x8(%rsp)
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jl     121b <dump_mem_stats+0xab>
test   %ebp,%ebp
je     1200 <dump_mem_stats+0x90>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
jmp    1200 <dump_mem_stats+0x90>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c" \22mem\22:\0A {\0A\00"
@.str.3 = [11 x i8] c"%s %lu %*s\00"
@.str.4 = [3 x i8] c",\0A\00"
@.str.5 = [14 x i8] c" \22%s\22: %lu\00"
@.str.6 = [5 x i8] c"\0A }\00"
define void @dump_mem_stats() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [32 x i8]
%5 = alloca i64
%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** %1
%7 = load i32*, i32** %1
%8 = icmp ne i32* %7, null
br i1 %8, label %10, label %9
9: ; preds = %0
br label %39
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %12
12: ; preds = %31, %25, %10
%13 = load i32*, i32** %1
%14 = call i32 @feof(i32* %13)
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br i1 %16, label %17, label %35
17: ; preds = %12
store i64 0, i64* %5
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%19 = load i32*, i32** %1
%20 = call i32 @fgets(i8* %18, i32 256, i32* %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%23 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %22, i64* %5)
%24 = icmp slt i32 %23, 2
br i1 %24, label %25, label %26
25: ; preds = %17
br label %12
26: ; preds = %17
%27 = load i32, i32* %2
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
store i32 1, i32* %2
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%33 = load i64, i64* %5
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %32, i64 %33)
br label %12
35: ; preds = %12
%36 = load i32*, i32** %1
%37 = call i32 @fclose(i32* %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %35, %9
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"%s %lu %*s\00"
@.str.5 = [14 x i8] c" \22%s\22: %lu\00"
@.str.6 = [5 x i8] c"\0A }\00"
@str = [13 x i8] c" \22mem\22:\0A {\00"
@str.7 = [2 x i8] c",\00"
define void @dump_mem_stats() {
%1 = alloca [256 x i8]
%2 = alloca [32 x i8]
%3 = alloca i64
%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 %33, label %6
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%8 = call i32 @feof(i32* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %6
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%13 = bitcast i64* %3 to i8*
br label %14
14: ; preds = %10, %26
%15 = phi i32 [ 0, %10 ], [ %27, %26 ]
store i64 0, i64* %3
%16 = call i32 @fgets(i8* %11, i32 256, i32* %4)
%17 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %12, i64* %3)
%18 = icmp slt i32 %17, 2
br i1 %18, label %26, label %19
19: ; preds = %14
%20 = icmp eq i32 %15, 0
br i1 %20, label %23, label %21
21: ; preds = %19
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.7, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
%24 = load i64, i64* %3
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %12, i64 %24)
br label %26
26: ; preds = %14, %23
%27 = phi i32 [ 1, %23 ], [ %15, %14 ]
%28 = call i32 @feof(i32* %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %14, label %30
30: ; preds = %26, %6
%31 = call i32 @fclose(i32* %4)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %33
33: ; preds = %0, %30
ret void
}