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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ read(int, void *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
ssize_t pv__transfer_read_repeated(int fd, void *buf, size_t count) {
  ssize_t total_read;
  total_read = 0;
  while (count > 0) {
    ssize_t nread;
    nread = read(fd, buf, count);
    if (nread < 0)
      return nread;
    total_read += nread;
    buf += nread;
    count -= nread;
    if (0 == nread)
      return total_read;
    if (count > 0) {
      fd_set readfds;
      struct timeval tv;
      tv.tv_sec = 0;
      tv.tv_usec = 0;
      FD_ZERO(&readfds);
      FD_SET(fd, &readfds);
      do {
      } while (0);
      if (select(fd + 1, &readfds, NULL, NULL, &tv) < 1)
        break;
    }
  }
  return total_read;
}


x86 assembly (clang, -O0):
<pv__transfer_read_repeated>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1239 <pv__transfer_read_repeated+0xf9>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1192 <pv__transfer_read_repeated+0x52>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1241 <pv__transfer_read_repeated+0x101>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    11d2 <pv__transfer_read_repeated+0x92>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1241 <pv__transfer_read_repeated+0x101>
cmpq   $0x0,-0x20(%rbp)
jbe    1234 <pv__transfer_read_repeated+0xf4>
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1060 <FD_SET@plt>
jmp    1207 <pv__transfer_read_repeated+0xc7>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x1,%eax
jge    122f <pv__transfer_read_repeated+0xef>
jmp    1239 <pv__transfer_read_repeated+0xf9>
jmp    1234 <pv__transfer_read_repeated+0xf4>
jmp    115b <pv__transfer_read_repeated+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pv__transfer_read_repeated>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rdx,%rdx
je     11c8 <pv__transfer_read_repeated+0x88>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
lea    0x1(%r12),%r13d
xor    %r14d,%r14d
lea    0xc(%rsp),%rbp
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
js     11cd <pv__transfer_read_repeated+0x8d>
add    %rax,%r14
test   %rax,%rax
je     11d0 <pv__transfer_read_repeated+0x90>
sub    %rax,%rbx
je     11d0 <pv__transfer_read_repeated+0x90>
add    %rax,%r15
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1050 <select@plt>
test   %eax,%eax
jg     1170 <pv__transfer_read_repeated+0x30>
jmp    11d0 <pv__transfer_read_repeated+0x90>
xor    %r14d,%r14d
jmp    11d0 <pv__transfer_read_repeated+0x90>
mov    %rax,%r14
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i64 @pv__transfer_read_repeated(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %12
12: ; preds = %55, %3
%13 = load i64, i64* %7
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %56
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = call i64 @read(i32 %16, i8* %17, i64 %18)
store i64 %19, i64* %9
%20 = load i64, i64* %9
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = load i64, i64* %9
store i64 %23, i64* %4
br label %58
24: ; preds = %15
%25 = load i64, i64* %9
%26 = load i64, i64* %8
%27 = add nsw i64 %26, %25
store i64 %27, i64* %8
%28 = load i64, i64* %9
%29 = load i8*, i8** %6
%30 = getelementptr i8, i8* %29, i64 %28
store i8* %30, i8** %6
%31 = load i64, i64* %9
%32 = load i64, i64* %7
%33 = sub i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %9
%35 = icmp eq i64 0, %34
br i1 %35, label %36, label %38
36: ; preds = %24
%37 = load i64, i64* %8
store i64 %37, i64* %4
br label %58
38: ; preds = %24
%39 = load i64, i64* %7
%40 = icmp ugt i64 %39, 0
br i1 %40, label %41, label %55
41: ; preds = %38
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %42
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i64 0, i64* %43
%44 = call i32 @FD_ZERO(i32* %10)
%45 = load i32, i32* %5
%46 = call i32 @FD_SET(i32 %45, i32* %10)
br label %47
47: ; preds = %41
br label %48
48: ; preds = %47
%49 = load i32, i32* %5
%50 = add nsw i32 %49, 1
%51 = call i32 @select(i32 %50, i32* %10, i32* null, i32* null, %struct.timeval* %11)
%52 = icmp slt i32 %51, 1
br i1 %52, label %53, label %54
53: ; preds = %48
br label %56
54: ; preds = %48
br label %55
55: ; preds = %54, %38
br label %12
56: ; preds = %53, %12
%57 = load i64, i64* %8
store i64 %57, i64* %4
br label %58
58: ; preds = %56, %36, %22
%59 = load i64, i64* %4
ret i64 %59
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i64 @pv__transfer_read_repeated(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = add nsw i32 %0, 1
%9 = bitcast %struct.timeval* %5 to i8*
%10 = icmp eq i64 %2, 0
br i1 %10, label %29, label %11
11: ; preds = %3, %23
%12 = phi i64 [ %18, %23 ], [ 0, %3 ]
%13 = phi i64 [ %19, %23 ], [ %2, %3 ]
%14 = phi i8* [ %24, %23 ], [ %1, %3 ]
%15 = call i64 @read(i32 %0, i8* %14, i64 %13)
%16 = icmp slt i64 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %11
%18 = add nsw i64 %15, %12
%19 = sub i64 %13, %15
%20 = icmp eq i64 %15, 0
%21 = icmp eq i64 %19, 0
%22 = or i1 %20, %21
br i1 %22, label %29, label %23
23: ; preds = %17
%24 = getelementptr i8, i8* %14, i64 %15
%25 = call i32 @FD_ZERO(i32* %4)
%26 = call i32 @FD_SET(i32 %0, i32* %4)
%27 = call i32 @select(i32 %8, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%28 = icmp slt i32 %27, 1
br i1 %28, label %29, label %11
29: ; preds = %11, %17, %23, %3
%30 = phi i64 [ 0, %3 ], [ %15, %11 ], [ %18, %17 ], [ %18, %23 ]
ret i64 %30
}