// File: /workspace/github_c_makefile_star2_O0/canboat[P]canboat/actisense-serial/actisense-serial.c
// Function: parseAndWriteIn
static void parseAndWriteIn(int handle, const char *cmd)
{
  unsigned char  msg[500];
  unsigned char *m;

  unsigned int prio;
  unsigned int pgn;
  unsigned int src;
  unsigned int dst;
  unsigned int bytes;

  char        *p;
  int          i;
  int          b;
  unsigned int byt;
  int          r;
  uint64_t     when = 0;

  if (!cmd || !*cmd || *cmd == '\n')
  {
    return;
  }

  parseTimestamp(cmd, &when);
  p = strchr(cmd, ',');
  if (!p)
  {
    return;
  }

  r = sscanf(p, ",%u,%u,%u,%u,%u,%n", &prio, &pgn, &src, &dst, &bytes, &i);
  logDebug("parseAndWriteIn %.20s = %d\n", p, r);
  if (r == 5)
  {
    if (pgn >= ACTISENSE_BEM)
    { // Ignore synthetic CANboat PGNs that report original device status.
      return;
    }

    p += i - 1;
    m    = msg;
    *m++ = (unsigned char) prio;
    *m++ = (unsigned char) pgn;
    *m++ = (unsigned char) (pgn >> 8);
    *m++ = (unsigned char) (pgn >> 16);
    *m++ = (unsigned char) dst;
    //*m++ = (unsigned char) 0;
    *m++ = (unsigned char) bytes;
    for (b = 0; m < msg + sizeof(msg) && b < bytes; b++)
    {
      if ((sscanf(p, ",%x%n", &byt, &i) == 1) && (byt < 256))
      {
        *m++ = byt;
      }
      else
      {
        logError("Unable to parse incoming message '%s' at offset %u\n", cmd, b);
        return;
      }
      p += i;
    }
  }
  else
  {
    logError("Unable to parse incoming message '%s', r = %d\n", cmd, r);
    return;
  }

  logDebug("About to write:  %s\n", cmd);
  writeMessage(handle, N2K_MSG_SEND, msg, m - msg, when);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x250(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x33b9
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x33b9
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x33be
jmp 0x36ae
movq -0x10(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x77e0
movq -0x10(%rbp), %rdi
movl $0x2c, %esi
callq 0x20e0
movq %rax, -0x238(%rbp)
cmpq $0x0, -0x238(%rbp)
jne 0x33f2
jmp 0x36ae
movq -0x238(%rbp), %rdi
leaq 0x5333(%rip), %rsi      # 0x8733
leaq -0x21c(%rbp), %rdx
leaq -0x220(%rbp), %rcx
leaq -0x224(%rbp), %r8
leaq -0x228(%rbp), %r9
leaq -0x22c(%rbp), %r10
leaq -0x23c(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2210
movl %eax, -0x248(%rbp)
movq -0x238(%rbp), %rsi
movl -0x248(%rbp), %edx
leaq 0x52f2(%rip), %rdi      # 0x8746
movb $0x0, %al
callq 0x42e0
cmpl $0x5, -0x248(%rbp)
jne 0x3656
cmpl $0x40000, -0x220(%rbp)  # imm = 0x40000
jb 0x3479
jmp 0x36ae
movl -0x23c(%rbp), %ecx
subl $0x1, %ecx
movq -0x238(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x238(%rbp)
leaq -0x210(%rbp), %rax
movq %rax, -0x218(%rbp)
movl -0x21c(%rbp), %eax
movb %al, %cl
movq -0x218(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x218(%rbp)
movb %cl, (%rax)
movl -0x220(%rbp), %eax
movb %al, %cl
movq -0x218(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x218(%rbp)
movb %cl, (%rax)
movl -0x220(%rbp), %eax
shrl $0x8, %eax
movb %al, %cl
movq -0x218(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x218(%rbp)
movb %cl, (%rax)
movl -0x220(%rbp), %eax
shrl $0x10, %eax
movb %al, %cl
movq -0x218(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x218(%rbp)
movb %cl, (%rax)
movl -0x228(%rbp), %eax
movb %al, %cl
movq -0x218(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x218(%rbp)
movb %cl, (%rax)
movl -0x22c(%rbp), %eax
movb %al, %cl
movq -0x218(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x218(%rbp)
movb %cl, (%rax)
movl $0x0, -0x240(%rbp)
movq -0x218(%rbp), %rcx
leaq -0x210(%rbp), %rdx
addq $0x1f4, %rdx            # imm = 0x1F4
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x251(%rbp)
jae 0x35a5
movl -0x240(%rbp), %eax
cmpl -0x22c(%rbp), %eax
setb %al
movb %al, -0x251(%rbp)
movb -0x251(%rbp), %al
testb $0x1, %al
jne 0x35b4
jmp 0x3654
movq -0x238(%rbp), %rdi
leaq 0x51a0(%rip), %rsi      # 0x8762
leaq -0x244(%rbp), %rdx
leaq -0x23c(%rbp), %rcx
movb $0x0, %al
callq 0x2210
cmpl $0x1, %eax
jne 0x3609
cmpl $0x100, -0x244(%rbp)    # imm = 0x100
jae 0x3609
movl -0x244(%rbp), %eax
movb %al, %cl
movq -0x218(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x218(%rbp)
movb %cl, (%rax)
jmp 0x3626
movq -0x10(%rbp), %rsi
movl -0x240(%rbp), %edx
leaq 0x514e(%rip), %rdi      # 0x8768
movb $0x0, %al
callq 0x4390
jmp 0x36ae
movl -0x23c(%rbp), %ecx
movq -0x238(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x238(%rbp)
movl -0x240(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x240(%rbp)
jmp 0x356e
jmp 0x3670
movq -0x10(%rbp), %rsi
movl -0x248(%rbp), %edx
leaq 0x5135(%rip), %rdi      # 0x879c
movb $0x0, %al
callq 0x4390
jmp 0x36ae
movq -0x10(%rbp), %rsi
leaq 0x5150(%rip), %rdi      # 0x87cb
movb $0x0, %al
callq 0x42e0
movl -0x4(%rbp), %edi
leaq -0x210(%rbp), %rdx
movq -0x218(%rbp), %rcx
leaq -0x210(%rbp), %rax
subq %rax, %rcx
movq -0x250(%rbp), %r8
movl $0x94, %esi
callq 0x2c40
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
nopw (%rax,%rax)