#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 sg_trama_cambio_read(int serial_fd, unsigned char *trama) {
  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 = 100000;
  ret = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
  switch (ret) {
  case 1:
    n = read(serial_fd, trama, 5);
    if (n != 5)
      return -1;
    return 1;
    break;
  case 0:
    trama[0] = 0;
    return 0;
    break;
  default:
    trama[0] = 0;
    return -1;
    break;
  }
  return 1;
}


x86 assembly (clang, -O0):
<sg_trama_cambio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
mov    %rdi,-0x38(%rbp)
call   1040 <FD_ZERO@plt>
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1060 <FD_SET@plt>
mov    -0x38(%rbp),%rsi
movq   $0x0,-0x20(%rbp)
movl   $0x186a0,-0x28(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
test   %eax,%eax
je     11f1 <sg_trama_cambio_read+0xb1>
jmp    11aa <sg_trama_cambio_read+0x6a>
mov    -0x30(%rbp),%eax
sub    $0x1,%eax
jne    1204 <sg_trama_cambio_read+0xc4>
jmp    11bb <sg_trama_cambio_read+0x7b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x5,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x5,-0x18(%rbp)
je     11e5 <sg_trama_cambio_read+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <sg_trama_cambio_read+0xd2>
movl   $0x1,-0x4(%rbp)
jmp    1212 <sg_trama_cambio_read+0xd2>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1212 <sg_trama_cambio_read+0xd2>
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):
<sg_trama_cambio_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
movl   $0x186a0,0x8(%rsp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
lea    0x8(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     11be <sg_trama_cambio_read+0x7e>
cmp    $0x1,%eax
jne    11c4 <sg_trama_cambio_read+0x84>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <read@plt>
xor    %ecx,%ecx
cmp    $0x5,%eax
sete   %cl
lea    (%rcx,%rcx,1),%ebx
add    $0xffffffff,%ebx
jmp    11cd <sg_trama_cambio_read+0x8d>
movb   $0x0,(%r14)
jmp    11cd <sg_trama_cambio_read+0x8d>
movb   $0x0,(%r14)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define i32 @sg_trama_cambio_read(i32 %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @FD_ZERO(i32* %3)
%8 = call i32 @FD_SET(i32 %0, i32* %3)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 100000, i32* %10
%11 = load i32, i32* @FD_SETSIZE
%12 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %4)
switch i32 %12, label %18 [
i32 1, label %13
i32 0, label %17
]
13: ; preds = %2
%14 = call i32 @read(i32 %0, i8* %1, i32 5)
%15 = icmp eq i32 %14, 5
%16 = select i1 %15, i32 1, i32 -1
br label %19
17: ; preds = %2
store i8 0, i8* %1
br label %19
18: ; preds = %2
store i8 0, i8* %1
br label %19
19: ; preds = %13, %18, %17
%20 = phi i32 [ -1, %18 ], [ 0, %17 ], [ %16, %13 ]
ret i32 %20
}