// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, -0x58(%rbp)
movl 0x28(%rdx), %r14d
movq %rdx, -0x50(%rbp)
movq 0x48(%rdx), %r11
cmpq $0x1, %r14
jne 0x5df9c
movswq 0x40(%rsi), %rdx
xorl %eax, %eax
testq %rdx, %rdx
js 0x5dfda
testq %r11, %r11
je 0x5e1d9
movq %rcx, -0x38(%rbp)
movq 0x8(%rsi), %rcx
leaq (%rdx,%rdx,2), %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movb (%rdx), %al
testb %al, %al
je 0x5dfe8
incq %rdx
leaq 0x180e6(%rip), %r9      # 0x76060
movq %r11, %rcx
movzbl %al, %edi
movb (%rdi,%r9), %dil
movzbl (%rcx), %r8d
cmpb (%r8,%r9), %dil
jne 0x5dfeb
incq %rcx
movb (%rdx), %al
incq %rdx
testb %al, %al
jne 0x5df7d
jmp 0x5dfeb
movq %r8, %rbx
movq %rcx, -0x38(%rbp)
testq %r8, %r8
je 0x5e008
movq %r11, %r12
movq %rsi, %r15
movq -0x58(%rbp), %rax
movq (%rax), %rdi
leal (,%r14,4), %esi
callq 0x115e9
testq %rax, %rax
je 0x5e1d4
movq %rax, -0x30(%rbp)
movq %rax, (%rbx)
movq %r15, %rsi
movq %r12, %r11
jmp 0x5e010
movq $0x0, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
jmp 0x5e010
movq %r11, %rcx
movzbl %al, %eax
leaq 0x1806b(%rip), %rdx     # 0x76060
movb (%rax,%rdx), %dil
movzbl (%rcx), %ecx
xorl %eax, %eax
cmpb (%rcx,%rdx), %dil
je 0x5e1d9
movq $0x0, -0x30(%rbp)
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x5e19c
movq -0x50(%rbp), %rcx
addq $0x40, %rcx
leaq 0x18034(%rip), %r10     # 0x76060
movq %rsi, -0x48(%rbp)
movq %r11, -0x40(%rbp)
movzwl 0x54(%rax), %edx
cmpl %edx, %r14d
jne 0x5e18f
cmpb $0x0, 0x56(%rax)
je 0x5e18f
testq %r11, %r11
je 0x5e177
testl %r14d, %r14d
jle 0x5e184
movq 0x8(%rax), %rdx
movq %rdx, -0x68(%rbp)
movq 0x40(%rax), %rdx
movq %rdx, -0x60(%rbp)
movq 0x8(%rsi), %rsi
xorl %edi, %edi
movq -0x68(%rbp), %rdx
movslq (%rdx,%rdi,4), %rdx
leaq (%rdx,%rdx,2), %r11
shlq $0x4, %r11
movq 0x20(%rsi,%r11), %rbx
testq %rbx, %rbx
leaq 0x1d4ac(%rip), %rdx     # 0x7b53e
cmoveq %rdx, %rbx
movq -0x60(%rbp), %rdx
movq (%rdx,%rdi,8), %rdx
movb (%rdx), %r15b
testb %r15b, %r15b
je 0x5e0ca
incq %rdx
movzbl %r15b, %r12d
movb (%r12,%r10), %r12b
movzbl (%rbx), %r13d
cmpb (%r13,%r10), %r12b
jne 0x5e0ca
incq %rbx
movb (%rdx), %r15b
incq %rdx
testb %r15b, %r15b
jne 0x5e0a9
movzbl %r15b, %edx
movb (%rdx,%r10), %dl
movzbl (%rbx), %ebx
cmpb (%rbx,%r10), %dl
jne 0x5e16d
movq (%rsi,%r11), %rdx
xorl %r11d, %r11d
movq %r11, %r15
shlq $0x4, %r15
movq 0x8(%rcx,%r15), %r13
movb (%r13), %r12b
testb %r12b, %r12b
je 0x5e124
incq %r13
movq %rdx, %rbx
movzbl %r12b, %r8d
movb (%r8,%r10), %r8b
movzbl (%rbx), %r9d
cmpb (%r9,%r10), %r8b
jne 0x5e127
incq %rbx
movb (%r13), %r12b
incq %r13
testb %r12b, %r12b
jne 0x5e101
jmp 0x5e127
movq %rdx, %rbx
movzbl %r12b, %r8d
movb (%r8,%r10), %r8b
movzbl (%rbx), %r9d
cmpb (%r9,%r10), %r8b
je 0x5e146
incq %r11
cmpq %r14, %r11
jne 0x5e0e6
movl %r14d, %r11d
jmp 0x5e159
movq -0x30(%rbp), %r8
testq %r8, %r8
je 0x5e159
addq %rcx, %r15
movl (%r15), %edx
movl %edx, (%r8,%rdi,4)
cmpl %r14d, %r11d
je 0x5e16d
incq %rdi
cmpq %r14, %rdi
jne 0x5e073
movl %r14d, %edi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %r11
jmp 0x5e186
movb 0x57(%rax), %dl
andb $0x3, %dl
cmpb $0x2, %dl
jne 0x5e18f
jmp 0x5e1e8
xorl %edi, %edi
cmpl %r14d, %edi
je 0x5e215
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x5e034
movq -0x58(%rbp), %rbx
cmpb $0x0, 0x1f6(%rbx)
jne 0x5e1c8
movq -0x50(%rbp), %rcx
movq (%rcx), %rax
movq 0x10(%rcx), %rcx
movq (%rax), %rdx
leaq 0x1dbcc(%rip), %rsi     # 0x7bd8a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x11724
movq (%rbx), %rdi
movq -0x30(%rbp), %rsi
callq 0x9c3a
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, -0x30(%rbp)
sete %dl
testl %r14d, %r14d
setle %sil
orb %dl, %sil
jne 0x5e215
shlq $0x2, %r14
xorl %edx, %edx
movq -0x30(%rbp), %rdi
movl (%rcx,%rdx,4), %esi
movl %esi, (%rdi,%rdx)
addq $0x4, %rdx
cmpq %rdx, %r14
jne 0x5e206
movq -0x38(%rbp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
jmp 0x5e1d9