#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 */
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int carmen_serial_readn(int dev_fd, unsigned char *buf, int nChars) {
  int amountRead = 0, bytes_read = 0;
  struct timeval t;
  fd_set set;
  int err;
  while (nChars > 0) {
    t.tv_sec = 0;
    t.tv_usec = 250000;
    FD_ZERO(&set);
    FD_SET(dev_fd, &set);
    err = select(dev_fd + 1, &set, NULL, NULL, &t);
    if (err == 0)
      return -2;
    amountRead = read(dev_fd, buf, nChars);
    if (amountRead < 0 && errno != EWOULDBLOCK)
      return -1;
    else if (amountRead > 0) {
      bytes_read += amountRead;
      nChars -= amountRead;
      buf += amountRead;
    }
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<carmen_serial_readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    123c <carmen_serial_readn+0xfc>
movq   $0x0,-0x28(%rbp)
movl   $0x3d090,-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    11c1 <carmen_serial_readn+0x81>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1242 <carmen_serial_readn+0x102>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1203 <carmen_serial_readn+0xc3>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
je     1203 <carmen_serial_readn+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <carmen_serial_readn+0x102>
cmpl   $0x0,-0x18(%rbp)
jle    1232 <carmen_serial_readn+0xf2>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1237 <carmen_serial_readn+0xf7>
jmp    1160 <carmen_serial_readn+0x20>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<carmen_serial_readn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %edx,%edx
jle    120e <carmen_serial_readn+0xce>
mov    %edx,%r13d
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x1(%rbx),%r12d
xor    %ecx,%ecx
lea    0x14(%rsp),%rbp
lea    0x18(%rsp),%r14
mov    %rcx,0x8(%rsp)
jmp    1184 <carmen_serial_readn+0x44>
cs nopw 0x0(%rax,%rax,1)
nop
test   %eax,%eax
jg     11f0 <carmen_serial_readn+0xb0>
movq   $0x0,0x20(%rsp)
movl   $0x3d090,0x18(%rsp)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <select@plt>
test   %eax,%eax
je     1207 <carmen_serial_readn+0xc7>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r13d,%edx
call   1030 <read@plt>
test   %eax,%eax
jns    1180 <carmen_serial_readn+0x40>
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2df9(%rip),%rdx
cmp    (%rdx),%rcx
je     1180 <carmen_serial_readn+0x40>
jmp    1212 <carmen_serial_readn+0xd2>
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%ecx
add    %rcx,%r15
mov    0x8(%rsp),%rcx
add    %eax,%ecx
sub    %eax,%r13d
jg     116e <carmen_serial_readn+0x2e>
jmp    1217 <carmen_serial_readn+0xd7>
mov    $0xfffffffe,%ecx
jmp    1217 <carmen_serial_readn+0xd7>
xor    %ecx,%ecx
jmp    1217 <carmen_serial_readn+0xd7>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x28,%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 @carmen_serial_readn(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %13
13: ; preds = %55, %3
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %56
16: ; preds = %13
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 250000, i32* %18
%19 = call i32 @FD_ZERO(i32* %11)
%20 = load i32, i32* %5
%21 = call i32 @FD_SET(i32 %20, i32* %11)
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
%24 = call i32 @select(i32 %23, i32* %11, i32* null, i32* null, %struct.timeval* %10)
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %16
store i32 -2, i32* %4
br label %58
28: ; preds = %16
%29 = load i32, i32* %5
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = call i32 @read(i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i64, i64* @errno
%37 = load i64, i64* @EWOULDBLOCK
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %4
br label %58
40: ; preds = %35, %28
%41 = load i32, i32* %8
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %40
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = add nsw i32 %45, %44
store i32 %46, i32* %9
%47 = load i32, i32* %8
%48 = load i32, i32* %7
%49 = sub nsw i32 %48, %47
store i32 %49, i32* %7
%50 = load i32, i32* %8
%51 = load i8*, i8** %6
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8* %53, i8** %6
br label %54
54: ; preds = %43, %40
br label %55
55: ; preds = %54
br label %13
56: ; preds = %13
%57 = load i32, i32* %9
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %39, %27
%59 = load i32, i32* %4
ret i32 %59
}

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