// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x158(%rbp)
movb 0x2c(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x15d2b
movl $0x28, %edi
callq 0xd1e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x160(%rbp)
leaq 0x1766f(%rip), %rsi     # 0x2d35c
callq 0x18610
jmp 0x15cf4
movq -0x160(%rbp), %rdi
leaq 0x1ffce(%rip), %rsi     # 0x35cd0
leaq 0x2a47(%rip), %rdx      # 0x18750
callq 0xd720
movq -0x160(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xd2c0
jmp 0x1621c
movq -0x158(%rbp), %rax
movq $0x0, -0xb0(%rbp)
movl 0x3c(%rax), %ecx
movq %rcx, -0xb8(%rbp)
movl 0x40(%rax), %eax
imulq -0x18(%rbp), %rax
addq -0xb8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movl %eax, %esi
leaq -0xc8(%rbp), %rdi
callq 0x14090
movq -0xb0(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x1620c
leaq -0xc8(%rbp), %rdi
callq 0x14180
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jg 0x15da5
jmp 0x1620c
movq -0xd0(%rbp), %rax
movl %eax, %edi
callq 0x14200
movq %rax, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl $0x0, -0xe4(%rbp)
movl -0xe4(%rbp), %eax
cmpq $0x10, %rax
jae 0x15e0b
movq -0xf0(%rbp), %rax
movl -0xe4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x15dd9
jmp 0x15e0d
movq -0x158(%rbp), %rax
movl 0x28(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x158(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, -0x168(%rbp)
movl 0x28(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x168(%rbp), %rdx
movl %eax, %ecx
movq -0x158(%rbp), %rax
movslq %ecx, %rcx
orq -0xa8(%rbp,%rcx,8), %rdx
movq %rdx, -0xa8(%rbp,%rcx,8)
movl 0x28(%rax), %edi
addl $0x1, %edi
xorl %eax, %eax
movl %eax, %r9d
leaq -0xa8(%rbp), %rdx
leaq -0xe0(%rbp), %r8
movq %r9, %rsi
movq %r9, %rcx
callq 0xd450
movl %eax, -0xf4(%rbp)
cmpl $0x0, -0xf4(%rbp)
jge 0x15f87
callq 0xd060
cmpl $0x4, (%rax)
jne 0x15eab
jmp 0x15d72
movb $0x1, -0x11a(%rbp)
movl $0x58, %edi
callq 0xd1e0
movq %rax, -0x178(%rbp)
leaq -0x119(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0xd760
movq -0x170(%rbp), %rdx
leaq 0x172cd(%rip), %rsi     # 0x2d1b1
leaq -0x118(%rbp), %rdi
callq 0xf2c0
jmp 0x15ef2
callq 0xd060
movq -0x178(%rbp), %rdi
movl (%rax), %ecx
leaq -0x118(%rbp), %rsi
movl $0x283, %edx            # imm = 0x283
callq 0x183c0
jmp 0x15f13
movq -0x178(%rbp), %rdi
movb $0x0, -0x11a(%rbp)
leaq 0x1fd90(%rip), %rsi     # 0x35cb8
leaq 0x2441(%rip), %rdx      # 0x18370
callq 0xd720
jmp 0x16225
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x15f5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x118(%rbp), %rdi
callq 0xd240
leaq -0x119(%rbp), %rdi
callq 0xd430
testb $0x1, -0x11a(%rbp)
jne 0x15f76
jmp 0x15f82
movq -0x178(%rbp), %rdi
callq 0xd2c0
jmp 0x1621c
cmpl $0x0, -0xf4(%rbp)
jne 0x15f95
jmp 0x1620c
cmpl $0x0, -0xf4(%rbp)
jle 0x16207
movq -0x158(%rbp), %rax
movl 0x28(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x158(%rbp), %rax
movslq %ecx, %rcx
movq -0xa8(%rbp,%rcx,8), %rcx
movq %rcx, -0x180(%rbp)
movl 0x28(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x180(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x16131
movq -0x158(%rbp), %rax
movl 0x28(%rax), %edi
movq -0x10(%rbp), %rsi
addq -0xb0(%rbp), %rsi
movq -0x18(%rbp), %rdx
subq -0xb0(%rbp), %rdx
callq 0xd120
movq %rax, -0x128(%rbp)
cmpq $0x1, -0x128(%rbp)
jge 0x1608d
movl $0x28, %edi
callq 0xd1e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x188(%rbp)
leaq 0x1731b(%rip), %rsi     # 0x2d36a
callq 0x17ff0
jmp 0x16056
movq -0x188(%rbp), %rdi
leaq 0x1fc3c(%rip), %rsi     # 0x35ca0
leaq 0x20c5(%rip), %rdx      # 0x18130
callq 0xd720
movq -0x188(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xd2c0
jmp 0x1621c
movq -0x128(%rbp), %rax
addq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x160b4
jmp 0x1620c
movq -0xb0(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x160c6
jmp 0x15d72
movq -0xb0(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x1612f
movl $0x28, %edi
callq 0xd1e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x190(%rbp)
leaq 0x172c7(%rip), %rsi     # 0x2d3b8
callq 0x17ff0
jmp 0x160f8
movq -0x190(%rbp), %rdi
leaq 0x1fb9a(%rip), %rsi     # 0x35ca0
leaq 0x2023(%rip), %rdx      # 0x18130
callq 0xd720
movq -0x190(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xd2c0
jmp 0x1621c
jmp 0x16131
movb $0x1, -0x14a(%rbp)
movl $0x58, %edi
callq 0xd1e0
movq %rax, -0x1a0(%rbp)
leaq -0x149(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0xd760
movq -0x198(%rbp), %rdx
leaq 0x17047(%rip), %rsi     # 0x2d1b1
leaq -0x148(%rbp), %rdi
callq 0xf2c0
jmp 0x16178
movq -0x1a0(%rbp), %rdi
leaq 0x17293(%rip), %rcx     # 0x2d419
leaq -0x148(%rbp), %rsi
movl $0x2ac, %edx            # imm = 0x2AC
callq 0x18170
jmp 0x16199
movq -0x1a0(%rbp), %rdi
movb $0x0, -0x14a(%rbp)
leaq 0x1fb0a(%rip), %rsi     # 0x35cb8
leaq 0x21bb(%rip), %rdx      # 0x18370
callq 0xd720
jmp 0x16225
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x161e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x148(%rbp), %rdi
callq 0xd240
leaq -0x149(%rbp), %rdi
callq 0xd430
testb $0x1, -0x14a(%rbp)
jne 0x161f9
jmp 0x16205
movq -0x1a0(%rbp), %rdi
callq 0xd2c0
jmp 0x1621c
jmp 0x15d72
movq -0xb0(%rbp), %rax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0xd750
nopw %cs:(%rax,%rax)