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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int to_read(int fd, char *buf, int count, int timeout) {
  fd_set rfds;
  struct timeval tv;
  int i;
  tv.tv_sec = 0;
  tv.tv_usec = 500;
  do {
    i = select(fd + 1, &rfds, NULL, NULL, &tv);
  } while (i < 0 && (errno == EINTR));
  if (i > 0)
    return read(fd, buf, count);
  if (i == 0)
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<to_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x1f4,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x35(%rbp)
jge    118a <to_read+0x6a>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1144 <to_read+0x24>
cmpl   $0x0,-0x34(%rbp)
jle    11b6 <to_read+0x96>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    11d3 <to_read+0xb3>
cmpl   $0x0,-0x34(%rbp)
jne    11cc <to_read+0xac>
movl   $0x0,-0x4(%rbp)
jmp    11d3 <to_read+0xb3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,0x4(%rsp)
mov    %rsi,0x8(%rsp)
mov    %edi,%r12d
movq   $0x0,0x20(%rsp)
movl   $0x1f4,0x18(%rsp)
lea    0x1(%r12),%ebx
lea    0x14(%rsp),%r13
lea    0x18(%rsp),%rbp
mov    0x2e7f(%rip),%r14
mov    0x2e70(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1040 <select@plt>
test   %eax,%eax
jns    118d <to_read+0x6d>
mov    (%r15),%rcx
cmp    %rcx,(%r14)
je     1170 <to_read+0x50>
test   %eax,%eax
jle    11a4 <to_read+0x84>
mov    %r12d,%edi
mov    0x8(%rsp),%rsi
mov    0x4(%rsp),%edx
call   1030 <read@plt>
jmp    11a8 <to_read+0x88>
neg    %eax
sbb    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @to_read(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 500, i32* %14
br label %15
15: ; preds = %26, %4
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
%18 = call i32 @select(i32 %17, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %18, i32* %12
br label %19
19: ; preds = %15
%20 = load i32, i32* %12
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br label %26
26: ; preds = %22, %19
%27 = phi i1 [ false, %19 ], [ %25, %22 ]
br i1 %27, label %15, label %28
28: ; preds = %26
%29 = load i32, i32* %12
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* %6
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i32 @read(i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %5
br label %41
36: ; preds = %28
%37 = load i32, i32* %12
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
store i32 0, i32* %5
br label %41
40: ; preds = %36
store i32 -1, i32* %5
br label %41
41: ; preds = %40, %39, %31
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @to_read(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 500, i32* %10
%11 = add nsw i32 %0, 1
br label %12
12: ; preds = %12, %4
%13 = call i32 @select(i32 %11, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%14 = icmp slt i32 %13, 0
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %12, label %19
19: ; preds = %12
%20 = icmp sgt i32 %13, 0
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = call i32 @read(i32 %0, i8* %1, i32 %2)
br label %26
23: ; preds = %19
%24 = icmp ne i32 %13, 0
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %23, %21
%27 = phi i32 [ %22, %21 ], [ %25, %23 ]
ret i32 %27
}