#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 */
int /*<<< orphan*/ EBADF;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int zed_file_unlock(int fd) {
  struct flock lock;
  if (fd < 0) {
    errno = EBADF;
    return (-1);
  }
  lock.l_type = F_UNLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  if (fcntl(fd, F_SETLK, &lock) < 0)
    return (-1);
  return (0);
}


x86 assembly (clang, -O0):
<zed_file_unlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1143 <zed_file_unlock+0x33>
mov    0x2e94(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <zed_file_unlock+0x8d>
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1196 <zed_file_unlock+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <zed_file_unlock+0x8d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zed_file_unlock>:
sub    $0x18,%rsp
test   %edi,%edi
js     1153 <zed_file_unlock+0x43>
mov    0x2ec1(%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>
sar    $0x3f,%rax
add    $0x18,%rsp
ret
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e6d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@EBADF = global i32 0
@errno = global i32 0
@F_UNLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
define i32 @zed_file_unlock(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 %22
9: ; preds = %1
%10 = load i32, i32* @F_UNLCK
%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 %21
20: ; preds = %9
store i32 -1, i32* %2
br label %22
21: ; preds = %9
store i32 0, i32* %2
br label %22
22: ; preds = %21, %20, %7
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@EBADF = global i32 0
@errno = global i32 0
@F_UNLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
define i32 @zed_file_unlock(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 %17
7: ; preds = %1
%8 = load i32, i32* @F_UNLCK
%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 = ashr i64 %14, 63
%16 = trunc i64 %15 to i32
br label %17
17: ; preds = %7, %5
%18 = phi i32 [ -1, %5 ], [ %16, %7 ]
ret i32 %18
}