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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int tsd_fltest(int fd, pid_t *pid) {
  struct flock lock;
  memset(&lock, 0, sizeof lock);
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  if (fcntl(fd, F_GETLK, &lock) == -1)
    return (-1);
  if (lock.l_type == F_UNLCK)
    return (0);
  if (pid != NULL)
    *pid = lock.l_pid;
  return (1);
}


x86 assembly (clang, -O0):
<tsd_fltest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1183 <tsd_fltest+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <tsd_fltest+0x9e>
mov    -0x20(%rbp),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
jne    11a3 <tsd_fltest+0x83>
movl   $0x0,-0x4(%rbp)
jmp    11be <tsd_fltest+0x9e>
cmpq   $0x0,-0x10(%rbp)
je     11b7 <tsd_fltest+0x97>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tsd_fltest>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x8(%rsp),%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     119d <tsd_fltest+0x7d>
mov    0x8(%rsp),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
je     11a2 <tsd_fltest+0x82>
mov    $0x1,%ebx
test   %r14,%r14
je     11a2 <tsd_fltest+0x82>
mov    0x10(%rsp),%eax
mov    %eax,(%r14)
jmp    11a2 <tsd_fltest+0x82>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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