// File: /workspace/github_c_makefile_star2_O1/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, -0x48(%rbp)
cmpb $0x0, 0x2c(%rdi)
je 0xcd79
movq %rdx, %r13
movl 0x40(%rdi), %esi
imull %r13d, %esi
movq %rdi, -0x30(%rbp)
addl 0x3c(%rdi), %esi
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
callq 0xb720
movq (%rbx), %rax
movq %rax, -0x50(%rbp)
movq 0x8(%rbx), %r15
xorl %r14d, %r14d
leaq -0x40(%rbp), %rsi
movq %r13, %rbx
subq %r14, %rbx
jbe 0xcc6e
xorl %edi, %edi
movq %rsi, %r12
callq 0x82d0
movsd 0x146ae(%rip), %xmm3    # 0x21160
movsd 0x146ae(%rip), %xmm2    # 0x21168
movq -0x50(%rbp), %rax
subq -0x40(%rbp), %rax
movq %r15, %rcx
subq -0x38(%rbp), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
divsd %xmm3, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
testq %rax, %rax
jle 0xcc15
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, -0x40(%rbp)
movq %rcx, -0x38(%rbp)
xorpd %xmm0, %xmm0
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)
movapd %xmm0, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %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, -0x110(%rbp,%rax,8)
incl %edi
xorl %esi, %esi
leaq -0x110(%rbp), %rdx
xorl %ecx, %ecx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x8370
testl %eax, %eax
js 0xcc19
je 0xcc33
movq -0x30(%rbp), %rax
movl 0x28(%rax), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq -0x110(%rbp,%rax,8), %rax
btq %rdi, %rax
jae 0xcc83
movq -0x48(%rbp), %rax
leaq (%rax,%r14), %rsi
movq %rbx, %rdx
callq 0x8130
testq %rax, %rax
jle 0xcce7
addq %r14, %rax
cmpq %r13, %rax
jne 0xcc3d
movl $0x3, %ecx
jmp 0xcc49
movb $0x1, %cl
jmp 0xcc62
callq 0x8060
movq %rax, %rbx
movl $0x2, %ecx
cmpl $0x4, (%rax)
movq %r14, %rax
je 0xcc59
jmp 0xcd19
movl $0x3, %ecx
movq %r14, %rax
jmp 0xcc59
xorl %ecx, %ecx
cmpq %r13, %rax
jae 0xcc4d
movl $0x2, %ecx
xorl %edx, %edx
jmp 0xcc55
movb $0x1, %dl
ja 0xcdab
testb %dl, %dl
jne 0xcc83
cmpl $0x3, %ecx
sete %cl
movq %rax, %r14
testb %cl, %cl
leaq -0x40(%rbp), %rsi
je 0xca94
movq %r14, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x58, %edi
callq 0x81a0
movq %rax, %r15
leaq -0x60(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x14572(%rip), %rsi     # 0x21212
leaq 0x14581(%rip), %rdx     # 0x21228
leaq -0x70(%rbp), %rdi
callq 0x9f82
movb $0x1, %bl
leaq 0x147c1(%rip), %rcx     # 0x2147a
leaq -0x70(%rbp), %rsi
movq %r15, %rdi
movl $0x2ac, %edx            # imm = 0x2AC
callq 0xe3c0
xorl %ebx, %ebx
leaq 0x1afe5(%rip), %rsi     # 0x27cb8
leaq 0x18e8(%rip), %rdx      # 0xe5c2
movq %r15, %rdi
callq 0x85c0
jmp 0xcd79
movl $0x28, %edi
callq 0x81a0
movq %rax, %r15
leaq 0x146d0(%rip), %rsi     # 0x213cb
movq %rax, %rdi
callq 0xe21e
leaq 0x1af96(%rip), %rsi     # 0x27ca0
leaq 0x1675(%rip), %rdx      # 0xe386
movq %r15, %rdi
callq 0x85c0
movl $0x58, %edi
callq 0x81a0
movq %rax, %r15
leaq -0x80(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x144dc(%rip), %rsi     # 0x21212
leaq 0x144eb(%rip), %rdx     # 0x21228
leaq -0x90(%rbp), %rdi
callq 0x9f82
movl (%rbx), %ecx
movb $0x1, %bl
leaq -0x90(%rbp), %rsi
movq %r15, %rdi
movl $0x283, %edx            # imm = 0x283
callq 0xe614
xorl %ebx, %ebx
leaq 0x1af4e(%rip), %rsi     # 0x27cb8
leaq 0x1851(%rip), %rdx      # 0xe5c2
movq %r15, %rdi
callq 0x85c0
movl $0x28, %edi
callq 0x81a0
movq %rax, %rbx
leaq 0x14630(%rip), %rsi     # 0x213bd
movq %rax, %rdi
callq 0xe848
leaq 0x1af34(%rip), %rsi     # 0x27cd0
leaq 0x1c0d(%rip), %rdx      # 0xe9b0
movq %rbx, %rdi
callq 0x85c0
movl $0x28, %edi
callq 0x81a0
movq %rax, %r15
leaq 0x1465a(%rip), %rsi     # 0x21419
movq %rax, %rdi
callq 0xe21e
jmp 0xcd03
jmp 0xcded
movq %rax, %r14
movq %rbx, %rdi
jmp 0xce18
movq %rax, %r14
movq -0x90(%rbp), %rdi
cmpq %r12, %rdi
je 0xce11
movq -0x80(%rbp), %rsi
jmp 0xce02
jmp 0xce0c
movq %rax, %r14
jmp 0xce15
movq %rax, %r14
movq -0x70(%rbp), %rdi
cmpq %r12, %rdi
je 0xce11
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x8320
jmp 0xce11
movq %rax, %r14
movb $0x1, %bl
testb %bl, %bl
je 0xce1d
movq %r15, %rdi
callq 0x8250
movq %r14, %rdi
callq 0x8600
nop