// File: /workspace/github_c_makefile_star2_O3/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, %r12
movl %r8d, 0x14(%rsp)
movl %edx, %ebx
movq %rsi, 0x40(%rsp)
movq %rdi, %r13
movl 0x15c0(%rdi), %esi
movzbl 0x15ed(%rdi), %edx
leaq 0xf854(%rip), %rdi      # 0x1e878
xorl %eax, %eax
callq 0x1990d
movq $0xff, 0x792ba(%rip) # 0x882f0
movq $0x0, (%r12)
movb $0x1, %al
testl %ebx, %ebx
je 0xf2f5
movslq %ebx, %rax
movq %rax, 0x20(%rsp)
leaq 0x18(%r13), %rax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
movq $0x0, 0x38(%rsp)
xorl %r14d, %r14d
movq %r13, %rbx
movq %r13, 0x28(%rsp)
movq %r12, 0x18(%rsp)
imulq $0xa8, %r14, %r13
addq 0x30(%rsp), %r13
movq (%r12), %r9
testq %r9, %r9
movl $0x0, %eax
cmovel %eax, %ebp
movb 0x15eb(%rbx), %al
testb %al, %al
je 0xf121
movb 0x4b(%r13), %cl
cmpb 0x15ed(%rbx), %cl
jne 0xf121
testl %ebp, %ebp
jne 0xf121
cmpb $0x0, 0x14(%rsp)
je 0xf101
callq 0x16dbb
leaq 0xf7d7(%rip), %rdi      # 0x1e89a
movq %rax, %rsi
xorl %eax, %eax
callq 0x16c1c
leaq 0x7920c(%rip), %rbp     # 0x882e0
movq %rbp, %rdi
callq 0xd0f0
movw $0x7d5d, (%rax,%rbp)    # imm = 0x7D5D
movb $0x0, 0x2(%rax,%rbp)
leaq 0xf57b(%rip), %rax      # 0x1e669
movq %rax, 0x730cb(%rip)     # 0x821c0
movb 0x15eb(%rbx), %al
movb 0x15ed(%rbx), %cl
movslq 0x791e8(%rip), %rdx     # 0x882f0
movzbl %al, %r9d
imulq %rdx, %r9
movq %r9, (%r12)
movl $0x1, %ebp
movb %cl, 0x13(%rsp)
movb %al, 0x12(%rsp)
movb 0x15ec(%rbx), %al
testb %al, %al
je 0xf18a
movb 0x4b(%r13), %cl
cmpb 0x15ee(%rbx), %cl
jne 0xf18a
testl %ebp, %ebp
jne 0xf18a
cmpb $0x0, 0x14(%rsp)
je 0xf16a
leaq 0xf760(%rip), %rdi      # 0x1e8a9
xorl %eax, %eax
callq 0x16c1c
leaq 0xf512(%rip), %rax      # 0x1e669
movq %rax, 0x73062(%rip)     # 0x821c0
movb 0x15ec(%rbx), %al
movb 0x15ee(%rbx), %cl
movslq 0x79183(%rip), %rdx     # 0x882f4
movzbl %al, %r9d
imulq %rdx, %r9
movq %r9, (%r12)
movl $0x1, %ebp
movb %cl, 0x13(%rsp)
movb %al, 0x12(%rsp)
testq %r9, %r9
je 0xf20d
leaq 0x1(%r14), %rax
movzbl 0x13(%rsp), %ebx
movzbl 0x12(%rsp), %r12d
leal (%r12,%rbx), %ecx
cmpq %rcx, %rax
jne 0xf1e6
leal -0x1(%rbx), %eax
movslq %eax, %r14
imulq $0xa8, %r14, %r13
addq 0x30(%rsp), %r13
incl %ebp
cmpb $0x0, 0x14(%rsp)
je 0xf1e6
leaq 0xf6ee(%rip), %rdi      # 0x1e8b7
xorl %eax, %eax
callq 0x16c1c
leaq 0xf492(%rip), %rax      # 0x1e669
movq %rax, 0x72fe2(%rip)     # 0x821c0
movq 0x18(%rsp), %rax
movq (%rax), %r9
leaq 0x1(%r14), %rdx
leaq 0xf6ca(%rip), %rdi      # 0x1e8bb
movl %ebp, %esi
movl %ebx, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1990d
movq 0x18(%rsp), %r12
decq (%r12)
movq 0x28(%rsp), %rbx
movq 0x58(%r13), %rsi
testq %rsi, %rsi
je 0xf225
cmpb $0x0, 0x790af(%rip)     # 0x882cc
jne 0xf232
movq (%r13), %rsi
jmp 0xf232
movq (%r13), %rsi
testq %rsi, %rsi
je 0xf2da
leaq 0x50(%rsp), %r15
movq %r15, %rdi
callq 0xd0b0
testl %ebp, %ebp
setle %al
orb 0x14(%rsp), %al
jne 0xf28e
cmpq $0x0, 0x58(%r13)
movl $0x5f, %ebx
movl $0x20, %eax
cmovel %eax, %ebx
movq %r15, %rdi
callq 0xd0f0
movw %bx, 0x50(%rsp,%rax)
movq 0x28(%rsp), %rbx
movq %r15, %rdi
callq 0xd0f0
leaq (%rsp,%rax), %rdi
addq $0x50, %rdi
leaq 0x1f416(%rip), %rsi     # 0x2e69b
movl %ebp, %edx
xorl %eax, %eax
callq 0xd300
movl $0x1, (%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq 0x40(%rsp), %rdx
movq 0x20(%rsp), %r13
movq %r13, %rcx
movq 0x38(%rsp), %r15
movq %r15, %r8
leaq 0x48(%rsp), %r9
callq 0xf307
testb %al, %al
je 0xf2f5
addq 0x48(%rsp), %r15
incq %r14
movq %r15, 0x38(%rsp)
shrq $0x3, %r15
cmpq %r13, %r15
jb 0xf077
jmp 0xf2f5
movl 0x8(%rbx), %esi
movq 0x20(%rsp), %rdx
subq %r15, %rdx
leaq 0xf631(%rip), %rdi      # 0x1e91d
xorl %eax, %eax
callq 0x1990d
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq