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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
double pow(int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, size_t);
void *rint(double);
double round(double);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int serial_read(int fd, char *data, size_t size, double timeout) {
  int read_size = 0;
  int read_result = 0;
  fd_set read_fds;
  int selected_fd;
  struct timeval timeout_struct;
  FD_ZERO(&read_fds);
  FD_SET(fd, &read_fds);
  timeout_struct.tv_sec = rint(timeout);
  timeout_struct.tv_usec = rint((timeout - round(timeout)) * pow(10, 6));
  while (read_size < size) {
    selected_fd = select(fd + 1, &read_fds, NULL, NULL, &timeout_struct);
    if (selected_fd < 0) {
      fprintf(stderr, "Failed to select a file descriptor: %s.\n",
              strerror(errno));
      break;
    } else if (selected_fd == 0) {
      fprintf(stderr, "A timeout occurred.\n");
      break;
    } else {
      read_result = read(fd, &(data[read_size]), size - read_size);
      if (read_result < 0) {
        fprintf(stderr, "Failed to read data: %s.\n", strerror(errno));
        break;
      }
      read_size += read_result;
    }
  }
  size_t i;
  printf("Reading: ");
  for (i = 0; i < read_size; i++) {
    printf("0x%02x ", (unsigned char)data[i]);
  }
  printf("\n");
  return read_size;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10c0 <FD_SET@plt>
movsd  -0x20(%rbp),%xmm0
call   1060 <rint@plt>
mov    %rax,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x20(%rbp),%xmm0
call   10a0 <round@plt>
movaps %xmm0,%xmm1
movsd  -0x58(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    $0xa,%edi
mov    $0x6,%esi
call   1090 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
call   1060 <rint@plt>
mov    %rax,-0x40(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1331 <serial_read+0x191>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    129a <serial_read+0xfa>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1331 <serial_read+0x191>
cmpl   $0x0,-0x30(%rbp)
jne    12c0 <serial_read+0x120>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1331 <serial_read+0x191>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x24(%rbp),%rax
sub    %rax,%rdx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    131e <serial_read+0x17e>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xd2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1331 <serial_read+0x191>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    132c <serial_read+0x18c>
jmp    1232 <serial_read+0x92>
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
jae    1383 <serial_read+0x1e3>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1347 <serial_read+0x1a7>
lea    0xcb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x60,%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
movsd  %xmm0,0x8(%rsp)
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
movsd  0x8(%rsp),%xmm0
call   1070 <rint@plt>
mov    %rax,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
call   10b0 <round@plt>
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    $0xa,%edi
mov    $0x6,%esi
call   10a0 <pow@plt>
mulsd  0x8(%rsp),%xmm0
call   1070 <rint@plt>
mov    %rax,0x18(%rsp)
test   %rbx,%rbx
je     127e <serial_read+0xce>
lea    0x1(%r15),%r12d
xor    %r13d,%r13d
xor    %ebp,%ebp
nopl   (%rax)
mov    %r12d,%edi
lea    0x14(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
call   1090 <select@plt>
test   %eax,%eax
js     1293 <serial_read+0xe3>
je     12b3 <serial_read+0x103>
lea    (%r14,%r13,1),%rsi
mov    %rbx,%rdx
sub    %r13,%rdx
mov    %r15d,%edi
call   1050 <read@plt>
test   %eax,%eax
js     12cc <serial_read+0x11c>
add    %eax,%ebp
movslq %ebp,%r13
cmp    %rbx,%r13
jb     1240 <serial_read+0x90>
jmp    12f6 <serial_read+0x146>
lea    0xdd3(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1338 <serial_read+0x188>
mov    0x2d46(%rip),%rax
mov    (%rax),%ebx
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd4f(%rip),%rsi
jmp    12ea <serial_read+0x13a>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xd66(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12f6 <serial_read+0x146>
mov    0x2d0d(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd54(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd5b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r13,%r13
je     1338 <serial_read+0x188>
lea    0xd52(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%r14,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1320 <serial_read+0x170>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i8*, i8* }
@stderr = global i32 0
@.str = [41 x i8] c"Failed to select a file descriptor: %s.\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"A timeout occurred.\0A\00"
@.str.2 = [26 x i8] c"Failed to read data: %s.\0A\00"
@.str.3 = [10 x i8] c"Reading: \00"
@.str.4 = [8 x i8] c"0x%02x \00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @serial_read(i32 %0, i8* %1, i64 %2, double %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store double %3, double* %8
store i32 0, i32* %9
store i32 0, i32* %10
%15 = call i32 @FD_ZERO(i32* %11)
%16 = load i32, i32* %5
%17 = call i32 @FD_SET(i32 %16, i32* %11)
%18 = load double, double* %8
%19 = call i8* @rint(double %18)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i8* %19, i8** %20
%21 = load double, double* %8
%22 = load double, double* %8
%23 = call double @llvm.round.f64(double %22)
%24 = fsub double %21, %23
%25 = call double @pow(i32 10, i32 6)
%26 = fmul double %24, %25
%27 = call i8* @rint(double %26)
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i8* %27, i8** %28
br label %29
29: ; preds = %74, %4
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %75
34: ; preds = %29
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
%37 = call i32 @select(i32 %36, i32* %11, i32* null, i32* null, %struct.timeval* %13)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %43)
br label %75
45: ; preds = %34
%46 = load i32, i32* %12
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %75
51: ; preds = %45
%52 = load i32, i32* %5
%53 = load i8*, i8** %6
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i64, i64* %7
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = sub i64 %57, %59
%61 = call i32 @read(i32 %52, i8* %56, i64 %60)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %51
%65 = load i32, i32* @stderr
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %67)
br label %75
69: ; preds = %51
%70 = load i32, i32* %10
%71 = load i32, i32* %9
%72 = add nsw i32 %71, %70
store i32 %72, i32* %9
br label %73
73: ; preds = %69
br label %74
74: ; preds = %73
br label %29
75: ; preds = %64, %48, %40, %29
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i64 0, i64* %14
br label %77
77: ; preds = %89, %75
%78 = load i64, i64* %14
%79 = load i32, i32* %9
%80 = sext i32 %79 to i64
%81 = icmp ult i64 %78, %80
br i1 %81, label %82, label %92
82: ; preds = %77
%83 = load i8*, i8** %6
%84 = load i64, i64* %14
%85 = getelementptr inbounds i8, i8* %83, i64 %84
%86 = load i8, i8* %85
%87 = zext i8 %86 to i32
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %82
%90 = load i64, i64* %14
%91 = add i64 %90, 1
store i64 %91, i64* %14
br label %77
92: ; preds = %77
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%94 = load i32, i32* %9
ret i32 %94
}

LLVM IR (-O2):
%struct.timeval = type { i8*, i8* }
@stderr = global i32 0
@.str = [41 x i8] c"Failed to select a file descriptor: %s.\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"A timeout occurred.\0A\00"
@.str.2 = [26 x i8] c"Failed to read data: %s.\0A\00"
@.str.3 = [10 x i8] c"Reading: \00"
@.str.4 = [8 x i8] c"0x%02x \00"
define i32 @serial_read(i32 %0, i8* nocapture %1, i64 %2, double %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = call i32 @FD_ZERO(i32* %5)
%10 = call i32 @FD_SET(i32 %0, i32* %5)
%11 = call i8* @rint(double %3)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i8* %11, i8** %12
%13 = call double @llvm.round.f64(double %3)
%14 = fsub double %3, %13
%15 = call double @pow(i32 10, i32 6)
%16 = fmul double %14, %15
%17 = call i8* @rint(double %16)
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i8* %17, i8** %18
%19 = icmp eq i64 %2, 0
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %66
22: ; preds = %4
%23 = add nsw i32 %0, 1
br label %27
24: ; preds = %42
%25 = sext i32 %47 to i64
%26 = icmp ult i64 %25, %2
br i1 %26, label %27, label %53
27: ; preds = %22, %24
%28 = phi i64 [ 0, %22 ], [ %25, %24 ]
%29 = phi i32 [ 0, %22 ], [ %47, %24 ]
%30 = call i32 @select(i32 %23, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %35)
br label %53
37: ; preds = %27
%38 = icmp eq i32 %30, 0
br i1 %38, label %39, label %42
39: ; preds = %37
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %53
42: ; preds = %37
%43 = getelementptr inbounds i8, i8* %1, i64 %28
%44 = sub i64 %2, %28
%45 = call i32 @read(i32 %0, i8* %43, i64 %44)
%46 = icmp slt i32 %45, 0
%47 = add nsw i32 %45, %29
br i1 %46, label %48, label %24
48: ; preds = %42
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %24, %48, %39, %32
%54 = phi i32 [ %29, %48 ], [ %29, %39 ], [ %29, %32 ], [ %47, %24 ]
%55 = phi i64 [ %28, %48 ], [ %28, %39 ], [ %28, %32 ], [ %25, %24 ]
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%57 = icmp eq i64 %55, 0
br i1 %57, label %66, label %58
58: ; preds = %53, %58
%59 = phi i64 [ %64, %58 ], [ 0, %53 ]
%60 = getelementptr inbounds i8, i8* %1, i64 %59
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = add nuw i64 %59, 1
%65 = icmp eq i64 %64, %55
br i1 %65, label %66, label %58
66: ; preds = %58, %20, %53
%67 = phi i32 [ 0, %20 ], [ %54, %53 ], [ %54, %58 ]
%68 = call i32 @putchar(i32 10)
ret i32 %67
}