// File: /workspace/github_c_makefile_star2_O0/canboat[P]canboat/ikonvert-serial/ikonvert-serial.c
// Function: processReadBuffer
static bool processReadBuffer(StringBuffer *in, int out)
{
  RawMessage  msg;
  char       *p;
  const char *w;
  bool        allowInit = true;
  bool        ret = false;

  logDebug("processReadBuffer len=%zu\n", sbGetLength(in));
  while ((p = sbSearchChar(in, '\n')) != 0)
  {
    w = sbGet(in);
    if ((p - w > sizeof IKONVERT_ASCII_PREFIX) && (w[0] == '$' || w[0] == '!'))
    {
      logDebug("processReadBuffer found record len=%zu\n", p - w);
      memset(&msg, 0, sizeof msg);

      p[0] = 0;

      if (p > w + 1 && p[-1] == '\r')
      {
        p[-1] = 0;
      }

      logDebug("Received [%s]\n", w);

      if (writeonly)
      {
        // ignore message
      }
      else if (parseIKonvertAsciiMessage(sbGet(in), &msg))
      {
        logDebug("ASCII message [%s] handled\n", sbGet(in));
        // great
        if (allowInit)
        {
          sendNextInitCommand();
          allowInit = false;
        }
      }
      else if (parseIKonvertFormat(in, &msg))
      {
        sequentialStatusMessages = 0;
        if (sendInitState > 0)
        {
          msg.len = 0;
        }
        ret = true; // Got some actual N2K message from iKonvert, meaning it is still working so return true.
      }
      else
      {
        logError("Ignoring unknown or invalid message '%s'\n", sbGet(in));
      }

      if (msg.len > 0)
      {
        ssize_t r;

        // Format msg as FAST message
        sbAppendFormat(&dataBuffer, "%s,%u,%u,%u,%u,%u,", msg.timestamp, msg.prio, msg.pgn, msg.src, msg.dst, msg.len);
        sbAppendEncodeHex(&dataBuffer, msg.data, msg.len, ',');
        sbAppendString(&dataBuffer, "\n");

        r = write(out, sbGet(&dataBuffer), sbGetLength(&dataBuffer));
        if (r <= 0)
        {
          logAbort("Cannot write to output\n");
        }

        sbEmpty(&dataBuffer);
      }
    }
    else
    {
      logDebug("Junk record len=%zu\n", p + 1 - sbGet(in));
    }
    sbDelete(in, 0, p + 1 - sbGet(in));
  }
  if (sbGetLength(in) > 0 && *sbGet(in) != '$' && *sbGet(in) != '!')
  {
    // Remove any gibberish from buffer
    sbEmpty(in);
  }

  return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movb $0x1, -0x149(%rbp)
movb $0x0, -0x14a(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x66d0(%rip), %rdi      # 0x9a8f
movb $0x0, %al
callq 0x4640
movq -0x8(%rbp), %rdi
movl $0xa, %esi
callq 0x58f0
movq %rax, -0x140(%rbp)
cmpq $0x0, %rax
je 0x3816
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x33ff
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x160(%rbp)
jmp 0x340f
leaq 0x6347(%rip), %rax      # 0x974d
movq %rax, -0x160(%rbp)
jmp 0x340f
movq -0x160(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rax
movq -0x148(%rbp), %rcx
subq %rcx, %rax
cmpq $0x7, %rax
jbe 0x374e
movq -0x148(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x24, %eax
je 0x345a
movq -0x148(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x21, %eax
jne 0x374e
movq -0x140(%rbp), %rsi
movq -0x148(%rbp), %rax
subq %rax, %rsi
leaq 0x6638(%rip), %rdi      # 0x9aaa
movb $0x0, %al
callq 0x4640
leaq -0x134(%rbp), %rdi
xorl %esi, %esi
movl $0x128, %edx            # imm = 0x128
callq 0x2140
movq -0x140(%rbp), %rax
movb $0x0, (%rax)
movq -0x140(%rbp), %rax
movq -0x148(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jbe 0x34c8
movq -0x140(%rbp), %rax
movsbl -0x1(%rax), %eax
cmpl $0xd, %eax
jne 0x34c8
movq -0x140(%rbp), %rax
movb $0x0, -0x1(%rax)
movq -0x148(%rbp), %rsi
leaq 0x65fc(%rip), %rdi      # 0x9ad2
movb $0x0, %al
callq 0x4640
testb $0x1, 0x9534(%rip)      # 0xca18
je 0x34eb
jmp 0x3600
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3505
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x168(%rbp)
jmp 0x3515
leaq 0x6241(%rip), %rax      # 0x974d
movq %rax, -0x168(%rbp)
jmp 0x3515
movq -0x168(%rbp), %rdi
leaq -0x134(%rbp), %rsi
callq 0x3bd0
testb $0x1, %al
jne 0x352e
jmp 0x3584
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3548
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x170(%rbp)
jmp 0x3558
leaq 0x61fe(%rip), %rax      # 0x974d
movq %rax, -0x170(%rbp)
jmp 0x3558
movq -0x170(%rbp), %rsi
leaq 0x657b(%rip), %rdi      # 0x9ae1
movb $0x0, %al
callq 0x4640
testb $0x1, -0x149(%rbp)
je 0x3582
callq 0x3900
movb $0x0, -0x149(%rbp)
jmp 0x35fe
movq -0x8(%rbp), %rdi
leaq -0x134(%rbp), %rsi
callq 0x4100
testb $0x1, %al
jne 0x359a
jmp 0x35bd
movl $0x0, 0x9534(%rip)      # 0xcad8
cmpl $0x0, 0x94d5(%rip)      # 0xca80
jle 0x35b4
movb $0x0, -0xee(%rbp)
movb $0x1, -0x14a(%rbp)
jmp 0x35fc
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x35d7
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x178(%rbp)
jmp 0x35e7
leaq 0x616f(%rip), %rax      # 0x974d
movq %rax, -0x178(%rbp)
jmp 0x35e7
movq -0x178(%rbp), %rsi
leaq 0x6508(%rip), %rdi      # 0x9afd
movb $0x0, %al
callq 0x46f0
jmp 0x35fe
jmp 0x3600
movzbl -0xee(%rbp), %eax
cmpl $0x0, %eax
jle 0x374c
leaq -0x134(%rbp), %rdx
movzbl -0xf8(%rbp), %ecx
movl -0xf4(%rbp), %r8d
movzbl -0xef(%rbp), %r9d
movzbl -0xf0(%rbp), %r10d
movzbl -0xee(%rbp), %eax
leaq 0x947d(%rip), %rdi      # 0xcac0
leaq 0x64dd(%rip), %rsi      # 0x9b27
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x4e10
leaq -0x134(%rbp), %rsi
addq $0x47, %rsi
movzbl -0xee(%rbp), %eax
movl %eax, %edx
leaq 0x944c(%rip), %rdi      # 0xcac0
movl $0x2c, %ecx
callq 0x4d40
leaq 0x943b(%rip), %rdi      # 0xcac0
leaq 0x60c0(%rip), %rsi      # 0x974c
callq 0x5060
movl -0xc(%rbp), %eax
movl %eax, -0x17c(%rbp)
cmpq $0x0, 0x941e(%rip)      # 0xcac0
je 0x36b4
movq 0x9415(%rip), %rax      # 0xcac0
movq %rax, -0x188(%rbp)
jmp 0x36c4
leaq 0x6092(%rip), %rax      # 0x974d
movq %rax, -0x188(%rbp)
jmp 0x36c4
movl -0x17c(%rbp), %edi
movq -0x188(%rbp), %rsi
movq 0x93f0(%rip), %rdx      # 0xcac8
callq 0x20a0
movq %rax, -0x158(%rbp)
cmpq $0x0, -0x158(%rbp)
jg 0x36fc
leaq 0x625e(%rip), %rdi      # 0x9953
movb $0x0, %al
callq 0x47a0
xorl %eax, %eax
cmpq 0x93c3(%rip), %rax      # 0xcac8
jae 0x3712
xorl %eax, %eax
movq %rax, -0x190(%rbp)
jmp 0x3720
movq 0x93af(%rip), %rax      # 0xcac8
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rax
movq %rax, 0x939a(%rip)      # 0xcac8
cmpq $0x0, 0x938a(%rip)      # 0xcac0
je 0x374a
movq 0x9381(%rip), %rax      # 0xcac0
movq 0x9382(%rip), %rcx      # 0xcac8
movb $0x0, (%rax,%rcx)
jmp 0x374c
jmp 0x37a9
movq -0x140(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x198(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x377a
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1a0(%rbp)
jmp 0x378a
leaq 0x5fcc(%rip), %rax      # 0x974d
movq %rax, -0x1a0(%rbp)
jmp 0x378a
movq -0x198(%rbp), %rsi
movq -0x1a0(%rbp), %rax
subq %rax, %rsi
leaq 0x6398(%rip), %rdi      # 0x9b3a
movb $0x0, %al
callq 0x4640
movq -0x8(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x140(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1a8(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x37e0
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1b8(%rbp)
jmp 0x37f0
leaq 0x5f66(%rip), %rax      # 0x974d
movq %rax, -0x1b8(%rbp)
jmp 0x37f0
movq -0x1b0(%rbp), %rdi
movq -0x1a8(%rbp), %rdx
movq -0x1b8(%rbp), %rax
subq %rax, %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x4b50
jmp 0x33c6
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x38ef
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x383f
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1c0(%rbp)
jmp 0x384f
leaq 0x5f07(%rip), %rax      # 0x974d
movq %rax, -0x1c0(%rbp)
jmp 0x384f
movq -0x1c0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x24, %eax
je 0x38ef
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x387c
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1c8(%rbp)
jmp 0x388c
leaq 0x5eca(%rip), %rax      # 0x974d
movq %rax, -0x1c8(%rbp)
jmp 0x388c
movq -0x1c8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x21, %eax
je 0x38ef
movq -0x8(%rbp), %rcx
xorl %eax, %eax
cmpq 0x8(%rcx), %rax
jae 0x38b2
xorl %eax, %eax
movq %rax, -0x1d0(%rbp)
jmp 0x38c1
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x38ed
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movb $0x0, (%rax,%rcx)
jmp 0x38ef
movb -0x14a(%rbp), %al
andb $0x1, %al
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq