#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_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
scalar_t__ EBADF;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int zed_file_lock(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_SETLK, &lock) < 0) {
    if ((errno == EACCES) || (errno == EAGAIN))
      return (1);
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<zed_file_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1145 <zed_file_lock+0x35>
mov    0x2e8c(%rip),%rax
mov    (%rax),%rcx
mov    0x2e9a(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <zed_file_lock+0xcf>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%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    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11d8 <zed_file_lock+0xc8>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <zed_file_lock+0xb0>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <zed_file_lock+0xbc>
movl   $0x1,-0x4(%rbp)
jmp    11df <zed_file_lock+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <zed_file_lock+0xcf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zed_file_lock>:
sub    $0x18,%rsp
test   %edi,%edi
js     1159 <zed_file_lock+0x49>
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1177 <zed_file_lock+0x67>
add    $0x18,%rsp
ret
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    0x2e66(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
sete   %cl
mov    0x2e13(%rip),%rdx
cmp    (%rdx),%rax
sete   %al
or     %cl,%al
movzbl %al,%eax
add    %eax,%eax
add    $0xffffffff,%eax
add    $0x18,%rsp
ret

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

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