#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 LOCK_EX;
int LOCK_NB;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ close(int);
int flock(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int);
int /*<<< orphan*/ stderr;
int open_pci_resource(const char *path) {
  int fd;
  if ((fd = open(path, O_RDWR | O_SYNC)) == -1) {
    return -1;
  }
  if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
    fprintf(stderr, "failed to lock %s\n", path);
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_pci_resource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    117f <open_pci_resource+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <open_pci_resource+0x95>
mov    -0x14(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
call   1060 <flock@plt>
cmp    $0xffffffff,%eax
jne    11cf <open_pci_resource+0x8f>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4a(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <open_pci_resource+0x95>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_pci_resource>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e9a(%rip),%rax
mov    0x2e8b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11ab <open_pci_resource+0x6b>
mov    %eax,%ebp
mov    0x2e67(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   1060 <flock@plt>
cmp    $0xffffffff,%eax
je     118c <open_pci_resource+0x4c>
mov    %ebp,%ebx
jmp    11ab <open_pci_resource+0x6b>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"failed to lock %s\0A\00"
define i32 @open_pci_resource(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 = load i32, i32* @O_SYNC
%8 = or i32 %6, %7
%9 = call i32 @open(i8* %5, i32 %8)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %27
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @LOCK_EX
%15 = load i32, i32* @LOCK_NB
%16 = or i32 %14, %15
%17 = call i32 @flock(i32 %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %3
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %2
br label %27
25: ; preds = %12
%26 = load i32, i32* %4
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %19, %11
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"failed to lock %s\0A\00"
define i32 @open_pci_resource(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_SYNC
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = load i32, i32* @LOCK_EX
%9 = load i32, i32* @LOCK_NB
%10 = or i32 %9, %8
%11 = tail call i32 @flock(i32 %5, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = tail call i32 @close(i32 %5)
br label %17
17: ; preds = %7, %1, %13
%18 = phi i32 [ -1, %13 ], [ -1, %1 ], [ %5, %7 ]
ret i32 %18
}