#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 l_pid;
  void *l_len;
  void *l_start;
};
typedef void *off_t;

/* Variables and functions */
int F_UNLCK;
scalar_t__ fcntl(int, int, struct flock *);
int lock_test(int fd, int cmd, int type, off_t start, int whence, off_t len) {
  struct flock lock;
  lock.l_type = type;
  lock.l_start = start;
  lock.l_whence = whence;
  lock.l_len = len;
  if (fcntl(fd, cmd, &lock) < 0)
    return -1;
  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    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x10(%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    -0xc(%rbp),%esi
lea    -0x48(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    116e <lock_test+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <lock_test+0x82>
mov    -0x48(%rbp),%eax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%eax
jne    118c <lock_test+0x7c>
movl   $0x0,-0x4(%rbp)
jmp    1192 <lock_test+0x82>
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    %edx,0x8(%rsp)
mov    %rcx,0x20(%rsp)
mov    %r8d,0xc(%rsp)
mov    %r9,0x18(%rsp)
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
js     1150 <lock_test+0x40>
mov    0x8(%rsp),%ecx
mov    0x2ea7(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     114b <lock_test+0x3b>
mov    0x10(%rsp),%eax
add    $0x28,%rsp
ret
mov    $0xffffffff,%eax
add    $0x28,%rsp
ret

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

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