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


x86 assembly (clang, -O0):
<getswapusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    0xe9e(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x98(%rbp)
movl   $0x0,-0x9c(%rbp)
movl   $0x0,-0xa0(%rbp)
cmpl   $0x7,-0xa0(%rbp)
je     12ad <getswapusage+0x15d>
mov    -0x8(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    0xe61(%rip),%rsi
lea    -0x94(%rbp),%rcx
call   1060 <fscanf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe51(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    120f <getswapusage+0xbf>
mov    -0x94(%rbp),%eax
mov    %eax,-0x98(%rbp)
mov    -0x94(%rbp),%eax
add    -0x9c(%rbp),%eax
mov    %eax,-0x9c(%rbp)
mov    -0xa0(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0xa0(%rbp)
jmp    12a8 <getswapusage+0x158>
lea    -0x90(%rbp),%rdi
lea    0xe08(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1259 <getswapusage+0x109>
mov    -0x94(%rbp),%ecx
mov    -0x9c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x9c(%rbp)
mov    -0xa0(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0xa0(%rbp)
jmp    12a3 <getswapusage+0x153>
lea    -0x90(%rbp),%rdi
lea    0xdc8(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    129e <getswapusage+0x14e>
mov    -0x94(%rbp),%ecx
mov    -0x9c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x9c(%rbp)
mov    -0xa0(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0xa0(%rbp)
jmp    12a3 <getswapusage+0x153>
jmp    12a8 <getswapusage+0x158>
jmp    1190 <getswapusage+0x40>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
imul   $0x64,-0x9c(%rbp),%eax
cltd
idivl  -0x98(%rbp)
mov    %eax,%esi
lea    0xd6e(%rip),%rdi
call   1040 <smprintf@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getswapusage>:
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    %ebp,%ebp
lea    0x10(%rsp),%rbx
lea    0xe95(%rip),%r13
xor    %r12d,%r12d
xor    %r14d,%r14d
jmp    11a0 <getswapusage+0x50>
nopl   (%rax)
mov    0xc(%rsp),%r14d
add    %r14d,%r12d
or     $0x1,%ebp
cmp    $0x7,%ebp
je     121a <getswapusage+0xca>
mov    %r15,%rdi
lea    0xe66(%rip),%rsi
mov    %rbx,%rdx
lea    0xc(%rsp),%rcx
call   1060 <fscanf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1190 <getswapusage+0x40>
mov    %rbx,%rdi
lea    0xe4f(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1210 <getswapusage+0xc0>
mov    %rbx,%rdi
lea    0xe40(%rip),%rsi
mov    $0x80,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    119b <getswapusage+0x4b>
sub    0xc(%rsp),%r12d
or     $0x4,%ebp
jmp    119b <getswapusage+0x4b>
nopl   0x0(%rax,%rax,1)
sub    0xc(%rsp),%r12d
or     $0x2,%ebp
jmp    119b <getswapusage+0x4b>
mov    %r15,%rdi
call   1050 <fclose@plt>
imul   $0x64,%r12d,%eax
cltd
idiv   %r14d
lea    0xe0a(%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 = [10 x i8] c"%s %d %*s\00"
@.str.3 = [11 x i8] c"SwapTotal:\00"
@.str.4 = [10 x i8] c"SwapFree:\00"
@.str.5 = [12 x i8] c"SwapCached:\00"
@.str.6 = [3 x i8] c"%d\00"
define i8* @getswapusage() {
%1 = alloca i32*
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = 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* %7, i32** %1
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %47, %0
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 7
br i1 %10, label %11, label %48
11: ; preds = %8
%12 = load i32*, i32** %1
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%14 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32* %3)
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%16 = call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 128)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = load i32, i32* %3
store i32 %19, i32* %4
%20 = load i32, i32* %3
%21 = load i32, i32* %5
%22 = add nsw i32 %21, %20
store i32 %22, i32* %5
%23 = load i32, i32* %6
%24 = or i32 %23, 1
store i32 %24, i32* %6
br label %47
25: ; preds = %11
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%27 = call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 128)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* %3
%31 = load i32, i32* %5
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %5
%33 = load i32, i32* %6
%34 = or i32 %33, 2
store i32 %34, i32* %6
br label %46
35: ; preds = %25
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%37 = call i64 @strncmp(i8* %36, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 128)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i32, i32* %3
%41 = load i32, i32* %5
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %5
%43 = load i32, i32* %6
%44 = or i32 %43, 4
store i32 %44, i32* %6
br label %45
45: ; preds = %39, %35
br label %46
46: ; preds = %45, %29
br label %47
47: ; preds = %46, %18
br label %8
48: ; preds = %8
%49 = load i32*, i32** %1
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %5
%52 = mul nsw i32 %51, 100
%53 = load i32, i32* %4
%54 = sdiv i32 %52, %53
%55 = call i8* @smprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %54)
ret i8* %55
}

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