#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;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int flock(int fd, int operation) {
  struct flock lock;
  switch (operation & ~4) {
  case 1:
    lock.l_type = F_RDLCK;
    break;
  case 2:
    lock.l_type = F_WRLCK;
    break;
  case 8:
    lock.l_type = F_UNLCK;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  lock.l_whence = SEEK_SET;
  lock.l_start = lock.l_len = 0L;
  return fcntl(fd, (operation & 4) ? F_SETLK : F_SETLKW, &lock);
}


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
and    $0xfffffffb,%eax
mov    %eax,-0x2c(%rbp)
sub    $0x1,%eax
je     1157 <flock+0x47>
jmp    1135 <flock+0x25>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     1168 <flock+0x58>
jmp    1146 <flock+0x36>
mov    -0x2c(%rbp),%eax
sub    $0x8,%eax
je     1179 <flock+0x69>
jmp    118a <flock+0x7a>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11a8 <flock+0x98>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11a8 <flock+0x98>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11a8 <flock+0x98>
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <flock+0xf8>
mov    0x2e11(%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
and    $0x4,%eax
cmp    $0x0,%eax
je     11ea <flock+0xda>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    11f6 <flock+0xe6>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x34(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <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    %esi,%eax
and    $0xfffffffb,%eax
cmp    $0x1,%eax
je     1131 <flock+0x21>
cmp    $0x8,%eax
je     113a <flock+0x2a>
cmp    $0x2,%eax
jne    1180 <flock+0x70>
mov    0x2e81(%rip),%rax
jmp    1141 <flock+0x31>
mov    0x2e68(%rip),%rax
jmp    1141 <flock+0x31>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
test   $0x4,%sil
jne    116a <flock+0x5a>
mov    0x2e80(%rip),%rax
jmp    1171 <flock+0x61>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
add    $0x18,%rsp
ret
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    0x2e38(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@F_RDLCK = global i32 0
@F_WRLCK = global i32 0
@F_UNLCK = global i32 0
@EINVAL = global i32 0
@errno = 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 %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = and i32 %1, -5
switch i32 %5, label %8 [
i32 1, label %10
i32 2, label %6
i32 8, label %7
]
6: ; preds = %2
br label %10
7: ; preds = %2
br label %10
8: ; preds = %2
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
br label %23
10: ; preds = %2, %7, %6
%11 = phi i32* [ @F_UNLCK, %7 ], [ @F_WRLCK, %6 ], [ @F_RDLCK, %2 ]
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @SEEK_SET
%15 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = and i32 %1, 4
%17 = icmp eq i32 %16, 0
%18 = bitcast %struct.flock* %3 to i8*
%19 = load i32, i32* @F_SETLK
%20 = load i32, i32* @F_SETLKW
%21 = select i1 %17, i32 %20, i32 %19
%22 = call i32 @fcntl(i32 %0, i32 %21, %struct.flock* %3)
br label %23
23: ; preds = %10, %8
%24 = phi i32 [ -1, %8 ], [ %22, %10 ]
ret i32 %24
}