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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, unsigned char *, int);
int chronopic_estado(int serial_fd, int *estado) {
  unsigned char trama[10];
  char respuesta[10];
  fd_set fds;
  struct timeval timeout;
  int status;
  int ret;
  trama[0] = 'E';
  write(serial_fd, trama, 1);
  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:
    read(serial_fd, respuesta, 10);
    if (respuesta[0] == 'E') {
      status = 1;
      *estado = (int)respuesta[1];
    } else {
      status = -1;
      *estado = 0;
    }
    break;
  case 0:
    tcflush(serial_fd, TCIOFLUSH);
    status = 0;
    *estado = 0;
    break;
  default:
    status = -1;
    *estado = 0;
    break;
  }
  return status;
}


x86 assembly (clang, -O0):
<chronopic_estado>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x45,-0x1a(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    -0x28(%rbp),%rdi
mov    %rdi,-0x50(%rbp)
call   1060 <FD_ZERO@plt>
mov    -0x50(%rbp),%rsi
mov    -0x4(%rbp),%edi
call   1080 <FD_SET@plt>
mov    -0x50(%rbp),%rsi
movq   $0x0,-0x30(%rbp)
movl   $0x186a0,-0x38(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x44(%rbp)
test   %eax,%eax
je     123a <chronopic_estado+0xda>
jmp    11df <chronopic_estado+0x7f>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
jne    1261 <chronopic_estado+0x101>
jmp    11f0 <chronopic_estado+0x90>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
movsbl -0x24(%rbp),%eax
cmp    $0x45,%eax
jne    1224 <chronopic_estado+0xc4>
movl   $0x1,-0x3c(%rbp)
movsbl -0x23(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1235 <chronopic_estado+0xd5>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    1272 <chronopic_estado+0x112>
mov    -0x4(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    1272 <chronopic_estado+0x112>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chronopic_estado>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movb   $0x45,0x1e(%rsp)
lea    0x1e(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
movq   $0x0,0x30(%rsp)
movl   $0x186a0,0x28(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
xor    %r15d,%r15d
lea    0x28(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
je     11fb <chronopic_estado+0x9b>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    1210 <chronopic_estado+0xb0>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0xa,%edx
call   1050 <read@plt>
cmpb   $0x45,0xe(%rsp)
jne    1210 <chronopic_estado+0xb0>
movsbl 0xf(%rsp),%r15d
mov    $0x1,%ebx
jmp    1210 <chronopic_estado+0xb0>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
xor    %r15d,%r15d
xor    %ebx,%ebx
mov    %r15d,(%r14)
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
@TCIOFLUSH = global i32 0
define i32 @chronopic_estado(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [10 x i8]
%6 = alloca [10 x i8]
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
store i8 69, i8* %11
%12 = load i32, i32* %3
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%14 = call i32 @write(i32 %12, i8* %13, i32 1)
%15 = call i32 @FD_ZERO(i32* %7)
%16 = load i32, i32* %3
%17 = call i32 @FD_SET(i32 %16, i32* %7)
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 100000, i32* %19
%20 = load i32, i32* @FD_SETSIZE
%21 = call i32 @select(i32 %20, i32* %7, i32* null, i32* null, %struct.timeval* %8)
store i32 %21, i32* %10
%22 = load i32, i32* %10
switch i32 %22, label %44 [
i32 1, label %23
i32 0, label %39
]
23: ; preds = %2
%24 = load i32, i32* %3
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 10)
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 69
br i1 %30, label %31, label %36
31: ; preds = %23
store i32 1, i32* %9
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 1
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = load i32*, i32** %4
store i32 %34, i32* %35
br label %38
36: ; preds = %23
store i32 -1, i32* %9
%37 = load i32*, i32** %4
store i32 0, i32* %37
br label %38
38: ; preds = %36, %31
br label %46
39: ; preds = %2
%40 = load i32, i32* %3
%41 = load i32, i32* @TCIOFLUSH
%42 = call i32 @tcflush(i32 %40, i32 %41)
store i32 0, i32* %9
%43 = load i32*, i32** %4
store i32 0, i32* %43
br label %46
44: ; preds = %2
store i32 -1, i32* %9
%45 = load i32*, i32** %4
store i32 0, i32* %45
br label %46
46: ; preds = %44, %39, %38
%47 = load i32, i32* %9
ret i32 %47
}

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