#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*/ exit(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "fcntl error");
    exit(1);
  }
  if (lock.l_type == F_UNLCK)
    return (0);
  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    %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    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11a3 <lock_test+0x73>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x48(%rbp),%eax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%eax
jne    11c1 <lock_test+0x91>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <lock_test+0x97>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_test>:
sub    $0x28,%rsp
mov    %esi,0x8(%rsp)
mov    %rdx,0x20(%rsp)
mov    %ecx,0xc(%rsp)
mov    %r8,0x18(%rsp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
js     1178 <lock_test+0x48>
mov    0x8(%rsp),%ecx
mov    0x2e77(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     1173 <lock_test+0x43>
mov    0x10(%rsp),%eax
add    $0x28,%rsp
ret
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@F_GETLK = global i32 0
@stderr = 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 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 4
store i8* %15, i8** %16
%17 = load i32, i32* %10
%18 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i8*, i8** %11
%20 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 3
store i8* %19, i8** %20
%21 = load i32, i32* %7
%22 = load i32, i32* @F_GETLK
%23 = call i64 @fcntl(i32 %21, i32 %22, %struct.flock* %12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %5
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %5
%30 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @F_UNLCK
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 0, i32* %6
br label %38
35: ; preds = %29
%36 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 2
%37 = load i32, i32* %36
store i32 %37, i32* %6
br label %38
38: ; preds = %35, %34
%39 = load i32, i32* %6
ret i32 %39
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i8*, i8* }
@F_GETLK = global i32 0
@stderr = 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 %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 i64 @fcntl(i32 %0, i32 %12, %struct.flock* %6)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %5
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %5
%20 = load i32, i32* %8
%21 = load i32, i32* @F_UNLCK
%22 = icmp eq i32 %20, %21
%23 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
%24 = load i32, i32* %23
%25 = select i1 %22, i32 0, i32 %24
ret i32 %25
}