// File: /workspace/github_c_makefile_star2_O3/shifengbin[P]bishe/Output/../Persistence/sqlite3.c
// Function: whereLoopAddBtree
static int whereLoopAddBtree(
  WhereLoopBuilder *pBuilder, /* WHERE clause information */
  Bitmask mExtra              /* Extra prerequesites for using this table */
){
  WhereInfo *pWInfo;          /* WHERE analysis context */
  Index *pProbe;              /* An index we are evaluating */
  Index sPk;                  /* A fake index object for the primary key */
  LogEst aiRowEstPk[2];       /* The aiRowLogEst[] value for the sPk index */
  i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
  SrcList *pTabList;          /* The FROM clause */
  struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
  WhereLoop *pNew;            /* Template WhereLoop object */
  int rc = SQLITE_OK;         /* Return code */
  int iSortIdx = 1;           /* Index number */
  int b;                      /* A boolean value */
  LogEst rSize;               /* number of rows in the table */
  LogEst rLogSize;            /* Logarithm of the number of rows in the table */
  WhereClause *pWC;           /* The parsed WHERE clause */
  Table *pTab;                /* Table being queried */
  
  pNew = pBuilder->pNew;
  pWInfo = pBuilder->pWInfo;
  pTabList = pWInfo->pTabList;
  pSrc = pTabList->a + pNew->iTab;
  pTab = pSrc->pTab;
  pWC = pBuilder->pWC;
  assert( !IsVirtual(pSrc->pTab) );

  if( pSrc->pIBIndex ){
    /* An INDEXED BY clause specifies a particular index to use */
    pProbe = pSrc->pIBIndex;
  }else if( !HasRowid(pTab) ){
    pProbe = pTab->pIndex;
  }else{
    /* There is no INDEXED BY clause.  Create a fake Index object in local
    ** variable sPk to represent the rowid primary key index.  Make this
    ** fake index the first in a chain of Index objects with all of the real
    ** indices to follow */
    Index *pFirst;                  /* First of real indices on the table */
    memset(&sPk, 0, sizeof(Index));
    sPk.nKeyCol = 1;
    sPk.nColumn = 1;
    sPk.aiColumn = &aiColumnPk;
    sPk.aiRowLogEst = aiRowEstPk;
    sPk.onError = OE_Replace;
    sPk.pTable = pTab;
    sPk.szIdxRow = pTab->szTabRow;
    aiRowEstPk[0] = pTab->nRowLogEst;
    aiRowEstPk[1] = 0;
    pFirst = pSrc->pTab->pIndex;
    if( pSrc->fg.notIndexed==0 ){
      /* The real indices of the table are only considered if the
      ** NOT INDEXED qualifier is omitted from the FROM clause */
      sPk.pNext = pFirst;
    }
    pProbe = &sPk;
  }
  rSize = pTab->nRowLogEst;
  rLogSize = estLog(rSize);

#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  /* Automatic indexes */
  if( !pBuilder->pOrSet      /* Not part of an OR optimization */
   && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0
   && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
   && pSrc->pIBIndex==0      /* Has no INDEXED BY clause */
   && !pSrc->fg.notIndexed   /* Has no NOT INDEXED clause */
   && HasRowid(pTab)         /* Not WITHOUT ROWID table. (FIXME: Why not?) */
   && !pSrc->fg.isCorrelated /* Not a correlated subquery */
   && !pSrc->fg.isRecursive  /* Not a recursive common table expression. */
  ){
    /* Generate auto-index WhereLoops */
    WhereTerm *pTerm;
    WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
    for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
      if( pTerm->prereqRight & pNew->maskSelf ) continue;
      if( termCanDriveIndex(pTerm, pSrc, 0) ){
        pNew->u.btree.nEq = 1;
        pNew->nSkip = 0;
        pNew->u.btree.pIndex = 0;
        pNew->nLTerm = 1;
        pNew->aLTerm[0] = pTerm;
        /* TUNING: One-time cost for computing the automatic index is
        ** estimated to be X*N*log2(N) where N is the number of rows in
        ** the table being indexed and where X is 7 (LogEst=28) for normal
        ** tables or 1.375 (LogEst=4) for views and subqueries.  The value
        ** of X is smaller for views and subqueries so that the query planner
        ** will be more aggressive about generating automatic indexes for
        ** those objects, since there is no opportunity to add schema
        ** indexes on subqueries and views. */
        pNew->rSetup = rLogSize + rSize + 4;
        if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
          pNew->rSetup += 24;
        }
        ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
        /* TUNING: Each index lookup yields 20 rows in the table.  This
        ** is more than the usual guess of 10 rows, since we have no way
        ** of knowing how selective the index will ultimately be.  It would
        ** not be unreasonable to make this value much larger. */
        pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
        pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
        pNew->wsFlags = WHERE_AUTO_INDEX;
        pNew->prereq = mExtra | pTerm->prereqRight;
        rc = whereLoopInsert(pBuilder, pNew);
      }
    }
  }
