// File: /workspace/github_c_makefile_star2_O2/canboat[P]canboat/n2kd/main.c
// Function: getFullStateJSON
static char *getFullStateJSON(StreamType stream, int64_t now)
{
  StringBuffer state     = sbNew;
  char         separator = '{';

  int  i, s;
  Pgn *pgn;

  if (stream == CLIENT_STATUS_STREAM)
  {
    for (i = 0; i < maxPgnList; i++)
    {
      pgn = *pgnList[i];

      sbAppendFormat(&state, "%c\"%u\":\n  {\"description\":\"%s\"\n", separator, pgn->p_prn, pgn->p_description);

      for (s = 0; s < pgn->p_maxSrc; s++)
      {
        Message *m = &pgn->p_message[s];
        char     last_ts[DATE_LENGTH];

        sbAppendFormat(&state,
                       "  ,\"%u%s%s\":{\"last\":\"%s\",\"interval\":%u,\"count\":%u}\n",
                       m->m_src,
                       m->m_key2 ? "_" : "",
                       m->m_key2 ? m->m_key2 : "",
                       fmtTimestamp(last_ts, m->m_last),
                       m->m_interval,
                       m->m_count);
      }
      sbAppendFormat(&state, "  }\n");
      separator = ',';
    }
  }
  else
  {
    for (i = 0; i < maxPgnList; i++)
    {
      pgn = *pgnList[i];

      // AIS data only goes to AIS clients, non-AIS data to non-AIS clients, but
      // PRNs 129026 and 129029 go to both.
      if ((stream == CLIENT_AIS) == (strncmp(pgn->p_description, "AIS", 3) == 0) || pgn->p_prn == 129026 || pgn->p_prn == 129029)
      {
        sbAppendFormat(&state, "%c\"%u\":\n  {\"description\":\"%s\"\n", separator, pgn->p_prn, pgn->p_description);

        for (s = 0; s < pgn->p_maxSrc; s++)
        {
          Message *m = &pgn->p_message[s];

          if (m->m_valid >= now || pgn->p_prn == PRN_PROD_INFO)
          {
            sbAppendFormat(&state, "  ,\"%u%s%s\":%s", m->m_src, m->m_key2 ? "_" : "", m->m_key2 ? m->m_key2 : "", m->m_text);
          }
        }
        sbAppendString(&state, "  }\n");

        separator = ',';
      }
    }
  }
  if (separator == ',')
  {
    sbAppendString(&state, "}\n");
  }
  else
  {
    sbAppendString(&state, "\n");
  }

  logDebug("state %d bytes\n", sbGetLength(&state));
  return state.data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
leaq 0x20aaa95(%rip), %rax   # 0x20aec28
movq 0x10(%rax), %rcx
movq %rcx, 0x20(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0x10(%rsp)
movl %edi, 0x4(%rsp)
cmpl $0x6, %edi
jne 0x428c
movb $0x7b, %al
xorl %edx, %edx
cmpq %rdx, 0x20a51a4(%rip)   # 0x20a9360
jbe 0x439b
leaq 0x20a4197(%rip), %rcx   # 0x20a8360
movq %rdx, 0x8(%rsp)
movq (%rcx,%rdx,8), %rcx
movq (%rcx), %r12
movzbl %al, %edx
movl (%r12), %ecx
movq 0x8(%r12), %r8
leaq 0x10(%rsp), %rdi
leaq 0x4df9(%rip), %rsi      # 0x8fe6
xorl %eax, %eax
callq 0x71e6
leaq 0x30(%r12), %r14
xorl %r15d, %r15d
movl 0x4(%r12), %eax
cmpq %rax, %r15
jae 0x426a
movzbl -0x20(%r14), %ebp
movq -0x10(%r14), %rsi
movq (%r14), %rbx
testq %rbx, %rbx
leaq 0x4e1d(%rip), %r13      # 0x9039
leaq 0x463c(%rip), %rax      # 0x885f
cmoveq %rax, %r13
cmoveq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x6bd1
movl -0x1c(%r14), %r10d
movl -0x8(%r14), %r11d
leaq 0x10(%rsp), %rdi
leaq 0x4dbc(%rip), %rsi      # 0x9005
movl %ebp, %edx
movq %r13, %rcx
movq %rbx, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x71e6
popq %rax
popq %rcx
incq %r15
addq $0x30, %r14
jmp 0x41fc
leaq 0x10(%rsp), %rdi
leaq 0x4dc5(%rip), %rsi      # 0x903b
xorl %eax, %eax
callq 0x71e6
movq 0x8(%rsp), %rdx
incq %rdx
movb $0x2c, %al
jmp 0x41b5
movq %rsi, %rbx
movb $0x7b, %al
movq 0x20a50c8(%rip), %rdx   # 0x20a9360
leaq 0x10(%rsp), %r15
leaq 0x45bb(%rip), %r13      # 0x885f
xorl %edi, %edi
cmpq %rdi, %rdx
jbe 0x439b
leaq 0x20a40aa(%rip), %rcx   # 0x20a8360
movq (%rcx,%rdi,8), %rcx
movq (%rcx), %rbp
movq 0x8(%rbp), %r8
cmpb $0x41, (%r8)
jne 0x42e9
cmpb $0x49, 0x1(%r8)
jne 0x42e9
cmpl $0x1, 0x4(%rsp)
sete %cl
cmpb $0x53, 0x2(%r8)
setne %sil
xorb %cl, %sil
movl (%rbp), %ecx
je 0x42f3
jmp 0x4307
movl (%rbp), %ecx
cmpl $0x1, 0x4(%rsp)
jne 0x4307
cmpl $0x1f805, %ecx          # imm = 0x1F805
je 0x4307
cmpl $0x1f802, %ecx          # imm = 0x1F802
jne 0x4393
movq %rdi, 0x8(%rsp)
movzbl %al, %edx
movq %r15, %rdi
leaq 0x4ccd(%rip), %rsi      # 0x8fe6
xorl %eax, %eax
callq 0x71e6
leaq 0x38(%rbp), %r14
xorl %r12d, %r12d
movl 0x4(%rbp), %eax
cmpq %rax, %r12
jae 0x4376
cmpq %rbx, -0x20(%r14)
jae 0x433e
cmpl $0x1f014, (%rbp)        # imm = 0x1F014
jne 0x436d
movzbl -0x28(%r14), %edx
movq -0x8(%r14), %r8
movq (%r14), %r9
testq %r8, %r8
leaq 0x4ce5(%rip), %rcx      # 0x9039
cmoveq %r13, %rcx
cmoveq %r13, %r8
movq %r15, %rdi
leaq 0x4cda(%rip), %rsi      # 0x9040
xorl %eax, %eax
callq 0x71e6
incq %r12
addq $0x30, %r14
jmp 0x4327
movq %r15, %rdi
leaq 0x4cbb(%rip), %rsi      # 0x903b
callq 0x7327
movb $0x2c, %al
movq 0x20a4fd2(%rip), %rdx   # 0x20a9360
movq 0x8(%rsp), %rdi
incq %rdi
jmp 0x42a6
cmpb $0x2c, %al
leaq 0x4c99(%rip), %rax      # 0x903d
leaq 0x44b3(%rip), %rsi      # 0x885e
cmoveq %rax, %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x7327
movq 0x8(%rbx), %rsi
leaq 0x4c88(%rip), %rdi      # 0x904f
xorl %eax, %eax
callq 0x6d78
movq (%rbx), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq