// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
movl 0x54(%rdi), %r15d
leal 0x1(%r15), %eax
movl %eax, 0x54(%rdi)
callq 0x45ece
movq %rbx, %rsi
movq %rax, %rdi
testb $0x8, 0x3(%rbx)
je 0x4f02e
movq 0x20(%rsi), %rax
cmpl $0x0, 0x50(%r14)
setne %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x4f02e
cmpq $0x0, 0x50(%rax)
jne 0x4f02e
testb $0x5, 0xa(%rax)
jne 0x4f02e
cmpq $0x0, 0x68(%rax)
jne 0x4f02e
cmpq $0x0, 0x30(%rax)
jne 0x4f02e
movq 0x28(%rax), %rcx
cmpb $0x1, (%rcx)
jne 0x4f02e
cmpq $0x0, 0x30(%rcx)
je 0x4f0c5
movl 0x1e8(%r14), %ebx
testq %r13, %r13
je 0x4f06c
movl 0x58(%r14), %r15d
incl %r15d
movl %r15d, 0x58(%r14)
movl %r15d, (%r13)
xorl %r13d, %r13d
movq %rsi, %r12
movl $0x18, %esi
xorl %edx, %edx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq %r12, %rsi
movl $0x2, %r12d
jmp 0x4f09e
movl $0x0, 0x1e8(%r14)
movq 0x10(%rsi), %rax
movl $0x2, %r12d
xorl %r15d, %r15d
cmpw $0x0, 0x30(%rax)
js 0x4f090
xorl %r13d, %r13d
jmp 0x4f09e
xorl %r13d, %r13d
testb $0x8, 0x3(%rsi)
sete %r13b
subl %r13d, %r12d
movq %r14, %rdi
movl %r15d, %edx
movl %r13d, %ecx
callq 0x4e7d5
movl %ebx, 0x1e8(%r14)
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rcx), %rcx
testq %rcx, %rcx
je 0x4f02e
testb $0x10, 0x46(%rcx)
jne 0x4f02e
movq (%rax), %rax
cmpl $0x1, (%rax)
jne 0x4f02e
movq 0x8(%rax), %rax
movq (%rax), %rdx
cmpb $-0x68, (%rdx)
jne 0x4f02e
movq %rdi, -0x30(%rbp)
movq %rsi, -0x48(%rbp)
movq (%r14), %rax
movq %rax, -0x50(%rbp)
movq %rdx, -0x38(%rbp)
movswl 0x30(%rdx), %eax
movl %eax, -0x40(%rbp)
movq %rcx, -0x58(%rbp)
movq 0x68(%rcx), %rax
testq %rax, %rax
je 0x4f14f
movq -0x50(%rbp), %rcx
movl 0x28(%rcx), %ecx
testl %ecx, %ecx
jle 0x4f157
movq -0x50(%rbp), %rdx
movq 0x20(%rdx), %rdx
addq $0x18, %rdx
xorl %r12d, %r12d
cmpq %rax, (%rdx)
je 0x4f15a
incq %r12
addq $0x20, %rdx
cmpq %r12, %rcx
jne 0x4f139
movl %ecx, %r12d
jmp 0x4f15a
movl $0xfff0bdc0, %r12d      # imm = 0xFFF0BDC0
jmp 0x4f15a
xorl %r12d, %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x4632f
movq -0x58(%rbp), %rbx
movl 0x3c(%rbx), %edx
movq (%rbx), %r8
movq %r14, %rdi
movq %r12, -0x60(%rbp)
movl %r12d, %esi
xorl %ecx, %ecx
callq 0x46018
cmpw $0x0, -0x40(%rbp)
js 0x4f1de
movq -0x48(%rbp), %r12
movq 0x10(%r12), %rsi
movq %r14, %rdi
movq -0x38(%rbp), %rdx
callq 0x4ef2c
movq %rax, -0x38(%rbp)
movq 0x8(%rbx), %rax
movl -0x40(%rbp), %ecx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq %rcx, -0x68(%rbp)
movb 0x29(%rax,%rcx), %bl
movq %r12, %rdi
callq 0x4f3c0
cmpb $0x62, %al
je 0x4f255
movzbl %al, %eax
cmpl $0x61, %eax
jne 0x4f259
cmpb $0x61, %bl
sete %al
jmp 0x4f25f
movq %r14, %rdi
callq 0x45ece
movl 0x60(%r14), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%r14)
movq %rax, %rdi
movl $0x26, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, %r13d
movq %r14, %rdi
movl %r15d, %esi
movq -0x60(%rbp), %rdx
movq %rbx, %rcx
movl $0x30, %r8d
callq 0x45fa0
movl $0x1, %r12d
testl %r13d, %r13d
js 0x4f248
movq -0x30(%rbp), %rax
movl 0x34(%rax), %eax
cmpl %r13d, %eax
jbe 0x4f248
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %r13d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
movq -0x48(%rbp), %rax
movl %r15d, 0x2c(%rax)
jmp 0x4f0b3
movb $0x1, %al
jmp 0x4f25f
cmpb $0x63, %bl
setge %al
movq -0x58(%rbp), %rcx
movq 0x10(%rcx), %r12
testq %r12, %r12
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rbx
je 0x4f02e
testb %al, %al
je 0x4f02e
movq 0x8(%r12), %rax
movl -0x40(%rbp), %ecx
cmpl %ecx, (%rax)
jne 0x4f2d2
movq 0x20(%rbx), %rax
movq 0x18(%rax), %rax
movzbl 0x71(%rax), %esi
movq 0x40(%r12), %rax
movq (%rax), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x4b5e8
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
cmpq -0x38(%rbp), %rax
jne 0x4f2d2
testq %r13, %r13
jne 0x4f2e1
cmpw $0x1, 0x54(%r12)
jne 0x4f2d2
cmpb $0x0, 0x56(%r12)
jne 0x4f2e1
movq 0x28(%r12), %r12
testq %r12, %r12
jne 0x4f284
jmp 0x4f02e
movq %rdi, %rbx
movq %r14, %rdi
movq %r12, %rsi
callq 0x4c2fa
movq %rax, -0x40(%rbp)
movq %r14, %rdi
callq 0x45ece
movl 0x60(%r14), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%r14)
movq %rax, %rdi
movl $0x26, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, -0x38(%rbp)
movl 0x50(%r12), %ecx
movq %rbx, %rdi
movl $0x30, %esi
movl %r15d, %edx
movq -0x60(%rbp), %r8
callq 0x45f15
movq %rbx, %rdi
movl %eax, %esi
movq -0x40(%rbp), %rdx
movl $0xfffffff0, %ecx       # imm = 0xFFFFFFF0
callq 0x11a9f
movq 0x38(%r12), %rax
movzbl (%rax), %r12d
movl -0x38(%rbp), %eax
testl %eax, %eax
js 0x4f376
movl %eax, %edx
movq -0x30(%rbp), %rax
movl 0x34(%rax), %eax
cmpl %edx, %eax
jbe 0x4f376
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %edx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
addl $0x3, %r12d
testq %r13, %r13
je 0x4f248
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x68(%rbp), %rcx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x4f248
movl 0x58(%r14), %ecx
incl %ecx
movl %ecx, 0x58(%r14)
movl %ecx, (%r13)
movq -0x30(%rbp), %rdi
movl $0x18, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x45f15
jmp 0x4f248