#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 /*<<< orphan*/ EXIT_FAILURE;
int LOCK_EX;
int LOCK_NB;
int O_CREAT;
int O_RDONLY;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int flock(int, int);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(scalar_t__);
int open_lockfile(const char *filename) {
  int fd;
  fd = open(filename, O_CREAT | O_RDONLY, 0600);
  if (fd < 0) {
    printf("Failed to access lock file: %s\nerror: %s\nDid you sudo?\n",
           filename, strerror(errno));
    exit(EXIT_FAILURE);
  }
  while (flock(fd, LOCK_EX | LOCK_NB) == -1) {
    if (errno == EWOULDBLOCK) {
      printf("Lock file is in use, exiting...\n");
      exit(EXIT_FAILURE);
    }
    perror("Flock failed");
    exit(EXIT_FAILURE);
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_lockfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d3 <open_lockfile+0x73>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11d8 <open_lockfile+0x78>
mov    -0xc(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
or     (%rax),%esi
call   1060 <flock@plt>
cmp    $0xffffffff,%eax
jne    124b <open_lockfile+0xeb>
mov    0x2dc6(%rip),%rax
mov    (%rax),%rax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <open_lockfile+0xd1>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe21(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_lockfile>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7b(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11bf <open_lockfile+0x4f>
mov    %eax,%ebp
mov    0x2e41(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
je     11e4 <open_lockfile+0x74>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e02(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1214 <open_lockfile+0xa4>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <open_lockfile+0x98>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
jmp    1214 <open_lockfile+0xa4>
lea    0xe29(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [56 x i8] c"Failed to access lock file: %s\0Aerror: %s\0ADid you sudo?\0A\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@EWOULDBLOCK = global i64 0
@.str.1 = [33 x i8] c"Lock file is in use, exiting...\0A\00"
@.str.2 = [13 x i8] c"Flock failed\00"
define i32 @open_lockfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDONLY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* %4, i32 %7, i32 384)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = load i64, i64* @errno
%14 = call i8* @strerror(i64 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
br label %19
19: ; preds = %18
%20 = load i32, i32* %3
%21 = load i32, i32* @LOCK_EX
%22 = load i32, i32* @LOCK_NB
%23 = or i32 %21, %22
%24 = call i32 @flock(i32 %20, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %38
26: ; preds = %19
%27 = load i64, i64* @errno
%28 = load i64, i64* @EWOULDBLOCK
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %26
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %19
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [56 x i8] c"Failed to access lock file: %s\0Aerror: %s\0ADid you sudo?\0A\00"
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@EWOULDBLOCK = global i64 0
@.str.2 = [13 x i8] c"Flock failed\00"
@str = [32 x i8] c"Lock file is in use, exiting...\00"
define i32 @open_lockfile(i8* %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_RDONLY
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* %0, i32 %4, i32 384)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = tail call i8* @strerror(i64 %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = load i32, i32* @LOCK_EX
%15 = load i32, i32* @LOCK_NB
%16 = or i32 %15, %14
%17 = tail call i32 @flock(i32 %5, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %31
19: ; preds = %13
%20 = load i64, i64* @errno
%21 = load i64, i64* @EWOULDBLOCK
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %13
ret i32 %5
}