// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdi, %r15
movslq 0x28(%rdx), %r13
movq %rdx, 0x30(%rsp)
movq 0x48(%rdx), %r12
pushq $0x1
popq %rbp
cmpq $0x1, %r13
movq %rsi, (%rsp)
jne 0x28087d
movswq 0x44(%rsi), %rax
xorl %r8d, %r8d
testq %rax, %rax
js 0x2808b9
testq %r12, %r12
je 0x280a7f
movq 0x8(%rsi), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %rdi
movq %r12, %rsi
callq 0x221779
xorl %r8d, %r8d
testl %eax, %eax
je 0x280a7f
movq %r12, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
jmp 0x2808b3
movq %r8, %r14
testq %r8, %r8
je 0x2808ca
movq (%r15), %rdi
leaq (,%r13,4), %rsi
callq 0x22d205
testq %rax, %rax
je 0x280a91
movq %rax, %r8
movq %r12, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %rax, (%r14)
movq (%rsp), %rsi
jmp 0x2808dc
movq %r12, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
jmp 0x2808dc
movq %r12, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
xorl %r8d, %r8d
movl %r13d, %r12d
leaq 0x10(%rsi), %rbp
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
movq 0x30(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x40(%rsp)
movq %r8, 0x20(%rsp)
movq %r13, 0x38(%rsp)
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x280a3b
movzwl 0x5e(%rbp), %eax
cmpl %eax, %r12d
jne 0x280a1b
cmpb $0x0, 0x62(%rbp)
je 0x280a1b
cmpq $0x0, 0x48(%rbp)
jne 0x280a1b
cmpq $0x0, 0x18(%rsp)
je 0x280a0e
xorl %r15d, %r15d
cmpq %r13, %r15
je 0x280a24
movq 0x8(%rbp), %rax
movswq (%rax,%r15,2), %rbx
testq %rbx, %rbx
js 0x280a04
movq 0x8(%rsi), %r14
shlq $0x5, %rbx
movq 0x10(%r14,%rbx), %rsi
testq %rsi, %rsi
leaq 0xf15fc(%rip), %rax     # 0x371f70
cmoveq %rax, %rsi
movq 0x40(%rbp), %rax
movq (%rax,%r15,8), %rdi
callq 0x221779
testl %eax, %eax
jne 0x280a29
movq %r15, 0x28(%rsp)
movq (%r14,%rbx), %r14
movq 0x40(%rsp), %r15
movq %r12, %r13
movq %r12, %rbx
subq $0x1, %rbx
jb 0x2809f1
movq (%r15), %rdi
movq %r14, %rsi
callq 0x221779
testl %eax, %eax
je 0x2809bf
addq $0x10, %r15
movq %rbx, %r13
jmp 0x2809a1
movq 0x20(%rsp), %r8
testq %r8, %r8
je 0x2809d6
movl -0x8(%r15), %eax
movq 0x28(%rsp), %rcx
movl %eax, (%r8,%rcx,4)
testl %r13d, %r13d
movq (%rsp), %rsi
je 0x280a34
movq 0x28(%rsp), %r15
incq %r15
movq 0x38(%rsp), %r13
jmp 0x280942
movq 0x20(%rsp), %r8
movq 0x38(%rsp), %r13
movq (%rsp), %rsi
movq 0x28(%rsp), %r15
cmpl %r12d, %r15d
jne 0x280a1b
jmp 0x280abc
movzwl 0x63(%rbp), %eax
andl $0x3, %eax
cmpw $0x2, %ax
je 0x280a96
addq $0x28, %rbp
jmp 0x280904
movl %r13d, %r15d
jmp 0x280a04
movq 0x20(%rsp), %r8
movq (%rsp), %rsi
jmp 0x280a04
movq 0x38(%rsp), %r13
jmp 0x2809ff
movq 0x10(%rsp), %rbx
cmpb $0x0, 0xd2(%rbx)
jne 0x280a6e
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x10(%rcx), %rcx
movq (%rax), %rdx
leaq 0xf8703(%rip), %rsi     # 0x379162
movq %rbx, %rdi
xorl %eax, %eax
callq 0x226dce
movq 0x20(%rsp), %r8
movq (%rbx), %rdi
movq %r8, %rsi
callq 0x21f26b
pushq $0x1
popq %rax
movl %eax, %r8d
movl %r8d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %r8d
jmp 0x280a7f
testq %r8, %r8
je 0x280abc
movl %r13d, %eax
shlq $0x2, %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x280abc
movq 0x30(%rsp), %rdx
movl 0x40(%rdx,%rcx,4), %edx
movl %edx, (%r8,%rcx)
addq $0x4, %rcx
jmp 0x280aa4
movq 0x8(%rsp), %rax
movq %rbp, (%rax)
xorl %r8d, %r8d
jmp 0x280a7f