// File: /workspace/github_c_makefile_star2_O3/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 $0xc8, %rsp
movq %rsi, 0x50(%rsp)
movq %rdi, %r15
movw $0xffff, 0xe(%rsp)      # imm = 0xFFFF
movq 0x18(%rdi), %r14
movq (%rdi), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rax), %rax
movzbl 0x10(%r14), %ecx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
addq $0x8, %rax
movq 0x20(%rax), %rdx
movq %rax, 0x10(%rsp)
movq 0x60(%rax), %rbx
movq %rbx, %r12
testq %rbx, %rbx
jne 0x5d32f
testb $0x20, 0x48(%rdx)
jne 0x5d32b
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %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, 0x5e(%r12)
leaq 0xe(%rsp), %rax
movq %rax, 0x8(%r12)
leaq 0x58(%rsp), %rax
movq %rax, 0x10(%r12)
movb $0x5, 0x62(%r12)
movq %rdx, 0x18(%r12)
movl 0x38(%rdx), %ecx
movl %ecx, (%rax)
movl $0x1, 0x4(%rax)
movq 0x10(%rsp), %rax
testb $0x1, 0x39(%rax)
jne 0x5d32f
movq 0x10(%rdx), %rax
movq %rax, 0x88(%rsp)
jmp 0x5d32f
movq 0x10(%rdx), %r12
movq 0x8(%r15), %rax
movq %rax, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movl 0x38(%rdx), %edi
callq 0x357b3
movq %rax, 0x18(%rsp)
movswq %ax, %rdi
callq 0x357b3
leal -0x21(%rax), %ecx
xorl %ebp, %ebp
cmpw $0x22, %ax
cmovll %ebp, %ecx
cmpq $0x0, 0x20(%r15)
movq %rcx, 0x30(%rsp)
movswl %cx, %eax
movl %eax, 0x8(%rsp)
movq %r14, 0x38(%rsp)
movq %r12, %r14
je 0x5d6f4
testl %ebp, %ebp
jne 0x5d833
testq %r14, %r14
je 0x5d833
movq %r14, %r12
movq 0x30(%rsp), %rax
addl 0x18(%rsp), %eax
movq %rax, 0x30(%rsp)
movl $0x1, %r13d
movq %r15, 0x48(%rsp)
movl %r13d, 0x4(%rsp)
movq %r12, %r13
movq 0x48(%r12), %r12
testq %r12, %r12
je 0x5d401
movq 0x20(%rsp), %rcx
movl 0x14(%rcx), %r15d
xorl %ebp, %ebp
testl %r15d, %r15d
jle 0x5d3f4
movq 0x38(%rsp), %rax
movzbl 0x10(%rax), %ebx
movq 0x20(%rcx), %r14
movq (%r14), %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x5e365
testl %eax, %eax
jne 0x5d401
addq $0x38, %r14
decl %r15d
jne 0x5d3da
movq %r13, %r12
movl 0x4(%rsp), %r13d
jmp 0x5d6d6
movq 0x38(%rsp), %r14
movl $0x0, 0x18(%r14)
xorl %ebp, %ebp
movw %bp, 0x2c(%r14)
movb $0x0, 0x11(%r14)
movw %bp, 0x12(%r14)
movq 0x50(%rsp), %rax
movq %rax, (%r14)
movq 0x18(%rsp), %rax
movw %ax, 0x16(%r14)
movq %r13, %r12
movq %r13, 0x20(%r14)
movb 0x63(%r13), %cl
movb $0x1, %al
testb $0x4, %cl
jne 0x5d4e8
movq 0x48(%rsp), %r15
movq (%r15), %rdx
movq 0x10(%rdx), %rdi
testq %rdi, %rdi
movl 0x4(%rsp), %r13d
je 0x5d4f6
movslq (%rdi), %rdx
testq %rdx, %rdx
jle 0x5d4f6
movq 0x10(%rsp), %rsi
movl 0x3c(%rsi), %esi
movq 0x8(%rdi), %rdi
xorl %r8d, %r8d
movq %r8, %r9
shlq $0x5, %r9
movq (%rdi,%r9), %r9
movl 0x4(%r9), %r10d
btl $0xc, %r10d
jae 0x5d4ac
btl $0x12, %r10d
jb 0x5d49f
addq $0x10, %r9
jmp 0x5d4a7
movq 0x20(%r9), %r9
movq 0x8(%r9), %r9
movq (%r9), %r9
jmp 0x5d487
cmpb $-0x67, (%r9)
jne 0x5d4f6
cmpl %esi, 0x2c(%r9)
jne 0x5d4de
movzwl 0x5e(%r12), %r10d
testq %r10, %r10
je 0x5d4de
movzwl 0x30(%r9), %r9d
movq 0x8(%r12), %r11
xorl %ebx, %ebx
cmpw (%r11,%rbx,2), %r9w
je 0x5d4f4
incq %rbx
cmpq %rbx, %r10
jne 0x5d4cf
incq %r8
cmpq %rdx, %r8
jne 0x5d47c
jmp 0x5d4f6
movq 0x48(%rsp), %r15
movl 0x4(%rsp), %r13d
jmp 0x5d4f6
xorl %eax, %eax
cmpl $0x0, 0x58(%r12)
jle 0x5d547
testb $0x20, %cl
jne 0x5d5a2
movzwl 0x60(%r12), %edx
testq %rdx, %rdx
je 0x5d5ab
movq 0x8(%r12), %rdi
incq %rdx
xorl %esi, %esi
movzwl -0x4(%rdi,%rdx,2), %ecx
movl $0x1, %r8d
shlq %cl, %r8
cmpw $0x3f, %cx
cmovaeq %rbp, %r8
orq %r8, %rsi
decq %rdx
cmpq $0x1, %rdx
ja 0x5d520
notq %rsi
jmp 0x5d5b2
movl $0x100, 0x28(%r14)      # imm = 0x100
testb %al, %al
movl %r13d, %eax
cmovnel %ebp, %eax
movb %al, 0x11(%r14)
movq 0x18(%rsp), %rbx
movswl %bx, %edi
movl 0x8(%rsp), %esi
callq 0x5dc9d
addl $0x10, %eax
movw %ax, 0x14(%r14)
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x5dcf4
movq (%r15), %rdi
movq 0x20(%r15), %rsi
movq %r14, %rdx
callq 0x5d908
movw %bx, 0x16(%r14)
testl %eax, %eax
je 0x5d6b4
jmp 0x5d831
movb $0x1, %cl
movl $0x240, %edx            # imm = 0x240
jmp 0x5d5c8
movq $-0x1, %rsi
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
testq %rsi, 0x50(%rdx)
sete %cl
movl %ecx, %edx
orl $0x8, %edx
shll $0x6, %edx
movl %edx, 0x28(%r14)
movl %r13d, %edx
testb %al, %al
je 0x5d640
xorl %edx, %edx
movq 0x28(%rsp), %rax
testb $0x20, 0x48(%rax)
jne 0x5d640
testb %cl, %cl
je 0x5d6b4
testb $0x4, 0x63(%r12)
jne 0x5d6b4
movzwl 0x5c(%r12), %ebp
movq 0x28(%rsp), %rax
movzwl 0x46(%rax), %ebx
cmpw %bx, %bp
jge 0x5d6b4
movq 0x40(%rsp), %rax
testb $0x4, 0x32(%rax)
jne 0x5d6b4
cmpl $0x0, 0x2cc7e(%rip)     # 0x8a2a0
je 0x5d6b4
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x40, 0x4c(%rax)
jne 0x5d6b4
movb $0x0, 0x11(%r14)
jmp 0x5d65c
movb %dl, 0x11(%r14)
movq 0x30(%rsp), %rax
testb %cl, %cl
je 0x5d67f
movzwl 0x5c(%r12), %ebp
movq 0x28(%rsp), %rax
movzwl 0x46(%rax), %ebx
movswl 0x18(%rsp), %edi
movl 0x8(%rsp), %esi
callq 0x5dc9d
movl %eax, %ecx
movswl %bp, %eax
leal (%rax,%rax,4), %eax
leal (%rax,%rax,2), %eax
movswl %bx, %esi
cltd
idivl %esi
addl %ecx, %eax
incl %eax
movw %ax, 0x14(%r14)
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x5dcf4
movq (%r15), %rdi
movq 0x20(%r15), %rsi
movq %r14, %rdx
callq 0x5d908
movl %eax, %ebp
movq 0x18(%rsp), %rax
movw %ax, 0x16(%r14)
testl %ebp, %ebp
jne 0x5d833
movq %r15, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x5dd9a
movl %eax, %ebp
cmpq $0x0, 0x60(%rbx)
jne 0x5d833
testl %ebp, %ebp
jne 0x5d833
movq 0x28(%r12), %r12
incl %r13d
testq %r12, %r12
jne 0x5d3ab
jmp 0x5d833
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x10, 0x2e(%rax)
je 0x5d37e
testq %rbx, %rbx
movq 0x28(%rsp), %rcx
jne 0x5d37e
movq 0x10(%rsp), %rax
movb 0x39(%rax), %al
testb $0x5, %al
jne 0x5d37e
movb 0x48(%rcx), %cl
andb $0x20, %cl
andb $0x2, %al
orb %cl, %al
jne 0x5d37e
movq 0x20(%rsp), %rax
movslq 0x14(%rax), %rax
testq %rax, %rax
jle 0x5d37e
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %r12
imulq $0x38, %rax, %r13
addq %r12, %r13
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
addl %ecx, %eax
addl $0x1c, %eax
movl %eax, 0x4(%rsp)
addq $0x38, %r12
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
xorl %ebp, %ebp
testq %rax, -0x10(%r12)
jne 0x5d813
leaq -0x38(%r12), %rbx
xorl %ebp, %ebp
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x5dc3d
testl %eax, %eax
je 0x5d813
movq %r13, %rbp
movq 0x38(%rsp), %r13
movl $0x1, 0x18(%r13)
movq $0x0, 0x20(%r13)
movw $0x1, 0x2c(%r13)
movq 0x30(%r13), %rax
movq %rbx, (%rax)
movl 0x4(%rsp), %eax
movw %ax, 0x12(%r13)
movw $0x2b, 0x16(%r13)
movl 0x8(%rsp), %edi
movl $0x2b, %esi
callq 0x5dc9d
movw %ax, 0x14(%r13)
movl $0x4000, 0x28(%r13)     # imm = 0x4000
movq -0x10(%r12), %rax
orq 0x50(%rsp), %rax
movq %rax, (%r13)
movq (%r15), %rdi
movq 0x20(%r15), %rsi
movq %r13, %rdx
movq %rbp, %r13
callq 0x5d908
movl %eax, %ebp
testl %ebp, %ebp
jne 0x5d37e
leaq 0x38(%r12), %rax
cmpq %r13, %r12
movq %rax, %r12
jb 0x5d770
jmp 0x5d37e
movl %eax, %ebp
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq