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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *, int);
int /*<<< orphan*/ stderr;
int isLocked(int h, int type, unsigned int iOfst, unsigned int iCnt,
             const char *zType) {
  struct flock lk;
  memset(&lk, 0, sizeof(lk));
  lk.l_type = type;
  lk.l_whence = SEEK_SET;
  lk.l_start = iOfst;
  lk.l_len = iCnt;
  if (fcntl(h, F_GETLK, &lk) == (-1)) {
    fprintf(stderr, "fcntl(%d) failed: errno=%d\n", h, errno);
    exit(1);
  }
  if (lk.l_type == F_UNLCK)
    return 0;
  printf("%s lock held by %d\n", zType, (int)lk.l_pid);
  return 1;
}


x86 assembly (clang, -O0):
<isLocked>:
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    %r8,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11df <isLocked+0x8f>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
lea    0xe30(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x40(%rbp),%eax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%eax
jne    11fd <isLocked+0xad>
movl   $0x0,-0x4(%rbp)
jmp    121a <isLocked+0xca>
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isLocked>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,(%rsp)
mov    %ecx,%r13d
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r15d
lea    0x8(%rsp),%r14
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %ebp,0x8(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %ebx,0xc(%rsp)
mov    %r13d,0x10(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r14,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11f1 <isLocked+0xa1>
mov    0x8(%rsp),%eax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%eax
je     11df <isLocked+0x8f>
mov    0x18(%rsp),%edx
lea    0xe4c(%rip),%rdi
mov    (%rsp),%rsi
call   1040 <printf@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
lea    0xdf6(%rip),%rsi
mov    %r15d,%edx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i64, i32 }
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"fcntl(%d) failed: errno=%d\0A\00"
@errno = global i32 0
@F_UNLCK = global i32 0
@.str.1 = [20 x i8] c"%s lock held by %d\0A\00"
define i32 @isLocked(i32 %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%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 i8* %4, i8** %11
%13 = call i32 @memset(%struct.flock* %12, i32 0, i32 32)
%14 = load i32, i32* %8
%15 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @SEEK_SET
%17 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* %9
%19 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %10
%21 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %7
%23 = load i32, i32* @F_GETLK
%24 = call i32 @fcntl(i32 %22, i32 %23, %struct.flock* %12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %5
%27 = load i32, i32* @stderr
%28 = load i32, i32* %7
%29 = load i32, i32* @errno
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %5
%33 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* @F_UNLCK
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 0, i32* %6
br label %44
38: ; preds = %32
%39 = load i8*, i8** %11
%40 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 3
%41 = load i64, i64* %40
%42 = trunc i64 %41 to i32
%43 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %39, i32 %42)
store i32 1, i32* %6
br label %44
44: ; preds = %38, %37
%45 = load i32, i32* %6
ret i32 %45
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i64, i32 }
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"fcntl(%d) failed: errno=%d\0A\00"
@errno = global i32 0
@F_UNLCK = global i32 0
@.str.1 = [20 x i8] c"%s lock held by %d\0A\00"
define i32 @isLocked(i32 %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca %struct.flock
%7 = bitcast %struct.flock* %6 to i8*
%8 = call i32 @memset(%struct.flock* %6, i32 0, i32 32)
%9 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 0
store i32 %1, i32* %9
%10 = load i32, i32* @SEEK_SET
%11 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 4
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 1
store i32 %2, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
store i32 %3, i32* %13
%14 = load i32, i32* @F_GETLK
%15 = call i32 @fcntl(i32 %0, i32 %14, %struct.flock* %6)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %5
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %5
%23 = load i32, i32* %9
%24 = load i32, i32* @F_UNLCK
%25 = icmp eq i32 %23, %24
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
%28 = load i64, i64* %27
%29 = trunc i64 %28 to i32
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 %29)
br label %31
31: ; preds = %22, %26
%32 = phi i32 [ 1, %26 ], [ 0, %22 ]
ret i32 %32
}