// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movb %sil, %al
movl %edi, -0x4(%rbp)
movb %al, -0x5(%rbp)
movq %rdx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0x130(%rbp)
testb $0x1, 0x85ca(%rip)      # 0xb24d
je 0x2d48
cmpq $0x0, -0x20(%rbp)
jne 0x2c99
callq 0x4020
movq %rax, -0x20(%rbp)
movabsq $0xa9730b66800, %rax    # imm = 0xA9730B66800
addq -0x20(%rbp), %rax
imulq $0x2710, %rax, %rax     # imm = 0x2710
movq %rax, -0x20(%rbp)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x1b, (%rax)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x1, (%rax)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x3, (%rax)
movq -0x20(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x36c0
addq -0x128(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x1b, (%rax)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0xa, (%rax)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x10, (%rax)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x2, (%rax)
movb -0x5(%rbp), %cl
movq -0x128(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x128(%rbp)
movb %cl, (%rax)
movb -0x5(%rbp), %al
movb %al, -0x131(%rbp)
movq -0x18(%rbp), %rax
movb %al, %cl
movq -0x128(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x128(%rbp)
movb %cl, (%rax)
cmpq $0x10, -0x18(%rbp)
jne 0x2dd7
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x10, (%rax)
movl $0x0, -0x138(%rbp)
movslq -0x138(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x2e75
movq -0x10(%rbp), %rax
movslq -0x138(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x10, %eax
jne 0x2e1e
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x10, (%rax)
movq -0x10(%rbp), %rax
movslq -0x138(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x128(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x128(%rbp)
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movslq -0x138(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
movzbl -0x131(%rbp), %eax
addl %ecx, %eax
movb %al, -0x131(%rbp)
movl -0x138(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x138(%rbp)
jmp 0x2de1
movl -0x138(%rbp), %ecx
movzbl -0x131(%rbp), %eax
addl %ecx, %eax
movb %al, -0x131(%rbp)
movzbl -0x131(%rbp), %ecx
movl $0x100, %eax            # imm = 0x100
subl %ecx, %eax
movb %al, -0x131(%rbp)
movzbl -0x131(%rbp), %eax
cmpl $0x10, %eax
jne 0x2ec2
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x10, (%rax)
movb -0x131(%rbp), %cl
movq -0x128(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x128(%rbp)
movb %cl, (%rax)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x10, (%rax)
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x128(%rbp)
movb $0x3, (%rax)
movl $0x5, -0x13c(%rbp)
movq -0x128(%rbp), %rax
leaq -0x120(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x140(%rbp)
movl -0x4(%rbp), %edi
movq -0x130(%rbp), %rsi
movslq -0x140(%rbp), %rdx
callq 0x20b0
movl %eax, -0x144(%rbp)
cmpl $-0x1, -0x144(%rbp)
je 0x2f85
movl -0x144(%rbp), %ecx
movq -0x130(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x130(%rbp)
movl -0x144(%rbp), %ecx
movl -0x140(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x140(%rbp)
jmp 0x2fae
callq 0x2050
cmpl $0xb, (%rax)
jne 0x2faa
movl -0x13c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x13c(%rbp)
movl $0x61a8, %edi           # imm = 0x61A8
callq 0x2340
jmp 0x2fac
jmp 0x2fdf
jmp 0x2fae
jmp 0x2fb0
xorl %eax, %eax
cmpl $0x0, -0x140(%rbp)
movb %al, -0x145(%rbp)
jle 0x2fd1
cmpl $0x0, -0x13c(%rbp)
setge %al
movb %al, -0x145(%rbp)
movb -0x145(%rbp), %al
testb $0x1, %al
jne 0x2f30
cmpl $-0x1, -0x144(%rbp)
jne 0x3000
movq -0x18(%rbp), %rax
movl %eax, %esi
movq -0x10(%rbp), %rdx
leaq 0x57e7(%rip), %rdi      # 0x87e0
movb $0x0, %al
callq 0x4390
movzbl -0x5(%rbp), %esi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x5801(%rip), %rdi      # 0x8812
movb $0x0, %al
callq 0x42e0
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw %cs:(%rax,%rax)