#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 op, off_t size) {
  struct flock l = {
      .l_type = F_WRLCK,
      .l_whence = SEEK_CUR,
      .l_len = size,
  };
  switch (op) {
  case F_TEST:
    l.l_type = F_RDLCK;
    if (fcntl(fd, F_GETLK, &l) < 0)
      return -1;
    if (l.l_type == F_UNLCK || l.l_pid == getpid())
      return 0;
    errno = EACCES;
    return -1;
  case F_ULOCK:
    l.l_type = F_UNLCK;
  case F_TLOCK:
    return fcntl(fd, F_SETLK, &l);
  case F_LOCK:
    return fcntl(fd, F_SETLKW, &l);
  }
  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     1251 <lockf+0x131>
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    126a <lockf+0x14a>
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    126a <lockf+0x14a>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ecx
mov    0x2db3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <lockf+0x14a>
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    126a <lockf+0x14a>
mov    -0x8(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    126a <lockf+0x14a>
mov    0x2d50(%rip),%rax
mov    (%rax),%ecx
mov    0x2d4f(%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 %49 [
i32 130, label %17
i32 128, label %38
i32 129, label %41
i32 131, label %45
]
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 %51
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 %51
36: ; preds = %30
%37 = load i32, i32* @EACCES
store i32 %37, i32* @errno
store i32 -1, i32* %4
br label %51
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
br label %41
41: ; preds = %3, %38
%42 = load i32, i32* %5
%43 = load i32, i32* @F_SETLK
%44 = call i32 @fcntl(i32 %42, i32 %43, %struct.flock* %8)
store i32 %44, i32* %4
br label %51
45: ; preds = %3
%46 = load i32, i32* %5
%47 = load i32, i32* @F_SETLKW
%48 = call i32 @fcntl(i32 %46, i32 %47, %struct.flock* %8)
store i32 %48, i32* %4
br label %51
49: ; preds = %3
%50 = load i32, i32* @EINVAL
store i32 %50, i32* @errno
store i32 -1, i32* %4
br label %51
51: ; preds = %49, %45, %41, %36, %35, %24
%52 = load i32, i32* %4
ret i32 %52
}

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 %35 [
i32 130, label %12
i32 128, label %27
i32 129, label %29
i32 131, label %32
]
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 %37, 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 %37, label %21
21: ; preds = %17
%22 = load i32, i32* %8
%23 = call i32 (...) @getpid()
%24 = icmp eq i32 %22, %23
br i1 %24, label %37, label %25
25: ; preds = %21
%26 = load i32, i32* @EACCES
store i32 %26, i32* @errno
br label %37
27: ; preds = %3
%28 = load i32, i32* @F_UNLCK
store i32 %28, i32* %6
br label %29
29: ; preds = %3, %27
%30 = load i32, i32* @F_SETLK
%31 = call i32 @fcntl(i32 %0, i32 %30, %struct.flock* %4)
br label %37
32: ; preds = %3
%33 = load i32, i32* @F_SETLKW
%34 = call i32 @fcntl(i32 %0, i32 %33, %struct.flock* %4)
br label %37
35: ; preds = %3
%36 = load i32, i32* @EINVAL
store i32 %36, i32* @errno
br label %37
37: ; preds = %17, %21, %12, %35, %32, %29, %25
%38 = phi i32 [ -1, %35 ], [ %34, %32 ], [ %31, %29 ], [ -1, %25 ], [ -1, %12 ], [ 0, %21 ], [ 0, %17 ]
ret i32 %38
}