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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ error(char *);
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;
  lock.l_pid = 0;
  if (fcntl(fd, F_GETLK, &lock) < 0)
    error("Unable to test for file lock: %m");
  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    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11a4 <fd_test_lock+0x74>
lea    0xe61(%rip),%rdi
call   1050 <error@plt>
mov    -0x38(%rbp),%eax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%eax
jne    11c3 <fd_test_lock+0x93>
movq   $0x0,-0x8(%rbp)
jmp    11cb <fd_test_lock+0x9b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%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,(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1189 <fd_test_lock+0x59>
lea    0xe7c(%rip),%rdi
call   1050 <error@plt>
mov    (%rsp),%eax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%eax
je     119e <fd_test_lock+0x6e>
mov    0x8(%rsp),%rax
jmp    11a0 <fd_test_lock+0x70>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i32, i64 }
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str = [33 x i8] c"Unable to test for file lock: %m\00"
@F_UNLCK = global i32 0
define i64 @fd_test_lock(i32 %0, i32 %1) {
%3 = alloca i64
%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 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %4
%19 = load i32, i32* @F_GETLK
%20 = call i64 @fcntl(i32 %18, i32 %19, %struct.flock* %6)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %2
%25 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* @F_UNLCK
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %24
store i64 0, i64* %3
br label %33
30: ; preds = %24
%31 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
%32 = load i64, i64* %31
store i64 %32, i64* %3
br label %33
33: ; preds = %30, %29
%34 = load i64, i64* %3
ret i64 %34
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32, i64 }
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str = [33 x i8] c"Unable to test for file lock: %m\00"
@F_UNLCK = global i32 0
define i64 @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 1
%13 = bitcast i64* %12 to i8*
%14 = load i32, i32* @F_GETLK
%15 = call i64 @fcntl(i32 %0, i32 %14, %struct.flock* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load i32, i32* %8
%21 = load i32, i32* @F_UNLCK
%22 = icmp eq i32 %20, %21
%23 = load i64, i64* %12
%24 = select i1 %22, i64 0, i64 %23
ret i64 %24
}