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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
bool read_serial_byte(int serial_fd, unsigned char *ch, int timeout_ms) {
  fd_set set;
  struct timeval tv;
  int ready_fds;
  FD_ZERO(&set);
  FD_SET(serial_fd, &set);
  tv.tv_sec = timeout_ms / 1000;
  tv.tv_usec = (timeout_ms % 1000) * 1000;
  do {
    ready_fds = select(FD_SETSIZE, &set, NULL, NULL, &tv);
  } while (ready_fds < 0 && errno == EINTR);
  if (ready_fds == 0)
    return false;
  if (read(serial_fd, ch, 1) != 1) {
    perror("read");
    return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<read_serial_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jge    11e2 <read_serial_byte+0x92>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1199 <read_serial_byte+0x49>
cmpl   $0x0,-0x24(%rbp)
jne    1203 <read_serial_byte+0xb3>
movl   $0x0,-0x4(%rbp)
jmp    123c <read_serial_byte+0xec>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1235 <read_serial_byte+0xe5>
lea    0xddc(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    123c <read_serial_byte+0xec>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_serial_byte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,0x8(%rsp)
mov    %edi,%r15d
lea    0x14(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   1070 <FD_SET@plt>
movslq %ebp,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0x4(%rsp)
mov    0x2e2d(%rip),%r13
mov    %rsp,%rbp
mov    0x2e1b(%rip),%rbx
mov    0x2e0c(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13),%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1050 <select@plt>
test   %eax,%eax
jns    11ef <read_serial_byte+0x9f>
mov    (%r14),%rcx
cmp    %rcx,(%rbx)
je     11d0 <read_serial_byte+0x80>
xor    %ebx,%ebx
test   %eax,%eax
je     1211 <read_serial_byte+0xc1>
mov    $0x1,%ebx
mov    %r15d,%edi
mov    0x8(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1222 <read_serial_byte+0xd2>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    1211 <read_serial_byte+0xc1>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
define i32 @read_serial_byte(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = call i32 @FD_ZERO(i32* %8)
%12 = load i32, i32* %5
%13 = call i32 @FD_SET(i32 %12, i32* %8)
%14 = load i32, i32* %7
%15 = sdiv i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = srem i32 %17, 1000
%19 = mul nsw i32 %18, 1000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 %19, i32* %20
br label %21
21: ; preds = %31, %3
%22 = load i32, i32* @FD_SETSIZE
%23 = call i32 @select(i32 %22, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %23, i32* %10
br label %24
24: ; preds = %21
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br label %31
31: ; preds = %27, %24
%32 = phi i1 [ false, %24 ], [ %30, %27 ]
br i1 %32, label %21, label %33
33: ; preds = %31
%34 = load i32, i32* %10
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 0, i32* %4
br label %45
37: ; preds = %33
%38 = load i32, i32* %5
%39 = load i8*, i8** %6
%40 = call i32 @read(i32 %38, i8* %39, i32 1)
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %45
44: ; preds = %37
store i32 1, i32* %4
br label %45
45: ; preds = %44, %42, %36
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
define i32 @read_serial_byte(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = call i32 @FD_ZERO(i32* %4)
%9 = call i32 @FD_SET(i32 %0, i32* %4)
%10 = sdiv i32 %2, 1000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %10, i32* %11
%12 = srem i32 %2, 1000
%13 = mul nsw i32 %12, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i32 %13, i32* %14
br label %15
15: ; preds = %15, %3
%16 = load i32, i32* @FD_SETSIZE
%17 = call i32 @select(i32 %16, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%18 = icmp slt i32 %17, 0
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %15, label %23
23: ; preds = %15
%24 = icmp eq i32 %17, 0
br i1 %24, label %30, label %25
25: ; preds = %23
%26 = call i32 @read(i32 %0, i8* %1, i32 1)
%27 = icmp eq i32 %26, 1
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %25, %23, %28
%31 = phi i32 [ 0, %28 ], [ 0, %23 ], [ 1, %25 ]
ret i32 %31
}