// File: /workspace/github_c_makefile_star2_O2/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, %r15d
movl %edi, 0xc(%rsp)
cmpb $0x1, 0x7592(%rip)      # 0xa21a
jne 0x2ce1
testq %r8, %r8
jne 0x2c97
callq 0x34e0
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)
pushq $0x8
popq %rsi
xorl %edx, %edx
movq %rcx, %rdi
subl $0x1, %esi
jb 0x2ce8
cmpb $0x1b, %al
jne 0x2cd3
movb %al, (%rdi)
incq %rdi
incq %rdx
movb %al, (%rdi)
incq %rdi
incq %rdx
shrq $0x8, %rax
jmp 0x2cc2
leaq 0x10(%rsp), %rbp
jmp 0x2cf7
movw $0xa1b, 0x13(%rsp,%rdx) # imm = 0xA1B
leaq (%rcx,%rdx), %rbp
addq $0x2, %rbp
movw $0x210, (%rbp)          # imm = 0x210
movb %r15b, 0x2(%rbp)
movb %bl, 0x3(%rbp)
cmpq $0x10, %rbx
jne 0x2d14
movb $0x10, 0x4(%rbp)
addq $0x5, %rbp
jmp 0x2d18
addq $0x4, %rbp
xorl %ecx, %ecx
movl %r15d, %eax
cmpq %rcx, %rbx
je 0x2d45
movb (%r14,%rcx), %dl
cmpb $0x10, %dl
jne 0x2d36
movb $0x10, (%rbp)
incq %rbp
movb (%r14,%rcx), %dl
movb %dl, (%rbp)
incq %rbp
addb (%r14,%rcx), %al
incq %rcx
jmp 0x2d1d
addb %bl, %al
movl %eax, %ecx
negb %cl
cmpb $-0x10, %al
movl %r15d, 0x8(%rsp)
jne 0x2d5b
movb $0x10, (%rbp)
incq %rbp
movb %cl, (%rbp)
movw $0x310, 0x1(%rbp)       # imm = 0x310
leaq 0x10(%rsp), %r12
subl %r12d, %ebp
addl $0x3, %ebp
pushq $0x5
popq %r15
movslq %ebp, %r13
movl 0xc(%rsp), %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0x20c0
cmpl $-0x1, %eax
jne 0x2db0
callq 0x2050
cmpl $0xb, (%rax)
jne 0x2dbc
movl $0x61a8, %edi           # imm = 0x61A8
callq 0x2340
testl %ebp, %ebp
jle 0x2dbc
leal -0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jg 0x2d76
jmp 0x2dbc
movslq %eax, %rcx
addq %rcx, %r12
subl %eax, %ebp
jg 0x2d73
jmp 0x2dcf
leaq 0x3a55(%rip), %rdi      # 0x6818
movl %ebx, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3765
movzbl 0x8(%rsp), %esi
leaq 0x3a6f(%rip), %rdi      # 0x684a
movl %ebx, %edx
xorl %eax, %eax
callq 0x36d4
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq