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

/* Variables and functions */
int /*<<< orphan*/ EACCES;
int /*<<< orphan*/ EINVAL;
int F_GETLK;
#define F_LOCK 131
void *F_RDLCK;
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 __fcntl(int, int, struct flock *);
int /*<<< orphan*/ __getpid();
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
int lockf(int fd, int cmd, off_t len) {
  struct flock fl;
  memset((char *)&fl, '\0', sizeof(fl));
  fl.l_whence = SEEK_CUR;
  fl.l_start = 0;
  fl.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;
    __set_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:
    __set_errno(EINVAL);
    return -1;
  }
  return __fcntl(fd, cmd, &fl);
}


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    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     128d <lockf+0x14d>
mov    -0x38(%rbp),%rax
lea    0xe65(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <__fcntl@plt>
cmp    $0x0,%eax
jge    11dc <lockf+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <lockf+0x179>
mov    -0x30(%rbp),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
je     120a <lockf+0xca>
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1040 <__getpid@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cmp    %ecx,%eax
jne    1216 <lockf+0xd6>
movl   $0x0,-0x4(%rbp)
jmp    12b9 <lockf+0x179>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <lockf+0x179>
mov    0x2d91(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    12a7 <lockf+0x167>
mov    0x2d52(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    12a7 <lockf+0x167>
mov    0x2d33(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    12a7 <lockf+0x167>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <lockf+0x179>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdx
call   1060 <__fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %ebp,0xc(%rsp)
add    $0xffffff80,%ebx
cmp    $0x3,%ebx
ja     1199 <lockf+0x59>
lea    0xe79(%rip),%rax
movslq (%rax,%rbx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    0x2e31(%rip),%rax
jmp    11b8 <lockf+0x78>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%ebx
jmp    1241 <lockf+0x101>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2e12(%rip),%rax
jmp    1232 <lockf+0xf2>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <__fcntl@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1241 <lockf+0x101>
mov    (%rsp),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
je     124c <lockf+0x10c>
mov    0x8(%rsp),%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <__getpid@plt>
cmp    %eax,%ebp
je     1241 <lockf+0x101>
mov    0x2da5(%rip),%rax
jmp    11a0 <lockf+0x60>
mov    0x2d84(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <__fcntl@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebx,%ebx
jmp    1241 <lockf+0x101>

LLVM IR (-O0):
%struct.flock = type { i8*, i32, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_RDLCK = global i8* null
@F_GETLK = global i32 0
@F_UNLCK = global i8* null
@EACCES = 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 = bitcast %struct.flock* %8 to i8*
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 32)
%11 = load i32, i32* @SEEK_CUR
%12 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 4
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i64 0, i64* %13
%14 = load i32, i32* %7
%15 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %6
switch i32 %16, label %51 [
i32 130, label %17
i32 128, label %39
i32 131, label %43
i32 129, label %47
]
17: ; preds = %3
%18 = load i8*, i8** @F_RDLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %18, i8** %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 %58
25: ; preds = %17
%26 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = load i8*, i8** @F_UNLCK
%29 = icmp eq i8* %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 %58
36: ; preds = %30
%37 = load i32, i32* @EACCES
%38 = call i32 @__set_errno(i32 %37)
store i32 -1, i32* %4
br label %58
39: ; preds = %3
%40 = load i8*, i8** @F_UNLCK
%41 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %40, i8** %41
%42 = load i32, i32* @F_SETLK
store i32 %42, i32* %6
br label %54
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_SETLKW
store i32 %46, i32* %6
br label %54
47: ; preds = %3
%48 = load i8*, i8** @F_WRLCK
%49 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %48, i8** %49
%50 = load i32, i32* @F_SETLK
store i32 %50, i32* %6
br label %54
51: ; preds = %3
%52 = load i32, i32* @EINVAL
%53 = call i32 @__set_errno(i32 %52)
store i32 -1, i32* %4
br label %58
54: ; preds = %47, %43, %39
%55 = load i32, i32* %5
%56 = load i32, i32* %6
%57 = call i32 @__fcntl(i32 %55, i32 %56, %struct.flock* %8)
store i32 %57, i32* %4
br label %58
58: ; preds = %54, %51, %36, %35, %24
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.flock = type { i8*, i32, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_RDLCK = global i8* null
@F_GETLK = global i32 0
@F_UNLCK = global i8* null
@EACCES = 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 = call i32 @memset(i8* %5, i8 signext 0, i32 32)
%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
switch i32 %1, label %38 [
i32 130, label %11
i32 128, label %29
i32 131, label %32
i32 129, label %35
]
11: ; preds = %3
%12 = load i8*, i8** @F_RDLCK
%13 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i8* %12, i8** %13
%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 %45, label %17
17: ; preds = %11
%18 = load i8*, i8** %13
%19 = load i8*, i8** @F_UNLCK
%20 = icmp eq i8* %18, %19
br i1 %20, label %45, label %21
21: ; preds = %17
%22 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 (...) @__getpid()
%25 = icmp eq i32 %23, %24
br i1 %25, label %45, label %26
26: ; preds = %21
%27 = load i32, i32* @EACCES
%28 = call i32 @__set_errno(i32 %27)
br label %45
29: ; preds = %3
%30 = load i8*, i8** @F_UNLCK
%31 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i8* %30, i8** %31
br label %41
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 %41
35: ; preds = %3
%36 = load i8*, i8** @F_WRLCK
%37 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i8* %36, i8** %37
br label %41
38: ; preds = %3
%39 = load i32, i32* @EINVAL
%40 = call i32 @__set_errno(i32 %39)
br label %45
41: ; preds = %35, %32, %29
%42 = phi i32* [ @F_SETLK, %35 ], [ @F_SETLKW, %32 ], [ @F_SETLK, %29 ]
%43 = load i32, i32* %42
%44 = call i32 @__fcntl(i32 %0, i32 %43, %struct.flock* %4)
br label %45
45: ; preds = %17, %21, %11, %41, %38, %26
%46 = phi i32 [ -1, %38 ], [ %44, %41 ], [ -1, %26 ], [ -1, %11 ], [ 0, %21 ], [ 0, %17 ]
ret i32 %46
}