#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 device {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ dev_close_immediate(struct device *);
int dev_open_flags(struct device *, int, int, int);
scalar_t__ vg_write_lock_held();
int dev_test_excl(struct device *dev) {
  int flags;
  int r;
  flags = vg_write_lock_held() ? 02 : 00;
  flags |= 0200;
  r = dev_open_flags(dev, flags, 1, 1);
  if (r)
    dev_close_immediate(dev);
  return r;
}


x86 assembly (clang, -O0):
<dev_test_excl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <vg_write_lock_held@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0x2,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
or     $0x80,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1040 <dev_open_flags@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     118b <dev_test_excl+0x5b>
mov    -0x8(%rbp),%rdi
call   1030 <dev_close_immediate@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dev_test_excl>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <vg_write_lock_held@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    (%rcx,%rcx,1),%esi
add    $0x80,%esi
mov    %rbx,%rdi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1040 <dev_open_flags@plt>
mov    %eax,%ebp
test   %eax,%eax
je     116e <dev_test_excl+0x3e>
mov    %rbx,%rdi
call   1030 <dev_close_immediate@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.device = type { i32 }
define i32 @dev_test_excl(%struct.device* %0) {
%2 = alloca %struct.device*
%3 = alloca i32
%4 = alloca i32
store %struct.device* %0, %struct.device** %2
%5 = call i64 (...) @vg_write_lock_held()
%6 = icmp ne i64 %5, 0
%7 = zext i1 %6 to i64
%8 = select i1 %6, i32 2, i32 0
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = or i32 %9, 128
store i32 %10, i32* %3
%11 = load %struct.device*, %struct.device** %2
%12 = load i32, i32* %3
%13 = call i32 @dev_open_flags(%struct.device* %11, i32 %12, i32 1, i32 1)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load %struct.device*, %struct.device** %2
%18 = call i32 @dev_close_immediate(%struct.device* %17)
br label %19
19: ; preds = %16, %1
%20 = load i32, i32* %4
ret i32 %20
}

LLVM IR (-O2):
%struct.device = type { i32 }
define i32 @dev_test_excl(%struct.device* %0) {
%2 = tail call i64 (...) @vg_write_lock_held()
%3 = icmp eq i64 %2, 0
%4 = select i1 %3, i32 128, i32 130
%5 = tail call i32 @dev_open_flags(%struct.device* %0, i32 %4, i32 1, i32 1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = tail call i32 @dev_close_immediate(%struct.device* %0)
br label %9
9: ; preds = %7, %1
ret i32 %5
}