// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3FkLocateIndex
SQLITE_PRIVATE int sqlite3FkLocateIndex(
  Parse *pParse,                  /* Parse context to store any error in */
  Table *pParent,                 /* Parent table of FK constraint pFKey */
  FKey *pFKey,                    /* Foreign key to find index for */
  Index **ppIdx,                  /* OUT: Unique index on parent table */
  int **paiCol                    /* OUT: Map of index columns in pFKey */
){
  Index *pIdx = 0;                    /* Value to return via *ppIdx */
  int *aiCol = 0;                     /* Value to return via *paiCol */
  int nCol = pFKey->nCol;             /* Number of columns in parent key */
  char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */

  /* The caller is responsible for zeroing output parameters. */
  assert( ppIdx && *ppIdx==0 );
  assert( !paiCol || *paiCol==0 );
  assert( pParse );

  /* If this is a non-composite (single column) foreign key, check if it
  ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
  ** and *paiCol set to zero and return early.
  **
  ** Otherwise, for a composite foreign key (more than one column), allocate
  ** space for the aiCol array (returned via output parameter *paiCol).
  ** Non-composite foreign keys do not require the aiCol array.
  */
  if( nCol==1 ){
    /* The FK maps to the IPK if any of the following are true:
    **
    **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
    **      mapped to the primary key of table pParent, or
    **   2) The FK is explicitly mapped to a column declared as INTEGER
    **      PRIMARY KEY.
    */
    if( pParent->iPKey>=0 ){
      if( !zKey ) return 0;
      if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
    }
  }else if( paiCol ){
    assert( nCol>1 );
    aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
    if( !aiCol ) return 1;
    *paiCol = aiCol;
  }

  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
    if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
      /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
      ** of columns. If each indexed column corresponds to a foreign key
      ** column of pFKey, then this index is a winner.  */

      if( zKey==0 ){
        /* If zKey is NULL, then this foreign key is implicitly mapped to
        ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
        ** identified by the test.  */
        if( IsPrimaryKeyIndex(pIdx) ){
          if( aiCol ){
            int i;
            for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
          }
          break;
        }
      }else{
        /* If zKey is non-NULL, then this foreign key was declared to
        ** map to an explicit list of columns in table pParent. Check if this
        ** index matches those columns. Also, check that the index uses
        ** the default collation sequences for each column. */
        int i, j;
        for(i=0; i<nCol; i++){
          i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
          const char *zDfltColl;            /* Def. collation for column */
          char *zIdxCol;                    /* Name of indexed column */

          if( iCol<0 ) break; /* No foreign keys against expression indexes */

          /* If the index uses a collation sequence that is different from
          ** the default collation sequence for the column, this index is
          ** unusable. Bail out early in this case.  */
          zDfltColl = pParent->aCol[iCol].zColl;
          if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
          if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;

          zIdxCol = pParent->aCol[iCol].zName;
          for(j=0; j<nCol; j++){
            if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
              if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
              break;
            }
          }
          if( j==nCol ) break;
        }
        if( i==nCol ) break;      /* pIdx is usable */
      }
    }
  }

  if( !pIdx ){
    if( !pParse->disableTriggers ){
      sqlite3ErrorMsg(pParse,
           "foreign key mismatch - \"%w\" referencing \"%w\"",
           pFKey->pFrom->zName, pFKey->zTo);
    }
    sqlite3DbFree(pParse->db, aiCol);
    return 1;
  }

  *ppIdx = pIdx;
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x50(%rbp)
cmpl $0x1, -0x44(%rbp)
jne 0x45c7e1
movq -0x18(%rbp), %rax
movswl 0x44(%rax), %eax
cmpl $0x0, %eax
jl 0x45c7df
cmpq $0x0, -0x50(%rbp)
jne 0x45c7a8
movl $0x0, -0x4(%rbp)
jmp 0x45ca79
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movswq 0x44(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0x3c3e60
cmpl $0x0, %eax
jne 0x45c7dd
movl $0x0, -0x4(%rbp)
jmp 0x45ca79
jmp 0x45c7df
jmp 0x45c820
cmpq $0x0, -0x30(%rbp)
je 0x45c81e
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movslq -0x44(%rbp), %rsi
shlq $0x2, %rsi
callq 0x3d62a0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x45c813
movl $0x1, -0x4(%rbp)
jmp 0x45ca79
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x45c820
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x45ca16
movq -0x38(%rbp), %rax
movzwl 0x5e(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x45ca03
movq -0x38(%rbp), %rax
movzbl 0x62(%rax), %eax
cmpl $0x0, %eax
je 0x45ca03
movq -0x38(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x45ca03
cmpq $0x0, -0x50(%rbp)
jne 0x45c8d0
movq -0x38(%rbp), %rax
movw 0x63(%rax), %ax
andw $0x3, %ax
movzwl %ax, %eax
cmpl $0x2, %eax
jne 0x45c8cb
cmpq $0x0, -0x40(%rbp)
je 0x45c8c6
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x45c8c4
movq -0x20(%rbp), %rax
addq $0x40, %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %edx
movq -0x40(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x45c891
jmp 0x45c8c6
jmp 0x45ca16
jmp 0x45ca01
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x45c9f5
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x58(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x5e(%rbp)
movswl -0x5e(%rbp), %eax
cmpl $0x0, %eax
jge 0x45c905
jmp 0x45c9f5
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movswq -0x5e(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x45c933
leaq 0x14a3a6(%rip), %rax    # 0x5a6cd5
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x68(%rbp), %rsi
callq 0x3c3e60
cmpl $0x0, %eax
je 0x45c956
jmp 0x45c9f5
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movswq -0x5e(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x45c9db
movq -0x20(%rbp), %rax
addq $0x40, %rax
movslq -0x5c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x70(%rbp), %rsi
callq 0x3c3e60
cmpl $0x0, %eax
jne 0x45c9ce
cmpq $0x0, -0x40(%rbp)
je 0x45c9cc
movq -0x20(%rbp), %rax
addq $0x40, %rax
movslq -0x5c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %edx
movq -0x40(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x45c9db
jmp 0x45c9d0
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x45c978
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jne 0x45c9e5
jmp 0x45c9f5
jmp 0x45c9e7
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x45c8d7
movl -0x58(%rbp), %eax
cmpl -0x44(%rbp), %eax
jne 0x45c9ff
jmp 0x45ca16
jmp 0x45ca01
jmp 0x45ca03
jmp 0x45ca05
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x45c82c
cmpq $0x0, -0x38(%rbp)
jne 0x45ca67
movq -0x10(%rbp), %rax
cmpb $0x0, 0xd2(%rax)
jne 0x45ca4e
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
leaq 0x151795(%rip), %rsi    # 0x5ae1dc
movb $0x0, %al
callq 0x3cb660
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x3c00a0
movl $0x1, -0x4(%rbp)
jmp 0x45ca79
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)