// File: /workspace/github_c_makefile_star2_O3/canboat[P]canboat/analyzer/pgn.c
// Function: getMatchingPgn
const Pgn *getMatchingPgn(int pgnId, const uint8_t *data, int length)
{
  const Pgn *pgn = searchForPgn(pgnId);
  int        prn;
  int        i;

  if (pgn == NULL)
  {
    pgn = searchForUnknownPgn(pgnId);
    logDebug("getMatchingPgn: Unknown PGN %u -> fallback %u\n", pgnId, (pgn != NULL) ? pgn->pgn : 0);
    return pgn;
  }

  if (!pgn->hasMatchFields)
  {
    logDebug("getMatchingPgn: PGN %u has no match fields, returning '%s'\n", pgnId, pgn->description);
    return pgn;
  }

  // Here if we have a PGN but it must be matched to the list of match fields.
  // This might end up without a solution, in that case return the catch-all fallback PGN.

  for (prn = pgn->pgn; pgn->pgn == prn; pgn++)
  {
    int  startBit          = 0;
    bool matchedFixedField = true;
    bool hasFixedField     = false;

    logDebug("getMatchingPgn: PGN %u matching with manufacturer specific '%s'\n", prn, pgn->description);

    // Iterate over fields
    for (i = 0, startBit = 0; i < pgn->fieldCount; i++)
    {
      const Field *field = &pgn->fieldList[i];
      int          bits  = field->size;

      if (field->unit != NULL && field->unit[0] == '=')
      {
        int64_t value, desiredValue;
        int64_t maxValue;

        hasFixedField = true;
        desiredValue  = strtol(field->unit + 1, 0, 10);
        if (!extractNumber(field, data, length, startBit, field->size, &value, &maxValue) || value != desiredValue)
        {
          logDebug("getMatchingPgn: PGN %u field '%s' value %" PRId64 " does not match %" PRId64 "\n",
                   prn,
                   field->name,
                   value,
                   desiredValue);
          matchedFixedField = false;
          break;
        }
        logDebug(
            "getMatchingPgn: PGN %u field '%s' value %" PRId64 " matches %" PRId64 "\n", prn, field->name, value, desiredValue);
      }
      startBit += bits;
    }
    if (!hasFixedField)
    {
      logDebug("getMatchingPgn: Cant determine prn choice, return prn=%d variation '%s'\n", prn, pgn->description);
      return pgn;
    }
    if (matchedFixedField)
    {
      logDebug("getMatchingPgn: PGN %u selected manufacturer specific '%s'\n", prn, pgn->description);
      return pgn;
    }
  }

  return searchForUnknownPgn(pgnId);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebp
movq %rsi, 0x20(%rsp)
movl %edi, %r14d
callq 0xd48c
testq %rax, %rax
je 0xd70b
movq %rax, %rbx
cmpb $0x0, 0x15d1(%rax)
je 0xd72c
movl 0x8(%rbx), %r15d
movslq %ebp, %rax
movq %rax, 0x18(%rsp)
movq %rbx, %r13
addq $0x18, %r13
leaq 0x12af2(%rip), %rdi     # 0x200de
movl %r14d, 0xc(%rsp)
movl %r15d, 0x8(%rsp)
movq (%rbx), %rdx
movl %r15d, %esi
xorl %eax, %eax
callq 0x1cd55
movl 0x15c0(%rbx), %eax
testl %eax, %eax
je 0xd751
xorl %r12d, %r12d
movq %r13, 0x10(%rsp)
xorl %ecx, %ecx
xorl %r15d, %r15d
movl 0x10(%r13), %r14d
movq 0x18(%r13), %rdi
testq %rdi, %rdi
je 0xd69b
cmpb $0x3d, (%rdi)
jne 0xd69b
incq %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0xd1d0
movq %rax, %rbp
movslq %r15d, %rcx
movl 0x10(%r13), %r8d
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x28(%rsp), %r9
callq 0x1a2c7
movq 0x28(%rsp), %rcx
movq (%r13), %rdx
testb %al, %al
je 0xd6b8
cmpq %rbp, %rcx
jne 0xd6b8
leaq 0x12add(%rip), %rdi     # 0x2015f
movl 0x8(%rsp), %esi
movq %rbp, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x1cd55
movl 0x15c0(%rbx), %eax
movb $0x1, %cl
addl %r14d, %r15d
incq %r12
movl %eax, %edx
addq $0xa8, %r13
cmpq %rdx, %r12
jb 0xd61e
jmp 0xd73b
leaq 0x12a60(%rip), %rdi     # 0x2011f
movl 0x8(%rsp), %r15d
movl %r15d, %esi
movq %rbp, %r8
xorl %eax, %eax
callq 0x1cd55
movl $0x15f8, %ecx           # imm = 0x15F8
leaq (%rbx,%rcx), %rax
movq 0x10(%rsp), %r13
addq %rcx, %r13
cmpl %r15d, 0x1600(%rbx)
movq %rax, %rbx
movl 0xc(%rsp), %r14d
leaq 0x129e6(%rip), %rdi     # 0x200de
je 0xd5f6
movl %r14d, %edi
callq 0xd525
movq %rax, %rbx
jmp 0xd765
movl %r14d, %edi
callq 0xd525
movq %rax, %rbx
movl 0x8(%rax), %edx
leaq 0x12953(%rip), %rdi     # 0x20073
movl %r14d, %esi
xorl %eax, %eax
callq 0x1cd55
jmp 0xd765
movq (%rbx), %rdx
leaq 0x1296c(%rip), %rdi     # 0x200a2
movl %r14d, %esi
jmp 0xd75e
testb $0x1, %cl
movl 0x8(%rsp), %r15d
je 0xd751
movq (%rbx), %rdx
leaq 0x12a92(%rip), %rdi     # 0x201e1
jmp 0xd75b
movq (%rbx), %rdx
leaq 0x12a3d(%rip), %rdi     # 0x20198
movl %r15d, %esi
xorl %eax, %eax
callq 0x1cd55
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq