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

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ F_GETLK;
int F_LOCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int F_TEST;
int F_TLOCK;
int F_ULOCK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
scalar_t__ getpid();
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int lockf(int fd, int cmd, off_t len) {
  struct flock lock;
  memset(&lock, 0, sizeof(struct flock));
  lock.l_whence = SEEK_CUR;
  lock.l_start = 0;
  lock.l_len = len;
  lock.l_type = F_WRLCK;
  if (cmd == F_LOCK) {
    return fcntl(fd, F_SETLKW, &lock);
  } else if (cmd == F_TLOCK) {
    return fcntl(fd, F_SETLK, &lock);
  } else if (cmd == F_ULOCK) {
    lock.l_type = F_UNLCK;
    return fcntl(fd, F_SETLK, &lock);
  } else if (cmd == F_TEST) {
    if (fcntl(fd, F_GETLK, &lock) != 0) {
      return -1;
    };
    if (lock.l_type == F_UNLCK) {
      return 0;
    };
    if (lock.l_pid == getpid()) {
      return 0;
    };
    errno = EAGAIN;
    return -1;
  } else {
    errno = EINVAL;
    return -1;
  };
}


x86 assembly (clang, -O0):
<lockf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%eax
jne    11a8 <lockf+0x78>
mov    -0x8(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    12d3 <lockf+0x1a3>
mov    -0xc(%rbp),%eax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%eax
jne    11d7 <lockf+0xa7>
mov    -0x8(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    12d3 <lockf+0x1a3>
mov    -0xc(%rbp),%eax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%eax
jne    1214 <lockf+0xe4>
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    12d3 <lockf+0x1a3>
mov    -0xc(%rbp),%eax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%eax
jne    12ba <lockf+0x18a>
mov    -0x8(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     1251 <lockf+0x121>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <lockf+0x1a3>
mov    -0x38(%rbp),%rax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%rax
jne    1271 <lockf+0x141>
movl   $0x0,-0x4(%rbp)
jmp    12d3 <lockf+0x1a3>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jne    129c <lockf+0x16c>
movl   $0x0,-0x4(%rbp)
jmp    12d3 <lockf+0x1a3>
mov    0x2cd5(%rip),%rax
mov    (%rax),%ecx
mov    0x2cec(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <lockf+0x1a3>
mov    0x2ccf(%rip),%rax
mov    (%rax),%ecx
mov    0x2cce(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    %ebx,0x18(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e48(%rip),%rax
cmp    %ebp,(%rax)
jne    118d <lockf+0x5d>
mov    0x2e45(%rip),%rax
jmp    11b9 <lockf+0x89>
mov    0x2e5c(%rip),%rax
cmp    %ebp,(%rax)
je     11b2 <lockf+0x82>
mov    0x2e39(%rip),%rax
cmp    %ebp,(%rax)
jne    11d6 <lockf+0xa6>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1030 <fcntl@plt>
mov    %rax,%rbx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2ddb(%rip),%rax
cmp    %ebp,(%rax)
jne    1230 <lockf+0x100>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1030 <fcntl@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11cb <lockf+0x9b>
mov    0x8(%rsp),%rax
mov    0x2d9b(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     11cb <lockf+0x9b>
mov    0x10(%rsp),%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %rax,%rbp
je     11cb <lockf+0x9b>
mov    0x2d4a(%rip),%rax
jmp    1237 <lockf+0x107>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    0x2d58(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    11cb <lockf+0x9b>

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_WRLCK = global i64 0
@F_LOCK = global i32 0
@F_SETLKW = global i32 0
@F_TLOCK = global i32 0
@F_SETLK = global i32 0
@F_ULOCK = global i32 0
@F_UNLCK = global i64 0
@F_TEST = global i32 0
@F_GETLK = global i32 0
@EAGAIN = global i32 0
@errno = 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 = call i32 @memset(%struct.flock* %8, i32 0, i32 40)
%10 = load i32, i32* @SEEK_CUR
%11 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 4
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i64 0, i64* %12
%13 = load i32, i32* %7
%14 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i64, i64* @F_WRLCK
%16 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i64 %15, i64* %16
%17 = load i32, i32* %6
%18 = load i32, i32* @F_LOCK
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i32, i32* %5
%22 = load i32, i32* @F_SETLKW
%23 = call i64 @fcntl(i32 %21, i32 %22, %struct.flock* %8)
%24 = trunc i64 %23 to i32
store i32 %24, i32* %4
br label %71
25: ; preds = %3
%26 = load i32, i32* %6
%27 = load i32, i32* @F_TLOCK
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* %5
%31 = load i32, i32* @F_SETLK
%32 = call i64 @fcntl(i32 %30, i32 %31, %struct.flock* %8)
%33 = trunc i64 %32 to i32
store i32 %33, i32* %4
br label %71
34: ; preds = %25
%35 = load i32, i32* %6
%36 = load i32, i32* @F_ULOCK
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %45
38: ; preds = %34
%39 = load i64, i64* @F_UNLCK
%40 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i64 %39, i64* %40
%41 = load i32, i32* %5
%42 = load i32, i32* @F_SETLK
%43 = call i64 @fcntl(i32 %41, i32 %42, %struct.flock* %8)
%44 = trunc i64 %43 to i32
store i32 %44, i32* %4
br label %71
45: ; preds = %34
%46 = load i32, i32* %6
%47 = load i32, i32* @F_TEST
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %69
49: ; preds = %45
%50 = load i32, i32* %5
%51 = load i32, i32* @F_GETLK
%52 = call i64 @fcntl(i32 %50, i32 %51, %struct.flock* %8)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 -1, i32* %4
br label %71
55: ; preds = %49
%56 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%57 = load i64, i64* %56
%58 = load i64, i64* @F_UNLCK
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %61
60: ; preds = %55
store i32 0, i32* %4
br label %71
61: ; preds = %55
%62 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
%63 = load i64, i64* %62
%64 = call i64 (...) @getpid()
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %61
store i32 0, i32* %4
br label %71
67: ; preds = %61
%68 = load i32, i32* @EAGAIN
store i32 %68, i32* @errno
store i32 -1, i32* %4
br label %71
69: ; preds = %45
%70 = load i32, i32* @EINVAL
store i32 %70, i32* @errno
store i32 -1, i32* %4
br label %71
71: ; preds = %69, %67, %66, %60, %54, %38, %29, %20
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_WRLCK = global i64 0
@F_LOCK = global i32 0
@F_SETLKW = global i32 0
@F_TLOCK = global i32 0
@F_SETLK = global i32 0
@F_ULOCK = global i32 0
@F_UNLCK = global i64 0
@F_TEST = global i32 0
@F_GETLK = global i32 0
@EAGAIN = global i32 0
@errno = 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 = call i32 @memset(%struct.flock* %4, i32 0, i32 40)
%7 = load i32, i32* @SEEK_CUR
%8 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 4
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
store i32 %2, i32* %10
%11 = load i64, i64* @F_WRLCK
%12 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i64 %11, i64* %12
%13 = load i32, i32* @F_LOCK
%14 = icmp eq i32 %13, %1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @F_SETLKW
%17 = call i64 @fcntl(i32 %0, i32 %16, %struct.flock* %4)
%18 = trunc i64 %17 to i32
br label %54
19: ; preds = %3
%20 = load i32, i32* @F_TLOCK
%21 = icmp eq i32 %20, %1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @F_SETLK
%24 = call i64 @fcntl(i32 %0, i32 %23, %struct.flock* %4)
%25 = trunc i64 %24 to i32
br label %54
26: ; preds = %19
%27 = load i32, i32* @F_ULOCK
%28 = icmp eq i32 %27, %1
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i64, i64* @F_UNLCK
store i64 %30, i64* %12
%31 = load i32, i32* @F_SETLK
%32 = call i64 @fcntl(i32 %0, i32 %31, %struct.flock* %4)
%33 = trunc i64 %32 to i32
br label %54
34: ; preds = %26
%35 = load i32, i32* @F_TEST
%36 = icmp eq i32 %35, %1
br i1 %36, label %37, label %52
37: ; preds = %34
%38 = load i32, i32* @F_GETLK
%39 = call i64 @fcntl(i32 %0, i32 %38, %struct.flock* %4)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %37
%42 = load i64, i64* %12
%43 = load i64, i64* @F_UNLCK
%44 = icmp eq i64 %42, %43
br i1 %44, label %54, label %45
45: ; preds = %41
%46 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
%47 = load i64, i64* %46
%48 = call i64 (...) @getpid()
%49 = icmp eq i64 %47, %48
br i1 %49, label %54, label %50
50: ; preds = %45
%51 = load i32, i32* @EAGAIN
store i32 %51, i32* @errno
br label %54
52: ; preds = %34
%53 = load i32, i32* @EINVAL
store i32 %53, i32* @errno
br label %54
54: ; preds = %45, %41, %37, %52, %50, %29, %22, %15
%55 = phi i32 [ %18, %15 ], [ %25, %22 ], [ %33, %29 ], [ -1, %50 ], [ -1, %52 ], [ -1, %37 ], [ 0, %41 ], [ 0, %45 ]
ret i32 %55
}