#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 */
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int open_device_control_file(int *fd, const char *pathname) {
  struct timespec ts;
  int i;
  int rc = 0;
  for (i = 0; i < 10; ++i) {
    *fd = open(pathname, O_WRONLY);
    if (*fd < 0) {
      fprintf(stderr, "open %s failed: %s\n", pathname, strerror(errno));
      if (errno == ENOENT) {
        ts.tv_sec = 0;
        ts.tv_nsec = 10 * 1000 * 1000;
        nanosleep(&ts, NULL);
        continue;
      } else {
        fprintf(stderr, "open %s failed: %s\n", pathname, strerror(errno));
        return -1;
      }
    }
    break;
  }
  return rc;
}


x86 assembly (clang, -O0):
<open_device_control_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0xa,-0x2c(%rbp)
jge    125b <open_device_control_file+0x11b>
mov    -0x18(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1248 <open_device_control_file+0x108>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe3d(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <open_device_control_file+0xc3>
movq   $0x0,-0x20(%rbp)
movl   $0x989680,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
jmp    124d <open_device_control_file+0x10d>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc9(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <open_device_control_file+0x121>
jmp    125b <open_device_control_file+0x11b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    115e <open_device_control_file+0x1e>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_device_control_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %rdi,%r14
mov    $0xa,%ebp
mov    0x2e78(%rip),%r12
mov    0x2e61(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1050 <open@plt>
mov    %eax,(%r14)
test   %eax,%eax
jns    11dd <open_device_control_file+0x9d>
mov    0x2e51(%rip),%rax
mov    (%rax),%ebx
mov    (%r12),%rdi
call   1060 <strerror@plt>
mov    %ebx,%edi
lea    0xe5d(%rip),%rsi
mov    %r13,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    (%r12),%rdi
cmp    (%r15),%rdi
jne    11e1 <open_device_control_file+0xa1>
movq   $0x0,0x10(%rsp)
movl   $0x989680,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    $0xffffffff,%ebp
jne    1170 <open_device_control_file+0x30>
jmp    1208 <open_device_control_file+0xc8>
xor    %ebx,%ebx
jmp    1208 <open_device_control_file+0xc8>
mov    0x2df8(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe0a(%rip),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"open %s failed: %s\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
define i32 @open_device_control_file(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.timespec
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 0, i32* %8
store i32 0, i32* %7
br label %9
9: ; preds = %40, %2
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %43
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
%16 = load i32*, i32** %4
store i32 %15, i32* %16
%17 = load i32*, i32** %4
%18 = load i32, i32* %17
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %39
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %5
%23 = load i64, i64* @errno
%24 = call i8* @strerror(i64 %23)
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24)
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENOENT
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 10000000, i32* %31
%32 = call i32 @nanosleep(%struct.timespec* %6, i32* null)
br label %40
33: ; preds = %20
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %5
%36 = load i64, i64* @errno
%37 = call i8* @strerror(i64 %36)
%38 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %37)
store i32 -1, i32* %3
br label %45
39: ; preds = %12
br label %43
40: ; preds = %29
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %9
43: ; preds = %39, %9
%44 = load i32, i32* %8
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %33
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"open %s failed: %s\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
define i32 @open_device_control_file(i32* nocapture writeonly %0, i8* %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
br label %7
7: ; preds = %2, %20
%8 = phi i32 [ 0, %2 ], [ %22, %20 ]
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %1, i32 %9)
store i32 %10, i32* %0
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = load i64, i64* @errno
%15 = call i8* @strerror(i64 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %15)
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %12
store i64 0, i64* %5
store i32 10000000, i32* %6
%21 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%22 = add nuw nsw i32 %8, 1
%23 = icmp eq i32 %22, 10
br i1 %23, label %28, label %7
24: ; preds = %12
%25 = load i32, i32* @stderr
%26 = call i8* @strerror(i64 %17)
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %26)
br label %28
28: ; preds = %20, %7, %24
%29 = phi i32 [ -1, %24 ], [ 0, %7 ], [ 0, %20 ]
ret i32 %29
}