#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*/ fname;
typedef int /*<<< orphan*/ cookie;

/* Variables and functions */
int PATH_MAX;
scalar_t__ memcmp(char const *, char *, int);
scalar_t__ ploop_get_attr(char const *, char *, int *);
scalar_t__ ploop_get_mnt_by_dev(char const *, char *, int);
scalar_t__ read_line_quiet(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int is_device_inuse(const char *dev) {
  int count;
  char fname[PATH_MAX];
  char cookie[64] = "";
  if (ploop_get_attr(dev, "open_count", &count))
    return 1;
  snprintf(fname, sizeof(fname), "/sys/block/%s/pstate/cookie",
           memcmp(dev, "/dev/", 5) == 0 ? dev + 5 : dev);
  if (read_line_quiet(fname, cookie, sizeof(cookie)))
    return 1;
  if (!strncmp(cookie, "~", sizeof("~") - 1))
    return 1;
  if (count >= 2)
    return 1;
  if (count == 1 && ploop_get_mnt_by_dev(dev, fname, sizeof(fname)) != 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<is_device_inuse>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x88(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1090 <ploop_get_attr@plt>
cmp    $0x0,%rax
je     11f3 <is_device_inuse+0x83>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1323 <is_device_inuse+0x1b3>
mov    -0x88(%rbp),%rax
mov    %eax,-0x8c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe1c(%rip),%rsi
mov    $0x5,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
jne    1233 <is_device_inuse+0xc3>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x98(%rbp)
jmp    123e <is_device_inuse+0xce>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x80(%rbp),%rdi
mov    -0x8c(%rbp),%esi
mov    -0x98(%rbp),%rcx
lea    0xdb5(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x80(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1040 <read_line_quiet@plt>
cmp    $0x0,%rax
je     128a <is_device_inuse+0x11a>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1323 <is_device_inuse+0x1b3>
lea    -0x70(%rbp),%rdi
lea    0xd98(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12bb <is_device_inuse+0x14b>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1323 <is_device_inuse+0x1b3>
cmpl   $0x2,-0x14(%rbp)
jl     12d8 <is_device_inuse+0x168>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1323 <is_device_inuse+0x1b3>
cmpl   $0x1,-0x14(%rbp)
jne    1315 <is_device_inuse+0x1a5>
mov    -0x80(%rbp),%rsi
mov    -0x88(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    %eax,%edx
call   1080 <ploop_get_mnt_by_dev@plt>
cmp    $0x0,%rax
je     1315 <is_device_inuse+0x1a5>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1323 <is_device_inuse+0x1b3>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_device_inuse>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%r12
mov    %rsp,%r13
mov    0x2e72(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movaps %xmm0,-0x60(%rbp)
movaps %xmm0,-0x70(%rbp)
lea    0xe54(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <ploop_get_attr@plt>
mov    $0x1,%r15d
test   %rax,%rax
jne    120b <is_device_inuse+0xab>
lea    0xe60(%rip),%rsi
mov    %r12,%rdi
mov    $0x5,%edx
call   1060 <memcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,4),%rcx
add    %r12,%rcx
lea    0xe21(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <snprintf@plt>
lea    -0x70(%rbp),%rsi
mov    %rbx,%rdi
mov    $0x40,%edx
call   1040 <read_line_quiet@plt>
test   %rax,%rax
je     1220 <is_device_inuse+0xc0>
mov    %r13,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe06(%rip),%rsi
lea    -0x70(%rbp),%rdi
mov    $0x1,%r15d
mov    $0x1,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     120b <is_device_inuse+0xab>
cmpl   $0x1,-0x2c(%rbp)
jg     120b <is_device_inuse+0xab>
jne    125a <is_device_inuse+0xfa>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1070 <ploop_get_mnt_by_dev@plt>
test   %rax,%rax
jne    120b <is_device_inuse+0xab>
xor    %r15d,%r15d
jmp    120b <is_device_inuse+0xab>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"open_count\00"
@.str.1 = [28 x i8] c"/sys/block/%s/pstate/cookie\00"
@.str.2 = [6 x i8] c"/dev/\00"
@.str.3 = [2 x i8] c"~\00"
define i32 @is_device_inuse(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [64 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = bitcast [64 x i8]* %7 to i8*
%14 = load i8*, i8** %3
%15 = call i64 @ploop_get_attr(i8* %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* %4)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 1, i32* %2
store i32 1, i32* %8
br label %54
18: ; preds = %1
%19 = trunc i64 %10 to i32
%20 = load i8*, i8** %3
%21 = call i64 @memcmp(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 5
br label %28
26: ; preds = %18
%27 = load i8*, i8** %3
br label %28
28: ; preds = %26, %23
%29 = phi i8* [ %25, %23 ], [ %27, %26 ]
%30 = call i32 @snprintf(i8* %12, i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%32 = call i64 @read_line_quiet(i8* %12, i8* %31, i32 64)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 1, i32* %2
store i32 1, i32* %8
br label %54
35: ; preds = %28
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%37 = call i32 @strncmp(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %35
store i32 1, i32* %2
store i32 1, i32* %8
br label %54
40: ; preds = %35
%41 = load i32, i32* %4
%42 = icmp sge i32 %41, 2
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 1, i32* %2
store i32 1, i32* %8
br label %54
44: ; preds = %40
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, 1
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i8*, i8** %3
%49 = trunc i64 %10 to i32
%50 = call i64 @ploop_get_mnt_by_dev(i8* %48, i8* %12, i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 1, i32* %2
store i32 1, i32* %8
br label %54
53: ; preds = %47, %44
store i32 0, i32* %2
store i32 1, i32* %8
br label %54
54: ; preds = %53, %52, %43, %39, %34, %17
%55 = load i8*, i8** %5
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"open_count\00"
@.str.1 = [28 x i8] c"/sys/block/%s/pstate/cookie\00"
@.str.2 = [6 x i8] c"/dev/\00"
@.str.3 = [2 x i8] c"~\00"
define i32 @is_device_inuse(i8* %0) {
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%10 = call i64 @ploop_get_attr(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %32
12: ; preds = %1
%13 = call i64 @memcmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, i64 5, i64 0
%16 = getelementptr i8, i8* %0, i64 %15
%17 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i64 @read_line_quiet(i8* %8, i8* %9, i32 64)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %12
%21 = call i32 @strncmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%22 = icmp eq i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = load i32, i32* %2
%25 = icmp sgt i32 %24, 1
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = icmp eq i32 %24, 1
br i1 %27, label %28, label %31
28: ; preds = %26
%29 = call i64 @ploop_get_mnt_by_dev(i8* %0, i8* %8, i32 %5)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28, %26
br label %32
32: ; preds = %28, %23, %20, %12, %1, %31
%33 = phi i32 [ 0, %31 ], [ 1, %1 ], [ 1, %12 ], [ 1, %20 ], [ 1, %23 ], [ 1, %28 ]
ret i32 %33
}