#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 {
  long l_start;
  long l_len;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
#define LOCK_EX 130
int LOCK_NB;
#define LOCK_SH 129
#define LOCK_UN 128
int /*<<< orphan*/ SEEK_SET;
int __fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int __flock(int fd, int operation) {
  struct flock lbuf;
  switch (operation & ~LOCK_NB) {
  case LOCK_SH:
    lbuf.l_type = F_RDLCK;
    break;
  case LOCK_EX:
    lbuf.l_type = F_WRLCK;
    break;
  case LOCK_UN:
    lbuf.l_type = F_UNLCK;
    break;
  default:
    __set_errno(EINVAL);
    return -1;
  }
  lbuf.l_whence = SEEK_SET;
  lbuf.l_start = lbuf.l_len = 0L;
  return __fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lbuf);
}


x86 assembly (clang, -O0):
<__flock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %ecx,%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     1199 <__flock+0x79>
jmp    1151 <__flock+0x31>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
je     1177 <__flock+0x57>
jmp    1164 <__flock+0x44>
mov    -0x2c(%rbp),%eax
sub    $0x82,%eax
je     1188 <__flock+0x68>
jmp    11aa <__flock+0x8a>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11c4 <__flock+0xa4>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11c4 <__flock+0xa4>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11c4 <__flock+0xa4>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <__flock+0x10a>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2db0(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     120c <__flock+0xec>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1218 <__flock+0xf8>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x34(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1040 <__fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__flock>:
sub    $0x18,%rsp
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
not    %ecx
and    %esi,%ecx
add    $0xffffff80,%ecx
cmp    $0x3,%ecx
jae    1170 <__flock+0x50>
movslq %ecx,%rcx
lea    0x2c9b(%rip),%rdx
mov    (%rdx,%rcx,8),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x14(%rsp)
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
test   %esi,%eax
jne    1188 <__flock+0x68>
mov    0x2e7a(%rip),%rax
jmp    118f <__flock+0x6f>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <__fcntl@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@LOCK_NB = global i32 0
@F_RDLCK = global i32 0
@F_WRLCK = global i32 0
@F_UNLCK = global i32 0
@EINVAL = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@F_SETLKW = global i32 0
define i32 @__flock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.flock
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = load i32, i32* @LOCK_NB
%9 = xor i32 %8, -1
%10 = and i32 %7, %9
switch i32 %10, label %20 [
i32 129, label %11
i32 130, label %14
i32 128, label %17
]
11: ; preds = %2
%12 = load i32, i32* @F_RDLCK
%13 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %12, i32* %13
br label %23
14: ; preds = %2
%15 = load i32, i32* @F_WRLCK
%16 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %15, i32* %16
br label %23
17: ; preds = %2
%18 = load i32, i32* @F_UNLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %18, i32* %19
br label %23
20: ; preds = %2
%21 = load i32, i32* @EINVAL
%22 = call i32 @__set_errno(i32 %21)
store i32 -1, i32* %3
br label %40
23: ; preds = %17, %14, %11
%24 = load i32, i32* @SEEK_SET
%25 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %26
%27 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %27
%28 = load i32, i32* %4
%29 = load i32, i32* %5
%30 = load i32, i32* @LOCK_NB
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = load i32, i32* @F_SETLK
br label %37
35: ; preds = %23
%36 = load i32, i32* @F_SETLKW
br label %37
37: ; preds = %35, %33
%38 = phi i32 [ %34, %33 ], [ %36, %35 ]
%39 = call i32 @__fcntl(i32 %28, i32 %38, %struct.flock* %6)
store i32 %39, i32* %3
br label %40
40: ; preds = %37, %20
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@LOCK_NB = global i32 0
@F_RDLCK = global i32 0
@F_WRLCK = global i32 0
@F_UNLCK = global i32 0
@EINVAL = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@F_SETLKW = global i32 0
@switch.table.__flock = [3 x i32*] [i32* @F_UNLCK, i32* @F_RDLCK, i32* @F_WRLCK]
define i32 @__flock(i32 %0, i32 %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = load i32, i32* @LOCK_NB
%6 = xor i32 %5, -1
%7 = and i32 %6, %1
%8 = add i32 %7, -128
%9 = icmp ult i32 %8, 3
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* @EINVAL
%12 = tail call i32 @__set_errno(i32 %11)
br label %28
13: ; preds = %2
%14 = sext i32 %8 to i64
%15 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.__flock, i64 0, i64 %14
%16 = load i32*, i32** %15
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %17, i32* %18
%19 = load i32, i32* @SEEK_SET
%20 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = and i32 %5, %1
%22 = icmp eq i32 %21, 0
%23 = bitcast %struct.flock* %3 to i8*
%24 = load i32, i32* @F_SETLK
%25 = load i32, i32* @F_SETLKW
%26 = select i1 %22, i32 %25, i32 %24
%27 = call i32 @__fcntl(i32 %0, i32 %26, %struct.flock* %3)
br label %28
28: ; preds = %13, %10
%29 = phi i32 [ -1, %10 ], [ %27, %13 ]
ret i32 %29
}