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

/* Variables and functions */
int /*<<< orphan*/ ETIMEDOUT;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
scalar_t__ read(int, void *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int kwboot_tty_recv(int fd, void *buf, size_t len, int timeo) {
  int rc, nfds;
  fd_set rfds;
  struct timeval tv;
  ssize_t n;
  rc = -1;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = timeo * 1000;
  if (tv.tv_usec > 1000000) {
    tv.tv_sec += tv.tv_usec / 1000000;
    tv.tv_usec %= 1000000;
  }
  do {
    nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
    if (nfds < 0)
      goto out;
    if (!nfds) {
      errno = ETIMEDOUT;
      goto out;
    }
    n = read(fd, buf, len);
    if (n < 0)
      goto out;
    buf = (char *)buf + n;
    len -= n;
  } while (len > 0);
  rc = 0;
out:
  return rc;
}


x86 assembly (clang, -O0):
<kwboot_tty_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x30(%rbp)
imul   $0x3e8,-0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0xf4240,-0x38(%rbp)
jle    11b4 <kwboot_tty_recv+0x74>
mov    -0x38(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cltq
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x38(%rbp)
jmp    11b9 <kwboot_tty_recv+0x79>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e5 <kwboot_tty_recv+0xa5>
jmp    1257 <kwboot_tty_recv+0x117>
cmpl   $0x0,-0x24(%rbp)
jne    1206 <kwboot_tty_recv+0xc6>
mov    0x2de2(%rip),%rax
mov    (%rax),%ecx
mov    0x2de1(%rip),%rax
mov    %ecx,(%rax)
jmp    1257 <kwboot_tty_recv+0x117>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    122a <kwboot_tty_recv+0xea>
jmp    1257 <kwboot_tty_recv+0x117>
mov    -0x10(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
ja     11b9 <kwboot_tty_recv+0x79>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_tty_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
imul   $0x3e8,%ebp,%eax
mov    %eax,0x8(%rsp)
cmp    $0xf4241,%eax
jl     11c0 <kwboot_tty_recv+0x80>
movslq %ebp,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %rcx,0x10(%rsp)
mov    %eax,%ecx
imul   $0x431bde83,%rcx,%rcx
shr    $0x32,%rcx
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0x8(%rsp)
lea    0x1(%r15),%ebp
lea    0x8(%rsp),%r13
mov    $0xffffffff,%r12d
nop
mov    %ebp,%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1050 <select@plt>
test   %eax,%eax
js     121b <kwboot_tty_recv+0xdb>
je     1209 <kwboot_tty_recv+0xc9>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
test   %rax,%rax
js     121b <kwboot_tty_recv+0xdb>
add    %rax,%rbx
sub    %rax,%r14
jne    11d0 <kwboot_tty_recv+0x90>
xor    %r12d,%r12d
jmp    121b <kwboot_tty_recv+0xdb>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    0x2dc7(%rip),%rcx
mov    %eax,(%rcx)
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @kwboot_tty_recv(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
%13 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
store i32 -1, i32* %9
%14 = call i32 @FD_ZERO(i32* %11)
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %11)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %8
%19 = mul nsw i32 %18, 1000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%22 = load i32, i32* %21
%23 = icmp sgt i32 %22, 1000000
br i1 %23, label %24, label %35
24: ; preds = %4
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sdiv i32 %26, 1000000
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%30 = load i64, i64* %29
%31 = add nsw i64 %30, %28
store i64 %31, i64* %29
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%33 = load i32, i32* %32
%34 = srem i32 %33, 1000000
store i32 %34, i32* %32
br label %35
35: ; preds = %24, %4
br label %36
36: ; preds = %63, %35
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
%39 = call i32 @select(i32 %38, i32* %11, i32* null, i32* null, %struct.timeval* %12)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
br label %67
43: ; preds = %36
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = load i32, i32* @ETIMEDOUT
store i32 %47, i32* @errno
br label %67
48: ; preds = %43
%49 = load i32, i32* %5
%50 = load i8*, i8** %6
%51 = load i64, i64* %7
%52 = call i64 @read(i32 %49, i8* %50, i64 %51)
store i64 %52, i64* %13
%53 = load i64, i64* %13
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %48
br label %67
56: ; preds = %48
%57 = load i8*, i8** %6
%58 = load i64, i64* %13
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8* %59, i8** %6
%60 = load i64, i64* %13
%61 = load i64, i64* %7
%62 = sub i64 %61, %60
store i64 %62, i64* %7
br label %63
63: ; preds = %56
%64 = load i64, i64* %7
%65 = icmp ugt i64 %64, 0
br i1 %65, label %36, label %66
66: ; preds = %63
store i32 0, i32* %9
br label %67
67: ; preds = %66, %55, %46, %42
%68 = load i32, i32* %9
ret i32 %68
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @kwboot_tty_recv(i32 %0, i8* nocapture %1, i64 %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 = mul nsw i32 %3, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %12, i32* %13
%14 = icmp sgt i32 %12, 1000000
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = sdiv i32 %3, 1000
%17 = zext i32 %16 to i64
store i64 %17, i64* %11
%18 = urem i32 %12, 1000000
store i32 %18, i32* %13
br label %19
19: ; preds = %15, %4
%20 = add nsw i32 %0, 1
br label %21
21: ; preds = %33, %19
%22 = phi i64 [ %2, %19 ], [ %35, %33 ]
%23 = phi i8* [ %1, %19 ], [ %34, %33 ]
%24 = call i32 @select(i32 %20, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%25 = icmp slt i32 %24, 0
br i1 %25, label %37, label %26
26: ; preds = %21
%27 = icmp eq i32 %24, 0
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = load i32, i32* @ETIMEDOUT
store i32 %29, i32* @errno
br label %37
30: ; preds = %26
%31 = call i64 @read(i32 %0, i8* %23, i64 %22)
%32 = icmp slt i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = getelementptr inbounds i8, i8* %23, i64 %31
%35 = sub i64 %22, %31
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %21
37: ; preds = %33, %30, %21, %28
%38 = phi i32 [ -1, %28 ], [ 0, %33 ], [ -1, %30 ], [ -1, %21 ]
ret i32 %38
}