#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 udev_queue {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_queue_file(struct udev_queue *, unsigned long long *);
unsigned long long udev_queue_get_kernel_seqnum(struct udev_queue *);
scalar_t__ udev_queue_read_seqnum(int /*<<< orphan*/ *, unsigned long long *);
scalar_t__ udev_queue_skip_devpath(int /*<<< orphan*/ *);
__attribute__((visibility("default"))) int
udev_queue_get_queue_is_empty(struct udev_queue *udev_queue) {
  unsigned long long int seqnum_kernel;
  unsigned long long int seqnum_udev = 0;
  int queued = 0;
  int is_empty = 0;
  FILE *queue_file;
  if (udev_queue == NULL)
    return -EINVAL;
  queue_file = open_queue_file(udev_queue, &seqnum_udev);
  if (queue_file == NULL)
    return 1;
  for (;;) {
    unsigned long long int seqnum;
    ssize_t devpath_len;
    if (udev_queue_read_seqnum(queue_file, &seqnum) < 0)
      break;
    devpath_len = udev_queue_skip_devpath(queue_file);
    if (devpath_len < 0)
      break;
    if (devpath_len > 0) {
      queued++;
      seqnum_udev = seqnum;
    } else {
      queued--;
    }
  }
  if (queued > 0)
    goto out;
  seqnum_kernel = udev_queue_get_kernel_seqnum(udev_queue);
  if (seqnum_udev < seqnum_kernel)
    goto out;
  is_empty = 1;
out:
  fclose(queue_file);
  return is_empty;
}


