#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_start;
  int l_len;
  int l_pid;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int fcntl(int, int /*<<< orphan*/, struct flock *);
pid_t regionIsLocked(int fd, int type, int whence, int start, int len) {
  struct flock fl;
  fl.l_type = type;
  fl.l_whence = whence;
  fl.l_start = start;
  fl.l_len = len;
  if (fcntl(fd, F_GETLK, &fl) == -1)
    return -1;
  return (fl.l_type == F_UNLCK) ? 0 : fl.l_pid;
}


x86 assembly (clang, -O0):
<regionIsLocked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    116a <regionIsLocked+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <regionIsLocked+0x82>
mov    -0x30(%rbp),%eax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%eax
jne    1186 <regionIsLocked+0x76>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    118c <regionIsLocked+0x7c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<regionIsLocked>:
sub    $0x18,%rsp
mov    %esi,(%rsp)
mov    %edx,0x4(%rsp)
mov    %ecx,0x8(%rsp)
mov    %r8d,0xc(%rsp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1153 <regionIsLocked+0x43>
mov    (%rsp),%ecx
mov    0x2e9c(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     114e <regionIsLocked+0x3e>
mov    0x10(%rsp),%eax
add    $0x18,%rsp
ret
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

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

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