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

/* Variables and functions */
int /*<<< orphan*/ EBADF;
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
pid_t zed_file_is_locked(int fd) {
  struct flock lock;
  if (fd < 0) {
    errno = EBADF;
    return (-1);
  }
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  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):
<zed_file_is_locked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1143 <zed_file_is_locked+0x33>
mov    0x2e8c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <zed_file_is_locked+0xae>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1198 <zed_file_is_locked+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <zed_file_is_locked+0xae>
mov    -0x30(%rbp),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <zed_file_is_locked+0xa8>
movl   $0x0,-0x4(%rbp)
jmp    11be <zed_file_is_locked+0xae>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zed_file_is_locked>:
sub    $0x28,%rsp
test   %edi,%edi
js     116c <zed_file_is_locked+0x5c>
mov    0x2ea1(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
js     117e <zed_file_is_locked+0x6e>
mov    (%rsp),%rcx
mov    0x2e84(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     1167 <zed_file_is_locked+0x57>
mov    0x8(%rsp),%eax
add    $0x28,%rsp
ret
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    0x2e54(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x28,%rsp
ret

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

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