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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, unsigned char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int chronopic_read(int serial_fd, unsigned char *trama, int bytes, int tout) {
  fd_set fds;
  ssize_t n;
  struct timeval timeout;
  int ret;
  FD_ZERO(&fds);
  FD_SET(serial_fd, &fds);
  timeout.tv_sec = 0;
  timeout.tv_usec = tout;
  ret = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
  switch (ret) {
  case 1:
    n = read(serial_fd, trama, bytes);
    return n;
    break;
  case 0:
    trama[0] = 0;
    return 0;
    break;
  default:
    trama[0] = 0;
    return -1;
    break;
  }
  return 1;
}


x86 assembly (clang, -O0):
<chronopic_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
mov    %rdi,-0x40(%rbp)
call   1040 <FD_ZERO@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1060 <FD_SET@plt>
mov    -0x40(%rbp),%rsi
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
test   %eax,%eax
je     11dd <chronopic_read+0x9d>
jmp    11af <chronopic_read+0x6f>
mov    -0x38(%rbp),%eax
sub    $0x1,%eax
jne    11f0 <chronopic_read+0xb0>
jmp    11c0 <chronopic_read+0x80>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fe <chronopic_read+0xbe>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11fe <chronopic_read+0xbe>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chronopic_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
mov    %ebx,0x10(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     11b3 <chronopic_read+0x73>
cmp    $0x1,%eax
jne    11b9 <chronopic_read+0x79>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
mov    %eax,%ebx
jmp    11c2 <chronopic_read+0x82>
movb   $0x0,(%r15)
jmp    11c2 <chronopic_read+0x82>
movb   $0x0,(%r15)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define i32 @chronopic_read(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = call i32 @FD_ZERO(i32* %10)
%15 = load i32, i32* %6
%16 = call i32 @FD_SET(i32 %15, i32* %10)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %9
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @FD_SETSIZE
%21 = call i32 @select(i32 %20, i32* %10, i32* null, i32* null, %struct.timeval* %12)
store i32 %21, i32* %13
%22 = load i32, i32* %13
switch i32 %22, label %32 [
i32 1, label %23
i32 0, label %29
]
23: ; preds = %4
%24 = load i32, i32* %6
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = call i32 @read(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %11
%28 = load i32, i32* %11
store i32 %28, i32* %5
br label %35
29: ; preds = %4
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 0
store i8 0, i8* %31
store i32 0, i32* %5
br label %35
32: ; preds = %4
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i64 0
store i8 0, i8* %34
store i32 -1, i32* %5
br label %35
35: ; preds = %32, %29, %23
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define i32 @chronopic_read(i32 %0, i8* nocapture %1, i32 %2, i32 %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 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %3, i32* %12
%13 = load i32, i32* @FD_SETSIZE
%14 = call i32 @select(i32 %13, i32* %5, i32* null, i32* null, %struct.timeval* %6)
switch i32 %14, label %18 [
i32 1, label %15
i32 0, label %17
]
15: ; preds = %4
%16 = call i32 @read(i32 %0, i8* %1, i32 %2)
br label %19
17: ; preds = %4
store i8 0, i8* %1
br label %19
18: ; preds = %4
store i8 0, i8* %1
br label %19
19: ; preds = %18, %17, %15
%20 = phi i32 [ -1, %18 ], [ 0, %17 ], [ %16, %15 ]
ret i32 %20
}