// File: /workspace/github_c_makefile_star2_O2/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
orw $-0x1, -0x52(%rbp)
movq %rsi, -0x88(%rbp)
movq %rdi, %r13
movq 0x18(%rdi), %rdx
movq (%rdi), %rax
movq %rax, -0x70(%rbp)
movq 0x8(%rax), %rax
movzbl 0x10(%rdx), %ecx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
addq $0x8, %rax
movq 0x20(%rax), %rsi
movq %rax, -0x50(%rbp)
movq 0x68(%rax), %r14
movq %r14, %rdi
testq %r14, %r14
jne 0x5ae30
testb $0x20, 0x46(%rsi)
jne 0x5ae2c
xorps %xmm0, %xmm0
leaq -0x110(%rbp), %rdi
movaps %xmm0, 0x50(%rdi)
movaps %xmm0, (%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
andq $0x0, 0x60(%rdi)
movw $0x1, %ax
movw %ax, 0x5e(%rdi)
movw %ax, 0x60(%rdi)
leaq -0x52(%rbp), %rax
movq %rax, 0x8(%rdi)
leaq -0xa4(%rbp), %rax
movq %rax, 0x10(%rdi)
movb $0x5, 0x62(%rdi)
movq %rsi, 0x18(%rdi)
movzwl 0x44(%rsi), %ecx
movw %cx, 0x5c(%rdi)
movzwl 0x42(%rsi), %ecx
movw %cx, (%rax)
andw $0x0, 0x2(%rax)
movq -0x50(%rbp), %rax
testb $0x1, 0x3d(%rax)
jne 0x5ae30
movq 0x10(%rsi), %rax
movq %rax, -0xe8(%rbp)
jmp 0x5ae30
movq 0x10(%rsi), %rdi
movq %rdi, -0x38(%rbp)
movq %rdx, -0x60(%rbp)
movq 0x8(%r13), %rax
movq %rax, -0x78(%rbp)
movq %rsi, -0x68(%rbp)
movzwl 0x42(%rsi), %r12d
movswl %r12w, %edi
callq 0x5bbe2
cmpq $0x0, 0x20(%r13)
movl $0x0, %ebx
jne 0x5aeb7
movq -0x70(%rbp), %rcx
testb $-0x80, 0x32(%rcx)
movl $0x0, %ebx
jne 0x5aeb7
movq -0x70(%rbp), %rcx
movq (%rcx), %rcx
movq (%rcx), %rcx
testb $0x10, 0x2e(%rcx)
movl $0x0, %ebx
je 0x5aeb7
movl $0x0, %ebx
testq %r14, %r14
jne 0x5aeb7
movq -0x50(%rbp), %rcx
movb 0x3d(%rcx), %cl
movl $0x0, %ebx
testb $0x1, %cl
jne 0x5aeb7
movq -0x68(%rbp), %rdx
movb 0x46(%rdx), %dl
andb $0x20, %dl
andb $0x28, %cl
orb %dl, %cl
movl $0x0, %ebx
je 0x5b21f
pushq $0x1
popq %r12
movq %r13, -0x80(%rbp)
movq -0x60(%rbp), %r15
movq -0x38(%rbp), %r8
testl %ebx, %ebx
jne 0x5b20b
testq %r8, %r8
je 0x5b20b
movq 0x48(%r8), %rdx
testq %rdx, %rdx
movq %r8, -0x38(%rbp)
je 0x5af01
movq -0x50(%rbp), %rax
movl 0x44(%rax), %edi
movq -0x78(%rbp), %rsi
callq 0x5bc9b
movq -0x38(%rbp), %r8
testl %eax, %eax
je 0x5b061
movl %r12d, -0x3c(%rbp)
movq 0x10(%r8), %rax
movzwl (%rax), %edi
andw $0x0, 0x18(%r15)
andl $0x0, 0x2c(%r15)
movb $0x0, 0x11(%r15)
andw $0x0, 0x12(%r15)
movq -0x88(%rbp), %rax
movq %rax, (%r15)
movw %di, 0x16(%r15)
movq %r8, 0x20(%r15)
movb 0x63(%r8), %al
movb $0x1, %r12b
movb %al, -0x29(%rbp)
testb $0x4, %al
movq %rdi, -0x48(%rbp)
jne 0x5b077
movq (%r13), %rax
movq 0x10(%rax), %rax
movq %rax, -0x90(%rbp)
testq %rax, %rax
je 0x5b077
movq -0x50(%rbp), %rax
movl 0x44(%rax), %ebx
movq -0x90(%rbp), %rax
movl (%rax), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movq %rcx, -0x98(%rbp)
xorl %ecx, %ecx
cmpq -0x98(%rbp), %rcx
sete %r12b
je 0x5b077
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, -0xa0(%rbp)
shlq $0x5, %rcx
movq (%rax,%rcx), %rdi
callq 0x4cea1
movq %rax, %r14
cmpb $-0x68, (%rax)
jne 0x5aff9
cmpl %ebx, 0x2c(%r14)
jne 0x5aff9
movzwl 0x30(%r14), %eax
testw %ax, %ax
movq -0x38(%rbp), %r8
movq -0x48(%rbp), %rdi
js 0x5b077
movzwl 0x5e(%r8), %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x5b046
movq 0x8(%r8), %rsi
leaq 0x1(%rdx), %rdi
cmpw (%rsi,%rdx,2), %ax
movq %rdi, %rdx
jne 0x5afe1
jmp 0x5b068
movq -0x38(%rbp), %r8
movq 0x50(%r8), %r12
testq %r12, %r12
je 0x5b046
movzwl 0x5e(%r8), %r15d
xorl %r13d, %r13d
cmpq %r15, %r13
je 0x5b046
movq 0x8(%r8), %rax
cmpw $-0x2, (%rax,%r13,2)
jne 0x5b041
movq 0x8(%r12), %rax
movq %r13, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x4cee2
movq -0x38(%rbp), %r8
testl %eax, %eax
je 0x5b068
incq %r13
jmp 0x5b00e
movq -0xa0(%rbp), %rcx
incq %rcx
movq -0x80(%rbp), %r13
movq -0x60(%rbp), %r15
movq -0x48(%rbp), %rdi
jmp 0x5af86
xorl %ebx, %ebx
jmp 0x5b1fd
xorl %r12d, %r12d
movq -0x80(%rbp), %r13
movq -0x60(%rbp), %r15
movq -0x48(%rbp), %rdi
cmpl $0x0, 0x58(%r8)
movswl %di, %ebx
jle 0x5b0b8
testb $0x20, -0x29(%rbp)
jne 0x5b110
movq -0x50(%rbp), %rax
movq 0x58(%rax), %r14
movq %r8, %rdi
callq 0x5a5c7
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %r8
notq %rax
xorl %ecx, %ecx
testq %rax, %r14
sete %cl
movl %ecx, %eax
orl $0x8, %eax
shll $0x6, %eax
jmp 0x5b117
testb %r12b, %r12b
movl -0x3c(%rbp), %r12d
movl %r12d, %eax
movl $0x0, %ecx
cmovnel %ecx, %eax
movl $0x100, 0x28(%r15)      # imm = 0x100
movb %al, 0x11(%r15)
leal 0x10(%rdi), %eax
movw %ax, 0x14(%r15)
movq %rdi, %r14
movq -0x78(%rbp), %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x5bd2c
movq %r13, %rdi
movq %r15, %rsi
callq 0x5b776
movq -0x38(%rbp), %r8
movw %r14w, 0x16(%r15)
testl %eax, %eax
je 0x5b1dc
jmp 0x5b209
movb $0x1, %cl
movl $0x240, %eax            # imm = 0x240
movl %eax, 0x28(%r15)
movl -0x3c(%rbp), %edx
movl %edx, %eax
testb %r12b, %r12b
movl %edx, %r12d
je 0x5b17d
xorl %eax, %eax
movq -0x68(%rbp), %rdx
testb $0x20, 0x46(%rdx)
jne 0x5b17d
testb %cl, %cl
je 0x5b1dc
testb $0x4, 0x63(%r8)
jne 0x5b1dc
movzwl 0x5c(%r8), %edx
movq -0x68(%rbp), %rsi
cmpw 0x44(%rsi), %dx
jge 0x5b1dc
movq -0x70(%rbp), %rdx
testb $0x4, 0x32(%rdx)
jne 0x5b1dc
cmpl $0x0, 0x282c5(%rip)     # 0x83430
je 0x5b1dc
movq -0x70(%rbp), %rdx
movq (%rdx), %rdx
movq (%rdx), %rdx
testb $0x40, 0x4c(%rdx)
jne 0x5b1dc
movzwl %di, %esi
movb %al, 0x11(%r15)
movswl 0x5c(%r8), %eax
imull $0xf, %eax, %eax
movq -0x68(%rbp), %rdx
movq %rdi, %r14
movswl 0x44(%rdx), %edi
cltd
idivl %edi
addl %esi, %eax
incl %eax
testb %cl, %cl
jne 0x5b1b1
leal 0x10(%r14), %ecx
movswl %cx, %esi
movswl %ax, %edi
callq 0x5bc3b
movw %ax, 0x14(%r15)
movq -0x78(%rbp), %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x5bd2c
movq %r13, %rdi
movq %r15, %rsi
callq 0x5b776
movw %r14w, 0x16(%r15)
testl %eax, %eax
movq -0x38(%rbp), %r8
jne 0x5b209
movq %r13, %rdi
movq -0x50(%rbp), %r14
movq %r14, %rsi
movq %r8, %rdx
xorl %ecx, %ecx
callq 0x5be4b
movq -0x38(%rbp), %r8
movl %eax, %ebx
cmpq $0x0, 0x68(%r14)
jne 0x5b20b
movq 0x28(%r8), %r8
incl %r12d
jmp 0x5aec7
movl %eax, %ebx
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x78(%rbp), %rcx
movq 0x20(%rcx), %rdx
movslq 0x14(%rcx), %rcx
imulq $0x38, %rcx, %r14
addq %rdx, %r14
leal (%rax,%r12), %r15d
addl $0x4, %r15d
leal (%rax,%r12), %ecx
addl $0x1c, %ecx
movl %ecx, -0x3c(%rbp)
xorl %ebx, %ebx
cwtl
movl %eax, -0x48(%rbp)
testl %ebx, %ebx
jne 0x5aeb7
cmpq %r14, %rdx
jae 0x5aeb7
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
xorl %ebx, %ebx
testq %rax, 0x28(%rdx)
jne 0x5b2fe
xorl %ebx, %ebx
movq %rdx, %r12
movq %rdx, %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x5bbfc
movq %r12, %rdx
testl %eax, %eax
je 0x5b2fe
movq -0x60(%rbp), %rbx
movw $0x1, 0x18(%rbx)
andq $0x0, 0x20(%rbx)
movl $0x1, 0x2c(%rbx)
movq 0x38(%rbx), %rax
movq %rdx, (%rax)
movw %r15w, 0x12(%rbx)
movq -0x68(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x5b2c3
testb $0x2, 0x46(%rax)
jne 0x5b2c3
movl -0x3c(%rbp), %eax
movw %ax, 0x12(%rbx)
movw $0x2b, 0x16(%rbx)
movl -0x48(%rbp), %edi
pushq $0x2b
popq %rsi
callq 0x5bc3b
movw %ax, 0x14(%rbx)
movl $0x4000, 0x28(%rbx)     # imm = 0x4000
movq 0x28(%r12), %rax
orq -0x88(%rbp), %rax
movq %rax, (%rbx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5b776
movq %r12, %rdx
movl %eax, %ebx
addq $0x38, %rdx
jmp 0x5b24a