// File: /workspace/github_c_makefile_star2_O3/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, 0x6514(%rip)      # 0x921a
jne 0x2d6f
testq %r8, %r8
jne 0x2d15
callq 0x35cc
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 0x2d4e
movb %al, (%rdi)
incq %rdi
incq %rdx
movb %al, (%rdi)
incq %rdi
incq %rdx
shrq $0x8, %rax
decl %esi
jne 0x2d42
movw $0xa1b, 0x13(%rsp,%rdx) # imm = 0xA1B
leaq (%rcx,%rdx), %rax
addq $0x2, %rax
jmp 0x2d74
leaq 0x10(%rsp), %rax
movw $0x210, (%rax)          # imm = 0x210
movb %r15b, 0x2(%rax)
leaq 0x4(%rax), %rbp
movb %bl, 0x3(%rax)
testq %rbx, %rbx
je 0x2dc9
cmpq $0x10, %rbx
jne 0x2d9a
movb $0x10, 0x4(%rax)
addq $0x5, %rax
movq %rax, %rbp
xorl %ecx, %ecx
movl %r15d, %eax
movb (%r14,%rcx), %dl
cmpb $0x10, %dl
jne 0x2db3
movb $0x10, (%rbp)
incq %rbp
movb (%r14,%rcx), %dl
movb %dl, (%rbp)
incq %rbp
addb (%r14,%rcx), %al
incq %rcx
cmpq %rcx, %rbx
jne 0x2d9f
movl %ebx, %ecx
jmp 0x2dce
xorl %ecx, %ecx
movl %r15d, %eax
addb %cl, %al
movl %eax, %ecx
negb %cl
cmpb $-0x10, %al
movl %r15d, 0x8(%rsp)
jne 0x2de4
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, %r15d
movslq %ebp, %r13
movl 0xc(%rsp), %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0x20c0
cmpl $-0x1, %eax
jne 0x2e3b
callq 0x2050
cmpl $0xb, (%rax)
jne 0x2e47
movl $0x61a8, %edi           # imm = 0x61A8
callq 0x2340
testl %ebp, %ebp
jle 0x2e47
leal -0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jg 0x2e01
jmp 0x2e47
movslq %eax, %rcx
addq %rcx, %r12
subl %eax, %ebp
jg 0x2dfe
jmp 0x2e5a
leaq 0x39ca(%rip), %rdi      # 0x6818
movl %ebx, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3880
movzbl 0x8(%rsp), %esi
leaq 0x39e4(%rip), %rdi      # 0x684a
movl %ebx, %edx
xorl %eax, %eax
callq 0x37ed
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq