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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int check_lock(int fd, int offset, int len) {
  struct flock lock;
  int ret;
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = offset;
  lock.l_len = len;
  lock.l_pid = 0;
  ret = fcntl(fd, F_GETLK, &lock);
  if (ret != 0) {
    printf("error calling fcntl F_GETLCK: %s\n", strerror(errno));
    return -1;
  }
  if (lock.l_type == F_UNLCK) {
    return 0;
  }
  printf("check_lock failed: lock held: "
         "pid='%d', type='%d', start='%d', len='%d'\n",
         (int)lock.l_pid, (int)lock.l_type, (int)lock.l_start, (int)lock.l_len);
  return 1;
}


x86 assembly (clang, -O0):
<check_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11bc <check_lock+0x8c>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <check_lock+0xd4>
mov    -0x30(%rbp),%rax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%rax
jne    11dc <check_lock+0xac>
movl   $0x0,-0x4(%rbp)
jmp    1204 <check_lock+0xd4>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%r8d
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_lock>:
sub    $0x28,%rsp
mov    0x2e85(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %esi,0x10(%rsp)
mov    %edx,0x14(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     11a1 <check_lock+0x71>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe73(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
ret
mov    0x8(%rsp),%rdx
mov    0x2e33(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%rdx
je     11d4 <check_lock+0xa4>
mov    0x18(%rsp),%esi
mov    0x10(%rsp),%ecx
mov    0x14(%rsp),%r8d
lea    0xe5a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i64, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str = [34 x i8] c"error calling fcntl F_GETLCK: %s\0A\00"
@errno = global i32 0
@F_UNLCK = global i64 0
@.str.1 = [73 x i8] c"check_lock failed: lock held: pid='%d', type='%d', start='%d', len='%d'\0A\00"
define i32 @check_lock(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.flock
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i64, i64* @F_WRLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i64 %10, i64* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* %6
%15 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* %7
%17 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i64 0, i64* %18
%19 = load i32, i32* %5
%20 = load i32, i32* @F_GETLK
%21 = call i32 @fcntl(i32 %19, i32 %20, %struct.flock* %8)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %3
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %26)
store i32 -1, i32* %4
br label %46
28: ; preds = %3
%29 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @F_UNLCK
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 0, i32* %4
br label %46
34: ; preds = %28
%35 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i32
%38 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
%41 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
%44 = load i32, i32* %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %40, i32 %42, i32 %44)
store i32 1, i32* %4
br label %46
46: ; preds = %34, %33, %24
%47 = load i32, i32* %4
ret i32 %47
}

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