// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl 0x28(%rdx), %r15d
movq %rdx, -0x50(%rbp)
movq 0x48(%rdx), %r12
cmpq $0x1, %r15
jne 0x5de3e
movswq 0x40(%rsi), %rdx
xorl %eax, %eax
testq %rdx, %rdx
js 0x5de76
testq %r12, %r12
je 0x5e074
movq %rdi, -0x40(%rbp)
movq %rcx, -0x38(%rbp)
movq %rsi, %r11
movq 0x8(%rsi), %rcx
leaq (%rdx,%rdx,2), %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movb (%rdx), %dil
xorl %eax, %eax
testb %dil, %dil
je 0x5de88
incq %rdx
leaq 0x18299(%rip), %rsi     # 0x760b0
xorl %ecx, %ecx
movq %r12, %r10
movzbl %dil, %edi
movb (%rdi,%rsi), %r8b
movzbl (%r10), %r9d
cmpb (%r9,%rsi), %r8b
jne 0x5de8f
incq %r10
movb (%rdx), %dil
incq %rdx
testb %dil, %dil
jne 0x5de1c
jmp 0x5de92
movq %r8, %r14
testq %r8, %r8
movq %rcx, -0x38(%rbp)
movq %rdi, -0x40(%rbp)
je 0x5de7e
movq %rsi, %rbx
movq (%rdi), %rdi
leal (,%r15,4), %esi
callq 0x11721
testq %rax, %rax
je 0x5e06f
movq %rax, -0x30(%rbp)
movq %rax, (%r14)
movq %rbx, %rsi
jmp 0x5deb4
movq %rdi, -0x40(%rbp)
movq %rcx, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
jmp 0x5deb4
movq %r12, %r10
xorl %ecx, %ecx
jmp 0x5de92
movq %rdi, %rcx
leaq 0x18217(%rip), %rdx     # 0x760b0
movb (%rcx,%rdx), %cl
movzbl (%r10), %esi
cmpb (%rsi,%rdx), %cl
je 0x5e074
movq $0x0, -0x30(%rbp)
movq %r11, %rsi
movq %rsi, -0x58(%rbp)
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x5e037
movq -0x50(%rbp), %rcx
addq $0x40, %rcx
leaq 0x181dc(%rip), %rsi     # 0x760b0
movq %r12, -0x48(%rbp)
movzwl 0x54(%rax), %edx
cmpl %edx, %r15d
jne 0x5e02a
cmpb $0x0, 0x56(%rax)
je 0x5e02a
testq %r12, %r12
je 0x5e012
testl %r15d, %r15d
jle 0x5e01f
movq 0x8(%rax), %r8
movq 0x40(%rax), %r9
movq -0x58(%rbp), %rdx
movq 0x8(%rdx), %r10
xorl %edi, %edi
movq %r8, -0x68(%rbp)
movq %r9, -0x60(%rbp)
movslq (%r8,%rdi,4), %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movq 0x20(%r10,%rdx), %r11
testq %r11, %r11
leaq 0x1d608(%rip), %r8      # 0x7b53e
cmoveq %r8, %r11
movq (%r9,%rdi,8), %r14
movb (%r14), %r12b
testb %r12b, %r12b
je 0x5df6a
incq %r14
movzbl %r12b, %ebx
movb (%rbx,%rsi), %r12b
movzbl (%r11), %r13d
cmpb (%r13,%rsi), %r12b
jne 0x5df6c
incq %r11
movb (%r14), %r12b
incq %r14
testb %r12b, %r12b
jne 0x5df49
xorl %ebx, %ebx
movb (%rbx,%rsi), %bl
movzbl (%r11), %r11d
cmpb (%r11,%rsi), %bl
jne 0x5e00c
movq (%r10,%rdx), %r12
xorl %r11d, %r11d
movq %r11, %rdx
shlq $0x4, %rdx
movq 0x8(%rcx,%rdx), %r13
movb (%r13), %bl
testb %bl, %bl
je 0x5dfbf
incq %r13
movq %r12, %r14
movzbl %bl, %ebx
movb (%rbx,%rsi), %r8b
movzbl (%r14), %r9d
cmpb (%r9,%rsi), %r8b
jne 0x5dfc4
incq %r14
movb (%r13), %bl
incq %r13
testb %bl, %bl
jne 0x5df9e
jmp 0x5dfc2
movq %r12, %r14
xorl %ebx, %ebx
movb (%rbx,%rsi), %r8b
movzbl (%r14), %r9d
cmpb (%r9,%rsi), %r8b
je 0x5dfdc
incq %r11
cmpq %r15, %r11
jne 0x5df84
jmp 0x5e00c
movq -0x30(%rbp), %r8
testq %r8, %r8
je 0x5dfee
addq %rcx, %rdx
movl (%rdx), %edx
movl %edx, (%r8,%rdi,4)
cmpl %r11d, %r15d
je 0x5e00c
incq %rdi
cmpq %r15, %rdi
movq -0x68(%rbp), %r8
movq -0x60(%rbp), %r9
jne 0x5df1b
jmp 0x5e0b0
movq -0x48(%rbp), %r12
jmp 0x5e021
movb 0x57(%rax), %dl
andb $0x3, %dl
cmpb $0x2, %dl
jne 0x5e02a
jmp 0x5e083
xorl %edi, %edi
cmpl %r15d, %edi
je 0x5e0b0
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x5ded8
movq -0x40(%rbp), %rbx
cmpb $0x0, 0x1f6(%rbx)
jne 0x5e063
movq -0x50(%rbp), %rcx
movq (%rcx), %rax
movq 0x10(%rcx), %rcx
movq (%rax), %rdx
leaq 0x1dd31(%rip), %rsi     # 0x7bd8a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x11842
movq (%rbx), %rdi
movq -0x30(%rbp), %rsi
callq 0x9ca4
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r15d, %r15d
setle %dl
cmpq $0x0, -0x30(%rbp)
sete %sil
orb %dl, %sil
jne 0x5e0b0
shlq $0x2, %r15
xorl %edx, %edx
movq -0x30(%rbp), %rdi
movl (%rcx,%rdx,4), %esi
movl %esi, (%rdi,%rdx)
addq $0x4, %rdx
cmpq %rdx, %r15
jne 0x5e0a1
movq -0x38(%rbp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
jmp 0x5e074