// File: /workspace/github_c_makefile_star2_O1/canboat[P]canboat/actisense-serial/actisense-serial.c
// Function: writeMessage
static void writeMessage(int handle, unsigned char command, const unsigned char *cmd, const size_t len, uint64_t when)
{
  unsigned char  bst[255];
  unsigned char *b = bst;
  unsigned char *r = bst;
  unsigned char  crc;

  int i;

  if (isEBL)
  {
    if (when == 0)
    {
      when = getNow();
    }
    // Prepend with timestamp
    when = (when + UINT64_C(11644473600000)) * UINT64_C(10000);

    *b++ = ESC;
    *b++ = SOH;
    *b++ = EBL_TIMESTAMP;
    b += writeUint64(when, b);
    *b++ = ESC;
    *b++ = LF;
  }

  *b++ = DLE;
  *b++ = STX;
  *b++ = command;
  crc  = command;
  *b++ = len;
  if (len == DLE)
  {
    *b++ = DLE;
  }

  for (i = 0; i < len; i++)
  {
    if (cmd[i] == DLE)
    {
      *b++ = DLE;
    }
    *b++ = cmd[i];
    crc += (unsigned char) cmd[i];
  }

  crc += i;

  crc = 256 - (int) crc;
  if (crc == DLE)
  {
    *b++ = DLE;
  }
  *b++ = crc;
  *b++ = DLE;
  *b++ = ETX;

  int retryCount    = 5;
  int needs_written = b - bst;
  int written;
  do
  {
    written = write(handle, r, needs_written);
    if (written != -1)
    {
      r += written;
      needs_written -= written;
    }
    else if (errno == EAGAIN)
    {
      retryCount--;
      usleep(25000);
    }
    else
    {
      break;
    }

  } while (needs_written > 0 && retryCount >= 0);

  if (written == -1)
  {
    logError("Unable to write command '%.*s' to NGT-1-A device\n", (int) len, cmd);
  }

  logDebug("Written command %X len %d\n", command, (int) len);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %r12d
movl %edi, %r15d
cmpb $0x1, 0x64e8(%rip)      # 0x921a
jne 0x2d9b
testq %r8, %r8
jne 0x2d41
callq 0x362c
movq %rax, %r8
imulq $0x2710, %r8, %rcx      # imm = 0x2710
movabsq $0x19db1ded53e8000, %rax # imm = 0x19DB1DED53E8000
addq %rcx, %rax
leaq 0x13(%rsp), %rcx
movw $0x11b, -0x3(%rcx)      # imm = 0x11B
movb $0x3, -0x1(%rcx)
movl $0x8, %esi
xorl %edx, %edx
movq %rcx, %rdi
cmpb $0x1b, %al
jne 0x2d7a
movb %al, (%rdi)
incq %rdi
incq %rdx
movb %al, (%rdi)
incq %rdi
incq %rdx
shrq $0x8, %rax
decl %esi
jne 0x2d6e
movw $0xa1b, 0x13(%rsp,%rdx) # imm = 0xA1B
leaq (%rcx,%rdx), %rbp
addq $0x2, %rbp
jmp 0x2da0
leaq 0x10(%rsp), %rbp
movw $0x210, (%rbp)          # imm = 0x210
movb %r12b, 0x2(%rbp)
movb %bl, 0x3(%rbp)
cmpq $0x10, %rbx
jne 0x2dbd
movb $0x10, 0x4(%rbp)
addq $0x5, %rbp
jmp 0x2dc1
addq $0x4, %rbp
testq %rbx, %rbx
je 0x2df3
xorl %ecx, %ecx
movl %r12d, %eax
cmpb $0x10, (%r14,%rcx)
jne 0x2dd9
movb $0x10, (%rbp)
incq %rbp
movb (%r14,%rcx), %dl
movb %dl, (%rbp)
incq %rbp
addb (%r14,%rcx), %al
incq %rcx
cmpq %rcx, %rbx
jne 0x2dcb
movl %ebx, %ecx
jmp 0x2df8
xorl %ecx, %ecx
movl %r12d, %eax
addb %cl, %al
movl %eax, %ecx
negb %cl
cmpb $-0x10, %al
movq %rbx, 0x8(%rsp)
movl %r12d, 0x4(%rsp)
jne 0x2e13
movb $0x10, (%rbp)
incq %rbp
movb %cl, (%rbp)
movw $0x310, 0x1(%rbp)       # imm = 0x310
leaq 0x10(%rsp), %r12
subl %r12d, %ebp
addl $0x3, %ebp
movl $0x5, %ebx
movslq %ebp, %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x20c0
movq %rax, %r13
cmpl $-0x1, %r13d
je 0x2e4e
movslq %r13d, %rax
addq %rax, %r12
subl %r13d, %ebp
jmp 0x2e64
callq 0x2050
cmpl $0xb, (%rax)
jne 0x2e6c
decl %ebx
movl $0x61a8, %edi           # imm = 0x61A8
callq 0x2340
testl %ebp, %ebp
jle 0x2e6c
testl %ebx, %ebx
jns 0x2e2c
cmpl $-0x1, %r13d
movq 0x8(%rsp), %rbx
jne 0x2e8a
leaq 0x39b6(%rip), %rdi      # 0x6834
movl %ebx, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x38e0
movzbl 0x4(%rsp), %esi
leaq 0x39d0(%rip), %rdi      # 0x6866
movl %ebx, %edx
xorl %eax, %eax
callq 0x384d
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq