#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 */

/* Variables and functions */
scalar_t__ EWOULDBLOCK;
int LOCK_EX;
int LOCK_NB;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ flock(int, int);
int /*<<< orphan*/ logger(int, scalar_t__, char *, ...);
int open(char *, int, int);
int _lock(char *lockfile, int blk) {
  int fd, op;
  op = (blk) ? LOCK_EX : LOCK_EX | LOCK_NB;
  if ((fd = open(lockfile, O_CREAT | O_RDWR,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
    logger(-1, errno, "Unable to create lock file %s", lockfile);
    return -1;
  }
  if (flock(fd, op)) {
    if (errno == EWOULDBLOCK) {
      close(fd);
      return -2;
    } else {
      logger(-1, errno, "Error in flock()");
      close(fd);
      return -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     116a <_lock+0x2a>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    117f <_lock+0x3f>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    0x2e16(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    0x2e25(%rip),%rax
or     (%rax),%edx
mov    0x2dfc(%rip),%rax
or     (%rax),%edx
mov    0x2deb(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11fd <_lock+0xbd>
mov    0x2de9(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe16(%rip),%rdx
mov    $0x0,%al
call   1040 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <_lock+0x137>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <flock@plt>
cmp    $0x0,%rax
je     1271 <_lock+0x131>
mov    0x2da7(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
jne    1240 <_lock+0x100>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1277 <_lock+0x137>
mov    0x2d79(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%edi
lea    0xdc8(%rip),%rdx
mov    $0x0,%al
call   1040 <logger@plt>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <_lock+0x137>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_lock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r14
mov    0x2e83(%rip),%rax
mov    (%rax),%r15d
mov    0x2e99(%rip),%rax
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0d(%rip),%rax
mov    0x2e26(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%ebp
mov    0x2e13(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11de <_lock+0x9e>
mov    %eax,%ebx
xor    %esi,%esi
test   %r12d,%r12d
cmove  %ebp,%esi
or     %r15d,%esi
mov    %eax,%edi
call   1060 <flock@plt>
test   %rax,%rax
je     1224 <_lock+0xe4>
mov    0x2dff(%rip),%rax
mov    (%rax),%rsi
mov    0x2ded(%rip),%rax
cmp    (%rax),%rsi
jne    1205 <_lock+0xc5>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xfffffffe,%ebx
jmp    1224 <_lock+0xe4>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rsi
lea    0xe11(%rip),%rdx
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <logger@plt>
jmp    1224 <_lock+0xe4>
lea    0xe12(%rip),%rdx
mov    $0xffffffff,%edi
xor    %eax,%eax
call   1040 <logger@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@errno = global i64 0
@.str = [30 x i8] c"Unable to create lock file %s\00"
@EWOULDBLOCK = global i64 0
@.str.1 = [17 x i8] c"Error in flock()\00"
define i32 @_lock(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @LOCK_EX
br label %16
12: ; preds = %2
%13 = load i32, i32* @LOCK_EX
%14 = load i32, i32* @LOCK_NB
%15 = or i32 %13, %14
br label %16
16: ; preds = %12, %10
%17 = phi i32 [ %11, %10 ], [ %15, %12 ]
store i32 %17, i32* %7
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IROTH
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %18, i32 %21, i32 %28)
store i32 %29, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %16
%32 = load i64, i64* @errno
%33 = load i8*, i8** %4
%34 = call i32 (i32, i64, i8*, ...) @logger(i32 -1, i64 %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 -1, i32* %3
br label %54
35: ; preds = %16
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = call i64 @flock(i32 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %35
%41 = load i64, i64* @errno
%42 = load i64, i64* @EWOULDBLOCK
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 -2, i32* %3
br label %54
47: ; preds = %40
%48 = load i64, i64* @errno
%49 = call i32 (i32, i64, i8*, ...) @logger(i32 -1, i64 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %3
br label %54
52: ; preds = %35
%53 = load i32, i32* %6
store i32 %53, i32* %3
br label %54
54: ; preds = %52, %47, %44, %31
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@errno = global i64 0
@.str = [30 x i8] c"Unable to create lock file %s\00"
@EWOULDBLOCK = global i64 0
@.str.1 = [17 x i8] c"Error in flock()\00"
define i32 @_lock(i8* %0, i32 %1) {
%3 = load i32, i32* @LOCK_EX
%4 = load i32, i32* @LOCK_NB
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %0, i32 %7, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i64, i64* @errno
%19 = tail call i32 (i32, i64, i8*, ...) @logger(i32 -1, i64 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %35
20: ; preds = %2
%21 = icmp eq i32 %1, 0
%22 = select i1 %21, i32 %4, i32 0
%23 = or i32 %22, %3
%24 = tail call i64 @flock(i32 %15, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %20
%27 = load i64, i64* @errno
%28 = load i64, i64* @EWOULDBLOCK
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @close(i32 %15)
br label %35
32: ; preds = %26
%33 = tail call i32 (i32, i64, i8*, ...) @logger(i32 -1, i64 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @close(i32 %15)
br label %35
35: ; preds = %20, %32, %30, %17
%36 = phi i32 [ -1, %17 ], [ -2, %30 ], [ -1, %32 ], [ %15, %20 ]
ret i32 %36
}