#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__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, char *);
int read(int, char **, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(scalar_t__);
void timeout_read2(int fd1, int fd2, int *src, char *char_read) {
  fd_set in_set;
  struct timeval timeout;
  char ch = '\0';
  int res;
  int max = 0;
  FD_ZERO(&in_set);
  FD_SET(fd1, &in_set);
  if (fd1 > max)
    max = fd1;
  FD_SET(fd2, &in_set);
  if (fd2 > max)
    max = fd2;
  timeout.tv_sec = 0;
  timeout.tv_usec = 250;
  res = select(max + 1, &in_set, NULL, NULL, &timeout);
  if (res < 0)
    printf("ERROR: %s\n", strerror(errno));
  if (res <= 0) {
    *src = 0;
    *char_read = '\0';
    return;
  }
  if (FD_ISSET(fd1, &in_set))
    *src = fd1;
  if (FD_ISSET(fd2, &in_set))
    *src = fd2;
retry:
  res = read(*src, &char_read, 1);
  if (res < 0)
    printf("ERROR: %s\n", strerror(errno));
  if (res < 0) {
    if (errno == EINTR)
      goto retry;
    *src = 0;
    *char_read = '\0';
  }
}


x86 assembly (clang, -O0):
<timeout_read2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movb   $0x0,-0x31(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    -0x1c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x4(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jle    11b8 <timeout_read2+0x48>
mov    -0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x8(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jle    11d6 <timeout_read2+0x66>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xfa,-0x30(%rbp)
mov    -0x3c(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    122a <timeout_read2+0xba>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x38(%rbp)
jg     124a <timeout_read2+0xda>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    1306 <timeout_read2+0x196>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1269 <timeout_read2+0xf9>
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1288 <timeout_read2+0x118>
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    128d <timeout_read2+0x11d>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12cc <timeout_read2+0x15c>
mov    0x2d2b(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd39(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x38(%rbp)
jge    1306 <timeout_read2+0x196>
mov    0x2d03(%rip),%rax
mov    (%rax),%rax
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%rax
jne    12f5 <timeout_read2+0x185>
jmp    128d <timeout_read2+0x11d>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeout_read2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%r15d
mov    %edi,%ebp
mov    %rcx,0x10(%rsp)
lea    0xc(%rsp),%r13
mov    %r13,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   1090 <FD_SET@plt>
xor    %ebx,%ebx
test   %ebp,%ebp
cmovns %ebp,%ebx
mov    %r15d,%edi
mov    %r13,%rsi
call   1090 <FD_SET@plt>
cmp    %r15d,%ebx
cmovl  %r15d,%ebx
movq   $0x0,0x20(%rsp)
movl   $0xfa,0x18(%rsp)
add    $0x1,%ebx
lea    0x18(%rsp),%r8
mov    %ebx,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
js     1281 <timeout_read2+0x111>
je     129f <timeout_read2+0x12f>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1208 <timeout_read2+0x98>
mov    %ebp,(%r12)
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     121e <timeout_read2+0xae>
mov    %r15d,(%r12)
lea    0x10(%rsp),%r14
mov    0x2db6(%rip),%rbp
lea    0xdcf(%rip),%r15
mov    0x2da0(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    12ab <timeout_read2+0x13b>
mov    0x0(%rbp),%rdi
call   1080 <strerror@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x0(%rbp),%rax
cmp    (%rbx),%rax
je     1240 <timeout_read2+0xd0>
movl   $0x0,(%r12)
mov    0x10(%rsp),%r14
jmp    12a7 <timeout_read2+0x137>
mov    0x2d58(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xd69(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
movl   $0x0,(%r12)
movb   $0x0,(%r14)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [11 x i8] c"ERROR: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define void @timeout_read2(i32 %0, i32 %1, i32* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i8* %3, i8** %8
store i8 0, i8* %11
store i32 0, i32* %13
%14 = call i32 @FD_ZERO(i32* %9)
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %9)
%17 = load i32, i32* %5
%18 = load i32, i32* %13
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = load i32, i32* %5
store i32 %21, i32* %13
br label %22
22: ; preds = %20, %4
%23 = load i32, i32* %6
%24 = call i32 @FD_SET(i32 %23, i32* %9)
%25 = load i32, i32* %6
%26 = load i32, i32* %13
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* %6
store i32 %29, i32* %13
br label %30
30: ; preds = %28, %22
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %31
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 250, i32* %32
%33 = load i32, i32* %13
%34 = add nsw i32 %33, 1
%35 = call i32 @select(i32 %34, i32* %9, i32* null, i32* null, %struct.timeval* %10)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %30
%39 = load i64, i64* @errno
%40 = call i8* @strerror(i64 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %38, %30
%43 = load i32, i32* %12
%44 = icmp sle i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %7
store i32 0, i32* %46
%47 = load i8*, i8** %8
store i8 0, i8* %47
br label %84
48: ; preds = %42
%49 = load i32, i32* %5
%50 = call i64 @FD_ISSET(i32 %49, i32* %9)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32, i32* %5
%54 = load i32*, i32** %7
store i32 %53, i32* %54
br label %55
55: ; preds = %52, %48
%56 = load i32, i32* %6
%57 = call i64 @FD_ISSET(i32 %56, i32* %9)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i32, i32* %6
%61 = load i32*, i32** %7
store i32 %60, i32* %61
br label %62
62: ; preds = %59, %55
br label %63
63: ; preds = %80, %62
%64 = load i32*, i32** %7
%65 = load i32, i32* %64
%66 = call i32 @read(i32 %65, i8** %8, i32 1)
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %63
%70 = load i64, i64* @errno
%71 = call i8* @strerror(i64 %70)
%72 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %69, %63
%74 = load i32, i32* %12
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %84
76: ; preds = %73
%77 = load i64, i64* @errno
%78 = load i64, i64* @EINTR
%79 = icmp eq i64 %77, %78
br i1 %79, label %80, label %81
80: ; preds = %76
br label %63
81: ; preds = %76
%82 = load i32*, i32** %7
store i32 0, i32* %82
%83 = load i8*, i8** %8
store i8 0, i8* %83
br label %84
84: ; preds = %45, %81, %73
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [11 x i8] c"ERROR: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define void @timeout_read2(i32 %0, i32 %1, i32* nocapture %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.timeval
store i8* %3, i8** %5
%8 = bitcast i32* %6 to i8*
%9 = bitcast %struct.timeval* %7 to i8*
%10 = call i32 @FD_ZERO(i32* %6)
%11 = call i32 @FD_SET(i32 %0, i32* %6)
%12 = icmp sgt i32 %0, 0
%13 = select i1 %12, i32 %0, i32 0
%14 = call i32 @FD_SET(i32 %1, i32* %6)
%15 = icmp slt i32 %13, %1
%16 = select i1 %15, i32 %1, i32 %13
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 250, i32* %18
%19 = add nuw nsw i32 %16, 1
%20 = call i32 @select(i32 %19, i32* %6, i32* null, i32* null, %struct.timeval* %7)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i64, i64* @errno
%24 = call i8* @strerror(i64 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %28
26: ; preds = %4
%27 = icmp eq i32 %20, 0
br i1 %27, label %28, label %29
28: ; preds = %22, %26
store i32 0, i32* %2
br label %51
29: ; preds = %26
%30 = call i64 @FD_ISSET(i32 %0, i32* %6)
%31 = icmp eq i64 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %29
store i32 %0, i32* %2
br label %33
33: ; preds = %32, %29
%34 = call i64 @FD_ISSET(i32 %1, i32* %6)
%35 = icmp eq i64 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %33
store i32 %1, i32* %2
br label %37
37: ; preds = %36, %33
br label %38
38: ; preds = %37, %42
%39 = load i32, i32* %2
%40 = call i32 @read(i32 %39, i8** %5, i32 1)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %53
42: ; preds = %38
%43 = load i64, i64* @errno
%44 = call i8* @strerror(i64 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %44)
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINTR
%48 = icmp eq i64 %46, %47
br i1 %48, label %38, label %49
49: ; preds = %42
store i32 0, i32* %2
%50 = load i8*, i8** %5
br label %51
51: ; preds = %28, %49
%52 = phi i8* [ %50, %49 ], [ %3, %28 ]
store i8 0, i8* %52
br label %53
53: ; preds = %38, %51
ret void
}