#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 */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *, int *, char *);
char *smprintf(char *, int);
scalar_t__ strncmp(char *, char *, int);
char *getmemusage(void) {
  FILE *fp;
  fp = fopen("/proc/meminfo", "r");
  char label[128];
  char unit[10];
  int value;
  int total = 0;
  int used = 0;
  int done = 0;
  while (done != 15) {
    fscanf(fp, "%s %d %s", label, &value, unit);
    if (strncmp(label, "MemTotal:", 128) == 0) {
      total = value;
      used += value;
      done |= 1;
    } else if (strncmp(label, "MemFree:", 128) == 0) {
      used -= value;
      done |= 2;
    } else if (strncmp(label, "Buffers:", 128) == 0) {
      used -= value;
      done |= 4;
    } else if (strncmp(label, "Cached:", 128) == 0) {
      used -= value;
      done |= 8;
    }
  }
  fclose(fp);
  return smprintf("%d", used * 100 / total);
}


x86 assembly (clang, -O0):
<getmemusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    0xe9e(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0xa4(%rbp)
movl   $0x0,-0xa8(%rbp)
movl   $0x0,-0xac(%rbp)
cmpl   $0xf,-0xac(%rbp)
je     1303 <getmemusage+0x1b3>
mov    -0x8(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    -0x9a(%rbp),%r8
lea    0xe5a(%rip),%rsi
lea    -0xa0(%rbp),%rcx
call   1060 <fscanf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe49(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1216 <getmemusage+0xc6>
mov    -0xa0(%rbp),%eax
mov    %eax,-0xa4(%rbp)
mov    -0xa0(%rbp),%eax
add    -0xa8(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0xac(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0xac(%rbp)
jmp    12fe <getmemusage+0x1ae>
lea    -0x90(%rbp),%rdi
lea    0xdff(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1260 <getmemusage+0x110>
mov    -0xa0(%rbp),%ecx
mov    -0xa8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xa8(%rbp)
mov    -0xac(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0xac(%rbp)
jmp    12f9 <getmemusage+0x1a9>
lea    -0x90(%rbp),%rdi
lea    0xdbe(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12aa <getmemusage+0x15a>
mov    -0xa0(%rbp),%ecx
mov    -0xa8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xa8(%rbp)
mov    -0xac(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0xac(%rbp)
jmp    12f4 <getmemusage+0x1a4>
lea    -0x90(%rbp),%rdi
lea    0xd7d(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12ef <getmemusage+0x19f>
mov    -0xa0(%rbp),%ecx
mov    -0xa8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xa8(%rbp)
mov    -0xac(%rbp),%eax
or     $0x8,%eax
mov    %eax,-0xac(%rbp)
jmp    12f4 <getmemusage+0x1a4>
jmp    12f9 <getmemusage+0x1a9>
jmp    12fe <getmemusage+0x1ae>
jmp    1190 <getmemusage+0x40>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
imul   $0x64,-0xa8(%rbp),%eax
cltd
idivl  -0xa4(%rbp)
mov    %eax,%esi
lea    0xd1a(%rip),%rdi
call   1040 <smprintf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getmemusage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xe98(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
xor    %r14d,%r14d
lea    0x10(%rsp),%rbx
lea    0xe93(%rip),%rbp
xor    %r13d,%r13d
xor    %r12d,%r12d
jmp    11a5 <getmemusage+0x55>
xchg   %ax,%ax
mov    (%rsp),%r12d
add    %r12d,%r13d
or     $0x1,%r14d
cmp    $0xf,%r14d
je     124a <getmemusage+0xfa>
mov    %r15,%rdi
lea    0xe61(%rip),%rsi
mov    %rbx,%rdx
mov    %rsp,%rcx
lea    0x6(%rsp),%r8
call   1060 <fscanf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1190 <getmemusage+0x40>
mov    %rbx,%rdi
lea    0xe45(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1230 <getmemusage+0xe0>
mov    %rbx,%rdi
lea    0xe35(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     123d <getmemusage+0xed>
mov    %rbx,%rdi
lea    0xe25(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    119b <getmemusage+0x4b>
sub    (%rsp),%r13d
or     $0x8,%r14d
jmp    119b <getmemusage+0x4b>
sub    (%rsp),%r13d
or     $0x2,%r14d
jmp    119b <getmemusage+0x4b>
sub    (%rsp),%r13d
or     $0x4,%r14d
jmp    119b <getmemusage+0x4b>
mov    %r15,%rdi
call   1050 <fclose@plt>
imul   $0x64,%r13d,%eax
cltd
idiv   %r12d
lea    0xddc(%rip),%rdi
mov    %eax,%esi
call   1040 <smprintf@plt>
add    $0x98,%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 = [9 x i8] c"%s %d %s\00"
@.str.3 = [10 x i8] c"MemTotal:\00"
@.str.4 = [9 x i8] c"MemFree:\00"
@.str.5 = [9 x i8] c"Buffers:\00"
@.str.6 = [8 x i8] c"Cached:\00"
@.str.7 = [3 x i8] c"%d\00"
define i8* @getmemusage() {
%1 = alloca i32*
%2 = alloca [128 x i8]
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = 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* %8, i32** %1
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %60, %0
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 15
br i1 %11, label %12, label %61
12: ; preds = %9
%13 = load i32*, i32** %1
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%16 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %14, i32* %4, i8* %15)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 128)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %12
%21 = load i32, i32* %4
store i32 %21, i32* %5
%22 = load i32, i32* %4
%23 = load i32, i32* %6
%24 = add nsw i32 %23, %22
store i32 %24, i32* %6
%25 = load i32, i32* %7
%26 = or i32 %25, 1
store i32 %26, i32* %7
br label %60
27: ; preds = %12
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%29 = call i64 @strncmp(i8* %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 128)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = load i32, i32* %4
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %6
%35 = load i32, i32* %7
%36 = or i32 %35, 2
store i32 %36, i32* %7
br label %59
37: ; preds = %27
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%39 = call i64 @strncmp(i8* %38, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 128)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i32, i32* %4
%43 = load i32, i32* %6
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %6
%45 = load i32, i32* %7
%46 = or i32 %45, 4
store i32 %46, i32* %7
br label %58
47: ; preds = %37
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%49 = call i64 @strncmp(i8* %48, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 128)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i32, i32* %4
%53 = load i32, i32* %6
%54 = sub nsw i32 %53, %52
store i32 %54, i32* %6
%55 = load i32, i32* %7
%56 = or i32 %55, 8
store i32 %56, i32* %7
br label %57
57: ; preds = %51, %47
br label %58
58: ; preds = %57, %41
br label %59
59: ; preds = %58, %31
br label %60
60: ; preds = %59, %20
br label %9
61: ; preds = %9
%62 = load i32*, i32** %1
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %6
%65 = mul nsw i32 %64, 100
%66 = load i32, i32* %5
%67 = sdiv i32 %65, %66
%68 = call i8* @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 %67)
ret i8* %68
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"%s %d %s\00"
@.str.3 = [10 x i8] c"MemTotal:\00"
@.str.4 = [9 x i8] c"MemFree:\00"
@.str.5 = [9 x i8] c"Buffers:\00"
@.str.6 = [8 x i8] c"Cached:\00"
@.str.7 = [3 x i8] c"%d\00"
define i8* @getmemusage() {
%1 = alloca [128 x i8]
%2 = alloca [10 x i8]
%3 = alloca i32
%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 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%7 = bitcast i32* %3 to i8*
br label %8
8: ; preds = %0, %40
%9 = phi i32 [ 0, %0 ], [ %43, %40 ]
%10 = phi i32 [ 0, %0 ], [ %42, %40 ]
%11 = phi i32 [ 0, %0 ], [ %41, %40 ]
%12 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %5, i32* %3, i8* %6)
%13 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 128)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = load i32, i32* %3
%17 = add nsw i32 %16, %10
%18 = or i32 %9, 1
br label %40
19: ; preds = %8
%20 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 128)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* %3
%24 = sub nsw i32 %10, %23
%25 = or i32 %9, 2
br label %40
26: ; preds = %19
%27 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 128)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* %3
%31 = sub nsw i32 %10, %30
%32 = or i32 %9, 4
br label %40
33: ; preds = %26
%34 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 128)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* %3
%38 = sub nsw i32 %10, %37
%39 = or i32 %9, 8
br label %40
40: ; preds = %22, %33, %36, %29, %15
%41 = phi i32 [ %16, %15 ], [ %11, %22 ], [ %11, %29 ], [ %11, %36 ], [ %11, %33 ]
%42 = phi i32 [ %17, %15 ], [ %24, %22 ], [ %31, %29 ], [ %38, %36 ], [ %10, %33 ]
%43 = phi i32 [ %18, %15 ], [ %25, %22 ], [ %32, %29 ], [ %39, %36 ], [ %9, %33 ]
%44 = icmp eq i32 %43, 15
br i1 %44, label %45, label %8
45: ; preds = %40
%46 = call i32 @fclose(i32* %4)
%47 = mul nsw i32 %42, 100
%48 = sdiv i32 %47, %41
%49 = call i8* @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 %48)
ret i8* %49
}