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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *);
pid_t test_file_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;
  if (fcntl(fd, F_GETLK, &lock) < 0)
    log_err(errno, "Unable to test for file lock");
  if (lock.l_type == F_UNLCK)
    return (0);
  return (lock.l_pid);
}


x86 assembly (clang, -O0):
<test_file_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11a5 <test_file_lock+0x75>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <log_err@plt>
mov    -0x30(%rbp),%eax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jne    11c3 <test_file_lock+0x93>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <test_file_lock+0x99>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_file_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,0x8(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1196 <test_file_lock+0x66>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1050 <log_err@plt>
mov    0x8(%rsp),%ecx
mov    0x2e3f(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     11ab <test_file_lock+0x7b>
mov    0xc(%rsp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

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