#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 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*/ *);
int _select_retry(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
scalar_t__ errno;
int read(int, void *, int);
ssize_t _read_retry(int sockfd, void *buf, int count, struct timeval *timeout) {
  int n, total = 0, remain = count, rv = 0;
  fd_set rfds, xfds;
  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, 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    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12fe <_read_retry+0x1ae>
lea    -0x34(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1070 <FD_SET@plt>
lea    -0x38(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%r8
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x38(%rbp),%rcx
call   1040 <_select_retry@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11e4 <_read_retry+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <_read_retry+0x1b4>
cmpl   $0x0,-0x30(%rbp)
jne    120e <_read_retry+0xbe>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rcx
mov    0x2dd9(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <_read_retry+0x1b4>
jmp    1213 <_read_retry+0xc3>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1249 <_read_retry+0xf9>
mov    0x2db0(%rip),%rax
mov    (%rax),%rcx
mov    0x2d9e(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <_read_retry+0x1b4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1296 <_read_retry+0x146>
cmpl   $0x1,-0x30(%rbp)
jne    1296 <_read_retry+0x146>
mov    0x2d63(%rip),%rax
mov    (%rax),%rcx
mov    0x2d51(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <_read_retry+0x1b4>
cmpl   $0xffffffff,-0x24(%rbp)
jne    12e5 <_read_retry+0x195>
mov    0x2d31(%rip),%rax
mov    (%rax),%rax
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%rax
je     12d4 <_read_retry+0x184>
mov    0x2d17(%rip),%rax
mov    (%rax),%rax
mov    0x2cfd(%rip),%rcx
cmp    (%rcx),%rax
jne    12d9 <_read_retry+0x189>
jmp    117a <_read_retry+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <_read_retry+0x1b4>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
jmp    117a <_read_retry+0x2a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read_retry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x18(%rsp)
test   %edx,%edx
jle    1264 <_read_retry+0x114>
mov    %edi,%ebp
lea    0x1(%rbp),%eax
mov    %eax,0x4(%rsp)
xor    %ecx,%ecx
lea    0x24(%rsp),%r15
lea    0x20(%rsp),%r12
mov    %rsi,0x8(%rsp)
mov    %rsi,%r13
mov    %edx,(%rsp)
mov    %edx,%r14d
mov    %rcx,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1070 <FD_SET@plt>
mov    %r12,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   1070 <FD_SET@plt>
mov    0x4(%rsp),%edi
mov    %r15,%rsi
xor    %edx,%edx
mov    %r12,%rcx
mov    0x18(%rsp),%r8
call   1040 <_select_retry@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1271 <_read_retry+0x121>
test   %ebx,%ebx
je     1275 <_read_retry+0x125>
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
jne    1268 <_read_retry+0x118>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    1212 <_read_retry+0xc2>
cmp    $0x1,%ebx
je     1268 <_read_retry+0x118>
cmp    $0xffffffff,%eax
jne    1243 <_read_retry+0xf3>
mov    0x2dba(%rip),%rax
mov    (%rax),%rax
mov    0x2d90(%rip),%rcx
cmp    (%rcx),%rax
je     11a0 <_read_retry+0x50>
mov    0x2d90(%rip),%rcx
cmp    (%rcx),%rax
je     11a0 <_read_retry+0x50>
jmp    1289 <_read_retry+0x139>
mov    0x10(%rsp),%rcx
add    %eax,%ecx
sub    %eax,%r14d
movslq %ecx,%rax
mov    0x8(%rsp),%rdx
lea    (%rdx,%rax,1),%r13
cmp    (%rsp),%eax
jl     118e <_read_retry+0x3e>
jmp    128e <_read_retry+0x13e>
xor    %ecx,%ecx
jmp    128e <_read_retry+0x13e>
mov    0x2d71(%rip),%rax
jmp    127c <_read_retry+0x12c>
mov    %ebx,%ecx
jmp    128e <_read_retry+0x13e>
mov    0x2d54(%rip),%rax
mov    (%rax),%rax
mov    0x2d52(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x28,%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 i32 @_read_retry(i32 %0, i8* %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.timeval*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.timeval* %3, %struct.timeval** %9
store i32 0, i32* %11
%16 = load i32, i32* %8
store i32 %16, i32* %12
store i32 0, i32* %13
br label %17
17: ; preds = %75, %73, %4
%18 = load i32, i32* %11
%19 = load i32, i32* %8
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %82
21: ; preds = %17
%22 = call i32 @FD_ZERO(i32* %14)
%23 = load i32, i32* %6
%24 = call i32 @FD_SET(i32 %23, i32* %14)
%25 = call i32 @FD_ZERO(i32* %15)
%26 = load i32, i32* %6
%27 = call i32 @FD_SET(i32 %26, i32* %15)
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
%30 = load %struct.timeval*, %struct.timeval** %9
%31 = call i32 @_select_retry(i32 %29, i32* %14, i32* null, i32* %15, %struct.timeval* %30)
store i32 %31, i32* %13
%32 = load i32, i32* %13
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %21
store i32 -1, i32* %5
br label %84
35: ; preds = %21
%36 = load i32, i32* %13
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i64, i64* @ETIMEDOUT
store i64 %39, i64* @errno
store i32 -1, i32* %5
br label %84
40: ; preds = %35
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = call i64 @FD_ISSET(i32 %42, i32* %15)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i64, i64* @EPIPE
store i64 %46, i64* @errno
store i32 -1, i32* %5
br label %84
47: ; preds = %41
%48 = load i32, i32* %6
%49 = load i8*, i8** %7
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = getelementptr i8, i8* %49, i64 %51
%53 = load i32, i32* %12
%54 = call i32 @read(i32 %48, i8* %52, i32 %53)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %47
%58 = load i32, i32* %13
%59 = icmp eq i32 %58, 1
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = load i64, i64* @EPIPE
store i64 %61, i64* @errno
store i32 -1, i32* %5
br label %84
62: ; preds = %57, %47
%63 = load i32, i32* %10
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %75
65: ; preds = %62
%66 = load i64, i64* @errno
%67 = load i64, i64* @EAGAIN
%68 = icmp eq i64 %66, %67
br i1 %68, label %73, label %69
69: ; preds = %65
%70 = load i64, i64* @errno
%71 = load i64, i64* @EINTR
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %74
73: ; preds = %69, %65
br label %17
74: ; preds = %69
store i32 -1, i32* %5
br label %84
75: ; preds = %62
%76 = load i32, i32* %10
%77 = load i32, i32* %11
%78 = add nsw i32 %77, %76
store i32 %78, i32* %11
%79 = load i32, i32* %10
%80 = load i32, i32* %12
%81 = sub nsw i32 %80, %79
store i32 %81, i32* %12
br label %17
82: ; preds = %17
%83 = load i32, i32* %11
store i32 %83, i32* %5
br label %84
84: ; preds = %82, %74, %60, %45, %38, %34
%85 = load i32, i32* %5
ret i32 %85
}

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 i32 @_read_retry(i32 %0, i8* nocapture %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = add nsw i32 %0, 1
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %47
11: ; preds = %4, %38
%12 = phi i8* [ %43, %38 ], [ %1, %4 ]
%13 = phi i32 [ %40, %38 ], [ %2, %4 ]
%14 = phi i32 [ %39, %38 ], [ 0, %4 ]
br label %15
15: ; preds = %11, %31
%16 = call i32 @FD_ZERO(i32* %5)
%17 = call i32 @FD_SET(i32 %0, i32* %5)
%18 = call i32 @FD_ZERO(i32* %6)
%19 = call i32 @FD_SET(i32 %0, i32* %6)
%20 = call i32 @_select_retry(i32 %9, i32* %5, i32* null, i32* %6, %struct.timeval* %3)
switch i32 %20, label %21 [
i32 -1, label %47
i32 0, label %44
]
21: ; preds = %15
%22 = call i64 @FD_ISSET(i32 %0, i32* %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %44
24: ; preds = %21
%25 = call i32 @read(i32 %0, i8* %12, i32 %13)
%26 = icmp eq i32 %25, 0
%27 = icmp eq i32 %20, 1
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %44, label %29
29: ; preds = %24
%30 = icmp eq i32 %25, -1
br i1 %30, label %31, label %38
31: ; preds = %29
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %32, %35
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %15, label %47
38: ; preds = %29
%39 = add nsw i32 %25, %14
%40 = sub nsw i32 %13, %25
%41 = icmp slt i32 %39, %2
%42 = sext i32 %39 to i64
%43 = getelementptr i8, i8* %1, i64 %42
br i1 %41, label %11, label %47
44: ; preds = %24, %21, %15
%45 = phi i64* [ @ETIMEDOUT, %15 ], [ @EPIPE, %21 ], [ @EPIPE, %24 ]
%46 = load i64, i64* %45
store i64 %46, i64* @errno
br label %47
47: ; preds = %38, %15, %31, %4, %44
%48 = phi i32 [ -1, %44 ], [ 0, %4 ], [ %20, %15 ], [ -1, %31 ], [ %39, %38 ]
ret i32 %48
}