#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 stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, char const *, ...);
int /*<<< orphan*/ sleep(int);
int stat(char const *, struct stat *);
char *strerror(int /*<<< orphan*/);
void wait_for_device(const char *fn) {
  int tries = 0;
  int ret;
  struct stat buf;
  do {
    ++tries;
    ret = stat(fn, &buf);
    if (ret) {
      printf("stat %s try %d: %s\n", fn, tries, strerror(errno));
      sleep(1);
    }
  } while (ret && tries < 10);
  if (ret) {
    printf("failed to stat %s\n", fn);
  }
}


x86 assembly (clang, -O0):
<wait_for_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11b4 <wait_for_device+0x74>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11b9 <wait_for_device+0x79>
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x29(%rbp)
je     11d2 <wait_for_device+0x92>
cmpl   $0xa,-0xc(%rbp)
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1153 <wait_for_device+0x13>
cmpl   $0x0,-0x10(%rbp)
je     11f9 <wait_for_device+0xb9>
mov    -0x8(%rbp),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_device>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    0x2e82(%rip),%r14
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xe93(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xe55(%rip),%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xe17(%rip),%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xdd9(%rip),%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xd9b(%rip),%rdi
mov    %rbx,%rsi
mov    $0x5,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xd5d(%rip),%rdi
mov    %rbx,%rsi
mov    $0x6,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xd1f(%rip),%rdi
mov    %rbx,%rsi
mov    $0x7,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xce1(%rip),%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xca7(%rip),%rdi
mov    %rbx,%rsi
mov    $0x9,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
je     13c7 <wait_for_device+0x287>
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xc6d(%rip),%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xc5e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [20 x i8] c"stat %s try %d: %s\0A\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"failed to stat %s\0A\00"
define void @wait_for_device(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %2
store i32 0, i32* %3
br label %6
6: ; preds = %27, %1
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
store i32 %8, i32* %3
%9 = load i8*, i8** %2
%10 = call i32 @stat(i8* %9, %struct.stat* %5)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %6
%14 = load i8*, i8** %2
%15 = load i32, i32* %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15, i8* %17)
%19 = call i32 @sleep(i32 1)
br label %20
20: ; preds = %13, %6
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 10
br label %27
27: ; preds = %24, %21
%28 = phi i1 [ false, %21 ], [ %26, %24 ]
br i1 %28, label %6, label %29
29: ; preds = %27
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %2
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %29
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [20 x i8] c"stat %s try %d: %s\0A\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"failed to stat %s\0A\00"
define void @wait_for_device(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %75, label %6
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 1, i8* %8)
%10 = tail call i32 @sleep(i32 1)
%11 = call i32 @stat(i8* %0, %struct.stat* %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %75, label %13
13: ; preds = %6
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 2, i8* %15)
%17 = tail call i32 @sleep(i32 1)
%18 = call i32 @stat(i8* %0, %struct.stat* %2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %75, label %20
20: ; preds = %13
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 3, i8* %22)
%24 = tail call i32 @sleep(i32 1)
%25 = call i32 @stat(i8* %0, %struct.stat* %2)
%26 = icmp eq i32 %25, 0
br i1 %26, label %75, label %27
27: ; preds = %20
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 4, i8* %29)
%31 = tail call i32 @sleep(i32 1)
%32 = call i32 @stat(i8* %0, %struct.stat* %2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %75, label %34
34: ; preds = %27
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 5, i8* %36)
%38 = tail call i32 @sleep(i32 1)
%39 = call i32 @stat(i8* %0, %struct.stat* %2)
%40 = icmp eq i32 %39, 0
br i1 %40, label %75, label %41
41: ; preds = %34
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 6, i8* %43)
%45 = tail call i32 @sleep(i32 1)
%46 = call i32 @stat(i8* %0, %struct.stat* %2)
%47 = icmp eq i32 %46, 0
br i1 %47, label %75, label %48
48: ; preds = %41
%49 = load i32, i32* @errno
%50 = tail call i8* @strerror(i32 %49)
%51 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 7, i8* %50)
%52 = tail call i32 @sleep(i32 1)
%53 = call i32 @stat(i8* %0, %struct.stat* %2)
%54 = icmp eq i32 %53, 0
br i1 %54, label %75, label %55
55: ; preds = %48
%56 = load i32, i32* @errno
%57 = tail call i8* @strerror(i32 %56)
%58 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 8, i8* %57)
%59 = tail call i32 @sleep(i32 1)
%60 = call i32 @stat(i8* %0, %struct.stat* %2)
%61 = icmp eq i32 %60, 0
br i1 %61, label %75, label %62
62: ; preds = %55
%63 = load i32, i32* @errno
%64 = tail call i8* @strerror(i32 %63)
%65 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 9, i8* %64)
%66 = tail call i32 @sleep(i32 1)
%67 = call i32 @stat(i8* %0, %struct.stat* %2)
%68 = icmp eq i32 %67, 0
br i1 %68, label %75, label %69
69: ; preds = %62
%70 = load i32, i32* @errno
%71 = tail call i8* @strerror(i32 %70)
%72 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i32 10, i8* %71)
%73 = tail call i32 @sleep(i32 1)
%74 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %75
75: ; preds = %1, %6, %13, %20, %27, %34, %41, %48, %55, %62, %69
ret void
}