#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 O_EXCL;
int O_NONBLOCK;
int O_RDONLY;
double RAND_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int);
double rand();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int openexcl(const char *device, int mode) {
  int f, i;
  f = open(device, mode | O_EXCL);
  if (f < 0) {
    f = open(device, mode);
    if (f >= 0) {
      close(f);
      f = -1;
      for (i = 0; (i < 10) && (f == -1); i++) {
        fprintf(
            stderr,
            "Error trying to open %s exclusively ... retrying in 1 second.\n",
            device);
        usleep(1000000 + 100000.0 * rand() / (RAND_MAX + 1.0));
        f = open(device, O_RDONLY | O_NONBLOCK | O_EXCL);
      }
    }
  }
  return f;
}


x86 assembly (clang, -O0):
<openexcl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1270 <openexcl+0x120>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     126b <openexcl+0x11b>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xa,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jge    11c9 <openexcl+0x79>
cmpl   $0xffffffff,-0x10(%rbp)
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11d9 <openexcl+0x89>
jmp    1266 <openexcl+0x116>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe2b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1060 <rand@plt>
movsd  0xe0f(%rip),%xmm1
mulsd  %xmm0,%xmm1
movsd  0xdfb(%rip),%xmm0
mov    0x2dc4(%rip),%rax
addsd  (%rax),%xmm0
divsd  %xmm0,%xmm1
movsd  0xddc(%rip),%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1070 <usleep@plt>
mov    -0x8(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2da3(%rip),%rax
or     (%rax),%esi
mov    0x2d72(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b0 <openexcl+0x60>
jmp    126b <openexcl+0x11b>
jmp    1270 <openexcl+0x120>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openexcl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
or     %ebp,%esi
call   1050 <open@plt>
test   %eax,%eax
js     1183 <openexcl+0x33>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <open@plt>
test   %eax,%eax
js     1174 <openexcl+0x24>
mov    %eax,%edi
call   1030 <close@plt>
lea    0xe79(%rip),%r14
mov    0x2e32(%rip),%r13
mov    0x2e33(%rip),%rbp
mov    0x2e34(%rip),%r15
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1060 <rand@plt>
mulsd  0xe1d(%rip),%xmm0
movsd  0x0(%r13),%xmm1
addsd  0xe17(%rip),%xmm1
divsd  %xmm1,%xmm0
addsd  0xe13(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1070 <usleep@plt>
mov    (%r15),%esi
or     0x0(%rbp),%esi
mov    0x2dad(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    1174 <openexcl+0x24>
lea    0x1(%r12),%ecx
cmp    $0x9,%r12d
mov    %ecx,%r12d
jb     11c0 <openexcl+0x70>
jmp    1174 <openexcl+0x24>

LLVM IR (-O0):
@O_EXCL = global i32 0
@stderr = global i32 0
@.str = [63 x i8] c"Error trying to open %s exclusively ... retrying in 1 second.\0A\00"
@RAND_MAX = global double 0.000000e+00
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @openexcl(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* %4
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %55
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = load i32, i32* %4
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %54
20: ; preds = %14
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %5
store i32 0, i32* %6
br label %23
23: ; preds = %50, %20
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 10
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, -1
br label %29
29: ; preds = %26, %23
%30 = phi i1 [ false, %23 ], [ %28, %26 ]
br i1 %30, label %31, label %53
31: ; preds = %29
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %3
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i8* %33)
%35 = call double (...) @rand()
%36 = fmul double 1.000000e+05, %35
%37 = load double, double* @RAND_MAX
%38 = fadd double %37, 1.000000e+00
%39 = fdiv double %36, %38
%40 = fadd double 1.000000e+06, %39
%41 = fptosi double %40 to i32
%42 = call i32 @usleep(i32 %41)
%43 = load i8*, i8** %3
%44 = load i32, i32* @O_RDONLY
%45 = load i32, i32* @O_NONBLOCK
%46 = or i32 %44, %45
%47 = load i32, i32* @O_EXCL
%48 = or i32 %46, %47
%49 = call i32 @open(i8* %43, i32 %48)
store i32 %49, i32* %5
br label %50
50: ; preds = %31
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %23
53: ; preds = %29
br label %54
54: ; preds = %53, %14
br label %55
55: ; preds = %54, %2
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@O_EXCL = global i32 0
@stderr = global i32 0
@.str = [63 x i8] c"Error trying to open %s exclusively ... retrying in 1 second.\0A\00"
@RAND_MAX = global double 0.000000e+00
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @openexcl(i8* %0, i32 %1) {
%3 = load i32, i32* @O_EXCL
%4 = or i32 %3, %1
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %34
7: ; preds = %2
%8 = tail call i32 @open(i8* %0, i32 %1)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %34
10: ; preds = %7
%11 = tail call i32 @close(i32 %8)
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ 0, %10 ], [ %30, %12 ]
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = tail call double (...) @rand()
%17 = fmul double %16, 1.000000e+05
%18 = load double, double* @RAND_MAX
%19 = fadd double %18, 1.000000e+00
%20 = fdiv double %17, %19
%21 = fadd double %20, 1.000000e+06
%22 = fptosi double %21 to i32
%23 = tail call i32 @usleep(i32 %22)
%24 = load i32, i32* @O_RDONLY
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = load i32, i32* @O_EXCL
%28 = or i32 %26, %27
%29 = tail call i32 @open(i8* %0, i32 %28)
%30 = add nuw nsw i32 %13, 1
%31 = icmp ult i32 %13, 9
%32 = icmp eq i32 %29, -1
%33 = and i1 %32, %31
br i1 %33, label %12, label %34
34: ; preds = %12, %7, %2
%35 = phi i32 [ %8, %7 ], [ %5, %2 ], [ %29, %12 ]
ret i32 %35
}