// File: /workspace/github_c_makefile_star2_O1/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 $0xf8, %rsp
movq %rsi, -0x98(%rbp)
movq %rdi, %r15
movw $0xffff, -0x42(%rbp)    # imm = 0xFFFF
movq 0x18(%rdi), %rdx
movq (%rdi), %rax
movq %rax, -0x80(%rbp)
movq 0x8(%rax), %rax
movzbl 0x10(%rdx), %ecx
imulq $0x70, %rcx, %rcx
leaq (%rax,%rcx), %r13
addq $0x8, %r13
movq 0x20(%r13), %rsi
movq 0x68(%r13), %r12
testq %r12, %r12
jne 0x67c08
testb $0x20, 0x46(%rsi)
jne 0x67c04
xorps %xmm0, %xmm0
leaq -0x120(%rbp), %r12
movaps %xmm0, 0x50(%r12)
movaps %xmm0, (%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, 0x40(%r12)
movaps %xmm0, 0x30(%r12)
movaps %xmm0, 0x20(%r12)
movq $0x0, 0x60(%r12)
movw $0x1, %ax
movw %ax, 0x5e(%r12)
movw %ax, 0x60(%r12)
leaq -0x42(%rbp), %rax
movq %rax, 0x8(%r12)
leaq -0xac(%rbp), %rax
movq %rax, 0x10(%r12)
movb $0x5, 0x62(%r12)
movq %rsi, 0x18(%r12)
movzwl 0x44(%rsi), %ecx
movw %cx, 0x5c(%r12)
movzwl 0x42(%rsi), %ecx
movw %cx, (%rax)
movw $0x0, 0x2(%rax)
testb $0x1, 0x3d(%r13)
jne 0x67c08
movq 0x20(%r13), %rax
movq 0x10(%rax), %rax
movq %rax, -0xf8(%rbp)
jmp 0x67c08
movq 0x10(%rsi), %r12
movq %rdx, -0x40(%rbp)
movq 0x8(%r15), %rax
movq %rax, -0x70(%rbp)
movq %rsi, -0x68(%rbp)
movswq 0x42(%rsi), %r14
xorl %ebx, %ebx
movl $0x0, %eax
cmpq $0xb, %r14
jl 0x67c35
movq %r14, %rdi
callq 0x3d559
addl $-0x21, %eax
cmpq $0x0, 0x20(%r15)
jne 0x67c77
movq -0x80(%rbp), %rcx
testb $-0x80, 0x32(%rcx)
jne 0x67c77
movq (%rcx), %rcx
movq (%rcx), %rcx
testb $0x10, 0x2e(%rcx)
je 0x67c77
cmpq $0x0, 0x68(%r13)
jne 0x67c77
movb 0x3d(%r13), %cl
testb $0x1, %cl
jne 0x67c77
movq -0x68(%rbp), %rdx
movb 0x46(%rdx), %dl
andb $0x20, %dl
andb $0x28, %cl
orb %dl, %cl
je 0x6809f
testl %ebx, %ebx
jne 0x6808b
testq %r12, %r12
movq -0x40(%rbp), %r14
je 0x6808b
movl $0x1, -0x60(%rbp)
movq %r15, -0x88(%rbp)
movq %r13, -0x50(%rbp)
movq 0x48(%r12), %rdx
testq %rdx, %rdx
je 0x67cbd
movl 0x44(%r13), %edi
movq -0x70(%rbp), %rsi
callq 0x68c1a
testl %eax, %eax
je 0x67e58
movq 0x10(%r12), %rax
movzwl (%rax), %ecx
xorl %r8d, %r8d
movw %r8w, 0x18(%r14)
movl $0x0, 0x2c(%r14)
movb $0x0, 0x11(%r14)
movw %r8w, 0x12(%r14)
movq -0x98(%rbp), %rax
movq %rax, (%r14)
movq %rcx, -0x58(%rbp)
movw %cx, 0x16(%r14)
movq %r12, 0x20(%r14)
movb 0x63(%r12), %al
movb $0x1, %r9b
testb $0x4, %al
jne 0x67e8f
movb %al, -0x29(%rbp)
movq (%r15), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x67e8c
movslq (%rax), %rcx
movq %rcx, -0xa0(%rbp)
testq %rcx, %rcx
jle 0x67e8c
movl 0x44(%r13), %ecx
movl %ecx, -0x78(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0xa8(%rbp)
xorl %eax, %eax
movq %r12, -0x38(%rbp)
movq %rax, -0x90(%rbp)
shlq $0x5, %rax
movq -0xa8(%rbp), %rcx
movq (%rcx,%rax), %r14
testq %r14, %r14
je 0x67d82
movl 0x4(%r14), %eax
btl $0xc, %eax
jae 0x67d82
btl $0x12, %eax
jb 0x67d75
addq $0x10, %r14
jmp 0x67d7d
movq 0x20(%r14), %rax
movq 0x8(%rax), %r14
movq (%r14), %r14
jmp 0x67d5a
cmpb $-0x68, (%r14)
jne 0x67dd2
movl -0x78(%rbp), %eax
cmpl %eax, 0x2c(%r14)
jne 0x67dd2
movzwl 0x30(%r14), %eax
testw %ax, %ax
js 0x68079
movzwl 0x5e(%r12), %ecx
testq %rcx, %rcx
je 0x67e23
movq -0x38(%rbp), %rdx
movq 0x8(%rdx), %rdx
xorl %esi, %esi
cmpw (%rdx,%rsi,2), %ax
movq -0x40(%rbp), %r14
movl $0x0, %r8d
je 0x67e7a
incq %rsi
cmpq %rsi, %rcx
jne 0x67db4
jmp 0x67e23
movq 0x50(%r12), %r15
testq %r15, %r15
je 0x67e23
movq -0x38(%rbp), %rax
movzwl 0x5e(%rax), %ebx
testq %rbx, %rbx
je 0x67e23
movq -0x38(%rbp), %rax
movq 0x8(%rax), %r13
shll $0x5, %ebx
xorl %r12d, %r12d
cmpw $-0x2, (%r13)
jne 0x67e16
movq 0x8(%r15), %rax
movq (%rax,%r12), %rsi
movq %r14, %rdi
movl -0x78(%rbp), %edx
callq 0x544b5
testl %eax, %eax
je 0x67e5f
addq $0x20, %r12
addq $0x2, %r13
cmpq %r12, %rbx
jne 0x67df7
movq -0x90(%rbp), %rax
incq %rax
cmpq -0xa0(%rbp), %rax
movq -0x88(%rbp), %r15
movq -0x50(%rbp), %r13
movq -0x38(%rbp), %r12
movl $0x0, %r8d
movb $0x1, %r9b
jne 0x67d44
movq -0x40(%rbp), %r14
jmp 0x67e8c
xorl %ebx, %ebx
jmp 0x68062
xorl %r9d, %r9d
movq -0x88(%rbp), %r15
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r13
movq -0x38(%rbp), %r12
xorl %r8d, %r8d
jmp 0x67e8c
xorl %r9d, %r9d
movq -0x88(%rbp), %r15
movq -0x50(%rbp), %r13
movq -0x38(%rbp), %r12
movb -0x29(%rbp), %al
cmpl $0x0, 0x58(%r12)
jle 0x67ede
testb $0x20, %al
jne 0x67f38
movzwl 0x60(%r12), %eax
testq %rax, %rax
je 0x67f44
movq 0x8(%r12), %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
jg 0x67eb8
notq %rdx
jmp 0x67f4b
testb %r9b, %r9b
movl -0x60(%rbp), %eax
cmovnel %r8d, %eax
movl $0x100, 0x28(%r14)      # imm = 0x100
movb %al, 0x11(%r14)
movq %r15, %r13
movq -0x58(%rbp), %r15
leal 0x10(%r15), %eax
movw %ax, 0x14(%r14)
movswl %r15w, %edx
movq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x68ca5
movq %r13, %rdi
movq %r14, %rsi
callq 0x686b4
movw %r15w, 0x16(%r14)
movq %r13, %r15
movq -0x50(%rbp), %r13
testl %eax, %eax
je 0x68049
jmp 0x68089
movl $0x240, 0x28(%r14)      # imm = 0x240
movb $0x1, %cl
jmp 0x67f60
movq $-0x1, %rdx
xorl %ecx, %ecx
testq %rdx, 0x58(%r13)
sete %cl
movl %ecx, %eax
orl $0x8, %eax
shll $0x6, %eax
movl %eax, 0x28(%r14)
movq -0x58(%rbp), %r8
movl -0x60(%rbp), %eax
testb %r9b, %r9b
je 0x67fcb
xorl %eax, %eax
movq -0x68(%rbp), %rdx
testb $0x20, 0x46(%rdx)
jne 0x67fcb
testb %cl, %cl
je 0x68041
testb $0x4, 0x63(%r12)
jne 0x68041
movzwl 0x5c(%r12), %edx
movq -0x68(%rbp), %rsi
cmpw 0x44(%rsi), %dx
jge 0x68041
movq -0x80(%rbp), %rdx
testb $0x4, 0x32(%rdx)
jne 0x68041
cmpl $0x0, 0x2b37b(%rip)     # 0x93330
je 0x68041
movq -0x80(%rbp), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
testb $0x40, 0x4c(%rdx)
jne 0x68041
movzwl %r8w, %esi
movb %al, 0x11(%r14)
movswl 0x5c(%r12), %eax
leal (%rax,%rax,4), %eax
leal (%rax,%rax,2), %eax
movq -0x68(%rbp), %rdx
movswl 0x44(%rdx), %edi
cltd
idivl %edi
addl %esi, %eax
incl %eax
movq %r15, %r13
testb %cl, %cl
jne 0x68008
leal 0x10(%r8), %ecx
movswl %cx, %esi
movswl %ax, %edi
callq 0x68bba
movq -0x58(%rbp), %r8
movw %ax, 0x14(%r14)
movswl %r8w, %edx
movq -0x70(%rbp), %rdi
movq %r14, %rsi
movq %r8, %r15
callq 0x68ca5
movq %r13, %rdi
movq %r14, %rsi
callq 0x686b4
movw %r15w, 0x16(%r14)
movq %r13, %r15
testl %eax, %eax
movq -0x50(%rbp), %r13
je 0x68041
movl %eax, %ebx
xorl %eax, %eax
jmp 0x68045
movb $0x1, %al
xorl %ebx, %ebx
testb %al, %al
je 0x6808b
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x68dd6
movl %eax, %ebx
cmpq $0x0, 0x68(%r13)
jne 0x6808b
testl %ebx, %ebx
jne 0x6808b
movq 0x28(%r12), %r12
incl -0x60(%rbp)
testq %r12, %r12
jne 0x67c9e
jmp 0x6808b
xorl %r9d, %r9d
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r13
jmp 0x67e75
movl %eax, %ebx
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x70(%rbp), %rcx
movslq 0x14(%rcx), %rcx
testq %rcx, %rcx
jle 0x67c77
movq -0x70(%rbp), %rdx
movq 0x20(%rdx), %rdx
imulq $0x38, %rcx, %rcx
addq %rdx, %rcx
movq %rcx, -0x78(%rbp)
leal (%rax,%r14), %ecx
addl $0x4, %ecx
movl %ecx, -0x58(%rbp)
leal (%rax,%r14), %ecx
addl $0x1c, %ecx
movl %ecx, -0x90(%rbp)
addq $0x38, %rdx
cwtl
movl %eax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
xorl %ebx, %ebx
testq %rax, -0x10(%rdx)
jne 0x6819b
leaq -0x38(%rdx), %r14
xorl %ebx, %ebx
movq %r14, %rdi
movq %r13, %rsi
movq %rdx, -0x60(%rbp)
xorl %edx, %edx
callq 0x68b50
movq -0x60(%rbp), %rdx
testl %eax, %eax
je 0x6819b
movq -0x40(%rbp), %rbx
movw $0x1, 0x18(%rbx)
movq $0x0, 0x20(%rbx)
movl $0x1, 0x2c(%rbx)
movq 0x38(%rbx), %rax
movq %r14, (%rax)
movl -0x58(%rbp), %eax
movw %ax, 0x12(%rbx)
movq -0x68(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x6815b
testb $0x2, 0x46(%rax)
jne 0x6815b
movl -0x90(%rbp), %eax
movw %ax, 0x12(%rbx)
movw $0x2b, 0x16(%rbx)
movl -0x38(%rbp), %edi
movl $0x2b, %esi
callq 0x68bba
movw %ax, 0x14(%rbx)
movl $0x4000, 0x28(%rbx)     # imm = 0x4000
movq -0x60(%rbp), %r14
movq -0x10(%r14), %rax
orq -0x98(%rbp), %rax
movq %rax, (%rbx)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x686b4
movq %r14, %rdx
movl %eax, %ebx
testl %ebx, %ebx
jne 0x67c77
leaq 0x38(%rdx), %rax
cmpq -0x78(%rbp), %rdx
movq %rax, %rdx
jb 0x680e2
jmp 0x67c77