// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movl %edx, %ebp
movq %rsi, 0x40(%rsp)
movl %edi, %ebx
callq 0xd48c
testq %rax, %rax
je 0xd79a
movq %rax, %r13
cmpb $0x0, 0x15d1(%rax)
je 0xd7b9
movl 0x8(%r13), %r14d
movslq %ebp, %rax
movq %rax, 0x38(%rsp)
movq %r13, %rbp
addq $0x18, %rbp
movl %ebx, 0x1c(%rsp)
movl %r14d, 0x14(%rsp)
movq (%r13), %rdx
leaq 0x1aad0(%rip), %rdi     # 0x280de
movl %r14d, %esi
xorl %eax, %eax
callq 0x254dd
cmpl $0x0, 0x15c0(%r13)
je 0xd737
xorl %r15d, %r15d
movb $0x1, %al
movl %eax, 0x18(%rsp)
movq %rbp, 0x30(%rsp)
xorl %eax, %eax
xorl %r14d, %r14d
movq %r13, 0x48(%rsp)
movl 0x10(%rbp), %r12d
movq 0x18(%rbp), %rcx
testq %rcx, %rcx
je 0xd6fd
cmpb $0x3d, (%rcx)
jne 0xd6fd
movq 0x18(%rbp), %rdi
incq %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0xd1d0
movq %rax, %rbx
movslq %r14d, %rcx
movl 0x10(%rbp), %r8d
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rdx
leaq 0x20(%rsp), %r9
callq 0x22937
testb %al, %al
je 0xd6c3
movq 0x20(%rsp), %rcx
cmpq %rbx, %rcx
jne 0xd6c3
movq (%rbp), %rdx
movq %rbx, %r8
xorl %ebx, %ebx
leaq 0x1aaac(%rip), %rdi     # 0x2815f
movl 0x14(%rsp), %esi
xorl %eax, %eax
callq 0x254dd
movb $0x1, %r13b
jmp 0xd6f1
movq (%rbp), %rdx
movq 0x20(%rsp), %rcx
xorl %r13d, %r13d
leaq 0x1aa49(%rip), %rdi     # 0x2811f
movl 0x14(%rsp), %esi
movq %rbx, %r8
xorl %eax, %eax
callq 0x254dd
movl $0x5, %ebx
movl $0x0, 0x18(%rsp)
movb $0x1, %al
testb %r13b, %r13b
movq 0x48(%rsp), %r13
je 0xd702
addl %r12d, %r14d
xorl %ebx, %ebx
testl %ebx, %ebx
jne 0xd720
incq %r15
movl 0x15c0(%r13), %ecx
addq $0xa8, %rbp
cmpq %rcx, %r15
jb 0xd63e
xorb $0x1, %al
movl 0x1c(%rsp), %ebx
movl 0x14(%rsp), %r14d
movq 0x30(%rsp), %rbp
movl 0x18(%rsp), %r15d
jmp 0xd73c
movb $0x1, %r15b
movb $0x1, %al
orb %al, %r15b
testb $0x1, %r15b
je 0xd76c
testb $0x1, %al
leaq 0x1aa93(%rip), %rdi     # 0x281e1
leaq 0x1aa43(%rip), %rax     # 0x28198
cmovneq %rax, %rdi
movq (%r13), %rdx
movl %r14d, %esi
xorl %eax, %eax
callq 0x254dd
movq %r13, 0x28(%rsp)
testb $0x1, %r15b
jne 0xd7cf
movl $0x15f8, %ecx           # imm = 0x15F8
leaq (%rcx,%r13), %rax
addq %rcx, %rbp
cmpl %r14d, 0x1600(%r13)
movq %rax, %r13
je 0xd603
movl %ebx, %edi
callq 0xd52d
movq %rax, %r13
jmp 0xd7d4
movl %ebx, %edi
callq 0xd52d
movq %rax, %r13
movl 0x8(%rax), %edx
leaq 0x1a8c5(%rip), %rdi     # 0x28073
movl %ebx, %esi
xorl %eax, %eax
callq 0x254dd
jmp 0xd7d4
movq (%r13), %rdx
leaq 0x1a8de(%rip), %rdi     # 0x280a2
movl %ebx, %esi
xorl %eax, %eax
callq 0x254dd
jmp 0xd7d4
movq 0x28(%rsp), %r13
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq