#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 flock {
  int l_type;
  int l_pid;
  scalar_t__ l_len;
  int /*<<< orphan*/ l_whence;
  scalar_t__ l_start;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
pid_t _fd_test_lock(int fd, int type) {
  struct flock lock;
  assert(fd >= 0);
  lock.l_type = type;
  lock.l_start = 0;
  lock.l_whence = SEEK_SET;
  lock.l_len = 0;
  if (fcntl(fd, F_GETLK, &lock) < 0) {
    return (-1);
  }
  if (lock.l_type == F_UNLCK) {
    return (0);
  }
  return (lock.l_pid);
}


x86 assembly (clang, -O0):
<_fd_test_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    118c <_fd_test_lock+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <_fd_test_lock+0x90>
mov    -0x30(%rbp),%eax
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%eax
jne    11aa <_fd_test_lock+0x8a>
movl   $0x0,-0x4(%rbp)
jmp    11b0 <_fd_test_lock+0x90>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fd_test_lock>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
mov    %ebx,0x8(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
js     1188 <_fd_test_lock+0x68>
mov    0x8(%rsp),%ecx
mov    0x2e64(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     118d <_fd_test_lock+0x6d>
mov    0xc(%rsp),%eax
jmp    118d <_fd_test_lock+0x6d>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i32, i64 }
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i32 0
define i32 @_fd_test_lock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.flock
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = icmp sge i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %5
%12 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 4
store i64 0, i64* %13
%14 = load i32, i32* @SEEK_SET
%15 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i64 0, i64* %16
%17 = load i32, i32* %4
%18 = load i32, i32* @F_GETLK
%19 = call i64 @fcntl(i32 %17, i32 %18, %struct.flock* %6)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 -1, i32* %3
br label %31
22: ; preds = %2
%23 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @F_UNLCK
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 0, i32* %3
br label %31
28: ; preds = %22
%29 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
%30 = load i32, i32* %29
store i32 %30, i32* %3
br label %31
31: ; preds = %28, %27, %21
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i32, i64 }
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i32 0
define i32 @_fd_test_lock(i32 %0, i32 %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = xor i32 %0, -1
%6 = lshr i32 %5, 31
%7 = tail call i32 @assert(i32 %6)
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %1, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i64 0, i64* %9
%10 = load i32, i32* @SEEK_SET
%11 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i64 0, i64* %12
%13 = load i32, i32* @F_GETLK
%14 = call i64 @fcntl(i32 %0, i32 %13, %struct.flock* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %2
%17 = load i32, i32* %8
%18 = load i32, i32* @F_UNLCK
%19 = icmp eq i32 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%22 = load i32, i32* %21
br label %23
23: ; preds = %16, %2, %20
%24 = phi i32 [ %22, %20 ], [ -1, %2 ], [ 0, %16 ]
ret i32 %24
}