#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 */

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ EOPNOTSUPP;
int LOCK_EX;
int LOCK_NB;
int LOCK_SH;
scalar_t__ errno;
scalar_t__ flock(int, int);
int lock_dev(int fd, int mode) {
  if (flock(fd, (mode ? LOCK_EX : LOCK_SH) | LOCK_NB) < 0)
    return errno == EINVAL || errno == EOPNOTSUPP ? 0 : 1;
  return 0;
}


x86 assembly (clang, -O0):
<lock_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     113f <lock_dev+0x2f>
mov    0x2eb3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    114b <lock_dev+0x3b>
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
call   1030 <flock@plt>
cmp    $0x0,%rax
jge    11ba <lock_dev+0xaa>
mov    0x2e58(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e44(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x15(%rbp)
je     11a2 <lock_dev+0x92>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%dl
mov    $0x1,%eax
xor    %ecx,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <lock_dev+0xb1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_dev>:
push   %rax
test   %esi,%esi
jne    111e <lock_dev+0xe>
mov    0x2ec4(%rip),%rax
jmp    1125 <lock_dev+0x15>
mov    0x2ec3(%rip),%rax
mov    (%rax),%esi
mov    0x2e82(%rip),%rax
or     (%rax),%esi
call   1030 <flock@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1141 <lock_dev+0x31>
pop    %rcx
ret
mov    0x2e80(%rip),%rax
mov    (%rax),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
setne  %cl
mov    0x2e71(%rip),%rdx
cmp    (%rdx),%rax
setne  %al
and    %cl,%al
movzbl %al,%eax
pop    %rcx
ret

LLVM IR (-O0):
@LOCK_EX = global i32 0
@LOCK_SH = global i32 0
@LOCK_NB = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EOPNOTSUPP = global i64 0
define i32 @lock_dev(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* %4
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @LOCK_EX
br label %13
11: ; preds = %2
%12 = load i32, i32* @LOCK_SH
br label %13
13: ; preds = %11, %9
%14 = phi i32 [ %10, %9 ], [ %12, %11 ]
%15 = load i32, i32* @LOCK_NB
%16 = or i32 %14, %15
%17 = call i64 @flock(i32 %6, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %13
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINVAL
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EOPNOTSUPP
%26 = icmp eq i64 %24, %25
br label %27
27: ; preds = %23, %19
%28 = phi i1 [ true, %19 ], [ %26, %23 ]
%29 = zext i1 %28 to i64
%30 = select i1 %28, i32 0, i32 1
store i32 %30, i32* %3
br label %32
31: ; preds = %13
store i32 0, i32* %3
br label %32
32: ; preds = %31, %27
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@LOCK_EX = global i32 0
@LOCK_SH = global i32 0
@LOCK_NB = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@EOPNOTSUPP = global i64 0
define i32 @lock_dev(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = load i32, i32* @LOCK_EX
%5 = load i32, i32* @LOCK_SH
%6 = select i1 %3, i32 %5, i32 %4
%7 = load i32, i32* @LOCK_NB
%8 = or i32 %6, %7
%9 = tail call i64 @flock(i32 %0, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINVAL
%14 = icmp ne i64 %12, %13
%15 = load i64, i64* @EOPNOTSUPP
%16 = icmp ne i64 %12, %15
%17 = select i1 %14, i1 %16, i1 false
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %2, %11
%20 = phi i32 [ %18, %11 ], [ 0, %2 ]
ret i32 %20
}