#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 timeval {
  int tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void wait_firmware(int fd) {
  unsigned char ready;
  fd_set rfds;
  int ret;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  while (1) {
    struct timeval tv = {.tv_sec = 10};
    ret = select(fd + 1, &rfds, NULL, NULL, &tv);
    if (ret < 0) {
      if (errno == EINTR)
        continue;
      err(EXIT_FAILURE, "cannot select");
    } else if (!ret)
      errx(EXIT_FAILURE, "ready timeout");
    break;
  }
  if (read(fd, &ready, 1) != 1)
    errx(EXIT_FAILURE, "cannot read the waiting byte");
  if (ready != 0xff)
    errx(EXIT_FAILURE, "invalid ready byte");
}


x86 assembly (clang, -O0):
<wait_firmware>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0xe7a(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e9 <wait_firmware+0x89>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
jne    11cf <wait_firmware+0x6f>
jmp    1180 <wait_firmware+0x20>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1070 <err@plt>
jmp    120d <wait_firmware+0xad>
cmpl   $0x0,-0x10(%rbp)
jne    1208 <wait_firmware+0xa8>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1030 <errx@plt>
jmp    120d <wait_firmware+0xad>
jmp    1212 <wait_firmware+0xb2>
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1241 <wait_firmware+0xe1>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1030 <errx@plt>
movzbl -0x5(%rbp),%eax
cmp    $0xff,%eax
je     1265 <wait_firmware+0x105>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1030 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_firmware>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%r14),%ebp
movl   $0xa,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
jns    11f7 <wait_firmware+0x97>
mov    0x2e2d(%rip),%r12
mov    0x2e16(%rip),%r13
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1212 <wait_firmware+0xb2>
movl   $0xa,0x10(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1060 <select@plt>
test   %eax,%eax
js     11d0 <wait_firmware+0x70>
test   %eax,%eax
jne    1227 <wait_firmware+0xc7>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1030 <errx@plt>
jmp    1227 <wait_firmware+0xc7>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
call   1070 <err@plt>
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1253 <wait_firmware+0xf3>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   1030 <errx@plt>
cmpb   $0xff,0xf(%rsp)
je     126f <wait_firmware+0x10f>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1030 <errx@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@__const.wait_firmware.tv = %struct.timeval { i32 10 }
@errno = global i64 0
@EINTR = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"cannot select\00"
@.str.1 = [14 x i8] c"ready timeout\00"
@.str.2 = [29 x i8] c"cannot read the waiting byte\00"
@.str.3 = [19 x i8] c"invalid ready byte\00"
define void @wait_firmware(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
store i32 %0, i32* %2
%7 = call i32 @FD_ZERO(i32* %4)
%8 = load i32, i32* %2
%9 = call i32 @FD_SET(i32 %8, i32* %4)
br label %10
10: ; preds = %1, %21
%11 = bitcast %struct.timeval* %6 to i8*
%12 = load i32, i32* %2
%13 = add nsw i32 %12, 1
%14 = call i32 @select(i32 %13, i32* %4, i32* null, i32* null, %struct.timeval* %6)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
br label %10
22: ; preds = %17
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @err(i32 %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %32
25: ; preds = %10
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @errx(i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %28, %25
br label %32
32: ; preds = %31, %22
br label %33
33: ; preds = %32
%34 = load i32, i32* %2
%35 = call i32 @read(i32 %34, i8* %3, i32 1)
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @errx(i32 %38, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %37, %33
%41 = load i8, i8* %3
%42 = zext i8 %41 to i32
%43 = icmp ne i32 %42, 255
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @errx(i32 %45, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %44, %40
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"cannot select\00"
@.str.1 = [14 x i8] c"ready timeout\00"
@.str.2 = [29 x i8] c"cannot read the waiting byte\00"
@.str.3 = [19 x i8] c"invalid ready byte\00"
define void @wait_firmware(i32 %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = call i32 @FD_ZERO(i32* %3)
%7 = call i32 @FD_SET(i32 %0, i32* %3)
%8 = bitcast %struct.timeval* %4 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%10 = add nsw i32 %0, 1
store i32 10, i32* %9
%11 = call i32 @select(i32 %10, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %1, %26
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %26, label %17
17: ; preds = %13
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @err(i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %29
20: ; preds = %26, %1
%21 = phi i32 [ %11, %1 ], [ %27, %26 ]
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @errx(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %29
26: ; preds = %13
store i32 10, i32* %9
%27 = call i32 @select(i32 %10, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %13, label %20
29: ; preds = %20, %23, %17
%30 = call i32 @read(i32 %0, i8* %2, i32 1)
%31 = icmp eq i32 %30, 1
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @errx(i32 %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %29
%36 = load i8, i8* %2
%37 = icmp eq i8 %36, -1
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @errx(i32 %39, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %38, %35
ret void
}