#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */

  /* Loop over all indices
  */
  for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
    if( pProbe->pPartIdxWhere!=0
     && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
      testcase( pNew->iTab!=pSrc->iCursor );  /* See ticket [98d973b8f5] */
      continue;  /* Partial index inappropriate for this query */
    }
    rSize = pProbe->aiRowLogEst[0];
    pNew->u.btree.nEq = 0;
    pNew->nSkip = 0;
    pNew->nLTerm = 0;
    pNew->iSortIdx = 0;
    pNew->rSetup = 0;
    pNew->prereq = mExtra;
    pNew->nOut = rSize;
    pNew->u.btree.pIndex = pProbe;
    b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
    /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
    assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
    if( pProbe->tnum<=0 ){
      /* Integer primary key index */
      pNew->wsFlags = WHERE_IPK;

      /* Full table scan */
      pNew->iSortIdx = b ? iSortIdx : 0;
      /* TUNING: Cost of full table scan is (N*3.0). */
      pNew->rRun = rSize + 16;
      ApplyCostMultiplier(pNew->rRun, pTab->costMult);
      whereLoopOutputAdjust(pWC, pNew, rSize);
      rc = whereLoopInsert(pBuilder, pNew);
      pNew->nOut = rSize;
      if( rc ) break;
    }else{
      Bitmask m;
      if( pProbe->isCovering ){
        pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
        m = 0;
      }else{
        m = pSrc->colUsed & ~columnsInIndex(pProbe);
        pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
      }

      /* Full scan via index */
      if( b
       || !HasRowid(pTab)
       || ( m==0
         && pProbe->bUnordered==0
         && (pProbe->szIdxRow<pTab->szTabRow)
         && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
         && sqlite3GlobalConfig.bUseCis
         && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
          )
      ){
        pNew->iSortIdx = b ? iSortIdx : 0;

        /* The cost of visiting the index rows is N*K, where K is
        ** between 1.1 and 3.0, depending on the relative sizes of the
        ** index and table rows. If this is a non-covering index scan,
        ** also add the cost of visiting table rows (N*3.0).  */
        pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
        if( m!=0 ){
          pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
        }
        ApplyCostMultiplier(pNew->rRun, pTab->costMult);
        whereLoopOutputAdjust(pWC, pNew, rSize);
        rc = whereLoopInsert(pBuilder, pNew);
        pNew->nOut = rSize;
        if( rc ) break;
      }
    }

    rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
    sqlite3Stat4ProbeFree(pBuilder->pRec);
    pBuilder->nRecValid = 0;
    pBuilder->pRec = 0;
