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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int read(int, void *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int myread(int fd, void *data, int size) {
  int pos = 0;
  while (1) {
    fd_set fdset;
    struct timeval timeout = {.tv_sec = 1, .tv_usec = 0};
    FD_ZERO(&fdset);
    FD_SET(fd, &fdset);
    int ret = select(fd + 1, &fdset, NULL, NULL, &timeout);
    if (ret == -1) {
      perror("select");
      return -1;
    } else if (ret == 0) {
      break;
    } else if (FD_ISSET(fd, &fdset)) {
      int len = read(fd, data + pos, size - pos);
      if (len < 0) {
        return -1;
      } else {
        pos += len;
        if (pos == size) {
          break;
        }
      }
    }
  }
  return pos;
}


x86 assembly (clang, -O0):
<myread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0xe80(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11d8 <myread+0x78>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <myread+0x108>
cmpl   $0x0,-0x2c(%rbp)
jne    11e7 <myread+0x87>
jmp    1262 <myread+0x102>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1253 <myread+0xf3>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    122f <myread+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <myread+0x108>
mov    -0x30(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1249 <myread+0xe9>
jmp    1262 <myread+0x102>
jmp    124e <myread+0xee>
jmp    1253 <myread+0xf3>
jmp    1258 <myread+0xf8>
jmp    125d <myread+0xfd>
jmp    1179 <myread+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x1(%rbp),%r12d
xor    %ebx,%ebx
lea    0x14(%rsp),%r13
jmp    119a <myread+0x3a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %ecx,%ecx
test   %ecx,%ecx
jne    121f <myread+0xbf>
movq   $0x1,0x8(%rsp)
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   1080 <FD_SET@plt>
mov    %r12d,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
je     1229 <myread+0xc9>
cmp    $0xffffffff,%eax
je     123a <myread+0xda>
mov    %ebp,%edi
mov    %r13,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1190 <myread+0x30>
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rsi
mov    %r15d,%edx
sub    %ebx,%edx
mov    %ebp,%edi
call   1040 <read@plt>
lea    (%rax,%rbx,1),%edx
xor    %ecx,%ecx
cmp    %r15d,%edx
sete   %cl
lea    (%rcx,%rcx,2),%ecx
test   %eax,%eax
cmovns %edx,%ebx
mov    $0x1,%eax
cmovs  %eax,%ecx
js     1192 <myread+0x32>
cmp    %r15d,%edx
jne    1190 <myread+0x30>
jmp    1192 <myread+0x32>
cmp    $0x3,%ecx
je     1229 <myread+0xc9>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbf(%rip),%rdi
call   1070 <perror@plt>
jmp    1224 <myread+0xc4>

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [7 x i8] c"select\00"
define i32 @myread(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = bitcast i64* %5 to %struct.timeval*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = add nsw i32 %0, 1
br label %10
10: ; preds = %35, %3
%11 = phi i32 [ 0, %3 ], [ %36, %35 ]
store i64 1, i64* %5
%12 = call i32 @FD_ZERO(i32* %4)
%13 = call i32 @FD_SET(i32 %0, i32* %4)
%14 = call i32 @select(i32 %9, i32* %4, i32* null, i32* null, %struct.timeval* %6)
switch i32 %14, label %18 [
i32 -1, label %16
i32 0, label %15
]
15: ; preds = %10
br label %39
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %39
18: ; preds = %10
%19 = call i64 @FD_ISSET(i32 %0, i32* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %18
%22 = sext i32 %11 to i64
%23 = getelementptr i8, i8* %1, i64 %22
%24 = sub nsw i32 %2, %11
%25 = call i32 @read(i32 %0, i8* %23, i32 %24)
%26 = icmp slt i32 %25, 0
%27 = add nsw i32 %25, %11
%28 = icmp eq i32 %27, %2
%29 = select i1 %28, i32 3, i32 0
%30 = select i1 %26, i32 %11, i32 %27
%31 = select i1 %26, i1 true, i1 %28
%32 = select i1 %26, i32 1, i32 %29
br i1 %31, label %35, label %33
33: ; preds = %21, %18
%34 = phi i32 [ %30, %21 ], [ %11, %18 ]
br label %35
35: ; preds = %21, %33
%36 = phi i32 [ %34, %33 ], [ %30, %21 ]
%37 = phi i32 [ 0, %33 ], [ %32, %21 ]
switch i32 %37, label %38 [
i32 0, label %10
i32 3, label %39
]
38: ; preds = %35
br label %39
39: ; preds = %35, %38, %15, %16
%40 = phi i32 [ -1, %16 ], [ %11, %15 ], [ -1, %38 ], [ %36, %35 ]
ret i32 %40
}