#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;
  scalar_t__ l_whence;
  long l_start;
  long l_len;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
scalar_t__ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/, long, long);
int /*<<< orphan*/ str_type(int);
int try_fcntl(int /*<<< orphan*/, struct flock *);
void lock_check(int type, long int start, long int len, int conflict_type,
                long int conflict_start, long int conflict_len) {
  assert(conflict_type != F_UNLCK);
  struct flock fl = {
      .l_type = type,
      .l_whence = SEEK_SET,
      .l_start = start,
      .l_len = len,
  };
  int ret = try_fcntl(F_GETLK, &fl);
  if (ret == -1)
    err(1, "fcntl");
  if (fl.l_type != conflict_type || fl.l_whence != SEEK_SET ||
      fl.l_start != conflict_start || fl.l_len != conflict_len) {
    errx(1, "F_GETLK returned wrong lock; expected {%s, SEEK_SET, %ld, %ld)",
         str_type(conflict_type), conflict_start, conflict_len);
  }
}


x86 assembly (clang, -O0):
<lock_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
call   1040 <try_fcntl@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11db <lock_check+0x8b>
mov    $0x1,%edi
lea    0xe2a(%rip),%rsi
call   1070 <err@plt>
mov    -0x50(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1217 <lock_check+0xc7>
mov    -0x48(%rbp),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <lock_check+0xc7>
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1217 <lock_check+0xc7>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     123a <lock_check+0xea>
mov    -0x1c(%rbp),%edi
call   1050 <str_type@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    $0x1,%edi
lea    0xdd1(%rip),%rsi
call   1030 <errx@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_check>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2e6a(%rip),%rax
xor    %edi,%edi
cmp    %ecx,(%rax)
setne  %dil
call   1060 <assert@plt>
mov    %ebx,0x8(%rsp)
mov    0x2e42(%rip),%rbx
mov    (%rbx),%rax
mov    %rax,0x10(%rsp)
mov    %rbp,0x18(%rsp)
mov    %r13,0x20(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <try_fcntl@plt>
cmp    $0xffffffff,%eax
je     11c1 <lock_check+0x71>
cmp    %r12d,0x8(%rsp)
je     11d9 <lock_check+0x89>
jmp    11f1 <lock_check+0xa1>
lea    0xe38(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
cmp    %r12d,0x8(%rsp)
jne    11f1 <lock_check+0xa1>
mov    0x10(%rsp),%rax
cmp    (%rbx),%rax
jne    11f1 <lock_check+0xa1>
cmp    %r15,0x18(%rsp)
jne    11f1 <lock_check+0xa1>
cmp    %r14,0x20(%rsp)
je     1212 <lock_check+0xc2>
mov    %r12d,%edi
call   1050 <str_type@plt>
lea    0xe06(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
mov    %r15,%rcx
mov    %r14,%r8
call   1030 <errx@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i64 }
@F_UNLCK = global i32 0
@SEEK_SET = global i64 0
@F_GETLK = global i32 0
@.str = [6 x i8] c"fcntl\00"
@.str.1 = [63 x i8] c"F_GETLK returned wrong lock; expected {%s, SEEK_SET, %ld, %ld)\00"
define void @lock_check(i32 %0, i64 %1, i64 %2, i32 %3, i64 %4, i64 %5) {
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca %struct.flock
%14 = alloca i32
store i32 %0, i32* %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i64 %4, i64* %11
store i64 %5, i64* %12
%15 = load i32, i32* %10
%16 = load i32, i32* @F_UNLCK
%17 = icmp ne i32 %15, %16
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 0
%21 = load i32, i32* %7
store i32 %21, i32* %20
%22 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 1
%23 = load i64, i64* @SEEK_SET
store i64 %23, i64* %22
%24 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 2
%25 = load i64, i64* %8
store i64 %25, i64* %24
%26 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 3
%27 = load i64, i64* %9
store i64 %27, i64* %26
%28 = load i32, i32* @F_GETLK
%29 = call i32 @try_fcntl(i32 %28, %struct.flock* %13)
store i32 %29, i32* %14
%30 = load i32, i32* %14
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %6
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %6
%35 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* %10
%38 = icmp ne i32 %36, %37
br i1 %38, label %54, label %39
39: ; preds = %34
%40 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 1
%41 = load i64, i64* %40
%42 = load i64, i64* @SEEK_SET
%43 = icmp ne i64 %41, %42
br i1 %43, label %54, label %44
44: ; preds = %39
%45 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 2
%46 = load i64, i64* %45
%47 = load i64, i64* %11
%48 = icmp ne i64 %46, %47
br i1 %48, label %54, label %49
49: ; preds = %44
%50 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 3
%51 = load i64, i64* %50
%52 = load i64, i64* %12
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %60
54: ; preds = %49, %44, %39, %34
%55 = load i32, i32* %10
%56 = call i32 @str_type(i32 %55)
%57 = load i64, i64* %11
%58 = load i64, i64* %12
%59 = call i32 @errx(i32 1, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %56, i64 %57, i64 %58)
br label %60
60: ; preds = %54, %49
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i64 }
@F_UNLCK = global i32 0
@SEEK_SET = global i64 0
@F_GETLK = global i32 0
@.str = [6 x i8] c"fcntl\00"
@.str.1 = [63 x i8] c"F_GETLK returned wrong lock; expected {%s, SEEK_SET, %ld, %ld)\00"
define void @lock_check(i32 %0, i64 %1, i64 %2, i32 %3, i64 %4, i64 %5) {
%7 = alloca %struct.flock
%8 = load i32, i32* @F_UNLCK
%9 = icmp ne i32 %8, %3
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = bitcast %struct.flock* %7 to i8*
%13 = getelementptr inbounds %struct.flock, %struct.flock* %7, i64 0, i32 0
store i32 %0, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %7, i64 0, i32 1
%15 = load i64, i64* @SEEK_SET
store i64 %15, i64* %14
%16 = getelementptr inbounds %struct.flock, %struct.flock* %7, i64 0, i32 2
store i64 %1, i64* %16
%17 = getelementptr inbounds %struct.flock, %struct.flock* %7, i64 0, i32 3
store i64 %2, i64* %17
%18 = load i32, i32* @F_GETLK
%19 = call i32 @try_fcntl(i32 %18, %struct.flock* %7)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %6
%22 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %6
%24 = load i32, i32* %13
%25 = icmp eq i32 %24, %3
br i1 %25, label %26, label %36
26: ; preds = %23
%27 = load i64, i64* %14
%28 = load i64, i64* @SEEK_SET
%29 = icmp eq i64 %27, %28
%30 = load i64, i64* %16
%31 = icmp eq i64 %30, %4
%32 = select i1 %29, i1 %31, i1 false
%33 = load i64, i64* %17
%34 = icmp eq i64 %33, %5
%35 = select i1 %32, i1 %34, i1 false
br i1 %35, label %39, label %36
36: ; preds = %26, %23
%37 = call i32 @str_type(i32 %3)
%38 = call i32 @errx(i32 1, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %37, i64 %4, i64 %5)
br label %39
39: ; preds = %26, %36
ret void
}