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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
#define F_LOCK 130
int F_SETLK;
int F_SETLKW;
#define F_TLOCK 129
#define F_ULOCK 128
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ bzero(struct flock *, int);
int /*<<< orphan*/ errno;
int fcntl(int, int, struct flock *);
int lockf(int fd, int command, off_t size) {
  struct flock params;
  bzero(&params, sizeof(params));
  params.l_whence = SEEK_CUR;
  params.l_start = 0;
  params.l_len = size;
  int fcntl_command = F_SETLK;
  switch (command) {
  case F_ULOCK:
    params.l_type = F_UNLCK;
    fcntl_command = F_SETLK;
    break;
  case F_LOCK:
    fcntl_command = F_SETLKW;
  case F_TLOCK:
    params.l_type = F_WRLCK;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  return fcntl(fd, fcntl_command, &params);
}


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)
lea    -0x28(%rbp),%rdi
mov    $0x18,%esi
call   1040 <bzero@plt>
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x80,%eax
je     11a1 <lockf+0x81>
jmp    117b <lockf+0x5b>
mov    -0x30(%rbp),%eax
sub    $0x81,%eax
je     11ca <lockf+0xaa>
jmp    118e <lockf+0x6e>
mov    -0x30(%rbp),%eax
sub    $0x82,%eax
je     11be <lockf+0x9e>
jmp    11db <lockf+0xbb>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11f9 <lockf+0xd9>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11f9 <lockf+0xd9>
mov    0x2dd6(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <lockf+0xeb>
mov    -0x8(%rbp),%edi
mov    -0x2c(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
mov    $0x18,%esi
call   1040 <bzero@plt>
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %ebx,0xc(%rsp)
cmp    $0x82,%ebp
je     1188 <lockf+0x68>
cmp    $0x81,%ebp
je     1191 <lockf+0x71>
cmp    $0x80,%ebp
jne    11b6 <lockf+0x96>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
jmp    11a7 <lockf+0x87>
mov    0x2e51(%rip),%rax
jmp    1198 <lockf+0x78>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1030 <fcntl@plt>
jmp    11cd <lockf+0xad>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    0x2dfa(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_SETLK = global i32 0
@F_UNLCK = global i32 0
@F_SETLKW = global i32 0
@F_WRLCK = global i32 0
@EINVAL = global i32 0
@errno = 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
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = call i32 @bzero(%struct.flock* %8, i32 24)
%11 = load i32, i32* @SEEK_CUR
%12 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i64 0, i64* %13
%14 = load i32, i32* %7
%15 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @F_SETLK
store i32 %16, i32* %9
%17 = load i32, i32* %6
switch i32 %17, label %27 [
i32 128, label %18
i32 130, label %22
i32 129, label %24
]
18: ; preds = %3
%19 = load i32, i32* @F_UNLCK
%20 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @F_SETLK
store i32 %21, i32* %9
br label %29
22: ; preds = %3
%23 = load i32, i32* @F_SETLKW
store i32 %23, i32* %9
br label %24
24: ; preds = %3, %22
%25 = load i32, i32* @F_WRLCK
%26 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %25, i32* %26
br label %29
27: ; preds = %3
%28 = load i32, i32* @EINVAL
store i32 %28, i32* @errno
store i32 -1, i32* %4
br label %33
29: ; preds = %24, %18
%30 = load i32, i32* %5
%31 = load i32, i32* %9
%32 = call i32 @fcntl(i32 %30, i32 %31, %struct.flock* %8)
store i32 %32, i32* %4
br label %33
33: ; preds = %29, %27
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i32 }
@SEEK_CUR = global i32 0
@F_SETLK = global i32 0
@F_UNLCK = global i32 0
@F_SETLKW = global i32 0
@F_WRLCK = global i32 0
@EINVAL = global i32 0
@errno = 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 @bzero(%struct.flock* %4, i32 24)
%7 = load i32, i32* @SEEK_CUR
%8 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
store i32 %2, i32* %10
switch i32 %1, label %21 [
i32 128, label %11
i32 130, label %15
i32 129, label %16
]
11: ; preds = %3
%12 = load i32, i32* @F_UNLCK
%13 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @F_SETLK
br label %23
15: ; preds = %3
br label %16
16: ; preds = %3, %15
%17 = phi i32* [ @F_SETLK, %3 ], [ @F_SETLKW, %15 ]
%18 = load i32, i32* %17
%19 = load i32, i32* @F_WRLCK
%20 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
store i32 %19, i32* %20
br label %23
21: ; preds = %3
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
br label %26
23: ; preds = %16, %11
%24 = phi i32 [ %18, %16 ], [ %14, %11 ]
%25 = call i32 @fcntl(i32 %0, i32 %24, %struct.flock* %4)
br label %26
26: ; preds = %23, %21
%27 = phi i32 [ -1, %21 ], [ %25, %23 ]
ret i32 %27
}