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

/* Variables and functions */
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
size_t read(int, unsigned char *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int serial_read(int fd, unsigned char *data, ssize_t num) {
  ssize_t num_read = 0;
  struct timeval time;
  fd_set set;
  int error;
  while (num_read < num) {
    time.tv_sec = 0;
    time.tv_usec = 5000;
    FD_ZERO(&set);
    FD_SET(fd, &set);
    error = select(fd + 1, &set, NULL, NULL, &time);
    if (error == 0)
      return -1;
    ssize_t n;
    n = read(fd, &data[num_read], num - num_read);
    if ((n < 0) && (errno != EWOULDBLOCK))
      return -1;
    if (n > 0)
      num_read += n;
  }
  return num_read;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1229 <serial_read+0xe9>
movq   $0x0,-0x28(%rbp)
movl   $0x1388,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    11c0 <serial_read+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <serial_read+0xf0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jae    120d <serial_read+0xcd>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
je     120d <serial_read+0xcd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <serial_read+0xf0>
cmpq   $0x0,-0x40(%rbp)
jbe    1224 <serial_read+0xe4>
mov    -0x40(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    115b <serial_read+0x1b>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     11c8 <serial_read+0x88>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
lea    0x1(%r12),%r13d
xor    %ebx,%ebx
lea    0x4(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x10(%rsp)
movl   $0x1388,0x8(%rsp)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1050 <select@plt>
test   %eax,%eax
je     11cc <serial_read+0x8c>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %r12d,%edi
call   1030 <read@plt>
add    %rax,%rbx
cmp    %r14,%rbx
jb     1170 <serial_read+0x30>
jmp    11d1 <serial_read+0x91>
xor    %ebx,%ebx
jmp    11d1 <serial_read+0x91>
mov    $0xffffffff,%ebx
mov    %ebx,%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 }
@errno = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @serial_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %13
13: ; preds = %52, %3
%14 = load i64, i64* %8
%15 = load i64, i64* %7
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %53
17: ; preds = %13
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 5000, i32* %19
%20 = call i32 @FD_ZERO(i32* %10)
%21 = load i32, i32* %5
%22 = call i32 @FD_SET(i32 %21, i32* %10)
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* %10, i32* null, i32* null, %struct.timeval* %9)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %17
store i32 -1, i32* %4
br label %56
29: ; preds = %17
%30 = load i32, i32* %5
%31 = load i8*, i8** %6
%32 = load i64, i64* %8
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i64, i64* %7
%35 = load i64, i64* %8
%36 = sub i64 %34, %35
%37 = call i64 @read(i32 %30, i8* %33, i64 %36)
store i64 %37, i64* %12
%38 = load i64, i64* %12
%39 = icmp ult i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %29
%41 = load i64, i64* @errno
%42 = load i64, i64* @EWOULDBLOCK
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %4
br label %56
45: ; preds = %40, %29
%46 = load i64, i64* %12
%47 = icmp ugt i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i64, i64* %12
%50 = load i64, i64* %8
%51 = add i64 %50, %49
store i64 %51, i64* %8
br label %52
52: ; preds = %48, %45
br label %13
53: ; preds = %13
%54 = load i64, i64* %8
%55 = trunc i64 %54 to i32
store i32 %55, i32* %4
br label %56
56: ; preds = %53, %44, %28
%57 = load i32, i32* %4
ret i32 %57
}

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