// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x50(%rbp)
cmpl $0x1, -0x44(%rbp)
jne 0x7e541
movq -0x18(%rbp), %rax
movswl 0x40(%rax), %eax
cmpl $0x0, %eax
jl 0x7e53f
cmpq $0x0, -0x50(%rbp)
jne 0x7e508
movl $0x0, -0x4(%rbp)
jmp 0x7e7b7
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movswq 0x40(%rcx), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0xc970
cmpl $0x0, %eax
jne 0x7e53d
movl $0x0, -0x4(%rbp)
jmp 0x7e7b7
jmp 0x7e53f
jmp 0x7e582
cmpq $0x0, -0x30(%rbp)
je 0x7e580
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movslq -0x44(%rbp), %rax
shlq $0x2, %rax
movl %eax, %esi
callq 0x134e0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x7e575
movl $0x1, -0x4(%rbp)
jmp 0x7e7b7
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x7e582
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x7e754
movq -0x38(%rbp), %rax
movzwl 0x54(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x7e741
movq -0x38(%rbp), %rax
movzbl 0x56(%rax), %eax
cmpl $0x0, %eax
je 0x7e741
cmpq $0x0, -0x50(%rbp)
jne 0x7e620
movq -0x38(%rbp), %rax
movb 0x57(%rax), %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0x7e61b
cmpq $0x0, -0x40(%rbp)
je 0x7e616
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x7e614
movq -0x20(%rbp), %rax
addq $0x40, %rax
movslq -0x54(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %edx
movq -0x40(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x7e5e1
jmp 0x7e616
jmp 0x7e754
jmp 0x7e73f
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x7e733
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x60(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x7e672
leaq 0x319cc(%rip), %rax     # 0xb003a
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x68(%rbp), %rsi
callq 0xc970
cmpl $0x0, %eax
je 0x7e695
jmp 0x7e733
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x60(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x7e719
movq -0x20(%rbp), %rax
addq $0x40, %rax
movslq -0x5c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x70(%rbp), %rsi
callq 0xc970
cmpl $0x0, %eax
jne 0x7e70c
cmpq $0x0, -0x40(%rbp)
je 0x7e70a
movq -0x20(%rbp), %rax
addq $0x40, %rax
movslq -0x5c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %edx
movq -0x40(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x7e719
jmp 0x7e70e
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x7e6b6
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jne 0x7e723
jmp 0x7e733
jmp 0x7e725
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x7e627
movl -0x58(%rbp), %eax
cmpl -0x44(%rbp), %eax
jne 0x7e73d
jmp 0x7e754
jmp 0x7e73f
jmp 0x7e741
jmp 0x7e743
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x7e58e
cmpq $0x0, -0x38(%rbp)
jne 0x7e7a5
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1f6(%rax)
jne 0x7e78c
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
leaq 0x32101(%rip), %rsi     # 0xb0886
movb $0x0, %al
callq 0x136f0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x9cd0
movl $0x1, -0x4(%rbp)
jmp 0x7e7b7
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq