// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3FindInIndex
SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
  Select *p;                            /* SELECT to the right of IN operator */
  int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
  int iTab = pParse->nTab++;            /* Cursor of the RHS table */
  int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
  Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */

  assert( pX->op==TK_IN );

  /* Check to see if an existing table or index can be used to
  ** satisfy the query.  This is preferable to generating a new 
  ** ephemeral table.
  */
  p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
  if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
    sqlite3 *db = pParse->db;              /* Database connection */
    Table *pTab;                           /* Table <table>. */
    Expr *pExpr;                           /* Expression <column> */
    int iCol;                              /* Index of column <column> */
    int iDb;                               /* Database idx for pTab */

    assert( p );                        /* Because of isCandidateForInOpt(p) */
    assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
    assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
    assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
    pTab = p->pSrc->a[0].pTab;
    pExpr = p->pEList->a[0].pExpr;
    iCol = pExpr->iColumn;
   
    /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
    sqlite3CodeVerifySchema(pParse, iDb);
    sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);

    /* This function is only called from two places. In both cases the vdbe
    ** has already been allocated. So assume sqlite3GetVdbe() is always
    ** successful here.
    */
    assert(v);
    if( iCol<0 ){
      int iAddr;

      iAddr = sqlite3CodeOnce(pParse);

      sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
      eType = IN_INDEX_ROWID;

      sqlite3VdbeJumpHere(v, iAddr);
    }else{
      Index *pIdx;                         /* Iterator variable */

      /* The collation sequence used by the comparison. If an index is to
      ** be used in place of a temp-table, it must be ordered according
      ** to this collation sequence.  */
      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);

      /* Check that the affinity that will be used to perform the 
      ** comparison is the same as the affinity of the column. If
      ** it is not, it is not possible to use any index.
      */
      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);

      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
        if( (pIdx->aiColumn[0]==iCol)
         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
         && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
        ){
          int iAddr;
          char *pKey;
  
          pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
          iAddr = sqlite3CodeOnce(pParse);
  
          sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
                               pKey,P4_KEYINFO_HANDOFF);
          VdbeComment((v, "%s", pIdx->zName));
          assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
          eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];

          sqlite3VdbeJumpHere(v, iAddr);
          if( prNotFound && !pTab->aCol[iCol].notNull ){
            *prNotFound = ++pParse->nMem;
            sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
          }
        }
      }
    }
  }

  if( eType==0 ){
    /* Could not found an existing table or index to use as the RHS b-tree.
    ** We will have to generate an ephemeral table to do the job.
    */
    u32 savedNQueryLoop = pParse->nQueryLoop;
    int rMayHaveNull = 0;
    eType = IN_INDEX_EPH;
    if( prNotFound ){
      *prNotFound = rMayHaveNull = ++pParse->nMem;
      sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
    }else{
      testcase( pParse->nQueryLoop>0 );
      pParse->nQueryLoop = 0;
      if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
        eType = IN_INDEX_ROWID;
      }
    }
    sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
    pParse->nQueryLoop = savedNQueryLoop;
  }else{
    pX->iTable = iTab;
  }
  return eType;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
