// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/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 */
  int 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 */
  WhereCost rSize;            /* number of rows in the table */
  WhereCost rLogSize;         /* Logarithm of the number of rows in the table */
  WhereClause *pWC;           /* The parsed WHERE clause */
  
  pNew = pBuilder->pNew;
  pWInfo = pBuilder->pWInfo;
  pTabList = pWInfo->pTabList;
  pSrc = pTabList->a + pNew->iTab;
  pWC = pBuilder->pWC;
  assert( !IsVirtual(pSrc->pTab) );

  if( pSrc->pIndex ){
    /* An INDEXED BY clause specifies a particular index to use */
    pProbe = pSrc->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.nColumn = 1;
    sPk.aiColumn = &aiColumnPk;
    sPk.aiRowEst = aiRowEstPk;
    sPk.onError = OE_Replace;
    sPk.pTable = pSrc->pTab;
    aiRowEstPk[0] = pSrc->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 = whereCost(pSrc->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
   && !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.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==whereCost(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==whereCost(20) );
        pNew->rRun = whereCostAdd(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->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.  A smaller constant 2 is used for covering
      **     index scans so that a covering index scan will be favored over
      **     a table scan. */
      pNew->rRun = whereCostAdd(rSize,rLogSize) + 16;
      rc = whereLoopInsert(pBuilder, pNew);
      if( rc ) break;
    }else{
      Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe);
      pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;

      /* Full scan via index */
      if( b
       || ( m==0
         && pProbe->bUnordered==0
         && (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 2*(N + log2(N)).
          **  +  The extra 2 factor is to encourage the use of indexed lookups
          **     over index scans.  A table scan uses a factor of 3 so that
          **     index scans are favored over table scans.
          **  +  If this covering index might also help satisfy the ORDER BY
          **     clause, then the cost is fudged down slightly so that this
          **     index is favored above other indices that have no hope of
          **     helping with the ORDER BY. */
          pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b;
        }else{
          assert( b!=0 ); 
          /* 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;
        }
        rc = whereLoopInsert(pBuilder, pNew);
        if( rc ) break;
      }
    }
    rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, -0x80(%rbp)
movq %rdi, %r15
orl $-0x1, -0x70(%rbp)
movq 0x18(%rdi), %rcx
movq %rcx, -0x40(%rbp)
movq (%rdi), %rax
movq %rax, -0x60(%rbp)
movq 0x8(%rax), %rax
movzbl 0x10(%rcx), %ecx
imulq $0x68, %rcx, %rcx
leaq (%rax,%rcx), %rdx
addq $0x8, %rdx
movq 0x60(%rdx), %r14
testq %r14, %r14
movq %rdx, -0x38(%rbp)
je 0x50e93
movq 0x20(%rdx), %rax
movl 0x38(%rax), %edi
movq %r14, %r13
jmp 0x50eff
xorps %xmm0, %xmm0
leaq -0xe0(%rbp), %r13
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, 0x40(%r13)
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x20(%r13)
andq $0x0, 0x50(%r13)
movw $0x1, 0x54(%r13)
leaq -0x70(%rbp), %rax
movq %rax, 0x8(%r13)
leaq -0x88(%rbp), %rcx
movq %rcx, 0x10(%r13)
movb $0x5, 0x56(%r13)
movq 0x20(%rdx), %rax
movq %rax, 0x18(%r13)
movl 0x38(%rax), %edi
movl %edi, (%rcx)
movl $0x1, 0x4(%rcx)
testb $0x1, 0x39(%rdx)
jne 0x50eff
movq 0x10(%rax), %rax
movq %rax, -0xb8(%rbp)
movq 0x8(%r15), %rax
movq %rax, -0x78(%rbp)
callq 0x5154b
movq %rax, -0x68(%rbp)
movzwl %ax, %edi
movl %edi, -0x48(%rbp)
callq 0x516bf
cmpq $0x0, 0x20(%r15)
movq %rax, -0x50(%rbp)
movzwl %ax, %eax
movl %eax, -0x44(%rbp)
movl $0x0, %ebx
je 0x5119d
movq -0x50(%rbp), %rax
addl -0x68(%rbp), %eax
movq %rax, -0x50(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
movq %r15, -0x58(%rbp)
testl %ebx, %ebx
jne 0x51294
testq %r13, %r13
je 0x51294
movq 0x48(%r13), %rbx
testq %rbx, %rbx
je 0x50fa8
movq -0x40(%rbp), %rax
movzbl 0x10(%rax), %r14d
movq -0x78(%rbp), %rax
movq 0x20(%rax), %r12
movl 0x14(%rax), %r15d
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
subl $0x1, %r15d
jb 0x5106a
movq (%r12), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x51c2d
testl %eax, %eax
jne 0x50fa8
addq $0x38, %r12
jmp 0x50f85
movq -0x40(%rbp), %r14
andl $0x0, 0x18(%r14)
andw $0x0, 0x2c(%r14)
movb $0x0, 0x11(%r14)
andw $0x0, 0x12(%r14)
movq -0x80(%rbp), %rax
movq %rax, (%r14)
movq -0x68(%rbp), %rax
movw %ax, 0x16(%r14)
movq %r13, 0x20(%r14)
movw $0xa, -0x2e(%rbp)
movb $0x1, %al
testb $0x4, 0x57(%r13)
jne 0x51064
movq -0x58(%rbp), %r15
movq (%r15), %rax
movq 0x10(%rax), %rbx
testq %rbx, %rbx
movq -0x38(%rbp), %r12
je 0x5108b
movl 0x3c(%r12), %eax
movl %eax, -0x6c(%rbp)
movl (%rbx), %r14d
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
xorl %r12d, %r12d
cmpq %r14, %r12
je 0x51083
movq 0x8(%rbx), %rax
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rdi
callq 0x45e0b
cmpb $-0x68, (%rax)
jne 0x51083
movl -0x6c(%rbp), %ecx
cmpl %ecx, 0x2c(%rax)
jne 0x5105f
movzwl 0x54(%r13), %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x5105f
movswl 0x30(%rax), %esi
movq 0x8(%r13), %rdi
leaq 0x1(%rdx), %r8
cmpl %esi, (%rdi,%rdx,4)
movq %r8, %rdx
jne 0x51044
jmp 0x51071
incq %r12
jmp 0x51017
movq -0x58(%rbp), %r15
jmp 0x5107d
xorl %ebx, %ebx
jmp 0x51191
xorl %eax, %eax
movw $0x9, -0x2e(%rbp)
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r12
jmp 0x5108d
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r12
movb $0x1, %al
cmpl $0x0, 0x50(%r13)
jle 0x51108
movq 0x50(%r12), %rbx
movq %r13, %rdi
movl %eax, %r15d
callq 0x5079b
notq %rax
xorl %ecx, %ecx
andq %rbx, %rax
sete %cl
orl $0x8, %ecx
shll $0x6, %ecx
movl %ecx, 0x28(%r14)
testb %r15b, %r15b
je 0x51136
testq %rax, %rax
movq -0x58(%rbp), %r15
jne 0x51173
testb $0x4, 0x57(%r13)
jne 0x51173
movq -0x60(%rbp), %rax
testb $0x4, 0x32(%rax)
jne 0x51173
cmpl $0x0, 0x36325(%rip)     # 0x87410
je 0x51173
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x40, 0x4c(%rax)
jne 0x51173
movb $0x0, 0x11(%r14)
jmp 0x5114a
movl $0x100, 0x28(%r14)      # imm = 0x100
testb %al, %al
movl -0x2c(%rbp), %eax
movl $0x0, %ecx
cmovnel %ecx, %eax
movb %al, 0x11(%r14)
movl -0x48(%rbp), %edi
movl -0x44(%rbp), %esi
callq 0x51712
addl $0x10, %eax
movw %ax, 0x14(%r14)
jmp 0x51160
movl -0x2c(%rbp), %ecx
movb %cl, 0x11(%r14)
movq -0x50(%rbp), %rcx
testq %rax, %rax
movq -0x58(%rbp), %r15
jne 0x5115b
movl -0x48(%rbp), %edi
movl -0x44(%rbp), %esi
callq 0x51712
movzwl -0x2e(%rbp), %ecx
addl %eax, %ecx
movw %cx, 0x14(%r14)
movq %r15, %rdi
movq %r14, %rsi
callq 0x5137b
testl %eax, %eax
jne 0x51292
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x51772
movl %eax, %ebx
cmpq $0x0, 0x60(%r12)
jne 0x51294
movq 0x28(%r13), %r13
incl -0x2c(%rbp)
jmp 0x50f4a
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x10, 0x2e(%rax)
movl $0x0, %ebx
je 0x50f35
movl $0x0, %ebx
testq %r14, %r14
jne 0x50f35
movq -0x38(%rbp), %rax
testb $0x7, 0x39(%rax)
movl $0x0, %ebx
jne 0x50f35
movq -0x78(%rbp), %rax
movq 0x20(%rax), %r14
movslq 0x14(%rax), %rax
imulq $0x38, %rax, %r12
addq %r14, %r12
movq -0x68(%rbp), %rax
movq -0x50(%rbp), %rcx
addl %ecx, %eax
addl $0x1c, %eax
movl %eax, -0x2c(%rbp)
xorl %ebx, %ebx
testl %ebx, %ebx
jne 0x50f35
cmpq %r12, %r14
jae 0x50f35
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
xorl %ebx, %ebx
testq %rax, 0x28(%r14)
jne 0x51289
xorl %ebx, %ebx
movq %r14, %rdi
movq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0x516d3
testl %eax, %eax
je 0x51289
movq -0x40(%rbp), %rbx
movl $0x1, 0x18(%rbx)
andq $0x0, 0x20(%rbx)
movw $0x1, 0x2c(%rbx)
movq 0x30(%rbx), %rax
movq %r14, (%rax)
movl -0x2c(%rbp), %eax
movw %ax, 0x12(%rbx)
movw $0x2b, 0x16(%rbx)
movl -0x44(%rbp), %edi
pushq $0x2b
popq %rsi
callq 0x51712
movw %ax, 0x14(%rbx)
movl $0x4000, 0x28(%rbx)     # imm = 0x4000
movq 0x28(%r14), %rax
orq -0x80(%rbp), %rax
movq %rax, (%rbx)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5137b
movl %eax, %ebx
addq $0x38, %r14
jmp 0x511fc
movl %eax, %ebx
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq