// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl -0xc(%rbp), %edi
callq 0xd490
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xd722
movl -0xc(%rbp), %edi
callq 0xd5f0
movq %rax, -0x28(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x64(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xd6fa
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x68(%rbp)
jmp 0xd701
xorl %eax, %eax
movl %eax, -0x68(%rbp)
jmp 0xd701
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
leaq 0x16965(%rip), %rdi     # 0x24073
movb $0x0, %al
callq 0x1f670
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xd928
movq -0x28(%rbp), %rax
testb $0x1, 0x15d1(%rax)
jne 0xd754
movl -0xc(%rbp), %esi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x16962(%rip), %rdi     # 0x240a2
movb $0x0, %al
callq 0x1f670
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xd928
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x2c(%rbp), %eax
jne 0xd91c
movl $0x0, -0x34(%rbp)
movb $0x1, -0x35(%rbp)
movb $0x0, -0x36(%rbp)
movl -0x2c(%rbp), %esi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x16950(%rip), %rdi     # 0x240de
movb $0x0, %al
callq 0x1f670
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x30(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x15c0(%rcx), %eax
jae 0xd8b7
movq -0x28(%rbp), %rax
addq $0x18, %rax
movslq -0x30(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xd8a0
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
jne 0xd8a0
movb $0x1, -0x36(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x1, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0xd1c0
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
movslq -0x34(%rbp), %rcx
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %r8d
leaq -0x50(%rbp), %r9
leaq -0x60(%rbp), %rax
movq %rax, (%rsp)
callq 0x1b150
testb $0x1, %al
jne 0xd850
jmp 0xd85a
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0xd880
movl -0x2c(%rbp), %esi
movq -0x40(%rbp), %rax
movq (%rax), %rdx
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %r8
leaq 0x168ac(%rip), %rdi     # 0x2411f
movb $0x0, %al
callq 0x1f670
movb $0x0, -0x35(%rbp)
jmp 0xd8b7
movl -0x2c(%rbp), %esi
movq -0x40(%rbp), %rax
movq (%rax), %rdx
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %r8
leaq 0x168c6(%rip), %rdi     # 0x2415f
movb $0x0, %al
callq 0x1f670
movl -0x44(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xd7a3
testb $0x1, -0x36(%rbp)
jne 0xd8df
movl -0x2c(%rbp), %esi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x168ca(%rip), %rdi     # 0x24198
movb $0x0, %al
callq 0x1f670
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xd928
testb $0x1, -0x35(%rbp)
je 0xd907
movl -0x2c(%rbp), %esi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x168eb(%rip), %rdi     # 0x241e1
movb $0x0, %al
callq 0x1f670
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xd928
jmp 0xd909
movq -0x28(%rbp), %rax
addq $0x15f8, %rax           # imm = 0x15F8
movq %rax, -0x28(%rbp)
jmp 0xd75e
movl -0xc(%rbp), %edi
callq 0xd5f0
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)