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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_UNLCK;
int /*<<< orphan*/ err_sys(char *);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
pid_t lock_test(int fd, int type, off_t *offset, int *whence, off_t *len) {
  struct flock lock;
  lock.l_type = type;
  lock.l_start = *offset;
  lock.l_whence = *whence;
  lock.l_len = *len;
  if (fcntl(fd, F_GETLK, &lock) < 0)
    err_sys("fcntl error");
  if (lock.l_type == F_UNLCK)
    return 0;
  *offset = lock.l_start;
  *whence = lock.l_whence;
  *len = lock.l_len;
  return 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    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    118a <lock_test+0x6a>
lea    0xe7b(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x48(%rbp),%eax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%eax
jne    11a8 <lock_test+0x88>
movl   $0x0,-0x4(%rbp)
jmp    11cd <lock_test+0xad>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x44(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_test>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %esi,(%rsp)
mov    (%rdx),%rax
mov    %rax,0x18(%rsp)
mov    (%rcx),%eax
mov    %eax,0x4(%rsp)
mov    (%r8),%rax
mov    %rax,0x10(%rsp)
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <fcntl@plt>
test   %rax,%rax
jns    116d <lock_test+0x4d>
lea    0xe98(%rip),%rdi
call   1030 <err_sys@plt>
mov    (%rsp),%ecx
mov    0x2e69(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     1198 <lock_test+0x78>
mov    0x18(%rsp),%rax
mov    %rax,(%rbx)
mov    0x4(%rsp),%eax
mov    %eax,(%r15)
mov    0x10(%rsp),%rax
mov    %rax,(%r14)
mov    0x8(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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