// File: /workspace/github_c_makefile_star2_O2/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 0xd6ed
movq %rax, %rbx
cmpb $0x0, 0x15d1(%rax)
je 0xd70e
movl 0x8(%rbx), %r15d
movslq %ebp, %rax
movq %rax, 0x18(%rsp)
movq %rbx, %r13
addq $0x18, %r13
leaq 0x11af7(%rip), %rdi     # 0x1f0de
movl %r15d, %eax
movl %r14d, 0xc(%rsp)
movl %r15d, 0x8(%rsp)
cmpl %r15d, %eax
jne 0xd751
movq (%rbx), %rdx
xorl %r12d, %r12d
movl %r15d, %esi
xorl %eax, %eax
callq 0x1bd48
movq %r13, 0x10(%rsp)
xorl %r15d, %r15d
xorl %eax, %eax
movl 0x15c0(%rbx), %ecx
cmpq %rcx, %r12
jae 0xd71d
movl 0x10(%r13), %r14d
movq 0x18(%r13), %rdi
testq %rdi, %rdi
je 0xd69b
cmpb $0x3d, (%rdi)
jne 0xd69b
incq %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
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 0x193d9
movq 0x28(%rsp), %rcx
movq (%r13), %rdx
testb %al, %al
je 0xd6ad
cmpq %rbp, %rcx
jne 0xd6ad
leaq 0x11ad7(%rip), %rdi     # 0x1f15f
movl 0x8(%rsp), %esi
movq %rbp, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x1bd48
movb $0x1, %al
addl %r14d, %r15d
incq %r12
addq $0xa8, %r13
jmp 0xd617
leaq 0x11a6b(%rip), %rdi     # 0x1f11f
movl 0x8(%rsp), %r15d
movl %r15d, %esi
movq %rbp, %r8
xorl %eax, %eax
callq 0x1bd48
movl 0x1600(%rbx), %eax
movl $0x15f8, %ecx           # imm = 0x15F8
addq %rcx, %rbx
movq 0x10(%rsp), %r13
addq %rcx, %r13
movl 0xc(%rsp), %r14d
leaq 0x119f6(%rip), %rdi     # 0x1f0de
jmp 0xd5f4
movl %r14d, %edi
callq 0xd526
movq %rax, %rbx
movl 0x8(%rax), %edx
leaq 0x11971(%rip), %rdi     # 0x1f073
movl %r14d, %esi
xorl %eax, %eax
callq 0x1bd48
jmp 0xd73f
movq (%rbx), %rdx
leaq 0x1198a(%rip), %rdi     # 0x1f0a2
movl %r14d, %esi
jmp 0xd738
movq (%rbx), %rdx
testb $0x1, %al
je 0xd72d
leaq 0x11ab6(%rip), %rdi     # 0x1f1e1
jmp 0xd734
leaq 0x11a64(%rip), %rdi     # 0x1f198
movl 0x8(%rsp), %esi
xorl %eax, %eax
callq 0x1bd48
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r14d, %edi
callq 0xd526
movq %rax, %rbx
jmp 0xd73f