#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 */
typedef int /*<<< orphan*/ uint8_t;
struct timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, int /*<<< orphan*/ *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_timeout(int fd, uint8_t *buf, size_t size) {
  fd_set rfds;
  struct timeval tv = {1, 0};
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  int err = select(fd + 1, &rfds, NULL, NULL, &tv);
  if (err == 1)
    err = read(fd, buf, size);
  return err;
}


x86 assembly (clang, -O0):
<read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xea6(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    11ad <read_timeout+0x6d>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

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

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

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