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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
double floor(double);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int serial_read(int fd, char *buffer, int len, double timeout_sec) {
  int try_read = (timeout_sec < 0.0 ? 1 : 0);
  if (timeout_sec > 0.0) {
    fd_set read_fds, write_fds, except_fds;
    FD_ZERO(&read_fds);
    FD_ZERO(&write_fds);
    FD_ZERO(&except_fds);
    FD_SET(fd, &read_fds);
    struct timeval timeout;
    timeout.tv_sec = floor(timeout_sec);
    timeout.tv_usec = (timeout_sec - timeout.tv_sec) * 1e6;
    if (select(fd + 1, &read_fds, &write_fds, &except_fds, &timeout) == 1)
      try_read = 1;
  }
  if (try_read) {
    int r = read(fd, buffer, len);
    buffer[r] = '\0';
    return r;
  } else
    return -1;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm1
xorps  %xmm0,%xmm0
xor    %eax,%eax
mov    $0x1,%ecx
ucomisd %xmm1,%xmm0
cmova  %ecx,%eax
mov    %eax,-0x24(%rbp)
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1213 <serial_read+0xc3>
lea    -0x28(%rbp),%rdi
call   1040 <FD_ZERO@plt>
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
lea    -0x30(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <FD_SET@plt>
movsd  -0x20(%rbp),%xmm0
call   1070 <floor@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
movsd  0xe26(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1050 <select@plt>
cmp    $0x1,%eax
jne    120e <serial_read+0xbe>
movl   $0x1,-0x24(%rbp)
jmp    1213 <serial_read+0xc3>
cmpl   $0x0,-0x24(%rbp)
je     1246 <serial_read+0xf6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <serial_read+0xfd>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1200 <serial_read+0xb0>
lea    0x14(%rsp),%r15
mov    %r15,%rdi
movsd  %xmm0,(%rsp)
call   1040 <FD_ZERO@plt>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
lea    0xc(%rsp),%r13
mov    %r13,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
movsd  (%rsp),%xmm0
call   1070 <floor@plt>
movsd  %xmm0,0x18(%rsp)
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
mulsd  0xe35(%rip),%xmm1
movsd  %xmm1,0x20(%rsp)
lea    0x1(%rbp),%edi
lea    0x18(%rsp),%r8
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1050 <select@plt>
xorpd  %xmm0,%xmm0
ucomisd (%rsp),%xmm0
ja     120b <serial_read+0xbb>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%ecx
jne    121f <serial_read+0xcf>
jmp    120b <serial_read+0xbb>
mov    $0xffffffff,%eax
ucomisd %xmm0,%xmm1
jbe    121f <serial_read+0xcf>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
movslq %eax,%rcx
movb   $0x0,(%rbx,%rcx,1)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { double, double }
define i32 @serial_read(i32 %0, i8* %1, i32 %2, double %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.timeval
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store double %3, double* %9
%16 = load double, double* %9
%17 = fcmp olt double %16, 0.000000e+00
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 1, i32 0
store i32 %19, i32* %10
%20 = load double, double* %9
%21 = fcmp ogt double %20, 0.000000e+00
br i1 %21, label %22, label %43
22: ; preds = %4
%23 = call i32 @FD_ZERO(i32* %11)
%24 = call i32 @FD_ZERO(i32* %12)
%25 = call i32 @FD_ZERO(i32* %13)
%26 = load i32, i32* %6
%27 = call i32 @FD_SET(i32 %26, i32* %11)
%28 = load double, double* %9
%29 = call double @llvm.floor.f64(double %28)
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store double %29, double* %30
%31 = load double, double* %9
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
%33 = load double, double* %32
%34 = fsub double %31, %33
%35 = fmul double %34, 1.000000e+06
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store double %35, double* %36
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
%39 = call i32 @select(i32 %38, i32* %11, i32* %12, i32* %13, %struct.timeval* %14)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %22
store i32 1, i32* %10
br label %42
42: ; preds = %41, %22
br label %43
43: ; preds = %42, %4
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %43
%47 = load i32, i32* %6
%48 = load i8*, i8** %7
%49 = load i32, i32* %8
%50 = call i32 @read(i32 %47, i8* %48, i32 %49)
store i32 %50, i32* %15
%51 = load i8*, i8** %7
%52 = load i32, i32* %15
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %15
store i32 %55, i32* %5
br label %57
56: ; preds = %43
store i32 -1, i32* %5
br label %57
57: ; preds = %56, %46
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
define i32 @serial_read(i32 %0, i8* nocapture %1, i32 %2, double %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = fcmp olt double %3, 0.000000e+00
%10 = fcmp ogt double %3, 0.000000e+00
br i1 %10, label %11, label %29
11: ; preds = %4
%12 = bitcast i32* %5 to i8*
%13 = bitcast i32* %6 to i8*
%14 = bitcast i32* %7 to i8*
%15 = call i32 @FD_ZERO(i32* %5)
%16 = call i32 @FD_ZERO(i32* %6)
%17 = call i32 @FD_ZERO(i32* %7)
%18 = call i32 @FD_SET(i32 %0, i32* %5)
%19 = bitcast %struct.timeval* %8 to i8*
%20 = call double @llvm.floor.f64(double %3)
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store double %20, double* %21
%22 = fsub double %3, %20
%23 = fmul double %22, 1.000000e+06
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store double %23, double* %24
%25 = add nsw i32 %0, 1
%26 = call i32 @select(i32 %25, i32* %5, i32* %6, i32* %7, %struct.timeval* %8)
%27 = icmp eq i32 %26, 1
%28 = or i1 %9, %27
br i1 %28, label %30, label %34
29: ; preds = %4
br i1 %9, label %30, label %34
30: ; preds = %11, %29
%31 = call i32 @read(i32 %0, i8* %1, i32 %2)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %1, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %11, %29, %30
%35 = phi i32 [ %31, %30 ], [ -1, %29 ], [ -1, %11 ]
ret i32 %35
}