#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 */
int /*<<< orphan*/ F_LOCK;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ usleep(int);
int lock(const char *lock_path) {
  int fd, i;
  if ((fd = open(lock_path, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) <
      0)
    return -1;
  for (i = 0; i < 60; i++) {
    if (!lockf(fd, F_LOCK, 0)) {
      ;
      return fd;
    } else
      usleep(1000000);
  }
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    0x2e87(%rip),%rax
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    0x2e55(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    119a <lock+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <lock+0xbe>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3c,-0x18(%rbp)
jge    11ef <lock+0xaf>
mov    -0x14(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lockf@plt>
cmp    $0x0,%eax
jne    11d2 <lock+0x92>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fe <lock+0xbe>
mov    $0xf4240,%edi
call   1060 <usleep@plt>
jmp    11e1 <lock+0xa1>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11a1 <lock+0x61>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2eaa(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1050 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11cb <lock+0x8b>
mov    %eax,%ebp
mov    $0x3c,%ebx
mov    0x2e47(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <lockf@plt>
test   %eax,%eax
je     11c8 <lock+0x88>
mov    $0xf4240,%edi
call   1060 <usleep@plt>
add    $0xffffffff,%ebx
jne    11a0 <lock+0x60>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11cb <lock+0x8b>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_LOCK = global i32 0
define i32 @lock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %6, i32 %11, i32 %14)
store i32 %15, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %38
18: ; preds = %1
store i32 0, i32* %5
br label %19
19: ; preds = %32, %18
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 60
br i1 %21, label %22, label %35
22: ; preds = %19
%23 = load i32, i32* %4
%24 = load i32, i32* @F_LOCK
%25 = call i32 @lockf(i32 %23, i32 %24, i32 0)
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %22
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %38
29: ; preds = %22
%30 = call i32 @usleep(i32 1000000)
br label %31
31: ; preds = %29
br label %32
32: ; preds = %31
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %19
35: ; preds = %19
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %2
br label %38
38: ; preds = %35, %27, %17
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_LOCK = global i32 0
define i32 @lock(i8* nocapture %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_WRONLY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %6, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %1, %17
%13 = phi i32 [ %19, %17 ], [ 0, %1 ]
%14 = load i32, i32* @F_LOCK
%15 = tail call i32 @lockf(i32 %10, i32 %14, i32 0)
%16 = icmp eq i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = tail call i32 @usleep(i32 1000000)
%19 = add nuw nsw i32 %13, 1
%20 = icmp eq i32 %19, 60
br i1 %20, label %21, label %12
21: ; preds = %17
%22 = tail call i32 @close(i32 %10)
br label %23
23: ; preds = %12, %1, %21
%24 = phi i32 [ -1, %21 ], [ -1, %1 ], [ %10, %12 ]
ret i32 %24
}