// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %r15
movslq 0x28(%rdx), %rbx
movq %rdx, 0x18(%rsp)
movq 0x48(%rdx), %r10
cmpq $0x1, %rbx
jne 0x2ebf7c
movswq 0x44(%r15), %rax
xorl %ebp, %ebp
testq %rax, %rax
js 0x2ebfc4
testq %r10, %r10
je 0x2ec1cf
movq %rcx, 0x10(%rsp)
movq 0x8(%r15), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %rax
xorl %ecx, %ecx
leaq 0xeeb08(%rip), %rdx     # 0x3daa60
movzbl (%rax,%rcx), %esi
movzbl (%r10,%rcx), %r9d
cmpb %r9b, %sil
jne 0x2ebf6d
testq %rsi, %rsi
jne 0x2ebf77
jmp 0x2ebfb8
movb (%rsi,%rdx), %r8b
cmpb (%r9,%rdx), %r8b
jne 0x2ebfb8
incq %rcx
jmp 0x2ebf58
movq %r8, %rbp
movq %rcx, 0x10(%rsp)
testq %r8, %r8
je 0x2ebfc9
movq %r10, %r12
movq %rdi, %r14
movq (%rdi), %rdi
leaq (,%rbx,4), %rsi
callq 0x28c518
testq %rax, %rax
je 0x2ec192
movq %rax, (%rsp)
movq %rax, (%rbp)
movq %r14, %rdi
movq %r12, %r10
jmp 0x2ebfd1
xorl %ebp, %ebp
cmpb %r9b, %sil
jne 0x2ebfc9
jmp 0x2ec1cf
movq %rcx, 0x10(%rsp)
movq $0x0, (%rsp)
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x2ec14e
movq 0x18(%rsp), %rcx
addq $0x40, %rcx
leaq 0xeea72(%rip), %rsi     # 0x3daa60
movq %rdi, 0x8(%rsp)
movq %r10, 0x20(%rsp)
movzwl 0x5e(%rax), %edx
cmpl %edx, %ebx
jne 0x2ec141
cmpb $0x0, 0x62(%rax)
je 0x2ec141
cmpq $0x0, 0x48(%rax)
jne 0x2ec141
testq %r10, %r10
je 0x2ec117
testl %ebx, %ebx
jle 0x2ec126
movq 0x8(%rax), %rdi
xorl %r14d, %r14d
movswq (%rdi,%r14,2), %r9
testq %r9, %r9
js 0x2ec110
movq %r15, %r8
movq 0x8(%r15), %r10
shlq $0x5, %r9
movq 0x10(%r10,%r9), %r11
testq %r11, %r11
leaq 0xef287(%rip), %rdx     # 0x3db2e0
cmoveq %rdx, %r11
movq 0x40(%rax), %rdx
movq (%rdx,%r14,8), %rbp
xorl %edx, %edx
movzbl (%rbp,%rdx), %r15d
movzbl (%r11,%rdx), %r13d
cmpb %r13b, %r15b
jne 0x2ec07e
testq %r15, %r15
jne 0x2ec089
jmp 0x2ec08e
movb (%r15,%rsi), %r12b
cmpb (%r13,%rsi), %r12b
jne 0x2ec08e
incq %rdx
jmp 0x2ec067
cmpb %r13b, %r15b
jne 0x2ec12b
movq (%r10,%r9), %r10
xorl %r9d, %r9d
movq %r9, %rdx
shlq $0x4, %rdx
leaq (%rcx,%rdx), %r11
movq 0x8(%rcx,%rdx), %rbp
xorl %edx, %edx
movzbl (%rbp,%rdx), %r15d
movzbl (%r10,%rdx), %r13d
cmpb %r13b, %r15b
jne 0x2ec0c7
testq %r15, %r15
jne 0x2ec0d2
jmp 0x2ec0d7
movb (%r15,%rsi), %r12b
cmpb (%r13,%rsi), %r12b
jne 0x2ec0d7
incq %rdx
jmp 0x2ec0b0
cmpb %r13b, %r15b
je 0x2ec0e9
incq %r9
cmpq %rbx, %r9
jne 0x2ec09e
movl %ebx, %r9d
jmp 0x2ec0f9
movq (%rsp), %r10
testq %r10, %r10
je 0x2ec0f9
movl (%r11), %edx
movl %edx, (%r10,%r14,4)
movq %r8, %r15
cmpl %ebx, %r9d
je 0x2ec110
incq %r14
cmpq %rbx, %r14
jne 0x2ec031
movl %ebx, %r14d
movq 0x8(%rsp), %rdi
jmp 0x2ec133
movzwl 0x63(%rax), %edx
andl $0x3, %edx
cmpw $0x2, %dx
jne 0x2ec141
jmp 0x2ec199
xorl %r14d, %r14d
jmp 0x2ec133
movq 0x8(%rsp), %rdi
movq %r8, %r15
cmpl %ebx, %r14d
movq 0x20(%rsp), %r10
je 0x2ec1c5
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x2ebff8
cmpb $0x0, 0xd2(%rdi)
jne 0x2ec17a
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x10(%rcx), %rcx
movq (%rax), %rdx
leaq 0xf632a(%rip), %rsi     # 0x3e2497
xorl %eax, %eax
movq %rdi, %rbx
callq 0x284eb8
movq %rbx, %rdi
movl $0x1, %ebp
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x2ec1cf
movq (%rdi), %rdi
callq 0x28c3e7
jmp 0x2ec1cf
movl $0x1, %ebp
jmp 0x2ec1cf
cmpq $0x0, (%rsp)
sete %dl
testl %ebx, %ebx
setle %sil
orb %dl, %sil
jne 0x2ec1c5
shlq $0x2, %rbx
xorl %edx, %edx
movq (%rsp), %rdi
movl (%rcx,%rdx,4), %esi
movl %esi, (%rdi,%rdx)
addq $0x4, %rdx
cmpq %rdx, %rbx
jne 0x2ec1b6
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq