#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 {
  void *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*/ EACCES;
int /*<<< orphan*/ EINVAL;
int F_GETLK;
#define F_LOCK 131
int F_SETLK;
int F_SETLKW;
#define F_TEST 130
#define F_TLOCK 129
#define F_ULOCK 128
void *F_UNLCK;
void *F_WRLCK;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ errno;
int fcntl(int, int, struct flock *);
scalar_t__ getpid();
int lockf(int fd, int cmd, off_t len) {
  struct flock fl;
  fl.l_whence = SEEK_CUR;
  fl.l_start = 0;
  fl.l_len = len;
  fl.l_pid = 0;
  switch (cmd) {
  case F_TEST:
    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;
    cmd = F_SETLK;
    break;
  case F_LOCK:
    fl.l_type = F_WRLCK;
    cmd = F_SETLKW;
    break;
  case F_TLOCK:
    fl.l_type = F_WRLCK;
    cmd = F_SETLK;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  return fcntl(fd, cmd, &fl);
}


x86 assembly (clang, -O0):
<lockf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2eb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x3,%eax
ja     1260 <lockf+0x140>
mov    -0x40(%rbp),%rax
lea    0xe8d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11a6 <lockf+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <lockf+0x170>
mov    -0x38(%rbp),%rax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
je     11d9 <lockf+0xb9>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jne    11e5 <lockf+0xc5>
movl   $0x0,-0x4(%rbp)
jmp    1290 <lockf+0x170>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbb(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <lockf+0x170>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    127e <lockf+0x15e>
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    127e <lockf+0x15e>
mov    0x2d58(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    127e <lockf+0x15e>
mov    0x2d41(%rip),%rax
mov    (%rax),%ecx
mov    0x2d40(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <lockf+0x170>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockf>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2eba(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    %edx,0x10(%rsp)
movq   $0x0,0x8(%rsp)
add    $0xffffff80,%esi
cmp    $0x3,%esi
ja     116b <lockf+0x4b>
lea    0xea7(%rip),%rax
movslq (%rax,%rsi,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    0x2e5f(%rip),%rax
jmp    118b <lockf+0x6b>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    0x2e35(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1202 <lockf+0xe2>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2e3f(%rip),%rax
jmp    11f6 <lockf+0xd6>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1202 <lockf+0xe2>
mov    (%rsp),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
je     120c <lockf+0xec>
mov    0x8(%rsp),%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    %rax,%r14
je     1202 <lockf+0xe2>
mov    0x2de1(%rip),%rax
jmp    1172 <lockf+0x52>
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
jmp    1202 <lockf+0xe2>

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

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