// File: /workspace/github_c_makefile_star2_O2/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, -0x38(%rbp)
cmpb $0x0, 0x2c(%rdi)
je 0xbce7
movq %rdx, %r13
movq %rdi, %r15
movl 0x40(%rdi), %esi
imull %r13d, %esi
addl 0x3c(%rdi), %esi
leaq -0x58(%rbp), %rdi
callq 0xab68
xorl %r12d, %r12d
pushq $0x40
popq %r14
movq %r13, %rbx
subq %r12, %rbx
jbe 0xbc33
leaq -0x58(%rbp), %rdi
callq 0xabfc
testq %rax, %rax
jle 0xbc33
movl %eax, %edi
callq 0xac4b
movq %rax, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0xbb39
andq $0x0, -0x118(%rbp,%rax,8)
incq %rax
jmp 0xbb25
movl 0x28(%r15), %edi
movl %edi, %eax
cltd
idivl %r14d
movslq %eax, %rdx
shll $0x6, %eax
movl %edi, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, -0x118(%rbp,%rdx,8)
incl %edi
xorl %esi, %esi
leaq -0x118(%rbp), %rdx
xorl %ecx, %ecx
leaq -0x48(%rbp), %r8
xorl %r9d, %r9d
callq 0x8360
testl %eax, %eax
js 0xbbc8
je 0xbc33
movl 0x28(%r15), %edi
movl %edi, %eax
cltd
idivl %r14d
cltq
movq -0x118(%rbp,%rax,8), %rax
btq %rdx, %rax
jae 0xbc96
movq -0x38(%rbp), %rax
leaq (%rax,%r12), %rsi
movq %rbx, %rdx
callq 0x8110
testq %rax, %rax
jle 0xbc48
addq %rax, %r12
cmpq %r13, %r12
je 0xbc30
jb 0xbaf6
jmp 0xbc64
callq 0x8050
cmpl $0x4, (%rax)
je 0xbaf6
movq %rax, %rbx
pushq $0x58
popq %rdi
callq 0x81a0
movq %rax, %r15
leaq 0xc627(%rip), %rsi      # 0x18212
leaq -0x98(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x95bc
movl (%rbx), %ecx
movb $0x1, %bl
leaq -0x98(%rbp), %rsi
movq %r15, %rdi
movl $0x283, %edx            # imm = 0x283
callq 0xd164
xorl %ebx, %ebx
leaq 0x140ac(%rip), %rsi     # 0x1fcc8
leaq 0x150d(%rip), %rdx      # 0xd130
movq %r15, %rdi
callq 0x8590
jmp 0xbce7
movq %r13, %r12
movq %r12, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x28
popq %rdi
callq 0x81a0
movq %rax, %r15
leaq 0xc771(%rip), %rsi      # 0x183cb
movq %rax, %rdi
callq 0xcee8
jmp 0xbc80
jbe 0xbc96
pushq $0x28
popq %rdi
callq 0x81a0
movq %rax, %r15
leaq 0xc7a1(%rip), %rsi      # 0x18419
movq %rax, %rdi
callq 0xcee8
leaq 0x14029(%rip), %rsi     # 0x1fcb0
leaq 0x1338(%rip), %rdx      # 0xcfc6
movq %r15, %rdi
callq 0x8590
pushq $0x58
popq %rdi
callq 0x81a0
movq %rax, %r15
leaq 0xc56a(%rip), %rsi      # 0x18212
leaq -0x78(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x95bc
movb $0x1, %bl
leaq 0xc7bc(%rip), %rcx      # 0x1847a
leaq -0x78(%rbp), %rsi
movq %r15, %rdi
movl $0x2ac, %edx            # imm = 0x2AC
callq 0xcff0
xorl %ebx, %ebx
leaq 0x13ff0(%rip), %rsi     # 0x1fcc8
leaq 0x1451(%rip), %rdx      # 0xd130
movq %r15, %rdi
callq 0x8590
pushq $0x28
popq %rdi
callq 0x81a0
movq %rax, %rbx
leaq 0xc6c4(%rip), %rsi      # 0x183bd
movq %rax, %rdi
callq 0xd2ca
leaq 0x13fd8(%rip), %rsi     # 0x1fce0
leaq 0x1699(%rip), %rdx      # 0xd3a8
movq %rbx, %rdi
callq 0x8590
movq %rax, %r14
movq %rbx, %rdi
jmp 0xbd49
jmp 0xbd43
movq %rax, %r14
leaq -0x98(%rbp), %rdi
jmp 0xbd38
jmp 0xbd43
jmp 0xbd43
movq %rax, %r14
leaq -0x78(%rbp), %rdi
callq 0x8170
testb %bl, %bl
jne 0xbd46
jmp 0xbd4e
movq %rax, %r14
movq %r15, %rdi
callq 0x8240
movq %r14, %rdi
callq 0x85c0