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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ gettimeofday(struct timeval *, void *);
int select(int, int /*<<< orphan*/ *, void *, void *, struct timeval *);
int /*<<< orphan*/ vbi_capture_io_update_timeout(struct timeval *,
                                                 struct timeval *);
int vbi_capture_io_select(int fd, struct timeval *timeout) {
  struct timeval tv_start;
  struct timeval tv;
  fd_set fds;
  int ret;
  while (1) {
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    tv = *timeout;
    gettimeofday(&tv_start, ((void *)0));
    ret = select(fd + 1, &fds, ((void *)0), ((void *)0), &tv);
    vbi_capture_io_update_timeout(timeout, &tv_start);
    if ((ret < 0) && (errno == EINTR))
      continue;
    return ret;
  }
}


x86 assembly (clang, -O0):
<vbi_capture_io_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <vbi_capture_io_update_timeout@plt>
cmpl   $0x0,-0x28(%rbp)
jge    11dd <vbi_capture_io_select+0x8d>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <vbi_capture_io_select+0x8d>
jmp    115f <vbi_capture_io_select+0xf>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vbi_capture_io_select>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0x1(%r14),%r15d
lea    0x14(%rsp),%rbp
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    %r12,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1050 <select@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <vbi_capture_io_update_timeout@plt>
test   %r13d,%r13d
jns    11e0 <vbi_capture_io_select+0x90>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <vbi_capture_io_select+0x30>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @vbi_capture_io_select(i32 %0, %struct.timeval* %1) {
%3 = alloca i32
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store %struct.timeval* %1, %struct.timeval** %4
br label %9
9: ; preds = %2, %28
%10 = call i32 @FD_ZERO(i32* %7)
%11 = load i32, i32* %3
%12 = call i32 @FD_SET(i32 %11, i32* %7)
%13 = load %struct.timeval*, %struct.timeval** %4
%14 = bitcast %struct.timeval* %6 to i8*
%15 = bitcast %struct.timeval* %13 to i8*
%16 = call i32 @gettimeofday(%struct.timeval* %5, i8* null)
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
%19 = call i32 @select(i32 %18, i32* %7, i8* null, i8* null, %struct.timeval* %6)
store i32 %19, i32* %8
%20 = load %struct.timeval*, %struct.timeval** %4
%21 = call i32 @vbi_capture_io_update_timeout(%struct.timeval* %20, %struct.timeval* %5)
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %9
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %9
29: ; preds = %24, %9
%30 = load i32, i32* %8
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @vbi_capture_io_select(i32 %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%11 = add nsw i32 %0, 1
br label %12
12: ; preds = %20, %2
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_SET(i32 %0, i32* %5)
%15 = load i32, i32* %9
store i32 %15, i32* %10
%16 = call i32 @gettimeofday(%struct.timeval* %3, i8* null)
%17 = call i32 @select(i32 %11, i32* %5, i8* null, i8* null, %struct.timeval* %4)
%18 = call i32 @vbi_capture_io_update_timeout(%struct.timeval* %1, %struct.timeval* %3)
%19 = icmp slt i32 %17, 0
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %12, label %24
24: ; preds = %20, %12
ret i32 %17
}