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

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ EINTR;
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ SEEK_SET;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *,
                           scalar_t__);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int _is_locked(char *path) {
  int ret, fd;
  struct flock flk;
  memset(&flk, 0, sizeof(flk));
  fd = open(path, O_RDONLY);
  if (0 > fd) {
    if (EEXIST == errno) {
      return 0;
    } else {
      fprintf(stderr, "Error opening file %s: %s (%d)\n", path, strerror(errno),
              errno);
      return -1;
    }
  }
  do {
    if (0 == (ret = fcntl(fd, F_GETLK, &flk))) {
      break;
    }
  } while (EINTR == errno);
  if (ret) {
    fprintf(stderr, "Error getting file lock information for %s: %s (%d)\n",
            path, strerror(errno), errno);
    return -1;
  }
  if (F_WRLCK == flk.l_type && SEEK_SET == flk.l_whence) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_is_locked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jle    1201 <_is_locked+0xb1>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <_is_locked+0x62>
movl   $0x0,-0x4(%rbp)
jmp    12e2 <_is_locked+0x192>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
mov    0x2dda(%rip),%rax
mov    (%rax),%r8
lea    0xe10(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <_is_locked+0x192>
jmp    1206 <_is_locked+0xb6>
mov    -0x18(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jne    122f <_is_locked+0xdf>
jmp    124e <_is_locked+0xfe>
jmp    1234 <_is_locked+0xe4>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%rax
je     1206 <_is_locked+0xb6>
cmpl   $0x0,-0x14(%rbp)
je     12a7 <_is_locked+0x157>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
mov    0x2d34(%rip),%rax
mov    (%rax),%r8
lea    0xd8a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <_is_locked+0x192>
mov    0x2cfa(%rip),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jne    12db <_is_locked+0x18b>
mov    0x2cf6(%rip),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jne    12db <_is_locked+0x18b>
movl   $0x1,-0x4(%rbp)
jmp    12e2 <_is_locked+0x192>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_is_locked>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e7 <_is_locked+0x97>
mov    %eax,%ebx
mov    0x2e4e(%rip),%r12
lea    0x8(%rsp),%rbp
mov    0x2e12(%rip),%r13
mov    0x2e1b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     1229 <_is_locked+0xd9>
mov    (%r15),%rdi
cmp    %rdi,0x0(%r13)
je     11b0 <_is_locked+0x60>
mov    0x2df6(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
mov    (%r15),%r8
lea    0xe3d(%rip),%rsi
mov    %ebx,%edi
jmp    1217 <_is_locked+0xc7>
mov    0x2de2(%rip),%rax
mov    0x2dcb(%rip),%rbx
mov    (%rbx),%rdi
cmp    %rdi,(%rax)
je     1252 <_is_locked+0x102>
mov    0x2dc4(%rip),%rax
mov    (%rax),%ebp
call   1070 <strerror@plt>
mov    (%rbx),%r8
lea    0xdeb(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1252 <_is_locked+0x102>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
cmp    0x8(%rsp),%rax
jne    1250 <_is_locked+0x100>
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
mov    $0x1,%ebp
cmp    0x10(%rsp),%rax
je     1252 <_is_locked+0x102>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64 }
@O_RDONLY = global i32 0
@EEXIST = global i64 0
@errno = global i64 0
@stderr = global i32 0
@.str = [32 x i8] c"Error opening file %s: %s (%d)\0A\00"
@F_GETLK = global i32 0
@EINTR = global i64 0
@.str.1 = [53 x i8] c"Error getting file lock information for %s: %s (%d)\0A\00"
@F_WRLCK = global i64 0
@SEEK_SET = global i64 0
define i32 @_is_locked(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.flock
store i8* %0, i8** %3
%7 = call i32 @memset(%struct.flock* %6, i32 0, i32 16)
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp sgt i32 0, %11
br i1 %12, label %13, label %25
13: ; preds = %1
%14 = load i64, i64* @EEXIST
%15 = load i64, i64* @errno
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 0, i32* %2
br label %59
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = load i64, i64* @errno
%22 = call i8* @strerror(i64 %21)
%23 = load i64, i64* @errno
%24 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %22, i64 %23)
store i32 -1, i32* %2
br label %59
25: ; preds = %1
br label %26
26: ; preds = %33, %25
%27 = load i32, i32* %5
%28 = load i32, i32* @F_GETLK
%29 = call i32 @fcntl(i32 %27, i32 %28, %struct.flock* %6)
store i32 %29, i32* %4
%30 = icmp eq i32 0, %29
br i1 %30, label %31, label %32
31: ; preds = %26
br label %37
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = load i64, i64* @EINTR
%35 = load i64, i64* @errno
%36 = icmp eq i64 %34, %35
br i1 %36, label %26, label %37
37: ; preds = %33, %31
%38 = load i32, i32* %4
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %3
%43 = load i64, i64* @errno
%44 = call i8* @strerror(i64 %43)
%45 = load i64, i64* @errno
%46 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %44, i64 %45)
store i32 -1, i32* %2
br label %59
47: ; preds = %37
%48 = load i64, i64* @F_WRLCK
%49 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
%50 = load i64, i64* %49
%51 = icmp eq i64 %48, %50
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = load i64, i64* @SEEK_SET
%54 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
%55 = load i64, i64* %54
%56 = icmp eq i64 %53, %55
br i1 %56, label %57, label %58
57: ; preds = %52
store i32 1, i32* %2
br label %59
58: ; preds = %52, %47
store i32 0, i32* %2
br label %59
59: ; preds = %58, %57, %40, %18, %17
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.flock = type { i64, i64 }
@O_RDONLY = global i32 0
@EEXIST = global i64 0
@errno = global i64 0
@stderr = global i32 0
@.str = [32 x i8] c"Error opening file %s: %s (%d)\0A\00"
@F_GETLK = global i32 0
@EINTR = global i64 0
@.str.1 = [53 x i8] c"Error getting file lock information for %s: %s (%d)\0A\00"
@F_WRLCK = global i64 0
@SEEK_SET = global i64 0
define i32 @_is_locked(i8* %0) {
%2 = alloca %struct.flock
%3 = bitcast %struct.flock* %2 to i8*
%4 = call i32 @memset(%struct.flock* %2, i32 0, i32 16)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = load i64, i64* @EEXIST
%10 = load i64, i64* @errno
%11 = icmp eq i64 %9, %10
br i1 %11, label %41, label %12
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = call i8* @strerror(i64 %10)
%15 = load i64, i64* @errno
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %14, i64 %15)
br label %41
17: ; preds = %1, %21
%18 = load i32, i32* @F_GETLK
%19 = call i32 @fcntl(i32 %6, i32 %18, %struct.flock* %2)
%20 = icmp eq i32 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %17
%22 = load i64, i64* @EINTR
%23 = load i64, i64* @errno
%24 = icmp eq i64 %22, %23
br i1 %24, label %17, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i8* @strerror(i64 %23)
%28 = load i64, i64* @errno
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %27, i64 %28)
br label %41
30: ; preds = %17
%31 = load i64, i64* @F_WRLCK
%32 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
%33 = load i64, i64* %32
%34 = icmp eq i64 %31, %33
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i64, i64* @SEEK_SET
%37 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%38 = load i64, i64* %37
%39 = icmp eq i64 %36, %38
br i1 %39, label %41, label %40
40: ; preds = %35, %30
br label %41
41: ; preds = %35, %8, %40, %25, %12
%42 = phi i32 [ -1, %12 ], [ -1, %25 ], [ 0, %40 ], [ 0, %8 ], [ 1, %35 ]
ret i32 %42
}