#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__ EINTR;
scalar_t__ ENOENT;
int /*<<< orphan*/ EX_CANTCREAT;
int LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
scalar_t__ errno;
int flock(int, int);
int open(char const *, int /*<<< orphan*/, int);
void wait_for_lock(const char *name) {
  int fd;
  if ((fd = open(name, O_RDONLY, 0666)) == -1) {
    if (errno == ENOENT || errno == EINTR)
      return;
    err(EX_CANTCREAT, "cannot open %s", name);
  }
  if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
    err(EX_CANTCREAT, "lock failed");
  }
  close(fd);
}


x86 assembly (clang, -O0):
<wait_for_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11c3 <wait_for_lock+0x83>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
je     11a3 <wait_for_lock+0x63>
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jne    11a8 <wait_for_lock+0x68>
jmp    1205 <wait_for_lock+0xc5>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0xc(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
call   1060 <flock@plt>
cmp    $0xffffffff,%eax
jne    11fd <wait_for_lock+0xbd>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_lock>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    119d <wait_for_lock+0x5d>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
je     11e0 <wait_for_lock+0xa0>
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
je     11e0 <wait_for_lock+0xa0>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <err@plt>
mov    0x2e44(%rip),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   1060 <flock@plt>
cmp    $0xffffffff,%eax
jne    11d2 <wait_for_lock+0x92>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
xor    %eax,%eax
call   1040 <err@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINTR = global i64 0
@EX_CANTCREAT = global i32 0
@.str = [15 x i8] c"cannot open %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.1 = [12 x i8] c"lock failed\00"
define void @wait_for_lock(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %4, i32 %5, i32 438)
store i32 %6, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12, %8
br label %34
17: ; preds = %12
%18 = load i32, i32* @EX_CANTCREAT
%19 = load i8*, i8** %2
%20 = call i32 (i32, i8*, ...) @err(i32 %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %17, %1
%22 = load i32, i32* %3
%23 = load i32, i32* @LOCK_EX
%24 = load i32, i32* @LOCK_NB
%25 = or i32 %23, %24
%26 = call i32 @flock(i32 %22, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32, i32* @EX_CANTCREAT
%30 = call i32 (i32, i8*, ...) @err(i32 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %28, %21
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %16
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EINTR = global i64 0
@EX_CANTCREAT = global i32 0
@.str = [15 x i8] c"cannot open %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.1 = [12 x i8] c"lock failed\00"
define void @wait_for_lock(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2, i32 438)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %15
5: ; preds = %1
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %6, %9
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %26, label %12
12: ; preds = %5
%13 = load i32, i32* @EX_CANTCREAT
%14 = tail call i32 (i32, i8*, ...) @err(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* @LOCK_EX
%17 = load i32, i32* @LOCK_NB
%18 = or i32 %17, %16
%19 = tail call i32 @flock(i32 %3, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i32, i32* @EX_CANTCREAT
%23 = tail call i32 (i32, i8*, ...) @err(i32 %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %15
%25 = tail call i32 @close(i32 %3)
br label %26
26: ; preds = %5, %24
ret void
}