x86 assembly (clang, -O0):
<udev_queue_get_queue_is_empty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1190 <udev_queue_get_queue_is_empty+0x40>
xor    %eax,%eax
mov    0x2e5a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <udev_queue_get_queue_is_empty+0x11a>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <open_queue_file@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b8 <udev_queue_get_queue_is_empty+0x68>
movl   $0x1,-0x4(%rbp)
jmp    126a <udev_queue_get_queue_is_empty+0x11a>
jmp    11bd <udev_queue_get_queue_is_empty+0x6d>
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1030 <udev_queue_read_seqnum@plt>
cmp    $0x0,%rax
jge    11d9 <udev_queue_get_queue_is_empty+0x89>
jmp    1225 <udev_queue_get_queue_is_empty+0xd5>
mov    -0x30(%rbp),%rdi
call   1050 <udev_queue_skip_devpath@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    11f6 <udev_queue_get_queue_is_empty+0xa6>
jmp    1225 <udev_queue_get_queue_is_empty+0xd5>
cmpq   $0x0,-0x40(%rbp)
jle    1217 <udev_queue_get_queue_is_empty+0xc7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1220 <udev_queue_get_queue_is_empty+0xd0>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11bd <udev_queue_get_queue_is_empty+0x6d>
cmpl   $0x0,-0x24(%rbp)
jle    1234 <udev_queue_get_queue_is_empty+0xe4>
jmp    125b <udev_queue_get_queue_is_empty+0x10b>
mov    -0x10(%rbp),%rdi
call   1060 <udev_queue_get_kernel_seqnum@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1254 <udev_queue_get_queue_is_empty+0x104>
jmp    125b <udev_queue_get_queue_is_empty+0x10b>
movl   $0x1,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_queue_get_queue_is_empty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
test   %rdi,%rdi
je     11d1 <udev_queue_get_queue_is_empty+0x81>
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1070 <open_queue_file@plt>
test   %rax,%rax
je     11e0 <udev_queue_get_queue_is_empty+0x90>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
call   1030 <udev_queue_read_seqnum@plt>
test   %rax,%rax
js     11ef <udev_queue_get_queue_is_empty+0x9f>
xor    %ebp,%ebp
lea    0x10(%rsp),%r15
jmp    11bd <udev_queue_get_queue_is_empty+0x6d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <udev_queue_read_seqnum@plt>
test   %rax,%rax
js     11e8 <udev_queue_get_queue_is_empty+0x98>
mov    %rbx,%rdi
call   1050 <udev_queue_skip_devpath@plt>
test   %rax,%rax
js     11e8 <udev_queue_get_queue_is_empty+0x98>
jne    11a0 <udev_queue_get_queue_is_empty+0x50>
add    $0xffffffff,%ebp
jmp    11ad <udev_queue_get_queue_is_empty+0x5d>
mov    0x2e08(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
jmp    120b <udev_queue_get_queue_is_empty+0xbb>
mov    $0x1,%r15d
jmp    120b <udev_queue_get_queue_is_empty+0xbb>
xor    %r15d,%r15d
test   %ebp,%ebp
jg     1203 <udev_queue_get_queue_is_empty+0xb3>
mov    %r14,%rdi
call   1060 <udev_queue_get_kernel_seqnum@plt>
xor    %r15d,%r15d
cmp    %rax,0x8(%rsp)
setae  %r15b
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev_queue = type { i32 }
@EINVAL = global i32 0
define i32 @udev_queue_get_queue_is_empty(%struct.udev_queue* %0) {
%2 = alloca i32
%3 = alloca %struct.udev_queue*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
store %struct.udev_queue* %0, %struct.udev_queue** %3
store i64 0, i64* %5
store i32 0, i32* %6
store i32 0, i32* %7
%11 = load %struct.udev_queue*, %struct.udev_queue** %3
%12 = icmp eq %struct.udev_queue* %11, null
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* %2
br label %61
16: ; preds = %1
%17 = load %struct.udev_queue*, %struct.udev_queue** %3
%18 = call i32* @open_queue_file(%struct.udev_queue* %17, i64* %5)
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 1, i32* %2
br label %61
22: ; preds = %16
br label %23
23: ; preds = %44, %22
%24 = load i32*, i32** %8
%25 = call i64 @udev_queue_read_seqnum(i32* %24, i64* %9)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %45
28: ; preds = %23
%29 = load i32*, i32** %8
%30 = call i64 @udev_queue_skip_devpath(i32* %29)
store i64 %30, i64* %10
%31 = load i64, i64* %10
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
br label %45
34: ; preds = %28
%35 = load i64, i64* %10
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
%40 = load i64, i64* %9
store i64 %40, i64* %5
br label %44
41: ; preds = %34
%42 = load i32, i32* %6
%43 = add nsw i32 %42, -1
store i32 %43, i32* %6
br label %44
44: ; preds = %41, %37
br label %23
45: ; preds = %33, %27
%46 = load i32, i32* %6
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
br label %57
49: ; preds = %45
%50 = load %struct.udev_queue*, %struct.udev_queue** %3
%51 = call i64 @udev_queue_get_kernel_seqnum(%struct.udev_queue* %50)
store i64 %51, i64* %4
%52 = load i64, i64* %5
%53 = load i64, i64* %4
%54 = icmp ult i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %49
br label %57
56: ; preds = %49
store i32 1, i32* %7
br label %57
57: ; preds = %56, %55, %48
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %7
store i32 %60, i32* %2
br label %61
61: ; preds = %57, %21, %13
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
%struct.udev_queue = type { i32 }
@EINVAL = global i32 0
define i32 @udev_queue_get_queue_is_empty(%struct.udev_queue* %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = icmp eq %struct.udev_queue* %0, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @EINVAL
%8 = sub nsw i32 0, %7
br label %43
9: ; preds = %1
%10 = call i32* @open_queue_file(%struct.udev_queue* %0, i64* %2)
%11 = icmp eq i32* %10, null
br i1 %11, label %43, label %12
12: ; preds = %9
%13 = bitcast i64* %3 to i8*
%14 = call i64 @udev_queue_read_seqnum(i32* %10, i64* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
br label %35
17: ; preds = %12, %28
%18 = phi i32 [ %29, %28 ], [ 0, %12 ]
%19 = call i64 @udev_queue_skip_devpath(i32* %10)
%20 = icmp slt i64 %19, 0
br i1 %20, label %32, label %21
21: ; preds = %17
%22 = icmp eq i64 %19, 0
br i1 %22, label %26, label %23
23: ; preds = %21
%24 = add nsw i32 %18, 1
%25 = load i64, i64* %3
store i64 %25, i64* %2
br label %28
26: ; preds = %21
%27 = add nsw i32 %18, -1
br label %28
28: ; preds = %23, %26
%29 = phi i32 [ %24, %23 ], [ %27, %26 ]
%30 = call i64 @udev_queue_read_seqnum(i32* %10, i64* %3)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %17
32: ; preds = %17, %28
%33 = phi i32 [ %18, %17 ], [ %29, %28 ]
%34 = icmp sgt i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %16, %32
%36 = call i64 @udev_queue_get_kernel_seqnum(%struct.udev_queue* %0)
%37 = load i64, i64* %2
%38 = icmp uge i64 %37, %36
%39 = zext i1 %38 to i32
br label %40
40: ; preds = %35, %32
%41 = phi i32 [ 0, %32 ], [ %39, %35 ]
%42 = call i32 @fclose(i32* %10)
br label %43
43: ; preds = %9, %40, %6
%44 = phi i32 [ %8, %6 ], [ %41, %40 ], [ 1, %9 ]
ret i32 %44
}