movl 0x54(%rdi), %r15d
leal 0x1(%r15), %eax
movl %eax, 0x54(%rdi)
callq 0x41ef4
movq %rbx, %rsi
movq %rax, %rdi
testb $0x8, 0x3(%rbx)
je 0x49cae
movq 0x20(%rsi), %rax
cmpl $0x0, 0x50(%r14)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x49cae
cmpq $0x0, 0x50(%rax)
jne 0x49cae
testb $0x5, 0xa(%rax)
jne 0x49cae
cmpq $0x0, 0x68(%rax)
jne 0x49cae
cmpq $0x0, 0x30(%rax)
jne 0x49cae
movq 0x28(%rax), %rcx
cmpb $0x1, (%rcx)
jne 0x49cae
cmpq $0x0, 0x30(%rcx)
je 0x49d39
movl 0x1e8(%r14), %r12d
testq %r13, %r13
je 0x49ce4
movl 0x58(%r14), %r15d
incl %r15d
movl %r15d, 0x58(%r14)
movl %r15d, (%r13)
movq %rsi, %rbx
pushq $0x18
popq %rsi
xorl %r13d, %r13d
xorl %edx, %edx
movl %r15d, %ecx
callq 0x41f42
movq %rbx, %rsi
pushq $0x2
popq %rbx
jmp 0x49d13
andl $0x0, 0x1e8(%r14)
movq 0x10(%rsi), %rax
xorl %r15d, %r15d
cmpw $0x0, 0x30(%rax)
js 0x49d02
pushq $0x2
popq %rbx
xorl %r13d, %r13d
jmp 0x49d13
xorl %r13d, %r13d
testb $0x8, 0x3(%rsi)
sete %r13b
pushq $0x2
popq %rbx
subl %r13d, %ebx
movq %r14, %rdi
movl %r15d, %edx
movl %r13d, %ecx
callq 0x49616
movl %r12d, 0x1e8(%r14)
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rcx), %r12
testq %r12, %r12
je 0x49cae
testb $0x10, 0x46(%r12)
jne 0x49cae
movq (%rax), %rax
cmpl $0x1, (%rax)
jne 0x49cae
movq 0x8(%rax), %rax
movq (%rax), %rax
cmpb $-0x68, (%rax)
jne 0x49cae
movq %rsi, -0x40(%rbp)
movq %rdi, -0x58(%rbp)
movq (%r14), %rdi
movq %rax, -0x30(%rbp)
movswl 0x30(%rax), %eax
movl %eax, -0x34(%rbp)
movq 0x68(%r12), %rsi
movq %rdi, -0x50(%rbp)
callq 0x1064e
movl %eax, %ebx
movq %r14, %rdi
movl %eax, %esi
callq 0x4234d
movl 0x3c(%r12), %edx
movq (%r12), %r8
movq %r14, %rdi
movl %ebx, -0x44(%rbp)
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x4207f
cmpl $0x0, -0x34(%rbp)
js 0x49f57
movq -0x40(%rbp), %rbx
movq 0x10(%rbx), %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rdx
callq 0x49bae
movq %rax, -0x80(%rbp)
movq 0x8(%r12), %rax
movl -0x34(%rbp), %ecx
imulq $0x30, %rcx, %rcx
movq %rcx, -0x68(%rbp)
movsbl 0x29(%rax,%rcx), %esi
movq %rbx, %rdi
callq 0x4a030
movl %eax, -0x5c(%rbp)
movq %r12, -0x70(%rbp)
leaq 0x10(%r12), %r8
xorl %r12d, %r12d
movq -0x50(%rbp), %rbx
movq (%r8), %r8
testq %r8, %r8
je 0x49f42
testl %r12d, %r12d
jne 0x49f42
cmpl $0x0, -0x5c(%rbp)
je 0x49f42
movq 0x8(%r8), %rax
xorl %r12d, %r12d
movl -0x34(%rbp), %ecx
cmpl %ecx, (%rax)
jne 0x49f39
movq 0x20(%rbx), %rax
movq 0x18(%rax), %rax
movzbl 0x71(%rax), %esi
movq %r8, -0x30(%rbp)
movq 0x40(%r8), %rax
movq (%rax), %rdx
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x46a5b
cmpq -0x80(%rbp), %rax
je 0x49e6c
movq -0x30(%rbp), %r8
jmp 0x49f39
testq %r13, %r13
movq -0x30(%rbp), %r8
je 0x49f26
movq %r14, %rdi
movq %r8, %rsi
callq 0x476ae
movq %rax, -0x78(%rbp)
movq %r14, %rdi
callq 0x49b86
movl %eax, %ebx
movq -0x30(%rbp), %rax
movl 0x50(%rax), %ecx
movl $0xfffffff0, (%rsp)     # imm = 0xFFFFFFF0
movq -0x58(%rbp), %r12
movq %r12, %rdi
pushq $0x30
popq %rsi
movl %r15d, %edx
movl -0x44(%rbp), %r8d
movq -0x78(%rbp), %r9
callq 0x41fd2
movl %ebx, %edx
movq -0x30(%rbp), %r8
movq 0x38(%r8), %rax
movzbl (%rax), %ebx
testl %edx, %edx
js 0x49ee3
movl 0x34(%r12), %eax
cmpl %edx, %eax
jbe 0x49ee3
movq 0x8(%r12), %rcx
movl %edx, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rcx,%rdx)
addl $0x3, %ebx
testq %r13, %r13
je 0x49f1d
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x68(%rbp), %rcx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x49f1d
movl 0x58(%r14), %ecx
incl %ecx
movl %ecx, 0x58(%r14)
movl %ecx, (%r13)
movq %r12, %rdi
pushq $0x18
popq %rsi
xorl %edx, %edx
callq 0x41f42
movq -0x30(%rbp), %r8
movl %ebx, %r12d
movq -0x50(%rbp), %rbx
jmp 0x49f39
cmpw $0x1, 0x54(%r8)
jne 0x49f39
cmpb $0x0, 0x56(%r8)
jne 0x49e79
addq $0x28, %r8
jmp 0x49e08
movl %r12d, %ebx
testl %r12d, %r12d
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rsi
jne 0x49fa2
jmp 0x49cae
movq %r14, %rdi
callq 0x49b86
movl %eax, %r13d
pushq $0x30
popq %r8
movq %r14, %rdi
movl %r15d, %esi
movl -0x44(%rbp), %edx
movq %r12, %rcx
callq 0x4200b
pushq $0x1
popq %rbx
testl %r13d, %r13d
js 0x49f9e
movq -0x58(%rbp), %rax
movl 0x34(%rax), %eax
cmpl %r13d, %eax
jbe 0x49f9e
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %r13d, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rcx,%rdx)
movq -0x40(%rbp), %rsi
movl %r15d, 0x2c(%rsi)
jmp 0x49d28