#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 /*<<< orphan*/ l_type;
  int /*<<< orphan*/ l_pid;
  int /*<<< orphan*/ l_len;
  int /*<<< orphan*/ l_whence;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EACCES;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ F_GETLK;
#define F_LOCK 131
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
#define F_TEST 130
#define F_TLOCK 129
#define F_ULOCK 128
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int lockf(int fd, int cmd, off_t len) {
  struct flock fl = {.l_type = F_WRLCK, .l_whence = SEEK_CUR, .l_len = len};
  switch (cmd) {
  case F_TEST:
    fl.l_type = F_RDLCK;
    if (fcntl(fd, F_GETLK, &fl) < 0)
      return -1;
    if (fl.l_type == F_UNLCK || fl.l_pid == getpid())
      return 0;
    errno = EACCES;
    return -1;
  case F_ULOCK:
    fl.l_type = F_UNLCK;
    return fcntl(fd, F_SETLK, &fl);
  case F_LOCK:
    return fcntl(fd, F_SETLKW, &fl);
  case F_TLOCK:
    return fcntl(fd, F_SETLK, &fl);
  }
  errno = EINVAL;
  return -1;
}


x86 assembly (clang, -O0):
<lockf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x3,%eax
ja     126e <lockf+0x14e>
mov    -0x28(%rbp),%rax
lea    0xe8a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11b5 <lockf+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <lockf+0x167>
mov    -0x20(%rbp),%eax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%eax
je     11e1 <lockf+0xc1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jne    11ed <lockf+0xcd>
movl   $0x0,-0x4(%rbp)
jmp    1287 <lockf+0x167>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ecx
mov    0x2db3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <lockf+0x167>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    1287 <lockf+0x167>
mov    -0x8(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    1287 <lockf+0x167>
mov    -0x8(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    1287 <lockf+0x167>
mov    0x2d33(%rip),%rax
mov    (%rax),%ecx
mov    0x2d32(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockf>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    %edx,0x10(%rsp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
add    $0xffffff80,%esi
cmp    $0x3,%esi
ja     117a <lockf+0x5a>
lea    0xea5(%rip),%rax
movslq (%rax,%rsi,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e60(%rip),%rax
jmp    11ee <lockf+0xce>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    0x2e26(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    11fc <lockf+0xdc>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11fc <lockf+0xdc>
mov    0x8(%rsp),%eax
mov    0x2e01(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%eax
je     11fc <lockf+0xdc>
mov    0xc(%rsp),%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %eax,%ebp
je     11fc <lockf+0xdc>
mov    0x2ddb(%rip),%rax
jmp    1181 <lockf+0x61>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_CUR = global i32 0
@F_RDLCK = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i32 0
@EACCES = global i32 0
@errno = global i32 0
@F_SETLK = global i32 0
@F_SETLKW = global i32 0
@EINVAL = global i32 0
define i32 @lockf(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.flock
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%10 = load i32, i32* @F_WRLCK
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
%13 = load i32, i32* %7
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
%15 = load i32, i32* @SEEK_CUR
store i32 %15, i32* %14
%16 = load i32, i32* %6
switch i32 %16, label %52 [
i32 130, label %17
i32 128, label %38
i32 131, label %44
i32 129, label %48
]
17: ; preds = %3
%18 = load i32, i32* @F_RDLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %5
%21 = load i32, i32* @F_GETLK
%22 = call i32 @fcntl(i32 %20, i32 %21, %struct.flock* %8)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %4
br label %54
25: ; preds = %17
%26 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @F_UNLCK
%29 = icmp eq i32 %27, %28
br i1 %29, label %35, label %30
30: ; preds = %25
%31 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 (...) @getpid()
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %30, %25
store i32 0, i32* %4
br label %54
36: ; preds = %30
%37 = load i32, i32* @EACCES
store i32 %37, i32* @errno
store i32 -1, i32* %4
br label %54
38: ; preds = %3
%39 = load i32, i32* @F_UNLCK
%40 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %5
%42 = load i32, i32* @F_SETLK
%43 = call i32 @fcntl(i32 %41, i32 %42, %struct.flock* %8)
store i32 %43, i32* %4
br label %54
44: ; preds = %3
%45 = load i32, i32* %5
%46 = load i32, i32* @F_SETLKW
%47 = call i32 @fcntl(i32 %45, i32 %46, %struct.flock* %8)
store i32 %47, i32* %4
br label %54
48: ; preds = %3
%49 = load i32, i32* %5
%50 = load i32, i32* @F_SETLK
%51 = call i32 @fcntl(i32 %49, i32 %50, %struct.flock* %8)
store i32 %51, i32* %4
br label %54
52: ; preds = %3
%53 = load i32, i32* @EINVAL
store i32 %53, i32* @errno
store i32 -1, i32* %4
br label %54
54: ; preds = %52, %48, %44, %38, %36, %35, %24
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_CUR = global i32 0
@F_RDLCK = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i32 0
@EACCES = global i32 0
@errno = global i32 0
@F_SETLK = global i32 0
@F_SETLKW = global i32 0
@EINVAL = global i32 0
define i32 @lockf(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.flock
%5 = bitcast %struct.flock* %4 to i8*
%6 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
%7 = load i32, i32* @F_WRLCK
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
store i32 %2, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
%11 = load i32, i32* @SEEK_CUR
store i32 %11, i32* %10
switch i32 %1, label %37 [
i32 130, label %12
i32 128, label %27
i32 131, label %31
i32 129, label %34
]
12: ; preds = %3
%13 = load i32, i32* @F_RDLCK
store i32 %13, i32* %6
%14 = load i32, i32* @F_GETLK
%15 = call i32 @fcntl(i32 %0, i32 %14, %struct.flock* %4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %39, label %17
17: ; preds = %12
%18 = load i32, i32* %6
%19 = load i32, i32* @F_UNLCK
%20 = icmp eq i32 %18, %19
br i1 %20, label %39, label %21
21: ; preds = %17
%22 = load i32, i32* %8
%23 = call i32 (...) @getpid()
%24 = icmp eq i32 %22, %23
br i1 %24, label %39, label %25
25: ; preds = %21
%26 = load i32, i32* @EACCES
store i32 %26, i32* @errno
br label %39
27: ; preds = %3
%28 = load i32, i32* @F_UNLCK
store i32 %28, i32* %6
%29 = load i32, i32* @F_SETLK
%30 = call i32 @fcntl(i32 %0, i32 %29, %struct.flock* %4)
br label %39
31: ; preds = %3
%32 = load i32, i32* @F_SETLKW
%33 = call i32 @fcntl(i32 %0, i32 %32, %struct.flock* %4)
br label %39
34: ; preds = %3
%35 = load i32, i32* @F_SETLK
%36 = call i32 @fcntl(i32 %0, i32 %35, %struct.flock* %4)
br label %39
37: ; preds = %3
%38 = load i32, i32* @EINVAL
store i32 %38, i32* @errno
br label %39
39: ; preds = %17, %21, %12, %37, %34, %31, %27, %25
%40 = phi i32 [ -1, %37 ], [ %36, %34 ], [ %33, %31 ], [ %30, %27 ], [ -1, %25 ], [ -1, %12 ], [ 0, %21 ], [ 0, %17 ]
ret i32 %40
}