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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
int serialreadchar(int fd, int timeout) {
  fd_set fds;
  struct timeval tv;
  int n;
  char ch;
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  if ((n = select(fd + 1, &fds, 0, 0, &tv)) < 0)
    return -1;
  if (n == 0)
    return -2;
  if ((n = read(fd, &ch, 1)) < 0)
    return -1;
  if (n == 0)
    return -3;
  return ch;
}


x86 assembly (clang, -O0):
<serialreadchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
mov    %ecx,%edx
call   1050 <select@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11a0 <serialreadchar+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <serialreadchar+0xbc>
cmpl   $0x0,-0x24(%rbp)
jne    11b6 <serialreadchar+0x76>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11fc <serialreadchar+0xbc>
mov    -0x8(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11df <serialreadchar+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <serialreadchar+0xbc>
cmpl   $0x0,-0x24(%rbp)
jne    11f5 <serialreadchar+0xb5>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11fc <serialreadchar+0xbc>
movsbl -0x25(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialreadchar>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %esi,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11b5 <serialreadchar+0x75>
je     11a9 <serialreadchar+0x69>
lea    0x3(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     11b5 <serialreadchar+0x75>
je     11b0 <serialreadchar+0x70>
movsbl 0x3(%rsp),%ebp
jmp    11b5 <serialreadchar+0x75>
mov    $0xfffffffe,%ebp
jmp    11b5 <serialreadchar+0x75>
mov    $0xfffffffd,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @serialreadchar(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i8
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %1, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %9
%10 = call i32 @FD_ZERO(i32* %3)
%11 = call i32 @FD_SET(i32 %0, i32* %3)
%12 = add nsw i32 %0, 1
%13 = call i32 @select(i32 %12, i32* %3, i32 0, i32 0, %struct.timeval* %4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %2
%16 = icmp eq i32 %13, 0
br i1 %16, label %25, label %17
17: ; preds = %15
%18 = call i32 @read(i32 %0, i8* %5, i32 1)
%19 = icmp slt i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = icmp eq i32 %18, 0
br i1 %21, label %25, label %22
22: ; preds = %20
%23 = load i8, i8* %5
%24 = sext i8 %23 to i32
br label %25
25: ; preds = %20, %17, %15, %2, %22
%26 = phi i32 [ %24, %22 ], [ -1, %2 ], [ -2, %15 ], [ -1, %17 ], [ -3, %20 ]
ret i32 %26
}