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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ F_RDLCK;
int F_SETLK;
int 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 /*<<< orphan*/ errno;
int fcntl(int, int, struct flock *);
int flock(int fd, int operation) {
  struct flock arg;
  int code, cmd;
  arg.l_whence = SEEK_SET;
  arg.l_start = 0;
  arg.l_len = 0;
  if (operation & LOCK_NB)
    cmd = F_SETLK;
  else
    cmd = F_SETLKW;
  switch (operation & (LOCK_SH | LOCK_EX | LOCK_UN)) {
  case LOCK_UN:
    arg.l_type = F_UNLCK;
    code = fcntl(fd, F_SETLK, &arg);
    break;
  case LOCK_SH:
    arg.l_type = F_RDLCK;
    code = fcntl(fd, cmd, &arg);
    break;
  case LOCK_EX:
    arg.l_type = F_WRLCK;
    code = fcntl(fd, cmd, &arg);
    break;
  default:
    errno = EINVAL;
    code = -1;
    break;
  }
  return code;
}


x86 assembly (clang, -O0):
<flock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e54(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1160 <flock+0x50>
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    116c <flock+0x5c>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
and    $0x83,%eax
mov    %eax,-0x34(%rbp)
sub    $0x80,%eax
je     11ad <flock+0x9d>
jmp    1187 <flock+0x77>
mov    -0x34(%rbp),%eax
sub    $0x81,%eax
je     11d6 <flock+0xc6>
jmp    119a <flock+0x8a>
mov    -0x34(%rbp),%eax
sub    $0x82,%eax
je     11f9 <flock+0xe9>
jmp    121c <flock+0x10c>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
jmp    1235 <flock+0x125>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
jmp    1235 <flock+0x125>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
jmp    1235 <flock+0x125>
mov    0x2d95(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flock>:
sub    $0x28,%rsp
mov    %esi,%ecx
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2e66(%rip),%rdx
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
mov    %esi,%eax
test   %ecx,(%rdx)
jne    114a <flock+0x3a>
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
and    $0x83,%cl
cmp    $0x82,%cl
je     1170 <flock+0x60>
cmp    $0x81,%cl
je     1179 <flock+0x69>
cmp    $0x80,%cl
jne    1197 <flock+0x87>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdx
jmp    118d <flock+0x7d>
mov    0x2e39(%rip),%rcx
jmp    1180 <flock+0x70>
mov    0x2e20(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %eax,%esi
call   1030 <fcntl@plt>
add    $0x28,%rsp
ret
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    0x2e21(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x28,%rsp
ret

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

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