// File: /workspace/github_c_makefile_star2_O3/patriciogonzalezvivo[P]tprint/include/serial/unix.cc
// Function: serial::Serial::SerialImpl::write(unsigned char const*, unsigned long)
size_t
Serial::SerialImpl::write (const uint8_t *data, size_t length)
{
  if (is_open_ == false) {
    throw PortNotOpenedException ("Serial::write");
  }
  fd_set writefds;
  size_t bytes_written = 0;

  // Calculate total timeout in milliseconds t_c + (t_m * N)
  long total_timeout_ms = timeout_.write_timeout_constant;
  total_timeout_ms += timeout_.write_timeout_multiplier * static_cast<long> (length);
  MillisecondTimer total_timeout(total_timeout_ms);

  while (bytes_written < length) {
    int64_t timeout_remaining_ms = total_timeout.remaining();
    if (timeout_remaining_ms <= 0) {
      // Timed out
      break;
    }
    timespec timeout(timespec_from_ms(timeout_remaining_ms));

    FD_ZERO (&writefds);
    FD_SET (fd_, &writefds);

    // Do the select
    int r = pselect (fd_ + 1, NULL, &writefds, NULL, &timeout, NULL);

    // Figure out what happened by looking at select's response 'r'
    /** Error **/
    if (r < 0) {
      // Select was interrupted, try again
      if (errno == EINTR) {
        continue;
      }
      // Otherwise there was some error
      THROW (IOException, errno);
    }
    /** Timeout **/
    if (r == 0) {
      break;
    }
    /** Port ready to write **/
    if (r > 0) {
      // Make sure our file descriptor is in the ready to write list
      if (FD_ISSET (fd_, &writefds)) {
        // This will write some
        ssize_t bytes_written_now =
          ::write (fd_, data + bytes_written, length - bytes_written);
        // write should always return some data as select reported it was
        // ready to write when we get to this point.
        if (bytes_written_now < 1) {
          // Disconnected devices, at least on Linux, show the
          // behavior that they are always ready to write immediately
          // but writing returns nothing.
          throw SerialException ("device reports readiness to write but "
                                 "returned no data (device disconnected?)");
        }
        // Update bytes_written
        bytes_written += static_cast<size_t> (bytes_written_now);
        // If bytes_written == size then we have written everything we need to
        if (bytes_written == length) {
          break;
        }
        // If bytes_written < size then we have more to write
        if (bytes_written < length) {
          continue;
        }
        // If bytes_written > size then we have over written, which shouldn't happen
        if (bytes_written > length) {
          throw SerialException ("write over wrote, too many bytes where "
                                 "written, this shouldn't happen, might be "
                                 "a logical error!");
        }
      }
      // This shouldn't happen, if r > 0 our fd has to be in the list!
      THROW (IOException, "select reports ready to write, but our fd isn't"
                          " in the list, this shouldn't happen!");
    }
  }
  return bytes_written;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, -0x40(%rbp)
cmpb $0x0, 0x2c(%rdi)
je 0xcc8d
movq %rdx, %rbx
movq %rdi, %r15
movl 0x40(%rdi), %esi
imull %ebx, %esi
addl 0x3c(%rdi), %esi
leaq -0x58(%rbp), %rdi
callq 0xb6d0
testq %rbx, %rbx
je 0xcb54
movq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %r13
xorl %r12d, %r12d
leaq -0x38(%rbp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x82c0
movq -0x48(%rbp), %rax
subq -0x38(%rbp), %rax
movq %r13, %rcx
subq -0x30(%rbp), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x13773(%rip), %xmm2    # 0x20168
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd 0x13752(%rip), %xmm3    # 0x20160
divsd %xmm3, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
testq %rax, %rax
jle 0xcb5c
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
cvttsd2si %xmm1, %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm3, %xmm0
cvttsd2si %xmm0, %rcx
movq %rax, -0x38(%rbp)
movq %rcx, -0x30(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x120(%rbp)
movapd %xmm0, -0x110(%rbp)
movapd %xmm0, -0x100(%rbp)
movapd %xmm0, -0xf0(%rbp)
movapd %xmm0, -0xe0(%rbp)
movapd %xmm0, -0xd0(%rbp)
movapd %xmm0, -0xc0(%rbp)
movapd %xmm0, -0xb0(%rbp)
movl 0x28(%r15), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %edi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, -0x120(%rbp,%rax,8)
incl %edi
xorl %esi, %esi
leaq -0x120(%rbp), %rdx
xorl %ecx, %ecx
movq %r14, %r8
xorl %r9d, %r9d
callq 0x8360
testl %eax, %eax
js 0xcb37
je 0xcb5c
movl 0x28(%r15), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq -0x120(%rbp,%rax,8), %rax
btq %rdi, %rax
jae 0xcbc3
movq -0x40(%rbp), %rax
leaq (%rax,%r12), %rsi
movq %rbx, %rdx
subq %r12, %rdx
callq 0x8130
testq %rax, %rax
jle 0xcb71
addq %rax, %r12
cmpq %rbx, %r12
je 0xcb59
jb 0xcb45
jmp 0xcb8f
callq 0x8060
cmpl $0x4, (%rax)
jne 0xcc25
cmpq %rbx, %r12
leaq -0x38(%rbp), %r14
jb 0xc9cc
jmp 0xcb5c
xorl %r12d, %r12d
jmp 0xcb5c
movq %rbx, %r12
movq %r12, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x8190
movq %rax, %rbx
leaq 0x13846(%rip), %rsi     # 0x203cb
movq %rax, %rdi
callq 0xe0e2
jmp 0xcbad
jbe 0xcbc3
movl $0x28, %edi
callq 0x8190
movq %rax, %rbx
leaq 0x13874(%rip), %rsi     # 0x20419
movq %rax, %rdi
callq 0xe0e2
leaq 0x1a0d4(%rip), %rsi     # 0x26c88
leaq 0x168f(%rip), %rdx      # 0xe24a
movq %rbx, %rdi
callq 0x85b0
movl $0x58, %edi
callq 0x8190
movq %rax, %rbx
leaq -0x68(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x13633(%rip), %rsi     # 0x20212
leaq 0x13642(%rip), %rdx     # 0x20228
leaq -0x78(%rbp), %rdi
callq 0x9f1e
movb $0x1, %r12b
leaq 0x13881(%rip), %rcx     # 0x2047a
leaq -0x78(%rbp), %rsi
movq %rbx, %rdi
movl $0x2ac, %edx            # imm = 0x2AC
callq 0xe284
xorl %r12d, %r12d
leaq 0x1a08c(%rip), %rsi     # 0x26ca0
leaq 0x186b(%rip), %rdx      # 0xe486
movq %rbx, %rdi
callq 0x85b0
jmp 0xcc8d
movq %rax, %r14
movl $0x58, %edi
callq 0x8190
movq %rax, %rbx
leaq -0x88(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x135cb(%rip), %rsi     # 0x20212
leaq 0x135da(%rip), %rdx     # 0x20228
leaq -0x98(%rbp), %rdi
callq 0x9f1e
movl (%r14), %ecx
movb $0x1, %r12b
leaq -0x98(%rbp), %rsi
movq %rbx, %rdi
movl $0x283, %edx            # imm = 0x283
callq 0xe4d8
xorl %r12d, %r12d
leaq 0x1a022(%rip), %rsi     # 0x26ca0
leaq 0x1801(%rip), %rdx      # 0xe486
movq %rbx, %rdi
callq 0x85b0
movl $0x28, %edi
callq 0x8190
movq %rax, %rbx
leaq 0x1371c(%rip), %rsi     # 0x203bd
movq %rax, %rdi
callq 0xe70c
leaq 0x1a008(%rip), %rsi     # 0x26cb8
leaq 0x1bbd(%rip), %rdx      # 0xe874
jmp 0xcbbb
jmp 0xccfb
jmp 0xccfb
movq %rax, %r14
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0xccf4
movq -0x88(%rbp), %rsi
jmp 0xccec
jmp 0xccfb
jmp 0xccfb
movq %rax, %r14
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0xccf4
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x8310
testb %r12b, %r12b
jne 0xccfe
jmp 0xcd06
movq %rax, %r14
movq %rbx, %rdi
callq 0x8240
movq %r14, %rdi
callq 0x85f0