#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 /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int);
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int open_lock(const char *fn) {
  int fd = open(fn, O_RDWR);
  if (fd == -1)
    return -1;
  fcntl(fd, F_SETFD, 1);
  if (lockf(fd, F_LOCK, 0) == -1) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1177 <open_lock+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <open_lock+0x83>
mov    -0x14(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
cmp    $0xffffffff,%eax
jne    11bd <open_lock+0x7d>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <open_lock+0x83>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_lock>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1194 <open_lock+0x54>
mov    %eax,%ebp
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <fcntl@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <lockf@plt>
cmp    $0xffffffff,%eax
je     118d <open_lock+0x4d>
mov    %ebp,%ebx
jmp    1194 <open_lock+0x54>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@F_SETFD = global i32 0
@F_LOCK = global i32 0
define i32 @open_lock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %24
11: ; preds = %1
%12 = load i32, i32* %4
%13 = load i32, i32* @F_SETFD
%14 = call i32 @fcntl(i32 %12, i32 %13, i32 1)
%15 = load i32, i32* %4
%16 = load i32, i32* @F_LOCK
%17 = call i32 @lockf(i32 %15, i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %2
br label %24
22: ; preds = %11
%23 = load i32, i32* %4
store i32 %23, i32* %2
br label %24
24: ; preds = %22, %19, %10
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@F_SETFD = global i32 0
@F_LOCK = global i32 0
define i32 @open_lock(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %13, label %5
5: ; preds = %1
%6 = load i32, i32* @F_SETFD
%7 = tail call i32 @fcntl(i32 %3, i32 %6, i32 1)
%8 = load i32, i32* @F_LOCK
%9 = tail call i32 @lockf(i32 %3, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @close(i32 %3)
br label %13
13: ; preds = %5, %1, %11
%14 = phi i32 [ -1, %11 ], [ -1, %1 ], [ %3, %5 ]
ret i32 %14
}