#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*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *seek_word(char *, int);
int /*<<< orphan*/ strcmp(char const *, char *);
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned swap_read_usage(const char *swap_name, unsigned *size,
                         unsigned *used) {
  unsigned found = 0;
  if (size)
    *size = 0;
  if (used)
    *used = 0;
  if (swap_name) {
    FILE *swaps = fopen("/proc/swaps", "rt");
    if (swaps) {
      char line[256];
      while (fgets(line, (sizeof(line) / sizeof(*line)), swaps)) {
        char *cursor = seek_word(line, 0);
        if (cursor && !strcmp(swap_name, cursor)) {
          cursor = seek_word(cursor, 1);
          cursor = seek_word(cursor, 1);
          if (size)
            *size = (unsigned)strtoul(cursor, NULL, 0);
          cursor = seek_word(cursor, 1);
          if (used)
            *used = (unsigned)strtoul(cursor, NULL, 0);
          found = 1;
          break;
        }
      }
      fclose(swaps);
    }
  }
  return found;
}


x86 assembly (clang, -O0):
<swap_read_usage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1193 <swap_read_usage+0x33>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     11a8 <swap_read_usage+0x48>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x8(%rbp)
je     12e6 <swap_read_usage+0x186>
lea    0xe46(%rip),%rdi
lea    0xe4b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12e1 <swap_read_usage+0x181>
jmp    11da <swap_read_usage+0x7a>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12d8 <swap_read_usage+0x178>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
call   1040 <seek_word@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
je     12d3 <swap_read_usage+0x173>
mov    -0x8(%rbp),%rdi
mov    -0x138(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12d3 <swap_read_usage+0x173>
mov    -0x138(%rbp),%rdi
mov    $0x1,%esi
call   1040 <seek_word@plt>
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rdi
mov    $0x1,%esi
call   1040 <seek_word@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     128a <swap_read_usage+0x12a>
mov    -0x138(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x138(%rbp),%rdi
mov    $0x1,%esi
call   1040 <seek_word@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12c7 <swap_read_usage+0x167>
mov    -0x138(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x1c(%rbp)
jmp    12d8 <swap_read_usage+0x178>
jmp    11da <swap_read_usage+0x7a>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12e6 <swap_read_usage+0x186>
mov    -0x1c(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap_read_usage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%rbp
test   %rsi,%rsi
je     1186 <swap_read_usage+0x26>
movl   $0x0,(%r15)
test   %rbx,%rbx
je     1191 <swap_read_usage+0x31>
movl   $0x0,(%rbx)
xor    %r12d,%r12d
test   %rbp,%rbp
je     128f <swap_read_usage+0x12f>
lea    0xe5c(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     128f <swap_read_usage+0x12f>
mov    %rax,%r13
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1284 <swap_read_usage+0x124>
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%rbx
jmp    1205 <swap_read_usage+0xa5>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1284 <swap_read_usage+0x124>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <seek_word@plt>
test   %rax,%rax
je     11f0 <swap_read_usage+0x90>
mov    %rax,%r14
mov    %rbp,%rdi
mov    %rax,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    11f0 <swap_read_usage+0x90>
mov    $0x1,%r12d
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <seek_word@plt>
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <seek_word@plt>
mov    %rax,%rbx
test   %r15,%r15
je     125d <swap_read_usage+0xfd>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,(%r15)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <seek_word@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1287 <swap_read_usage+0x127>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,(%rbx)
jmp    1287 <swap_read_usage+0x127>
xor    %r12d,%r12d
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/proc/swaps\00"
@.str.1 = [3 x i8] c"rt\00"
define i32 @swap_read_usage(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [256 x i8]
%10 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i32 0, i32* %7
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32*, i32** %5
store i32 0, i32* %14
br label %15
15: ; preds = %13, %3
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i32*, i32** %6
store i32 0, i32* %19
br label %20
20: ; preds = %18, %15
%21 = load i8*, i8** %4
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %71
23: ; preds = %20
%24 = 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* %24, i32** %8
%25 = load i32*, i32** %8
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %70
27: ; preds = %23
br label %28
28: ; preds = %66, %27
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%30 = load i32*, i32** %8
%31 = call i64 @fgets(i8* %29, i32 256, i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %67
33: ; preds = %28
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%35 = call i8* @seek_word(i8* %34, i32 0)
store i8* %35, i8** %10
%36 = load i8*, i8** %10
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %66
38: ; preds = %33
%39 = load i8*, i8** %4
%40 = load i8*, i8** %10
%41 = call i32 @strcmp(i8* %39, i8* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %66, label %43
43: ; preds = %38
%44 = load i8*, i8** %10
%45 = call i8* @seek_word(i8* %44, i32 1)
store i8* %45, i8** %10
%46 = load i8*, i8** %10
%47 = call i8* @seek_word(i8* %46, i32 1)
store i8* %47, i8** %10
%48 = load i32*, i32** %5
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %55
50: ; preds = %43
%51 = load i8*, i8** %10
%52 = call i64 @strtoul(i8* %51, i32* null, i32 0)
%53 = trunc i64 %52 to i32
%54 = load i32*, i32** %5
store i32 %53, i32* %54
br label %55
55: ; preds = %50, %43
%56 = load i8*, i8** %10
%57 = call i8* @seek_word(i8* %56, i32 1)
store i8* %57, i8** %10
%58 = load i32*, i32** %6
%59 = icmp ne i32* %58, null
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i8*, i8** %10
%62 = call i64 @strtoul(i8* %61, i32* null, i32 0)
%63 = trunc i64 %62 to i32
%64 = load i32*, i32** %6
store i32 %63, i32* %64
br label %65
65: ; preds = %60, %55
store i32 1, i32* %7
br label %67
66: ; preds = %38, %33
br label %28
67: ; preds = %65, %28
%68 = load i32*, i32** %8
%69 = call i32 @fclose(i32* %68)
br label %70
70: ; preds = %67, %23
br label %71
71: ; preds = %70, %20
%72 = load i32, i32* %7
ret i32 %72
}

LLVM IR (-O2):
@.str = [12 x i8] c"/proc/swaps\00"
@.str.1 = [3 x i8] c"rt\00"
define i32 @swap_read_usage(i8* %0, i32* writeonly %1, i32* writeonly %2) {
%4 = alloca [256 x i8]
%5 = icmp eq i32* %1, null
br i1 %5, label %7, label %6
6: ; preds = %3
store i32 0, i32* %1
br label %7
7: ; preds = %6, %3
%8 = icmp eq i32* %2, null
br i1 %8, label %10, label %9
9: ; preds = %7
store i32 0, i32* %2
br label %10
10: ; preds = %9, %7
%11 = icmp eq i8* %0, null
br i1 %11, label %42, label %12
12: ; preds = %10
%13 = 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))
%14 = icmp eq i32* %13, null
br i1 %14, label %42, label %15
15: ; preds = %12
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = call i64 @fgets(i8* %16, i32 256, i32* %13)
%18 = icmp eq i64 %17, 0
br i1 %18, label %39, label %19
19: ; preds = %15, %36
%20 = call i8* @seek_word(i8* %16, i32 0)
%21 = icmp eq i8* %20, null
br i1 %21, label %36, label %22
22: ; preds = %19
%23 = call i32 @strcmp(i8* %0, i8* %20)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %22
%26 = call i8* @seek_word(i8* %20, i32 1)
%27 = call i8* @seek_word(i8* %26, i32 1)
br i1 %5, label %31, label %28
28: ; preds = %25
%29 = call i64 @strtoul(i8* nocapture %27, i32* null, i32 0)
%30 = trunc i64 %29 to i32
store i32 %30, i32* %1
br label %31
31: ; preds = %28, %25
%32 = call i8* @seek_word(i8* %27, i32 1)
br i1 %8, label %39, label %33
33: ; preds = %31
%34 = call i64 @strtoul(i8* nocapture %32, i32* null, i32 0)
%35 = trunc i64 %34 to i32
store i32 %35, i32* %2
br label %39
36: ; preds = %19, %22
%37 = call i64 @fgets(i8* %16, i32 256, i32* %13)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %19
39: ; preds = %36, %15, %31, %33
%40 = phi i32 [ 1, %33 ], [ 1, %31 ], [ 0, %15 ], [ 0, %36 ]
%41 = call i32 @fclose(i32* %13)
br label %42
42: ; preds = %12, %39, %10
%43 = phi i32 [ 0, %10 ], [ %40, %39 ], [ 0, %12 ]
ret i32 %43
}