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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *);
int sscanf(char *, char *, unsigned long long *);
int get_swap(unsigned long long *swap) {
  FILE *fd;
  char str[128];
  if ((fd = fopen("/proc/meminfo", "r")) == NULL) {
    logger(-1, errno,
           "Cannot open "
           "/proc/meminfo");
    return -1;
  }
  while (fgets(str, sizeof(str), fd)) {
    if (sscanf(str, "SwapTotal: %llu", swap) == 1) {
      *swap *= 1024;
      fclose(fd);
      return 0;
    }
  }
  logger(-1, errno,
         "Swap: is not found in "
         "/proc/meminfo");
  fclose(fd);
  return -1;
}


x86 assembly (clang, -O0):
<get_swap>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xea8(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a6 <get_swap+0x56>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
lea    0xe6d(%rip),%rdx
call   1050 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <get_swap+0xec>
jmp    11ab <get_swap+0x5b>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1212 <get_swap+0xc2>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe40(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    120d <get_swap+0xbd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
shl    $0xa,%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    123c <get_swap+0xec>
jmp    11ab <get_swap+0x5b>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
lea    0xe05(%rip),%rdx
call   1050 <logger@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_swap>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%r14
lea    0xea6(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c8 <get_swap+0x78>
mov    %rax,%r15
mov    %rsp,%rbx
lea    0xe96(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11e9 <get_swap+0x99>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1190 <get_swap+0x40>
shlq   $0xa,(%r14)
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
jmp    1210 <get_swap+0xc0>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdx
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
call   1050 <logger@plt>
jmp    1210 <get_swap+0xc0>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdx
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
call   1050 <logger@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Cannot open /proc/meminfo\00"
@.str.3 = [16 x i8] c"SwapTotal: %llu\00"
@.str.4 = [36 x i8] c"Swap: is not found in /proc/meminfo\00"
define i32 @get_swap(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca i32*
%5 = alloca [128 x i8]
store i64* %0, i64** %3
%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** %4
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = call i32 @logger(i32 -1, i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
11: ; preds = %1
br label %12
12: ; preds = %28, %11
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i64 @fgets(i8* %13, i32 128, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %12
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%19 = load i64*, i64** %3
%20 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64* %19)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i64*, i64** %3
%24 = load i64, i64* %23
%25 = mul i64 %24, 1024
store i64 %25, i64* %23
%26 = load i32*, i32** %4
%27 = call i32 @fclose(i32* %26)
store i32 0, i32* %2
br label %34
28: ; preds = %17
br label %12
29: ; preds = %12
%30 = load i32, i32* @errno
%31 = call i32 @logger(i32 -1, i32 %30, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
store i32 -1, i32* %2
br label %34
34: ; preds = %29, %22, %8
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Cannot open /proc/meminfo\00"
@.str.3 = [16 x i8] c"SwapTotal: %llu\00"
@.str.4 = [36 x i8] c"Swap: is not found in /proc/meminfo\00"
define i32 @get_swap(i64* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%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 %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i32 @logger(i32 -1, i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %23
9: ; preds = %1, %12
%10 = call i64 @fgets(i8* %3, i32 128, i32* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9
%13 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64* %0)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %9
15: ; preds = %12
%16 = load i64, i64* %0
%17 = shl i64 %16, 10
store i64 %17, i64* %0
%18 = call i32 @fclose(i32* %4)
br label %23
19: ; preds = %9
%20 = load i32, i32* @errno
%21 = call i32 @logger(i32 -1, i32 %20, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @fclose(i32* %4)
br label %23
23: ; preds = %19, %15, %6
%24 = phi i32 [ -1, %6 ], [ 0, %15 ], [ -1, %19 ]
ret i32 %24
}