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

/* Variables and functions */
int EBADF;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int c_lock(int fd, int should_block, int is_write_lock) {
  if (fd < 0) {
    return EBADF;
  }
  struct flock fl;
  fl.l_type = is_write_lock ? F_WRLCK : F_RDLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  if (fcntl(fd, should_block ? F_SETLKW : F_SETLK, &fl) == -1) {
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<c_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    113c <c_lock+0x2c>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <c_lock+0xd2>
cmpl   $0x0,-0x10(%rbp)
je     1157 <c_lock+0x47>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1163 <c_lock+0x53>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e58(%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)
cmpl   $0x0,-0xc(%rbp)
je     11a6 <c_lock+0x96>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    11b2 <c_lock+0xa2>
mov    0x2e33(%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>
cmp    $0xffffffff,%eax
jne    11db <c_lock+0xcb>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <c_lock+0xd2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_lock>:
sub    $0x18,%rsp
test   %edi,%edi
js     1125 <c_lock+0x15>
test   %edx,%edx
jne    112e <c_lock+0x1e>
mov    0x2e85(%rip),%rax
jmp    1135 <c_lock+0x25>
mov    0x2e8c(%rip),%rax
jmp    117d <c_lock+0x6d>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
test   %esi,%esi
jne    115c <c_lock+0x4c>
mov    0x2e86(%rip),%rax
jmp    1163 <c_lock+0x53>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    117f <c_lock+0x6f>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
ret

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

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