#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_pid;
  void *l_len;
  void *l_start;
};
typedef int pid_t;
typedef void *off_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ printf(char *, int);
pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len) {
  struct flock lock;
  int ret;
  lock.l_type = type;
  lock.l_start = offset;
  lock.l_whence = whence;
  lock.l_len = len;
  ret = fcntl(fd, F_GETLK, &lock);
  if (ret < 0) {
    printf("lock_test: fcntl F_GETLK call returned %d\n", ret);
    return ret;
  }
  if (lock.l_type == F_UNLCK)
    return 0;
  return (lock.l_pid == 0) ? 1 : lock.l_pid;
}


x86 assembly (clang, -O0):
<lock_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1191 <lock_test+0x71>
mov    -0x4c(%rbp),%esi
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <lock_test+0xb2>
mov    -0x48(%rbp),%eax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%eax
jne    11af <lock_test+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <lock_test+0xb2>
cmpl   $0x0,-0x40(%rbp)
jne    11c6 <lock_test+0xa6>
mov    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    11cc <lock_test+0xac>
mov    -0x40(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_test>:
push   %rbx
sub    $0x20,%rsp
mov    %esi,(%rsp)
mov    %rdx,0x18(%rsp)
mov    %ecx,0x4(%rsp)
mov    %r8,0x10(%rsp)
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
js     116b <lock_test+0x4b>
mov    (%rsp),%eax
mov    0x2e8b(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%eax
je     117b <lock_test+0x5b>
mov    0x8(%rsp),%eax
test   %eax,%eax
mov    $0x1,%ebx
cmovne %eax,%ebx
jmp    117b <lock_test+0x5b>
mov    %eax,%ebx
lea    0xe8c(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@F_GETLK = global i32 0
@.str = [43 x i8] c"lock_test: fcntl F_GETLK call returned %d\0A\00"
@F_UNLCK = global i32 0
define i32 @lock_test(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.flock
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%14 = load i32, i32* %8
%15 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i8*, i8** %9
%17 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 4
store i8* %16, i8** %17
%18 = load i32, i32* %10
%19 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i8*, i8** %11
%21 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 3
store i8* %20, i8** %21
%22 = load i32, i32* %7
%23 = load i32, i32* @F_GETLK
%24 = call i32 @fcntl(i32 %22, i32 %23, %struct.flock* %12)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %5
%28 = load i32, i32* %13
%29 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %13
store i32 %30, i32* %6
br label %47
31: ; preds = %5
%32 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i32, i32* @F_UNLCK
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 0, i32* %6
br label %47
37: ; preds = %31
%38 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 2
%39 = load i32, i32* %38
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
br label %45
42: ; preds = %37
%43 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 2
%44 = load i32, i32* %43
br label %45
45: ; preds = %42, %41
%46 = phi i32 [ 1, %41 ], [ %44, %42 ]
store i32 %46, i32* %6
br label %47
47: ; preds = %45, %36, %27
%48 = load i32, i32* %6
ret i32 %48
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@F_GETLK = global i32 0
@.str = [43 x i8] c"lock_test: fcntl F_GETLK call returned %d\0A\00"
@F_UNLCK = global i32 0
define i32 @lock_test(i32 %0, i32 %1, i8* %2, i32 %3, i8* %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 4
store i8* %2, i8** %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 1
store i32 %3, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
store i8* %4, i8** %11
%12 = load i32, i32* @F_GETLK
%13 = call i32 @fcntl(i32 %0, i32 %12, %struct.flock* %6)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %26
17: ; preds = %5
%18 = load i32, i32* %8
%19 = load i32, i32* @F_UNLCK
%20 = icmp eq i32 %18, %19
br i1 %20, label %26, label %21
21: ; preds = %17
%22 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
%25 = select i1 %24, i32 1, i32 %23
br label %26
26: ; preds = %17, %21, %15
%27 = phi i32 [ %13, %15 ], [ %25, %21 ], [ 0, %17 ]
ret i32 %27
}