#endif

    /* If there was an INDEXED BY clause, then only that one index is
    ** considered. */
    if( pSrc->pIBIndex ) break;
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, -0x88(%rbp)
movq %rdi, %r15
movw $0xffff, -0x4a(%rbp)    # imm = 0xFFFF
movq 0x18(%rdi), %rdx
movq (%rdi), %rax
movq %rax, -0x78(%rbp)
movq 0x8(%rax), %rax
movzbl 0x10(%rdx), %ecx
imulq $0x70, %rcx, %rcx
leaq (%rax,%rcx), %rsi
addq $0x8, %rsi
movq 0x20(%rsi), %rdi
movq 0x68(%rsi), %r14
movq %r14, %r13
testq %r14, %r14
jne 0x66717
testb $0x20, 0x46(%rdi)
jne 0x66713
xorps %xmm0, %xmm0
leaq -0x110(%rbp), %r13
movaps %xmm0, 0x50(%r13)
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, 0x40(%r13)
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x20(%r13)
movq $0x0, 0x60(%r13)
movw $0x1, %ax
movw %ax, 0x5e(%r13)
movw %ax, 0x60(%r13)
leaq -0x4a(%rbp), %rax
movq %rax, 0x8(%r13)
leaq -0xa4(%rbp), %rax
movq %rax, 0x10(%r13)
movb $0x5, 0x62(%r13)
movq %rdi, 0x18(%r13)
movzwl 0x44(%rdi), %ecx
movw %cx, 0x5c(%r13)
movzwl 0x42(%rdi), %ecx
movw %cx, (%rax)
movw $0x0, 0x2(%rax)
testb $0x1, 0x3d(%rsi)
jne 0x66717
movq 0x10(%rdi), %rax
movq %rax, -0xe8(%rbp)
jmp 0x66717
movq 0x10(%rdi), %r13
movq %rsi, -0x38(%rbp)
movq %rdx, -0x48(%rbp)
movq 0x8(%r15), %rax
movq %rax, -0x68(%rbp)
movq %rdi, -0x60(%rbp)
movswq 0x42(%rdi), %r12
xorl %ebx, %ebx
movl $0x0, %eax
cmpq $0xb, %r12
jl 0x66748
movq %r12, %rdi
callq 0x3c565
addl $-0x21, %eax
cmpq $0x0, 0x20(%r15)
jne 0x6678b
movq -0x78(%rbp), %rcx
testb $-0x80, 0x32(%rcx)
jne 0x6678b
movq (%rcx), %rcx
movq (%rcx), %rcx
testb $0x10, 0x2e(%rcx)
je 0x6678b
testq %r14, %r14
jne 0x6678b
movq -0x38(%rbp), %rcx
movb 0x3d(%rcx), %cl
testb $0x1, %cl
jne 0x6678b
movq -0x60(%rbp), %rdx
movb 0x46(%rdx), %dl
andb $0x20, %dl
andb $0x28, %cl
orb %dl, %cl
je 0x66ba1
testl %ebx, %ebx
jne 0x66b8d
testq %r13, %r13
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %r12
je 0x66b8d
movl $0x1, -0x30(%rbp)
movq %r15, -0x80(%rbp)
movq 0x48(%r13), %rdx
testq %rdx, %rdx
je 0x667ce
movl 0x44(%r12), %edi
movq -0x68(%rbp), %rsi
callq 0x67709
testl %eax, %eax
je 0x6696c
movq 0x10(%r13), %rax
movzwl (%rax), %ecx
xorl %r8d, %r8d
movw %r8w, 0x18(%r14)
movl $0x0, 0x2c(%r14)
movb $0x0, 0x11(%r14)
movw %r8w, 0x12(%r14)
movq -0x88(%rbp), %rax
movq %rax, (%r14)
movq %rcx, -0x58(%rbp)
movw %cx, 0x16(%r14)
movq %r13, 0x20(%r14)
movb 0x63(%r13), %al
movb $0x1, %r9b
testb $0x4, %al
jne 0x6699d
movb %al, -0x29(%rbp)
movq (%r15), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x6699a
movslq (%rax), %rcx
movq %rcx, -0x90(%rbp)
testq %rcx, %rcx
jle 0x6699a
movl 0x44(%r12), %ecx
movl %ecx, -0x70(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
xorl %eax, %eax
movq %r13, -0x40(%rbp)
movq %rax, -0xa0(%rbp)
shlq $0x5, %rax
movq -0x98(%rbp), %rcx
movq (%rcx,%rax), %r12
testq %r12, %r12
je 0x66895
movl 0x4(%r12), %eax
btl $0xc, %eax
jae 0x66898
btl $0x12, %eax
jb 0x66886
addq $0x10, %r12
jmp 0x6688f
movq 0x20(%r12), %rax
movq 0x8(%rax), %r12
movq (%r12), %r12
jmp 0x6686a
xorl %r12d, %r12d
cmpb $-0x68, (%r12)
jne 0x668ea
movl -0x70(%rbp), %eax
cmpl %eax, 0x2c(%r12)
jne 0x668ea
movzwl 0x30(%r12), %eax
testw %ax, %ax
js 0x66b7b
movzwl 0x5e(%r13), %ecx
testq %rcx, %rcx
je 0x6693a
movq -0x40(%rbp), %rdx
movq 0x8(%rdx), %rdx
xorl %esi, %esi
cmpw (%rdx,%rsi,2), %ax
movq -0x48(%rbp), %r14
movl $0x0, %r8d
je 0x6698b
incq %rsi
cmpq %rsi, %rcx
jne 0x668cc
jmp 0x6693a
movq 0x50(%r13), %r15
testq %r15, %r15
je 0x6693a
movq -0x40(%rbp), %rax
movzwl 0x5e(%rax), %ebx
testq %rbx, %rbx
je 0x6693a
movq -0x40(%rbp), %rax
movq 0x8(%rax), %r13
shll $0x5, %ebx
xorl %r14d, %r14d
cmpw $-0x2, (%r13)
jne 0x6692d
movq 0x8(%r15), %rax
movq (%rax,%r14), %rsi
movq %r12, %rdi
movl -0x70(%rbp), %edx
callq 0x532d3
testl %eax, %eax
je 0x66973
addq $0x20, %r14
addq $0x2, %r13
cmpq %r14, %rbx
jne 0x6690e
movq -0xa0(%rbp), %rax
incq %rax
cmpq -0x90(%rbp), %rax
movq -0x80(%rbp), %r15
movq -0x40(%rbp), %r13
movl $0x0, %r8d
movb $0x1, %r9b
jne 0x66854
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %r12
jmp 0x6699a
xorl %ebx, %ebx
jmp 0x66b65
xorl %r9d, %r9d
movq -0x80(%rbp), %r15
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %r12
movq -0x40(%rbp), %r13
xorl %r8d, %r8d
jmp 0x6699a
xorl %r9d, %r9d
movq -0x80(%rbp), %r15
movq -0x38(%rbp), %r12
movq -0x40(%rbp), %r13
movb -0x29(%rbp), %al
cmpl $0x0, 0x58(%r13)
jle 0x669e9
testb $0x20, %al
jne 0x66a49
movzwl 0x60(%r13), %eax
testq %rax, %rax
je 0x66a52
movq 0x8(%r13), %rsi
incq %rax
xorl %edx, %edx
movzwl -0x4(%rsi,%rax,2), %ecx
movl $0x1, %edi
shlq %cl, %rdi
cmpw $0x3f, %cx
cmovaeq %r8, %rdi
orq %rdi, %rdx
decq %rax
cmpq $0x1, %rax
ja 0x669c3
notq %rdx
jmp 0x66a59
testb %r9b, %r9b
movl -0x30(%rbp), %eax
cmovnel %r8d, %eax
movl $0x100, 0x28(%r14)      # imm = 0x100
movb %al, 0x11(%r14)
movq %r15, %r12
movq -0x58(%rbp), %r15
leal 0x10(%r15), %eax
movw %ax, 0x14(%r14)
movswl %r15w, %edx
movq -0x68(%rbp), %rdi
movq %r14, %rsi
callq 0x67794
movq (%r12), %rdi
movq 0x20(%r12), %rsi
movq %r14, %rdx
callq 0x671b7
movw %r15w, 0x16(%r14)
movq %r12, %r15
movq -0x38(%rbp), %r12
testl %eax, %eax
je 0x66b4b
jmp 0x66b8b
movb $0x1, %cl
movl $0x240, %eax            # imm = 0x240
jmp 0x66a6b
movq $-0x1, %rdx
xorl %ecx, %ecx
testq %rdx, 0x58(%r12)
sete %cl
movl %ecx, %eax
orl $0x8, %eax
shll $0x6, %eax
movq -0x58(%rbp), %r8
movl %eax, 0x28(%r14)
movl -0x30(%rbp), %eax
testb %r9b, %r9b
je 0x66ad8
xorl %eax, %eax
movq -0x60(%rbp), %rdx
testb $0x20, 0x46(%rdx)
jne 0x66ad8
testb %cl, %cl
je 0x66b4b
testb $0x4, 0x63(%r13)
jne 0x66b4b
movzwl 0x5c(%r13), %edx
movq -0x60(%rbp), %rsi
cmpw 0x44(%rsi), %dx
jge 0x66b4b
movq -0x78(%rbp), %rdx
testb $0x4, 0x32(%rdx)
jne 0x66b4b
cmpl $0x0, 0x2a87e(%rip)     # 0x91340
je 0x66b4b
movq -0x78(%rbp), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
testb $0x40, 0x4c(%rdx)
jne 0x66b4b
movzwl %r8w, %esi
movb %al, 0x11(%r14)
movswl 0x5c(%r13), %eax
leal (%rax,%rax,4), %eax
leal (%rax,%rax,2), %eax
movq -0x60(%rbp), %rdx
movswl 0x44(%rdx), %edi
cltd
idivl %edi
addl %esi, %eax
incl %eax
movq %r15, %rbx
testb %cl, %cl
jne 0x66b14
leal 0x10(%r8), %ecx
movswl %cx, %esi
movswl %ax, %edi
callq 0x676a9
movq -0x58(%rbp), %r8
movw %ax, 0x14(%r14)
movswl %r8w, %edx
movq -0x68(%rbp), %rdi
movq %r14, %rsi
movq %r8, %r15
callq 0x67794
movq (%rbx), %rdi
movq 0x20(%rbx), %rsi
movq %r14, %rdx
callq 0x671b7
movw %r15w, 0x16(%r14)
movq %rbx, %r15
testl %eax, %eax
movq -0x38(%rbp), %r12
jne 0x66b8b
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x678cd
movl %eax, %ebx
cmpq $0x0, 0x68(%r12)
jne 0x66b8d
testl %ebx, %ebx
jne 0x66b8d
movq 0x28(%r13), %r13
incl -0x30(%rbp)
testq %r13, %r13
jne 0x667af
jmp 0x66b8d
xorl %r9d, %r9d
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %r12
jmp 0x66986
movl %eax, %ebx
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x68(%rbp), %rcx
movslq 0x14(%rcx), %rcx
testq %rcx, %rcx
jle 0x6678b
movq -0x68(%rbp), %rdx
movq 0x20(%rdx), %r14
imulq $0x38, %rcx, %rcx
addq %r14, %rcx
movq %rcx, -0x70(%rbp)
leal (%rax,%r12), %ecx
addl $0x4, %ecx
movl %ecx, -0x30(%rbp)
leal (%rax,%r12), %ecx
addl $0x1c, %ecx
movl %ecx, -0x40(%rbp)
addq $0x38, %r14
cwtl
movl %eax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
xorl %ebx, %ebx
testq %rax, -0x10(%r14)
jne 0x66c89
leaq -0x38(%r14), %r12
xorl %ebx, %ebx
movq %r12, %rdi
movq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0x6763f
testl %eax, %eax
je 0x66c89
movq -0x48(%rbp), %rbx
movw $0x1, 0x18(%rbx)
movq $0x0, 0x20(%rbx)
movl $0x1, 0x2c(%rbx)
movq 0x38(%rbx), %rax
movq %r12, (%rax)
movl -0x30(%rbp), %eax
movw %ax, 0x12(%rbx)
movq -0x60(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x66c4c
testb $0x2, 0x46(%rax)
jne 0x66c4c
movl -0x40(%rbp), %eax
movw %ax, 0x12(%rbx)
movw $0x2b, 0x16(%rbx)
movl -0x58(%rbp), %edi
movl $0x2b, %esi
callq 0x676a9
movw %ax, 0x14(%rbx)
movl $0x4000, 0x28(%rbx)     # imm = 0x4000
movq -0x10(%r14), %rax
orq -0x88(%rbp), %rax
movq %rax, (%rbx)
movq (%r15), %rdi
movq 0x20(%r15), %rsi
movq %rbx, %rdx
callq 0x671b7
movl %eax, %ebx
testl %ebx, %ebx
jne 0x6678b
leaq 0x38(%r14), %rax
cmpq -0x70(%rbp), %r14
movq %rax, %r14
jb 0x66be1
jmp 0x6678b