// File: /workspace/github_c_makefile_star2_O2/canboat[P]canboat/analyzer/analyzer.c
// Function: printFields
extern bool printFields(const Pgn *pgn, const uint8_t *data, int length, bool showData, bool showJson, size_t *variableFields)
{
  size_t  i;
  size_t  bits;
  size_t  startBit;
  int     repetition;
  char    fieldName[60];
  uint8_t variableFieldStart;
  uint8_t variableFieldCount;

  bool r = true;

  logDebug("fieldCount=%d repeatingStart1=%" PRIu8 "\n", pgn->fieldCount, pgn->repeatingStart1);

  g_variableFieldRepeat[0] = 255; // Can be overridden by '# of parameters'
  g_variableFieldRepeat[1] = 0;   // Can be overridden by '# of parameters'
  repetition               = 0;
  *variableFields          = 0;

  for (i = 0, startBit = 0; (startBit >> 3) < length; i++)
  {
    const Field *field = &pgn->fieldList[i];

    if (*variableFields == 0)
    {
      repetition = 0;
    }

    if (pgn->repeatingCount1 > 0 && field->order == pgn->repeatingStart1 && repetition == 0)
    {
      if (showJson)
      {
        mprintf("%s\"list\":[{", getSep());
        strcat(closingBraces, "]}");
        sep = "";
      }
      // Only now is g_variableFieldRepeat set
      *variableFields    = pgn->repeatingCount1 * g_variableFieldRepeat[0];
      variableFieldCount = pgn->repeatingCount1;
      variableFieldStart = pgn->repeatingStart1;
      repetition         = 1;
    }
    if (pgn->repeatingCount2 > 0 && field->order == pgn->repeatingStart2 && repetition == 0)
    {
      if (showJson)
      {
        mprintf("}],\"list2\":[{");
        sep = "";
      }
      // Only now is g_variableFieldRepeat set
      *variableFields    = pgn->repeatingCount2 * g_variableFieldRepeat[1];
      variableFieldCount = pgn->repeatingCount2;
      variableFieldStart = pgn->repeatingStart2;
      repetition         = 1;
    }

    if (*variableFields > 0)
    {
      if (i + 1 == variableFieldStart + variableFieldCount)
      {
        i     = variableFieldStart - 1;
        field = &pgn->fieldList[i];
        repetition++;
        if (showJson)
        {
          mprintf("},{");
          sep = "";
        }
      }
      logDebug("variableFields: repetition=%d field=%" PRIu8 " variableFieldStart=%" PRIu8 " variableFieldCount=%" PRIu8
               " remaining=%zu\n",
               repetition,
               i + 1,
               variableFieldStart,
               variableFieldCount,
               *variableFields);
      (*variableFields)--;
    }

    if (!field->camelName && !field->name)
    {
      logDebug("PGN %u has unknown bytes at end: %u\n", pgn->pgn, length - (startBit >> 3));
      break;
    }

    if (showCamel)
    {
      strcpy(fieldName, field->camelName ? field->camelName : field->name);
    }
    else
    {
      strcpy(fieldName, field->name);
    }

    if (repetition >= 1 && !showJson)
    {
      strcat(fieldName, field->camelName ? "_" : " ");
      sprintf(fieldName + strlen(fieldName), "%u", repetition);
    }

    if (!printField(field, fieldName, data, length, startBit, &bits, true))
    {
      r = false;
      break;
    }

    startBit += bits;
  }

  return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %r14
movl %r8d, %r15d
movl %edx, %ebx
movq %rsi, 0x38(%rsp)
movq %rdi, %r13
movl 0x15c0(%rdi), %esi
movzbl 0x15ed(%rdi), %edx
leaq 0xf9f1(%rip), %rdi      # 0x1e878
xorl %eax, %eax
callq 0x19050
movq $0xff, 0x79457(%rip) # 0x882f0
andq $0x0, (%r14)
movslq %ebx, %rbp
leaq 0x18(%r13), %rax
movq %rax, 0x20(%rsp)
xorl %esi, %esi
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r14, 0x18(%rsp)
movq %r13, 0x28(%rsp)
movq %rbp, 0x30(%rsp)
movq %rbx, 0x40(%rsp)
movq %rbx, %rax
shrq $0x3, %rax
movq %rbp, %rbx
subq %rax, %rbx
setbe 0xd(%rsp)
jbe 0xf180
imulq $0xa8, %rsi, %rbp
addq 0x20(%rsp), %rbp
movq (%r14), %r9
testq %r9, %r9
movl $0x0, %eax
cmovel %eax, %r12d
movb 0x15eb(%r13), %al
testb %al, %al
je 0xef96
movb 0x4b(%rbp), %cl
cmpb 0x15ed(%r13), %cl
jne 0xef96
testl %r12d, %r12d
jne 0xef96
testb %r15b, %r15b
je 0xef78
movq %rsi, 0x10(%rsp)
callq 0x16649
leaq 0xf969(%rip), %rdi      # 0x1e89a
movq %rax, %rsi
xorl %eax, %eax
callq 0x164ac
leaq 0x7939e(%rip), %r12     # 0x882e0
movq %r12, %rdi
callq 0xd0f0
movq 0x10(%rsp), %rsi
movw $0x7d5d, (%rax,%r12)    # imm = 0x7D5D
movb $0x0, 0x2(%rax,%r12)
leaq 0xf706(%rip), %rax      # 0x1e669
movq %rax, 0x73256(%rip)     # 0x821c0
movb 0x15eb(%r13), %al
movb 0x15ed(%r13), %cl
movslq 0x79371(%rip), %rdx     # 0x882f0
movzbl %al, %r9d
imulq %rdx, %r9
movq %r9, (%r14)
pushq $0x1
popq %r12
movb %cl, 0xf(%rsp)
movb %al, 0xe(%rsp)
movb 0x15ec(%r13), %al
testb %al, %al
je 0xf005
movb 0x4b(%rbp), %cl
cmpb 0x15ee(%r13), %cl
jne 0xf005
testl %r12d, %r12d
jne 0xf005
testb %r15b, %r15b
je 0xefe7
leaq 0xf8eb(%rip), %rdi      # 0x1e8a9
xorl %eax, %eax
movq %rsi, %r12
callq 0x164ac
movq %r12, %rsi
leaq 0xf697(%rip), %rax      # 0x1e669
movq %rax, 0x731e7(%rip)     # 0x821c0
movb 0x15ec(%r13), %al
movb 0x15ee(%r13), %cl
movslq 0x79306(%rip), %rdx     # 0x882f4
movzbl %al, %r9d
imulq %rdx, %r9
movq %r9, (%r14)
pushq $0x1
popq %r12
movb %cl, 0xf(%rsp)
movb %al, 0xe(%rsp)
testq %r9, %r9
je 0xf09f
leaq 0x1(%rsi), %rax
movzbl 0xf(%rsp), %r14d
movzbl 0xe(%rsp), %r13d
leal (%r14,%r13), %ecx
cmpq %rcx, %rax
jne 0xf070
leal -0x1(%r14), %eax
movslq %eax, %rsi
imulq $0xa8, %rsi, %rbp
addq 0x20(%rsp), %rbp
incl %r12d
testb %r15b, %r15b
je 0xf070
leaq 0xf86e(%rip), %rdi      # 0x1e8b7
xorl %eax, %eax
movq %rsi, 0x10(%rsp)
callq 0x164ac
movq 0x10(%rsp), %rsi
leaq 0xf608(%rip), %rax      # 0x1e669
movq %rax, 0x73158(%rip)     # 0x821c0
movq 0x18(%rsp), %rax
movq (%rax), %r9
movq %rsi, 0x10(%rsp)
leaq 0x1(%rsi), %rdx
leaq 0xf83b(%rip), %rdi      # 0x1e8bb
movl %r12d, %esi
movl %r14d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x19050
movq 0x18(%rsp), %r14
decq (%r14)
movq 0x28(%rsp), %r13
jmp 0xf0a4
movq %rsi, 0x10(%rsp)
movq 0x58(%rbp), %rsi
testq %rsi, %rsi
je 0xf0bc
cmpb $0x0, 0x79218(%rip)     # 0x882cc
jne 0xf0c9
movq (%rbp), %rsi
jmp 0xf0c9
movq (%rbp), %rsi
testq %rsi, %rsi
je 0xf166
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0xd0b0
testl %r12d, %r12d
setle %al
orb %r15b, %al
jne 0xf124
cmpq $0x0, 0x58(%rbp)
pushq $0x5f
popq %rbx
pushq $0x20
popq %rax
cmovel %eax, %ebx
leaq 0x50(%rsp), %rdi
callq 0xd0f0
movw %bx, 0x50(%rsp,%rax)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0xd0f0
leaq (%rsp,%rax), %rdi
addq $0x50, %rdi
leaq 0x1f581(%rip), %rsi     # 0x2e69b
movl %r12d, %edx
xorl %eax, %eax
callq 0xd300
movl $0x1, (%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rbp
movq %rbp, %rcx
movq 0x40(%rsp), %rbx
movq %rbx, %r8
leaq 0x48(%rsp), %r9
callq 0xf196
testb %al, %al
je 0xf180
addq 0x48(%rsp), %rbx
movq 0x10(%rsp), %rsi
incq %rsi
jmp 0xeebf
movl 0x8(%r13), %esi
leaq 0xf7ac(%rip), %rdi      # 0x1e91d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x19050
movb $0x1, 0xd(%rsp)
movb 0xd(%rsp), %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq