#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_usec;
  int /*<<< orphan*/ tv_sec;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
size_t read(int, char *, size_t);
size_t select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
              int /*<<< orphan*/ *, struct timeval *);
ssize_t readagain(int fd, void *buf, size_t count) {
  ssize_t rc;
  size_t pos = 0;
  struct timeval timeout = {.tv_sec = 0, .tv_usec = 200000};
  fd_set fds;
  rc = read(fd, buf, count);
  if (rc > 0) {
    pos += rc;
  }
  while ((rc == -1 && errno == EAGAIN) || (rc >= 0 && pos < count)) {
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    rc = select(fd + 1, &fds, NULL, NULL, &timeout);
    if (rc == 0) {
      break;
    } else if (rc == -1) {
      continue;
    }
    rc = read(fd, ((char *)buf) + pos, count - pos);
    if (rc > 0) {
      pos += rc;
    }
  }
  return (pos == 0) ? -1 : pos;
}


x86 assembly (clang, -O0):
<readagain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0xe9e(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1191 <readagain+0x51>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1196 <readagain+0x56>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11c0 <readagain+0x80>
mov    0x2e38(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e1c(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x35(%rbp)
je     11e4 <readagain+0xa4>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x36(%rbp)
jb     11de <readagain+0x9e>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x36(%rbp)
mov    -0x36(%rbp),%al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    11f4 <readagain+0xb4>
jmp    1284 <readagain+0x144>
lea    -0x34(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1237 <readagain+0xf7>
jmp    1284 <readagain+0x144>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1247 <readagain+0x107>
jmp    1196 <readagain+0x56>
jmp    124c <readagain+0x10c>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    127f <readagain+0x13f>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1196 <readagain+0x56>
cmpq   $0x0,-0x28(%rbp)
jne    129f <readagain+0x15f>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x40(%rbp)
jmp    12a7 <readagain+0x167>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readagain>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %edi,%ebx
movq   $0x30d40,0x8(%rsp)
mov    %rsi,(%rsp)
call   1030 <read@plt>
lea    0x1(%rbx),%r12d
lea    0x14(%rsp),%rbp
lea    0x8(%rsp),%r13
mov    %rax,%r14
cmp    $0xffffffffffffffff,%rax
je     11b5 <readagain+0x75>
nopl   0x0(%rax)
cmp    %r15,%r14
jae    11f2 <readagain+0xb2>
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1050 <select@plt>
cmp    $0xffffffffffffffff,%rax
jne    11cd <readagain+0x8d>
cmp    $0xffffffffffffffff,%rax
jne    1180 <readagain+0x40>
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <readagain+0x40>
jmp    1185 <readagain+0x45>
test   %rax,%rax
je     11f2 <readagain+0xb2>
mov    (%rsp),%rax
lea    (%rax,%r14,1),%rsi
mov    %r15,%rdx
sub    %r14,%rdx
mov    %ebx,%edi
call   1030 <read@plt>
add    %rax,%r14
cmp    $0xffffffffffffffff,%rax
jne    1180 <readagain+0x40>
jmp    11b5 <readagain+0x75>
cmp    $0x1,%r14
sbb    %rax,%rax
or     %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.readagain.timeout = %struct.timeval { i32 200000, i32 0 }
@errno = global i64 0
@EAGAIN = global i64 0
define i64 @readagain(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.timeval
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %8
%11 = bitcast %struct.timeval* %9 to i8*
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i64 @read(i32 %12, i8* %13, i64 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = add i64 %20, %19
store i64 %21, i64* %8
br label %22
22: ; preds = %18, %3
br label %23
23: ; preds = %71, %54, %22
%24 = load i64, i64* %7
%25 = icmp eq i64 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %27, %28
br i1 %29, label %39, label %30
30: ; preds = %26, %23
%31 = load i64, i64* %7
%32 = icmp uge i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i64, i64* %8
%35 = load i64, i64* %6
%36 = icmp ult i64 %34, %35
br label %37
37: ; preds = %33, %30
%38 = phi i1 [ false, %30 ], [ %36, %33 ]
br label %39
39: ; preds = %37, %26
%40 = phi i1 [ true, %26 ], [ %38, %37 ]
br i1 %40, label %41, label %72
41: ; preds = %39
%42 = call i32 @FD_ZERO(i32* %10)
%43 = load i32, i32* %4
%44 = call i32 @FD_SET(i32 %43, i32* %10)
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
%47 = call i64 @select(i32 %46, i32* %10, i32* null, i32* null, %struct.timeval* %9)
store i64 %47, i64* %7
%48 = load i64, i64* %7
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %41
br label %72
51: ; preds = %41
%52 = load i64, i64* %7
%53 = icmp eq i64 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %51
br label %23
55: ; preds = %51
br label %56
56: ; preds = %55
%57 = load i32, i32* %4
%58 = load i8*, i8** %5
%59 = load i64, i64* %8
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = load i64, i64* %6
%62 = load i64, i64* %8
%63 = sub i64 %61, %62
%64 = call i64 @read(i32 %57, i8* %60, i64 %63)
store i64 %64, i64* %7
%65 = load i64, i64* %7
%66 = icmp ugt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %56
%68 = load i64, i64* %7
%69 = load i64, i64* %8
%70 = add i64 %69, %68
store i64 %70, i64* %8
br label %71
71: ; preds = %67, %56
br label %23
72: ; preds = %50, %39
%73 = load i64, i64* %8
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %72
br label %78
76: ; preds = %72
%77 = load i64, i64* %8
br label %78
78: ; preds = %76, %75
%79 = phi i64 [ -1, %75 ], [ %77, %76 ]
ret i64 %79
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EAGAIN = global i64 0
define i64 @readagain(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = alloca i32
%7 = bitcast i64* %4 to i8*
store i64 200000, i64* %4
%8 = bitcast i32* %6 to i8*
%9 = tail call i64 @read(i32 %0, i8* %1, i64 %2)
%10 = add nsw i32 %0, 1
br label %11
11: ; preds = %28, %3
%12 = phi i64 [ %31, %28 ], [ %9, %3 ]
%13 = phi i64 [ %32, %28 ], [ %9, %3 ]
%14 = icmp ult i64 %13, %2
br label %15
15: ; preds = %11, %24
%16 = phi i64 [ %27, %24 ], [ %12, %11 ]
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %19, %20
%22 = select i1 %21, i1 true, i1 %14
br i1 %22, label %24, label %33
23: ; preds = %15
br i1 %14, label %24, label %33
24: ; preds = %18, %23
%25 = call i32 @FD_ZERO(i32* %6)
%26 = call i32 @FD_SET(i32 %0, i32* %6)
%27 = call i64 @select(i32 %10, i32* %6, i32* null, i32* null, %struct.timeval* %5)
switch i64 %27, label %28 [
i64 0, label %33
i64 -1, label %15
]
28: ; preds = %24
%29 = getelementptr inbounds i8, i8* %1, i64 %13
%30 = sub i64 %2, %13
%31 = call i64 @read(i32 %0, i8* %29, i64 %30)
%32 = add i64 %31, %13
br label %11
33: ; preds = %24, %18, %23
%34 = icmp eq i64 %13, 0
%35 = select i1 %34, i64 -1, i64 %13
ret i64 %35
}