#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__ ENOENT;
scalar_t__ errno;
int open(char const *, int);
int /*<<< orphan*/ usleep(int);
int open_udev_device(const char *path, int flags, int timeout) {
  int fd, step = 10000;
  fd = open(path, flags);
  while (fd == -1 && errno == ENOENT && timeout >= step) {
    usleep(step);
    timeout -= step;
    step *= 2;
    fd = open(path, flags);
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_udev_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x2710,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <open@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jne    1182 <open_udev_device+0x62>
mov    0x2e82(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e66(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x19(%rbp)
jne    1182 <open_udev_device+0x62>
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
setge  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1192 <open_udev_device+0x72>
jmp    11c2 <open_udev_device+0xa2>
mov    -0x18(%rbp),%edi
call   1040 <usleep@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <open@plt>
mov    %eax,-0x14(%rbp)
jmp    1148 <open_udev_device+0x28>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_udev_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
call   1030 <open@plt>
cmp    $0xffffffff,%eax
jne    117c <open_udev_device+0x5c>
mov    $0x2710,%ebp
mov    0x2e97(%rip),%r12
mov    0x2e80(%rip),%r13
mov    (%r12),%rcx
mov    $0xffffffff,%eax
cmp    0x0(%r13),%rcx
jne    117c <open_udev_device+0x5c>
sub    %ebp,%ebx
jl     117c <open_udev_device+0x5c>
mov    %ebp,%edi
call   1040 <usleep@plt>
add    %ebp,%ebp
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <open@plt>
cmp    $0xffffffff,%eax
je     1150 <open_udev_device+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOENT = global i64 0
define i32 @open_udev_device(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 10000, i32* %8
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
br label %12
12: ; preds = %25, %3
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* %6
%21 = load i32, i32* %8
%22 = icmp sge i32 %20, %21
br label %23
23: ; preds = %19, %15, %12
%24 = phi i1 [ false, %15 ], [ false, %12 ], [ %22, %19 ]
br i1 %24, label %25, label %36
25: ; preds = %23
%26 = load i32, i32* %8
%27 = call i32 @usleep(i32 %26)
%28 = load i32, i32* %8
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, %28
store i32 %30, i32* %6
%31 = load i32, i32* %8
%32 = mul nsw i32 %31, 2
store i32 %32, i32* %8
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %7
br label %12
36: ; preds = %23
%37 = load i32, i32* %7
ret i32 %37
}

LLVM IR (-O2):
@errno = global i64 0
@ENOENT = global i64 0
define i32 @open_udev_device(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 @open(i8* %0, i32 %1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %20
6: ; preds = %3, %14
%7 = phi i32 [ %17, %14 ], [ 10000, %3 ]
%8 = phi i32 [ %16, %14 ], [ %2, %3 ]
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp ne i64 %9, %10
%12 = icmp slt i32 %8, %7
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %20, label %14
14: ; preds = %6
%15 = tail call i32 @usleep(i32 %7)
%16 = sub nsw i32 %8, %7
%17 = shl nsw i32 %7, 1
%18 = tail call i32 @open(i8* %0, i32 %1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %6, label %20
20: ; preds = %6, %14, %3
%21 = phi i32 [ %4, %3 ], [ %18, %14 ], [ -1, %6 ]
ret i32 %21
}