#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 /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, void *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
ssize_t read_timeout(int fd, void *buf, size_t count, struct timeval *timeout) {
  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  int ret = select(fd + 1, &fds, NULL, NULL, timeout);
  if (ret == 1) {
    return read(fd, buf, count);
  }
  return ret;
}


x86 assembly (clang, -O0):
<read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%r8
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jne    11ab <read_timeout+0x6b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    11b1 <read_timeout+0x71>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_timeout>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
lea    0xc(%rsp),%r13
mov    %r13,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r13,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <select@plt>
cmp    $0x1,%eax
jne    1193 <read_timeout+0x53>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
define i32 @read_timeout(i32 %0, i8* %1, i64 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.timeval*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store %struct.timeval* %3, %struct.timeval** %9
%12 = call i32 @FD_ZERO(i32* %10)
%13 = load i32, i32* %6
%14 = call i32 @FD_SET(i32 %13, i32* %10)
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
%17 = load %struct.timeval*, %struct.timeval** %9
%18 = call i32 @select(i32 %16, i32* %10, i32* null, i32* null, %struct.timeval* %17)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %26
21: ; preds = %4
%22 = load i32, i32* %6
%23 = load i8*, i8** %7
%24 = load i64, i64* %8
%25 = call i32 @read(i32 %22, i8* %23, i64 %24)
store i32 %25, i32* %5
br label %28
26: ; preds = %4
%27 = load i32, i32* %11
store i32 %27, i32* %5
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* %5
ret i32 %29
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
define i32 @read_timeout(i32 %0, i8* nocapture %1, i64 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i32 @FD_ZERO(i32* %5)
%8 = call i32 @FD_SET(i32 %0, i32* %5)
%9 = add nsw i32 %0, 1
%10 = call i32 @select(i32 %9, i32* %5, i32* null, i32* null, %struct.timeval* %3)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = call i32 @read(i32 %0, i8* %1, i64 %2)
br label %14
14: ; preds = %4, %12
%15 = phi i32 [ %13, %12 ], [ %10, %4 ]
ret i32 %15
}