// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/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 */
  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] 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->pIndex ){
    /* An INDEXED BY clause specifies a particular index to use */
    pProbe = pSrc->pIndex;
  }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.aiColumn = &aiColumnPk;
    sPk.aiRowEst = aiRowEstPk;
    sPk.onError = OE_Replace;
    sPk.pTable = pTab;
    aiRowEstPk[0] = pTab->nRowEst;
    aiRowEstPk[1] = 1;
    pFirst = pSrc->pTab->pIndex;
    if( pSrc->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 = sqlite3LogEst(pTab->nRowEst);
  rLogSize = estLog(rSize);

#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  /* Automatic indexes */
  if( !pBuilder->pOrSet
   && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
   && pSrc->pIndex==0
   && !pSrc->viaCoroutine
   && !pSrc->notIndexed
   && HasRowid(pTab)
   && !pSrc->isCorrelated
  ){
    /* 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->u.btree.nSkip = 0;
        pNew->u.btree.pIndex = 0;
        pNew->nLTerm = 1;
        pNew->aLTerm[0] = pTerm;
        /* TUNING: One-time cost for computing the automatic index is
        ** approximately 7*N*log2(N) where N is the number of rows in
        ** the table being indexed. */
        pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
        /* 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 knowning 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(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
      continue;  /* Partial index inappropriate for this query */
    }
    pNew->u.btree.nEq = 0;
    pNew->u.btree.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 3*(N + log2(N)).
      **  +  The extra 3 factor is to encourage the use of indexed lookups
      **     over full scans.  FIXME */
      pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
      whereLoopOutputAdjust(pWC, pNew);
      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;
        if( m==0 ){
          /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
          **  +  The extra factor K of between 1.1 and 3.0 that depends
          **     on the relative sizes of the table and the index.  K
          **     is smaller for smaller indices, thus favoring them.
          */
          pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
                        (15*pProbe->szIdxRow)/pTab->szTabRow;
        }else{
          /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
          ** which we will simplify to just N*log2(N) */
          pNew->rRun = rSize + rLogSize;
        }
        whereLoopOutputAdjust(pWC, pNew);
        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->pIndex ) break;
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
orw $-0x1, 0x16(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, %rbx
movq 0x18(%rdi), %r15
movq (%rdi), %rax
movq %rax, 0x50(%rsp)
movq 0x8(%rax), %rax
movzbl 0x10(%r15), %ecx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
addq $0x8, %rax
movq 0x20(%rax), %rdx
movq %rax, 0x20(%rsp)
movq 0x60(%rax), %r14
movq %r14, %r13
testq %r14, %r14
jne 0x534c0
testb $0x20, 0x48(%rdx)
jne 0x534bc
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r13
movaps %xmm0, 0x50(%r13)
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, 0x40(%r13)
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x20(%r13)
andq $0x0, 0x60(%r13)
movw $0x1, 0x5e(%r13)
leaq 0x16(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x68(%rsp), %rax
movq %rax, 0x10(%r13)
movb $0x5, 0x62(%r13)
movq %rdx, 0x18(%r13)
movl 0x38(%rdx), %ecx
movl %ecx, (%rax)
movl $0x1, 0x4(%rax)
movq 0x20(%rsp), %rax
testb $0x1, 0x39(%rax)
jne 0x534c0
movq 0x10(%rdx), %rax
movq %rax, 0x98(%rsp)
jmp 0x534c0
movq 0x10(%rdx), %r13
movq 0x8(%rbx), %rax
movq %rax, 0x48(%rsp)
movq %rdx, 0x38(%rsp)
movl 0x38(%rdx), %edi
callq 0x30110
movq %rax, 0x28(%rsp)
movswl %ax, %edi
movl %edi, 0x34(%rsp)
callq 0x53cac
movq %rbx, 0x18(%rsp)
cmpq $0x0, 0x20(%rbx)
movq %rax, 0x40(%rsp)
cwtl
movl %eax, 0xc(%rsp)
movl $0x0, %ebx
je 0x537cc
movq 0x40(%rsp), %rax
addl 0x28(%rsp), %eax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
testl %ebx, %ebx
jne 0x537b8
testq %r13, %r13
je 0x537b8
movq 0x48(%r13), %rbx
testq %rbx, %rbx
je 0x53572
movzbl 0x10(%r15), %r14d
movq 0x48(%rsp), %rax
movq 0x20(%rax), %r12
movl 0x14(%rax), %ebp
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
subl $0x1, %ebp
jb 0x53630
movq (%r12), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5437f
testl %eax, %eax
jne 0x53572
addq $0x38, %r12
jmp 0x53550
andl $0x0, 0x18(%r15)
andw $0x0, 0x2c(%r15)
movb $0x0, 0x11(%r15)
andw $0x0, 0x12(%r15)
movq 0x60(%rsp), %rax
movq %rax, (%r15)
movq 0x28(%rsp), %rax
movw %ax, 0x16(%r15)
movq %r13, 0x20(%r15)
movb 0x63(%r13), %r14b
movb $0x1, %bpl
testb $0x4, %r14b
jne 0x53639
movq 0x18(%rsp), %r12
movq (%r12), %rax
movq 0x10(%rax), %rbp
testq %rbp, %rbp
je 0x53645
movq 0x20(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, 0x5c(%rsp)
movl (%rbp), %ebx
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
xorl %r12d, %r12d
cmpq %rbx, %r12
je 0x53640
movq 0x8(%rbp), %rax
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rdi
callq 0x46b5d
cmpb $-0x67, (%rax)
jne 0x53640
movl 0x5c(%rsp), %ecx
cmpl %ecx, 0x2c(%rax)
jne 0x5362b
movzwl 0x5e(%r13), %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x5362b
movzwl 0x30(%rax), %esi
movq 0x8(%r13), %rdi
leaq 0x1(%rdx), %r8
cmpw (%rdi,%rdx,2), %si
movq %r8, %rdx
jne 0x5360f
jmp 0x53637
incq %r12
jmp 0x535e1
xorl %ebx, %ebx
jmp 0x537ab
xorl %ebp, %ebp
movq 0x18(%rsp), %r12
jmp 0x53648
movq 0x18(%rsp), %r12
movb $0x1, %bpl
cmpl $0x0, 0x58(%r13)
jle 0x5367b
testb $0x20, %r14b
jne 0x536ab
movq 0x20(%rsp), %rax
movq 0x50(%rax), %rbx
movq %r13, %rdi
callq 0x52d21
notq %rax
xorl %ecx, %ecx
testq %rax, %rbx
sete %cl
movl %ecx, %eax
orl $0x8, %eax
shll $0x6, %eax
jmp 0x536b2
movl $0x100, 0x28(%r15)      # imm = 0x100
testb %bpl, %bpl
movl 0x10(%rsp), %eax
movl $0x0, %ecx
cmovnel %ecx, %eax
movb %al, 0x11(%r15)
movl 0x34(%rsp), %edi
movl 0xc(%rsp), %esi
callq 0x53cfe
addl $0x10, %eax
jmp 0x53760
movb $0x1, %cl
movl $0x240, %eax            # imm = 0x240
movl %eax, 0x28(%r15)
movl 0x10(%rsp), %eax
testb %bpl, %bpl
je 0x53726
xorl %eax, %eax
movq 0x38(%rsp), %rdx
testb $0x20, 0x48(%rdx)
jne 0x53726
testb %cl, %cl
je 0x5378d
testb $0x4, 0x63(%r13)
jne 0x5378d
movzwl 0x5c(%r13), %ebp
movq 0x38(%rsp), %rax
movzwl 0x46(%rax), %ebx
cmpw %bx, %bp
jge 0x5378d
movq 0x50(%rsp), %rax
testb $0x4, 0x32(%rax)
jne 0x5378d
cmpl $0x0, 0x2cba4(%rip)     # 0x802b0
je 0x5378d
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x40, 0x4c(%rax)
jne 0x5378d
movb $0x0, 0x11(%r15)
jmp 0x53741
movb %al, 0x11(%r15)
movq 0x40(%rsp), %rax
testb %cl, %cl
je 0x53760
movzwl 0x5c(%r13), %ebp
movq 0x38(%rsp), %rax
movzwl 0x46(%rax), %ebx
movl 0x34(%rsp), %edi
movl 0xc(%rsp), %esi
callq 0x53cfe
movl %eax, %ecx
movswl %bp, %eax
imull $0xf, %eax, %eax
movswl %bx, %esi
cltd
idivl %esi
addl %ecx, %eax
incl %eax
movw %ax, 0x14(%r15)
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x53d55
movq %r12, %rdi
movq %r15, %rsi
callq 0x539a0
movl %eax, %ebx
movq 0x28(%rsp), %rax
movw %ax, 0x16(%r15)
testl %ebx, %ebx
jne 0x537b8
movq %r12, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x53deb
movl %eax, %ebx
cmpq $0x0, 0x60(%r14)
jne 0x537b8
movq 0x28(%r13), %r13
incl 0x10(%rsp)
jmp 0x5351b
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x10, 0x2e(%rax)
movl $0x0, %ebx
je 0x53506
movl $0x0, %ebx
testq %r14, %r14
jne 0x53506
movq 0x20(%rsp), %rax
movb 0x39(%rax), %al
movl $0x0, %ebx
testb $0x5, %al
jne 0x53506
movq 0x38(%rsp), %rcx
movb 0x48(%rcx), %cl
andb $0x20, %cl
andb $0x2, %al
orb %cl, %al
movl $0x0, %ebx
jne 0x53506
movq 0x48(%rsp), %rax
movq 0x20(%rax), %rbp
movslq 0x14(%rax), %rax
imulq $0x38, %rax, %r14
addq %rbp, %r14
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rcx
leal (%rcx,%rax), %r12d
addl $0x1c, %r12d
xorl %ebx, %ebx
testl %ebx, %ebx
jne 0x53506
cmpq %r14, %rbp
jae 0x53506
movq 0x8(%r15), %rax
xorl %ebx, %ebx
testq %rax, 0x28(%rbp)
jne 0x538d8
xorl %ebx, %ebx
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x53cc5
testl %eax, %eax
je 0x538d8
movl $0x1, 0x18(%r15)
andq $0x0, 0x20(%r15)
movw $0x1, 0x2c(%r15)
movq 0x30(%r15), %rax
movq %rbp, (%rax)
movw %r12w, 0x12(%r15)
movw $0x2b, 0x16(%r15)
movl 0xc(%rsp), %edi
pushq $0x2b
popq %rsi
callq 0x53cfe
movw %ax, 0x14(%r15)
movl $0x4000, 0x28(%r15)     # imm = 0x4000
movq 0x28(%rbp), %rax
orq 0x60(%rsp), %rax
movq %rax, (%r15)
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x539a0
movl %eax, %ebx
addq $0x38, %rbp
jmp 0x5384b