#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 */
struct stat {
  scalar_t__ st_rdev;
  int /*<<< orphan*/ st_mode;
};
typedef scalar_t__ dev_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen_for_read(char *);
scalar_t__ stat(char const *, struct stat *);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char const *);
int is_swap_device(const char *file) {
  FILE *f;
  char buf[1024], *cp;
  dev_t file_dev;
  struct stat st_buf;
  int ret = 0;
  file_dev = 0;
  if ((stat(file, &st_buf) == 0) && S_ISBLK(st_buf.st_mode))
    file_dev = st_buf.st_rdev;
  if (!(f = fopen_for_read("/proc/swaps")))
    return 0;
  fgets(buf, sizeof(buf), f);
  while (!feof(f)) {
    if (!fgets(buf, sizeof(buf), f))
      break;
    if ((cp = strchr(buf, ' ')) != NULL)
      *cp = 0;
    if ((cp = strchr(buf, '\t')) != NULL)
      *cp = 0;
    if (strcmp(buf, file) == 0) {
      ret++;
      break;
    }
    if (file_dev && (stat(buf, &st_buf) == 0) && S_ISBLK(st_buf.st_mode) &&
        file_dev == st_buf.st_rdev) {
      ret++;
      break;
    }
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<is_swap_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x444(%rbp)
movq   $0x0,-0x430(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x440(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    11e1 <is_swap_device+0x61>
mov    -0x438(%rbp),%edi
call   10a0 <S_ISBLK@plt>
cmp    $0x0,%rax
je     11e1 <is_swap_device+0x61>
mov    -0x440(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    0xe18(%rip),%rdi
call   1030 <fopen_for_read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1207 <is_swap_device+0x87>
movl   $0x0,-0x4(%rbp)
jmp    1362 <is_swap_device+0x1e2>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123a <is_swap_device+0xba>
jmp    1350 <is_swap_device+0x1d0>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    125d <is_swap_device+0xdd>
jmp    1350 <is_swap_device+0x1d0>
lea    -0x420(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     1289 <is_swap_device+0x109>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x420(%rbp),%rdi
mov    $0x9,%esi
call   1050 <strchr@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     12b5 <is_swap_device+0x135>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12e3 <is_swap_device+0x163>
mov    -0x444(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x444(%rbp)
jmp    1350 <is_swap_device+0x1d0>
cmpq   $0x0,-0x430(%rbp)
je     134b <is_swap_device+0x1cb>
lea    -0x420(%rbp),%rdi
lea    -0x440(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    134b <is_swap_device+0x1cb>
mov    -0x438(%rbp),%edi
call   10a0 <S_ISBLK@plt>
cmp    $0x0,%rax
je     134b <is_swap_device+0x1cb>
mov    -0x430(%rbp),%rax
cmp    -0x440(%rbp),%rax
jne    134b <is_swap_device+0x1cb>
mov    -0x444(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x444(%rbp)
jmp    1350 <is_swap_device+0x1d0>
jmp    121c <is_swap_device+0x9c>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x444(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_swap_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
mov    %rsp,%rsi
call   1090 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11a7 <is_swap_device+0x27>
xor    %ebp,%ebp
jmp    11b9 <is_swap_device+0x39>
mov    0x8(%rsp),%edi
call   10a0 <S_ISBLK@plt>
test   %rax,%rax
je     11a3 <is_swap_device+0x23>
mov    (%rsp),%rbp
lea    0xe40(%rip),%rdi
call   1030 <fopen_for_read@plt>
test   %rax,%rax
je     11f5 <is_swap_device+0x75>
mov    %rax,%r12
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     1209 <is_swap_device+0x89>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%r15
mov    %rsp,%r13
jmp    122c <is_swap_device+0xac>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    11eb <is_swap_device+0x6b>
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %eax,%eax
je     11eb <is_swap_device+0x6b>
mov    %r15,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1255 <is_swap_device+0xd5>
movb   $0x0,(%rax)
mov    %r15,%rdi
mov    $0x9,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     126a <is_swap_device+0xea>
movb   $0x0,(%rax)
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     11ed <is_swap_device+0x6d>
test   %rbp,%rbp
je     1220 <is_swap_device+0xa0>
mov    %r15,%rdi
mov    %r13,%rsi
call   1090 <stat@plt>
test   %rax,%rax
jne    1220 <is_swap_device+0xa0>
mov    0x8(%rsp),%edi
call   10a0 <S_ISBLK@plt>
test   %rax,%rax
je     1220 <is_swap_device+0xa0>
cmp    (%rsp),%rbp
jne    1220 <is_swap_device+0xa0>
jmp    11ed <is_swap_device+0x6d>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [12 x i8] c"/proc/swaps\00"
define i32 @is_swap_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %9
store i64 0, i64* %7
%10 = load i8*, i8** %3
%11 = call i64 @stat(i8* %10, %struct.stat* %8)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i64 @S_ISBLK(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* %7
br label %21
21: ; preds = %18, %13, %1
%22 = call i32* @fopen_for_read(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %4
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21
store i32 0, i32* %2
br label %85
25: ; preds = %21
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = load i32*, i32** %4
%28 = call i32 @fgets(i8* %26, i32 1024, i32* %27)
br label %29
29: ; preds = %80, %25
%30 = load i32*, i32** %4
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %34, label %81
34: ; preds = %29
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = load i32*, i32** %4
%37 = call i32 @fgets(i8* %35, i32 1024, i32* %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %34
br label %81
40: ; preds = %34
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%42 = call i8* @strchr(i8* %41, i8 signext 32)
store i8* %42, i8** %6
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i8*, i8** %6
store i8 0, i8* %45
br label %46
46: ; preds = %44, %40
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%48 = call i8* @strchr(i8* %47, i8 signext 9)
store i8* %48, i8** %6
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i8*, i8** %6
store i8 0, i8* %51
br label %52
52: ; preds = %50, %46
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%54 = load i8*, i8** %3
%55 = call i64 @strcmp(i8* %53, i8* %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %81
60: ; preds = %52
%61 = load i64, i64* %7
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %80
63: ; preds = %60
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%65 = call i64 @stat(i8* %64, %struct.stat* %8)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %80
67: ; preds = %63
%68 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%69 = load i32, i32* %68
%70 = call i64 @S_ISBLK(i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %80
72: ; preds = %67
%73 = load i64, i64* %7
%74 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%75 = load i64, i64* %74
%76 = icmp eq i64 %73, %75
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i32, i32* %9
%79 = add nsw i32 %78, 1
store i32 %79, i32* %9
br label %81
80: ; preds = %72, %67, %63, %60
br label %29
81: ; preds = %77, %57, %39, %29
%82 = load i32*, i32** %4
%83 = call i32 @fclose(i32* %82)
%84 = load i32, i32* %9
store i32 %84, i32* %2
br label %85
85: ; preds = %81, %24
%86 = load i32, i32* %2
ret i32 %86
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [12 x i8] c"/proc/swaps\00"
define i32 @is_swap_device(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%10 = load i32, i32* %9
%11 = call i64 @S_ISBLK(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i64, i64* %14
br label %16
16: ; preds = %13, %8, %1
%17 = phi i64 [ %15, %13 ], [ 0, %8 ], [ 0, %1 ]
%18 = call i32* @fopen_for_read(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %59, label %20
20: ; preds = %16
%21 = call i32 @fgets(i8* %4, i32 1024, i32* %18)
%22 = call i32 @feof(i32* %18)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %56
24: ; preds = %20
%25 = icmp eq i64 %17, 0
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %28
28: ; preds = %24, %53
%29 = call i32 @fgets(i8* %4, i32 1024, i32* %18)
%30 = icmp eq i32 %29, 0
br i1 %30, label %56, label %31
31: ; preds = %28
%32 = call i8* @strchr(i8* %4, i8 signext 32)
%33 = icmp eq i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %31
store i8 0, i8* %32
br label %35
35: ; preds = %34, %31
%36 = call i8* @strchr(i8* %4, i8 signext 9)
%37 = icmp eq i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %35
store i8 0, i8* %36
br label %39
39: ; preds = %38, %35
%40 = call i64 @strcmp(i8* %4, i8* %0)
%41 = icmp eq i64 %40, 0
br i1 %41, label %56, label %42
42: ; preds = %39
br i1 %25, label %53, label %43
43: ; preds = %42
%44 = call i64 @stat(i8* %4, %struct.stat* %3)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = load i32, i32* %26
%48 = call i64 @S_ISBLK(i32 %47)
%49 = icmp ne i64 %48, 0
%50 = load i64, i64* %27
%51 = icmp eq i64 %17, %50
%52 = select i1 %49, i1 %51, i1 false
br i1 %52, label %56, label %53
53: ; preds = %46, %43, %42
%54 = call i32 @feof(i32* %18)
%55 = icmp eq i32 %54, 0
br i1 %55, label %28, label %56
56: ; preds = %53, %28, %39, %46, %20
%57 = phi i32 [ 0, %20 ], [ 1, %46 ], [ 1, %39 ], [ 0, %28 ], [ 0, %53 ]
%58 = call i32 @fclose(i32* %18)
br label %59
59: ; preds = %16, %56
%60 = phi i32 [ %57, %56 ], [ 0, %16 ]
ret i32 %60
}