// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/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 *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
    if( !aiCol ) return 1;
    *paiCol = aiCol;
  }

  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
    if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
      /* 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 (Index.autoIndex==2).  */
        if( pIdx->autoIndex==2 ){
          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++){
          int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
          char *zDfltColl;                  /* Def. collation for column */
          char *zIdxCol;                    /* Name of indexed column */

          /* 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 = "BINARY";
          }
          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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %rbx
movq %rsi, %r13
movq %rdi, -0x60(%rbp)
movl 0x28(%rdx), %r15d
movq %rdx, -0x40(%rbp)
movq 0x48(%rdx), %rax
movq %rax, -0x70(%rbp)
pushq $0x1
popq %r14
cmpq $0x1, %r15
jne 0x5493d
movswq 0x40(%r13), %rax
xorl %r12d, %r12d
testq %rax, %rax
js 0x54937
movq -0x70(%rbp), %rsi
testq %rsi, %rsi
je 0x54b1c
movq 0x8(%r13), %rcx
imulq $0x30, %rax, %rax
movq (%rcx,%rax), %rdi
callq 0xb1bf
xorl %r12d, %r12d
testl %eax, %eax
je 0x54b1c
movq %rbx, -0x38(%rbp)
jmp 0x54976
movq %r8, %r12
testq %r8, %r8
je 0x5496f
movq -0x60(%rbp), %rax
movq (%rax), %rdi
leal (,%r15,4), %esi
callq 0x103b4
testq %rax, %rax
je 0x54ae8
movq %rbx, -0x38(%rbp)
movq %rax, (%r12)
movq %rax, %r12
jmp 0x54976
movq %rbx, -0x38(%rbp)
xorl %r12d, %r12d
leaq 0x10(%r13), %r14
xorl %eax, %eax
testl %r15d, %r15d
cmovgl %r15d, %eax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x78(%rbp)
movq %r12, -0x68(%rbp)
movq %r13, -0x48(%rbp)
movq %r15, -0x30(%rbp)
movq (%r14), %r14
testq %r14, %r14
je 0x54aa9
movzwl 0x54(%r14), %eax
cmpl %eax, %r15d
jne 0x54a8e
cmpb $0x0, 0x56(%r14)
je 0x54a8e
cmpq $0x0, -0x70(%rbp)
je 0x54a84
xorl %edx, %edx
cmpq -0x50(%rbp), %rdx
je 0x54a97
movq 0x8(%r14), %rax
movq 0x40(%r14), %rcx
movslq (%rax,%rdx,4), %rax
movq 0x8(%r13), %rbx
imulq $0x30, %rax, %r15
movq 0x20(%rbx,%r15), %rsi
testq %rsi, %rsi
leaq 0x18b60(%rip), %rax     # 0x6d55e
cmoveq %rax, %rsi
movq %rdx, -0x58(%rbp)
movq (%rcx,%rdx,8), %rdi
callq 0xb1bf
testl %eax, %eax
jne 0x54a72
movq (%rbx,%r15), %r12
movq -0x78(%rbp), %r15
movq -0x30(%rbp), %r13
movq %r13, %rbx
subq $0x1, %rbx
jb 0x54a6a
movq (%r15), %rdi
movq %r12, %rsi
callq 0xb1bf
testl %eax, %eax
je 0x54a40
addq $0x10, %r15
movq %rbx, %r13
jmp 0x54a22
movq -0x68(%rbp), %r12
testq %r12, %r12
movq -0x58(%rbp), %rdx
je 0x54a55
movl -0x8(%r15), %eax
movl %eax, (%r12,%rdx,4)
testl %r13d, %r13d
je 0x54a9f
incq %rdx
movq -0x48(%rbp), %r13
movq -0x30(%rbp), %r15
jmp 0x549d1
movq -0x68(%rbp), %r12
movq -0x48(%rbp), %r13
movq -0x30(%rbp), %r15
movq -0x58(%rbp), %rdx
cmpl %r15d, %edx
jne 0x54a8e
jmp 0x54b12
movb 0x57(%r14), %al
andb $0x3, %al
cmpb $0x2, %al
je 0x54aed
addq $0x28, %r14
jmp 0x5499f
movq -0x50(%rbp), %rax
movl %eax, %edx
jmp 0x54a7a
movq -0x48(%rbp), %r13
movq -0x30(%rbp), %r15
jmp 0x54a7a
movq -0x60(%rbp), %rbx
cmpb $0x0, 0x1f6(%rbx)
jne 0x54ad5
movq -0x40(%rbp), %rcx
movq (%rcx), %rax
movq 0x10(%rcx), %rcx
movq (%rax), %rdx
leaq 0x192df(%rip), %rsi     # 0x6ddaa
movq %rbx, %rdi
xorl %eax, %eax
callq 0x104d6
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x968b
pushq $0x1
popq %rax
movl %eax, %r12d
jmp 0x54b1c
movl %r14d, %r12d
jmp 0x54b1c
testq %r12, %r12
je 0x54b12
movl -0x50(%rbp), %eax
shlq $0x2, %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x54b12
movq -0x40(%rbp), %rdx
movl 0x40(%rdx,%rcx,4), %edx
movl %edx, (%r12,%rcx)
addq $0x4, %rcx
jmp 0x54afb
movq -0x38(%rbp), %rax
movq %r14, (%rax)
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq