// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl 0x54(%rdi), %r13d
leal 0x1(%r13), %eax
movl %eax, 0x54(%rdi)
callq 0x45b57
movq %rax, %rdi
xorl %r12d, %r12d
testb $0x8, 0x3(%r14)
jne 0x4ef67
xorl %eax, %eax
jmp 0x4ef6b
movq 0x20(%r14), %rax
cmpl $0x0, 0x50(%rbx)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x4efab
cmpq $0x0, 0x50(%rax)
jne 0x4efab
testb $0x5, 0xa(%rax)
jne 0x4efab
cmpq $0x0, 0x68(%rax)
jne 0x4efab
cmpq $0x0, 0x30(%rax)
jne 0x4efab
movq 0x28(%rax), %rcx
cmpb $0x1, (%rcx)
jne 0x4efab
cmpq $0x0, 0x30(%rcx)
je 0x4f056
testl %r12d, %r12d
je 0x4efb9
movl %r13d, 0x2c(%r14)
jmp 0x4f044
movl 0x1e8(%rbx), %eax
movl %eax, -0x40(%rbp)
testq %r15, %r15
je 0x4eff2
movl 0x58(%rbx), %r13d
incl %r13d
movl %r13d, 0x58(%rbx)
movl %r13d, (%r15)
xorl %r15d, %r15d
movl $0x18, %esi
xorl %edx, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl $0x2, %r12d
jmp 0x4f02a
movl $0x0, 0x1e8(%rbx)
movq 0x10(%r14), %rax
movl $0x2, %r12d
xorl %r13d, %r13d
cmpw $0x0, 0x30(%rax)
js 0x4f015
xorl %r15d, %r15d
jmp 0x4f02a
xorl %r15d, %r15d
testb $0x8, 0x3(%r14)
sete %r15b
movl $0x2, %r12d
subl %r15d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
movl %r15d, %ecx
callq 0x4e782
movl -0x40(%rbp), %eax
movl %eax, 0x1e8(%rbx)
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rcx), %rcx
testq %rcx, %rcx
je 0x4efab
testb $0x10, 0x46(%rcx)
jne 0x4efab
movq (%rax), %rcx
cmpl $0x1, (%rcx)
jne 0x4efab
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
cmpb $-0x68, (%rcx)
jne 0x4efab
movq (%rbx), %rsi
movq (%rax), %rcx
movq 0x28(%rax), %rax
movq 0x28(%rax), %rdx
movq %rdx, -0x50(%rbp)
movq 0x8(%rcx), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movswl 0x30(%rax), %eax
movl %eax, -0x30(%rbp)
movq 0x68(%rdx), %rax
testq %rax, %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x78(%rbp)
movq %r14, -0x48(%rbp)
je 0x4f0ea
movl 0x28(%rsi), %ecx
testl %ecx, %ecx
jle 0x4f0f2
movq 0x20(%rsi), %rdx
addq $0x18, %rdx
xorl %r12d, %r12d
cmpq %rax, (%rdx)
je 0x4f0f5
incq %r12
addq $0x20, %rdx
cmpq %r12, %rcx
jne 0x4f0d4
movl %ecx, %r12d
jmp 0x4f0f5
movl $0xfff0bdc0, %r12d      # imm = 0xFFF0BDC0
jmp 0x4f0f5
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x45ff7
movq -0x50(%rbp), %rax
movl 0x3c(%rax), %edx
movq (%rax), %r8
movq %rbx, %rdi
movq %r12, -0x58(%rbp)
movl %r12d, %esi
xorl %ecx, %ecx
callq 0x45c9b
movl -0x30(%rbp), %r12d
testw %r12w, %r12w
js 0x4f182
movq -0x48(%rbp), %r14
movq 0x10(%r14), %rsi
movq %rbx, %rdi
movq -0x40(%rbp), %rdx
callq 0x4eea5
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movl %r12d, %ecx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq %rcx, -0x60(%rbp)
movb 0x29(%rax,%rcx), %r12b
movq %r14, %rdi
callq 0x4f3ae
cmpb $0x62, %al
je 0x4f1fe
movzbl %al, %eax
cmpl $0x61, %eax
jne 0x4f204
cmpb $0x61, %r12b
sete -0x29(%rbp)
jmp 0x4f20c
movq %rbx, %rdi
callq 0x45b57
movl 0x60(%rbx), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%rbx)
movq %rax, %rdi
movl $0x26, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %eax, %r14d
movq %rbx, %rdi
movl %r13d, %esi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
movl $0x30, %r8d
callq 0x45c23
movl $0x1, %r12d
testl %r14d, %r14d
js 0x4f3a1
movl %r14d, %edx
movq -0x38(%rbp), %rax
movl 0x34(%rax), %eax
cmpl %r14d, %eax
jbe 0x4f3a1
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rcx
movl %edx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
movq -0x48(%rbp), %r14
jmp 0x4efab
movb $0x1, -0x29(%rbp)
jmp 0x4f20c
cmpb $0x63, %r12b
setge -0x29(%rbp)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %r8
xorl %r12d, %r12d
testq %r8, %r8
je 0x4f3a1
cmpb $0x0, -0x29(%rbp)
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %edx
je 0x4efab
movq 0x8(%r8), %rax
movl $0x0, %r12d
cmpl %edx, (%rax)
jne 0x4f381
movq -0x78(%rbp), %rdi
movq 0x20(%rdi), %rax
movq 0x18(%rax), %rax
movzbl 0x71(%rax), %esi
movq %r8, -0x40(%rbp)
movq 0x40(%r8), %rax
movq (%rax), %rdx
xorl %r12d, %r12d
xorl %ecx, %ecx
callq 0x4b4a6
cmpq -0x70(%rbp), %rax
je 0x4f282
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %edx
movq -0x40(%rbp), %r8
jmp 0x4f381
testq %r15, %r15
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %edx
movq -0x40(%rbp), %r8
je 0x4f36e
movq %rbx, %rdi
movq %r8, %rsi
callq 0x4c1d4
movq %rax, -0x68(%rbp)
movq %rbx, %rdi
callq 0x45b57
movl 0x60(%rbx), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%rbx)
movq %rax, %rdi
movl $0x26, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %eax, %r12d
movq -0x40(%rbp), %rax
movl 0x50(%rax), %ecx
movq -0x38(%rbp), %rdi
movl $0x30, %esi
movl %r13d, %edx
movq -0x58(%rbp), %r8
callq 0x45b9e
movq -0x38(%rbp), %rdi
movl %eax, %esi
movq -0x68(%rbp), %rdx
movl $0xfffffff0, %ecx       # imm = 0xFFFFFFF0
callq 0x11975
movl %r12d, %edx
movq -0x40(%rbp), %r8
movq -0x38(%rbp), %rdi
movq 0x38(%r8), %rax
movzbl (%rax), %r12d
testl %edx, %edx
js 0x4f327
movl 0x34(%rdi), %eax
cmpl %edx, %eax
jbe 0x4f327
movq 0x8(%rdi), %rcx
movl %edx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
addl $0x3, %r12d
testq %r15, %r15
je 0x4f369
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x60(%rbp), %rcx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x4f369
movl 0x58(%rbx), %ecx
incl %ecx
movl %ecx, 0x58(%rbx)
movl %ecx, (%r15)
movq -0x38(%rbp), %rdi
movl $0x18, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x40(%rbp), %r8
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %edx
jmp 0x4f381
cmpw $0x1, 0x54(%r8)
jne 0x4f381
cmpb $0x0, 0x56(%r8)
jne 0x4f296
movq 0x28(%r8), %r8
testq %r8, %r8
setne %al
testl %r12d, %r12d
sete %cl
andb %al, %cl
testb %cl, -0x29(%rbp)
jne 0x4f235
jmp 0x4efab
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %rdi
jmp 0x4efab