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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int sscanf(char *, char *, char *, char *, size_t *);
size_t swapTotal() {
  FILE *fp = fopen("/proc/swaps", "rt");
  if (fp == NULL) {
    perror("/proc/swaps");
    return 0;
  }
  size_t ts = 0;
  char *line = NULL;
  size_t len = 0;
  ssize_t read = 0;
  while ((read = getline(&line, &len, fp)) != -1) {
    if (line[0] == '/') {
      size_t size;
      char name[1000], part[1000];
      int s = sscanf(line, "%s %s %zu", name, part, &size);
      if (s == 3) {
        ts += (size << 10);
      }
    }
  }
  free(line);
  fclose(fp);
  return ts;
}


x86 assembly (clang, -O0):
<swapTotal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
lea    0xe8e(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <swapTotal+0x46>
lea    0xe6c(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    125b <swapTotal+0xfb>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     1241 <swapTotal+0xe1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    123c <swapTotal+0xdc>
mov    -0x20(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    -0x810(%rbp),%rcx
lea    0xe04(%rip),%rsi
lea    -0x38(%rbp),%r8
call   1050 <sscanf@plt>
mov    %eax,-0x814(%rbp)
cmpl   $0x3,-0x814(%rbp)
jne    1237 <swapTotal+0xd7>
mov    -0x38(%rbp),%rax
shl    $0xa,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    123c <swapTotal+0xdc>
jmp    11c5 <swapTotal+0x65>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swapTotal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x7f8,%rsp
lea    0xe88(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     125a <swapTotal+0xfa>
mov    %rax,%r14
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1231 <swapTotal+0xd1>
lea    0x20(%rsp),%r13
lea    0x18(%rsp),%rbp
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
jmp    11f3 <swapTotal+0x93>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1233 <swapTotal+0xd3>
mov    0x8(%rsp),%rdi
cmpb   $0x2f,(%rdi)
jne    11e0 <swapTotal+0x80>
lea    0xe0b(%rip),%rsi
lea    0x410(%rsp),%rdx
mov    %r13,%rcx
mov    %rbp,%r8
call   1050 <sscanf@plt>
mov    0x18(%rsp),%rcx
shl    $0xa,%rcx
cmp    $0x3,%eax
mov    $0x0,%eax
cmovne %rax,%rcx
add    %rcx,%rbx
jmp    11e0 <swapTotal+0x80>
xor    %ebx,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x7f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
call   1070 <perror@plt>
xor    %ebx,%ebx
jmp    1245 <swapTotal+0xe5>

LLVM IR (-O0):
@.str = [12 x i8] c"/proc/swaps\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [10 x i8] c"%s %s %zu\00"
define i64 @swapTotal() {
%1 = alloca i64
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca [1000 x i8]
%9 = alloca [1000 x i8]
%10 = alloca i32
%11 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %1
br label %47
16: ; preds = %0
store i64 0, i64* %3
store i8* null, i8** %4
store i64 0, i64* %5
store i32 0, i32* %6
br label %17
17: ; preds = %40, %16
%18 = load i32*, i32** %2
%19 = call i32 @getline(i8** %4, i64* %5, i32* %18)
store i32 %19, i32* %6
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %41
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 47
br i1 %26, label %27, label %40
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%31 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* %30, i64* %7)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, 3
br i1 %33, label %34, label %39
34: ; preds = %27
%35 = load i64, i64* %7
%36 = shl i64 %35, 10
%37 = load i64, i64* %3
%38 = add i64 %37, %36
store i64 %38, i64* %3
br label %39
39: ; preds = %34, %27
br label %40
40: ; preds = %39, %21
br label %17
41: ; preds = %17
%42 = load i8*, i8** %4
%43 = call i32 @free(i8* %42)
%44 = load i32*, i32** %2
%45 = call i32 @fclose(i32* %44)
%46 = load i64, i64* %3
store i64 %46, i64* %1
br label %47
47: ; preds = %41, %14
%48 = load i64, i64* %1
ret i64 %48
}

LLVM IR (-O2):
@.str = [12 x i8] c"/proc/swaps\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [10 x i8] c"%s %s %zu\00"
define i64 @swapTotal() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i64
%4 = alloca [1000 x i8]
%5 = alloca [1000 x i8]
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %40
10: ; preds = %0
%11 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%12 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%13 = call i32 @getline(i8** %1, i64* %2, i32* %6)
%14 = icmp eq i32 %13, -1
br i1 %14, label %35, label %15
15: ; preds = %10
%16 = bitcast i64* %3 to i8*
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%18 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
br label %19
19: ; preds = %15, %31
%20 = phi i64 [ 0, %15 ], [ %32, %31 ]
%21 = load i8*, i8** %1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 47
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %18, i64* %3)
%26 = icmp eq i32 %25, 3
%27 = load i64, i64* %3
%28 = shl i64 %27, 10
%29 = select i1 %26, i64 %28, i64 0
%30 = add i64 %29, %20
br label %31
31: ; preds = %24, %19
%32 = phi i64 [ %30, %24 ], [ %20, %19 ]
%33 = call i32 @getline(i8** %1, i64* %2, i32* %6)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %19
35: ; preds = %31, %10
%36 = phi i64 [ 0, %10 ], [ %32, %31 ]
%37 = load i8*, i8** %1
%38 = call i32 @free(i8* %37)
%39 = call i32 @fclose(i32* %6)
br label %40
40: ; preds = %35, %8
%41 = phi i64 [ 0, %8 ], [ %36, %35 ]
ret i64 %41
}