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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int /*<<< orphan*/ err_sys(char *);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len) {
  struct flock lock;
  lock.l_type = type;
  lock.l_start = offset;
  lock.l_whence = whence;
  lock.l_len = len;
  if (fcntl(fd, F_GETLK, &lock) < 0)
    err_sys("fcntl error");
  if (lock.l_type == F_UNLCK)
    return (0);
  return (lock.l_pid);
}


x86 assembly (clang, -O0):
<lock_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    1180 <lock_test+0x60>
lea    0xe85(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x48(%rbp),%eax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%eax
jne    119e <lock_test+0x7e>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <lock_test+0x84>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_test>:
sub    $0x28,%rsp
mov    %esi,0x8(%rsp)
mov    %rdx,0x20(%rsp)
mov    %ecx,0xc(%rsp)
mov    %r8,0x18(%rsp)
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <fcntl@plt>
test   %rax,%rax
jns    115a <lock_test+0x3a>
lea    0xeab(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x8(%rsp),%ecx
mov    0x2e7b(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     116f <lock_test+0x4f>
mov    0x10(%rsp),%eax
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@F_GETLK = global i32 0
@.str = [12 x i8] c"fcntl error\00"
@F_UNLCK = global i32 0
define i32 @lock_test(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.flock
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%13 = load i32, i32* %8
%14 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i8*, i8** %9
%16 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 4
store i8* %15, i8** %16
%17 = load i32, i32* %10
%18 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i8*, i8** %11
%20 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 3
store i8* %19, i8** %20
%21 = load i32, i32* %7
%22 = load i32, i32* @F_GETLK
%23 = call i64 @fcntl(i32 %21, i32 %22, %struct.flock* %12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %5
%26 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %5
%28 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* @F_UNLCK
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 0, i32* %6
br label %36
33: ; preds = %27
%34 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 2
%35 = load i32, i32* %34
store i32 %35, i32* %6
br label %36
36: ; preds = %33, %32
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@F_GETLK = global i32 0
@.str = [12 x i8] c"fcntl error\00"
@F_UNLCK = global i32 0
define i32 @lock_test(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca %struct.flock
%7 = bitcast %struct.flock* %6 to i8*
%8 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 0
store i32 %1, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 4
store i8* %2, i8** %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 1
store i32 %3, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
store i8* %4, i8** %11
%12 = load i32, i32* @F_GETLK
%13 = call i64 @fcntl(i32 %0, i32 %12, %struct.flock* %6)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %5
%18 = load i32, i32* %8
%19 = load i32, i32* @F_UNLCK
%20 = icmp eq i32 %18, %19
%21 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
%22 = load i32, i32* %21
%23 = select i1 %20, i32 0, i32 %22
ret i32 %23
}