#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;
};
typedef int /*<<< orphan*/ lock;

/* 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*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int flock(int fd, int operation) {
  struct flock lock;
  memset(&lock, 0, sizeof(lock));
  switch (operation & ~LOCK_NB) {
  case LOCK_SH:
    lock.l_type = F_RDLCK;
    break;
  case LOCK_EX:
    lock.l_type = F_WRLCK;
    break;
  case LOCK_UN:
    lock.l_type = F_UNLCK;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  return fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &lock);
}


x86 assembly (clang, -O0):
<flock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%eax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %ecx,%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     11a9 <flock+0x89>
jmp    1161 <flock+0x41>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
je     1187 <flock+0x67>
jmp    1174 <flock+0x54>
mov    -0x14(%rbp),%eax
sub    $0x82,%eax
je     1198 <flock+0x78>
jmp    11ba <flock+0x9a>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11d8 <flock+0xb8>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11d8 <flock+0xb8>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11d8 <flock+0xb8>
mov    0x2dff(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfe(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <flock+0x102>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2db8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1204 <flock+0xe4>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1210 <flock+0xf0>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
lea    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flock>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
not    %ecx
and    %ebp,%ecx
cmp    $0x80,%ecx
je     1166 <flock+0x46>
cmp    $0x82,%ecx
je     116f <flock+0x4f>
cmp    $0x81,%ecx
jne    119d <flock+0x7d>
mov    0x2e44(%rip),%rcx
jmp    1176 <flock+0x56>
mov    0x2e6b(%rip),%rcx
jmp    1176 <flock+0x56>
mov    0x2e42(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rsp)
test   %ebp,%eax
jne    1188 <flock+0x68>
mov    0x2e62(%rip),%rax
jmp    118f <flock+0x6f>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
jmp    11b4 <flock+0x94>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    0x2e1b(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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