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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EPIPE;
scalar_t__ ETIMEDOUT;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int read(int, char *, size_t);
int select_retry(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/ *, struct timeval *);
ssize_t read_retry(int sockfd, void *buf, size_t count,
                   struct timeval *timeout) {
  int n, rv = 0;
  ssize_t total = 0, remain = count;
  fd_set rfds, xfds;
  memset(buf, 0, count);
  while (total < count) {
    FD_ZERO(&rfds);
    FD_SET(sockfd, &rfds);
    FD_ZERO(&xfds);
    FD_SET(sockfd, &xfds);
    rv = select_retry(sockfd + 1, &rfds, NULL, &xfds, timeout);
    if (rv == -1)
      return -1;
    else if (rv == 0) {
      errno = ETIMEDOUT;
      return -1;
    }
    if (FD_ISSET(sockfd, &xfds)) {
      errno = EPIPE;
      return -1;
    }
    n = read(sockfd, (char *)buf + (off_t)total, remain);
    if ((n == 0) && (rv == 1)) {
      errno = EPIPE;
      return -1;
    }
    if (n == -1) {
      if ((errno == EAGAIN) || (errno == EINTR)) {
        continue;
      }
      return -1;
    }
    total += n;
    remain -= n;
  }
  return total;
}


x86 assembly (clang, -O0):
<read_retry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1332 <read_retry+0x1d2>
lea    -0x44(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1080 <FD_SET@plt>
lea    -0x48(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%r8
lea    -0x44(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x48(%rbp),%rcx
call   1050 <select_retry@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    120a <read_retry+0xaa>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    133a <read_retry+0x1da>
cmpl   $0x0,-0x30(%rbp)
jne    1235 <read_retry+0xd5>
mov    0x2db5(%rip),%rax
mov    (%rax),%rcx
mov    0x2db3(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    133a <read_retry+0x1da>
jmp    123a <read_retry+0xda>
mov    -0xc(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1271 <read_retry+0x111>
mov    0x2d89(%rip),%rax
mov    (%rax),%rcx
mov    0x2d77(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    133a <read_retry+0x1da>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rax
cltq
add    %rax,%rsi
mov    -0x40(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12c2 <read_retry+0x162>
cmpl   $0x1,-0x30(%rbp)
jne    12c2 <read_retry+0x162>
mov    0x2d38(%rip),%rax
mov    (%rax),%rcx
mov    0x2d26(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    133a <read_retry+0x1da>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1312 <read_retry+0x1b2>
mov    0x2d05(%rip),%rax
mov    (%rax),%rax
mov    0x2cdb(%rip),%rcx
cmp    (%rcx),%rax
je     1300 <read_retry+0x1a0>
mov    0x2ceb(%rip),%rax
mov    (%rax),%rax
mov    0x2cd1(%rip),%rcx
cmp    (%rcx),%rax
jne    1305 <read_retry+0x1a5>
jmp    119d <read_retry+0x3d>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    133a <read_retry+0x1da>
movslq -0x2c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq -0x2c(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    119d <read_retry+0x3d>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_retry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,0x38(%rsp)
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %edi,%ebx
xor    %r13d,%r13d
mov    %rsi,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
test   %rbp,%rbp
je     12c2 <read_retry+0x162>
lea    0x1(%rbx),%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
mov    %rax,0x10(%rsp)
lea    0x44(%rsp),%r12
lea    0x40(%rsp),%r15
mov    0x2e28(%rip),%rax
mov    %r14,0x28(%rsp)
mov    %r14,0x18(%rsp)
mov    %rbp,0x30(%rsp)
mov    %rbp,0x20(%rsp)
mov    %rax,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1080 <FD_SET@plt>
mov    %r15,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
mov    0xc(%rsp),%edi
mov    %r12,%rsi
xor    %edx,%edx
mov    %r15,%rcx
mov    0x38(%rsp),%r8
call   1050 <select_retry@plt>
mov    $0xffffffffffffffff,%r13
cmp    $0xffffffff,%eax
je     12c2 <read_retry+0x162>
mov    %eax,%r14d
test   %eax,%eax
je     12b4 <read_retry+0x154>
mov    %ebx,%edi
mov    %r15,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
jne    12ab <read_retry+0x14b>
mov    %ebx,%edi
mov    0x18(%rsp),%rsi
mov    0x20(%rsp),%rdx
call   1060 <read@plt>
test   %eax,%eax
jne    124f <read_retry+0xef>
cmp    $0x1,%r14d
je     12ab <read_retry+0x14b>
cmp    $0xffffffff,%eax
jne    127a <read_retry+0x11a>
mov    0x0(%rbp),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <read_retry+0x70>
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <read_retry+0x70>
jmp    12c2 <read_retry+0x162>
cltq
mov    0x10(%rsp),%rcx
add    %rax,%rcx
sub    %rax,0x20(%rsp)
movslq %ecx,%rax
add    0x28(%rsp),%rax
mov    %rax,0x18(%rsp)
mov    %rcx,%r13
mov    %rcx,0x10(%rsp)
cmp    0x30(%rsp),%rcx
jb     11d0 <read_retry+0x70>
jmp    12c2 <read_retry+0x162>
mov    0x2d2e(%rip),%rax
jmp    12bb <read_retry+0x15b>
mov    0x2d15(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x0(%rbp)
mov    %r13,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@ETIMEDOUT = global i64 0
@errno = global i64 0
@EPIPE = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i64 @read_retry(i32 %0, i8* %1, i64 %2, %struct.timeval* %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.timeval*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store %struct.timeval* %3, %struct.timeval** %9
store i32 0, i32* %11
store i64 0, i64* %12
%16 = load i64, i64* %8
store i64 %16, i64* %13
%17 = load i8*, i8** %7
%18 = load i64, i64* %8
%19 = call i32 @memset(i8* %17, i32 0, i64 %18)
br label %20
20: ; preds = %79, %77, %4
%21 = load i64, i64* %12
%22 = load i64, i64* %8
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %88
24: ; preds = %20
%25 = call i32 @FD_ZERO(i32* %14)
%26 = load i32, i32* %6
%27 = call i32 @FD_SET(i32 %26, i32* %14)
%28 = call i32 @FD_ZERO(i32* %15)
%29 = load i32, i32* %6
%30 = call i32 @FD_SET(i32 %29, i32* %15)
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
%33 = load %struct.timeval*, %struct.timeval** %9
%34 = call i32 @select_retry(i32 %32, i32* %14, i32* null, i32* %15, %struct.timeval* %33)
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %24
store i64 -1, i64* %5
br label %90
38: ; preds = %24
%39 = load i32, i32* %11
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i64, i64* @ETIMEDOUT
store i64 %42, i64* @errno
store i64 -1, i64* %5
br label %90
43: ; preds = %38
br label %44
44: ; preds = %43
%45 = load i32, i32* %6
%46 = call i64 @FD_ISSET(i32 %45, i32* %15)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i64, i64* @EPIPE
store i64 %49, i64* @errno
store i64 -1, i64* %5
br label %90
50: ; preds = %44
%51 = load i32, i32* %6
%52 = load i8*, i8** %7
%53 = load i64, i64* %12
%54 = trunc i64 %53 to i32
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
%57 = load i64, i64* %13
%58 = call i32 @read(i32 %51, i8* %56, i64 %57)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %50
%62 = load i32, i32* %11
%63 = icmp eq i32 %62, 1
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = load i64, i64* @EPIPE
store i64 %65, i64* @errno
store i64 -1, i64* %5
br label %90
66: ; preds = %61, %50
%67 = load i32, i32* %10
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %79
69: ; preds = %66
%70 = load i64, i64* @errno
%71 = load i64, i64* @EAGAIN
%72 = icmp eq i64 %70, %71
br i1 %72, label %77, label %73
73: ; preds = %69
%74 = load i64, i64* @errno
%75 = load i64, i64* @EINTR
%76 = icmp eq i64 %74, %75
br i1 %76, label %77, label %78
77: ; preds = %73, %69
br label %20
78: ; preds = %73
store i64 -1, i64* %5
br label %90
79: ; preds = %66
%80 = load i32, i32* %10
%81 = sext i32 %80 to i64
%82 = load i64, i64* %12
%83 = add i64 %82, %81
store i64 %83, i64* %12
%84 = load i32, i32* %10
%85 = sext i32 %84 to i64
%86 = load i64, i64* %13
%87 = sub i64 %86, %85
store i64 %87, i64* %13
br label %20
88: ; preds = %20
%89 = load i64, i64* %12
store i64 %89, i64* %5
br label %90
90: ; preds = %88, %78, %64, %48, %41, %37
%91 = load i64, i64* %5
ret i64 %91
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@ETIMEDOUT = global i64 0
@errno = global i64 0
@EPIPE = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i64 @read_retry(i32 %0, i8* %1, i64 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%10 = add nsw i32 %0, 1
%11 = icmp eq i64 %2, 0
br i1 %11, label %50, label %12
12: ; preds = %4, %39
%13 = phi i8* [ %46, %39 ], [ %1, %4 ]
%14 = phi i64 [ %42, %39 ], [ %2, %4 ]
%15 = phi i64 [ %41, %39 ], [ 0, %4 ]
br label %16
16: ; preds = %12, %32
%17 = call i32 @FD_ZERO(i32* %5)
%18 = call i32 @FD_SET(i32 %0, i32* %5)
%19 = call i32 @FD_ZERO(i32* %6)
%20 = call i32 @FD_SET(i32 %0, i32* %6)
%21 = call i32 @select_retry(i32 %10, i32* %5, i32* null, i32* %6, %struct.timeval* %3)
switch i32 %21, label %22 [
i32 -1, label %50
i32 0, label %47
]
22: ; preds = %16
%23 = call i64 @FD_ISSET(i32 %0, i32* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %22
%26 = call i32 @read(i32 %0, i8* %13, i64 %14)
%27 = icmp eq i32 %26, 0
%28 = icmp eq i32 %21, 1
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %47, label %30
30: ; preds = %25
%31 = icmp eq i32 %26, -1
br i1 %31, label %32, label %39
32: ; preds = %30
%33 = load i64, i64* @errno
%34 = load i64, i64* @EAGAIN
%35 = icmp eq i64 %33, %34
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %33, %36
%38 = select i1 %35, i1 true, i1 %37
br i1 %38, label %16, label %50
39: ; preds = %30
%40 = sext i32 %26 to i64
%41 = add i64 %15, %40
%42 = sub i64 %14, %40
%43 = icmp ult i64 %41, %2
%44 = shl i64 %41, 32
%45 = ashr exact i64 %44, 32
%46 = getelementptr inbounds i8, i8* %1, i64 %45
br i1 %43, label %12, label %50
47: ; preds = %25, %22, %16
%48 = phi i64* [ @ETIMEDOUT, %16 ], [ @EPIPE, %22 ], [ @EPIPE, %25 ]
%49 = load i64, i64* %48
store i64 %49, i64* @errno
br label %50
50: ; preds = %39, %16, %32, %4, %47
%51 = phi i64 [ -1, %47 ], [ 0, %4 ], [ -1, %32 ], [ -1, %16 ], [ %41, %39 ]
ret i64 %51
}