// File: /workspace/github_c_makefile_star2_O3/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 $0xc8, %rsp
movq %rsi, -0x88(%rbp)
movq %rdi, %r13
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movq 0x18(%rdi), %rcx
movq %rcx, -0x50(%rbp)
movq (%rdi), %rax
movq %rax, -0x58(%rbp)
movq 0x8(%rax), %rax
movzbl 0x10(%rcx), %ecx
imulq $0x68, %rcx, %rcx
leaq (%rax,%rcx), %r14
addq $0x8, %r14
movq 0x60(%r14), %rbx
testq %rbx, %rbx
je 0x59be4
movq 0x20(%r14), %rax
movl 0x38(%rax), %edi
movq %rbx, %r12
jmp 0x59c5e
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %r12
movaps %xmm0, (%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, 0x40(%r12)
movaps %xmm0, 0x30(%r12)
movaps %xmm0, 0x20(%r12)
movq $0x0, 0x50(%r12)
movw $0x1, 0x54(%r12)
leaq -0x74(%rbp), %rax
movq %rax, 0x8(%r12)
leaq -0x90(%rbp), %rcx
movq %rcx, 0x10(%r12)
movb $0x5, 0x56(%r12)
movq 0x20(%r14), %rax
movq %rax, 0x18(%r12)
movl 0x38(%rax), %edi
movl %edi, (%rcx)
movl $0x1, 0x4(%rcx)
testb $0x1, 0x39(%r14)
jne 0x59c5e
movq 0x10(%rax), %rax
movq %rax, -0xc8(%rbp)
movq 0x8(%r13), %rax
movq %rax, -0x68(%rbp)
callq 0x5a316
movq %rax, -0x70(%rbp)
movzwl %ax, %edi
movl %edi, -0x3c(%rbp)
callq 0x5a316
xorl %ecx, %ecx
subw $0x21, %ax
cmovbl %ecx, %eax
cmpq $0x0, 0x20(%r13)
movq %rax, -0x48(%rbp)
movzwl %ax, %eax
movl %eax, -0x30(%rbp)
movq %r14, -0x60(%rbp)
je 0x59f34
movq -0x48(%rbp), %rax
addl -0x70(%rbp), %eax
movq %rax, -0x48(%rbp)
movl $0x1, -0x2c(%rbp)
movq %r13, -0x80(%rbp)
movq %r12, -0x38(%rbp)
movq 0x48(%r12), %r12
testq %r12, %r12
je 0x59d01
movq -0x68(%rbp), %rcx
movl 0x14(%rcx), %r15d
xorl %ebx, %ebx
testl %r15d, %r15d
jle 0x59cf8
movq -0x50(%rbp), %rax
movzbl 0x10(%rax), %r14d
movq 0x20(%rcx), %r13
movq (%r13), %rdi
movq %r12, %rsi
movl %r14d, %edx
callq 0x5aa20
testl %eax, %eax
jne 0x59d01
addq $0x38, %r13
decl %r15d
jne 0x59cdc
movq -0x38(%rbp), %r12
jmp 0x59f08
movq -0x50(%rbp), %r14
movl $0x0, 0x18(%r14)
xorl %r10d, %r10d
movw %r10w, 0x2c(%r14)
movb $0x0, 0x11(%r14)
movw %r10w, 0x12(%r14)
movq -0x88(%rbp), %rax
movq %rax, (%r14)
movq -0x70(%rbp), %rax
movw %ax, 0x16(%r14)
movq -0x38(%rbp), %r12
movq %r12, 0x20(%r14)
movw $0xa, %bx
movb $0x1, %al
testb $0x4, 0x57(%r12)
jne 0x59de3
movq -0x80(%rbp), %r13
movq (%r13), %rcx
movq 0x10(%rcx), %rsi
testq %rsi, %rsi
movq -0x60(%rbp), %r15
je 0x59deb
movslq (%rsi), %rcx
testq %rcx, %rcx
jle 0x59deb
movl 0x3c(%r15), %edx
movq 0x8(%rsi), %rsi
xorl %edi, %edi
movq %rdi, %r8
shlq $0x5, %r8
addq %rsi, %r8
movq (%r8), %r8
movzbl (%r8), %r9d
cmpl $0x18, %r9d
je 0x59d94
cmpl $0x5c, %r9d
jne 0x59d9a
addq $0x10, %r8
jmp 0x59d81
cmpl $0x98, %r9d
jne 0x59deb
cmpl %edx, 0x2c(%r8)
jne 0x59dd3
movzwl 0x54(%r12), %r9d
testq %r9, %r9
je 0x59dd3
movswl 0x30(%r8), %r8d
movq 0x8(%r12), %r10
xorl %r11d, %r11d
cmpl %r8d, (%r10,%r11,4)
je 0x59f26
incq %r11
cmpq %r11, %r9
jne 0x59dc1
incq %rdi
cmpq %rcx, %rdi
movl $0x0, %r10d
jne 0x59d77
jmp 0x59deb
movq -0x80(%rbp), %r13
movq -0x60(%rbp), %r15
cmpl $0x0, 0x50(%r12)
jle 0x59e2d
movzwl 0x54(%r12), %esi
testq %rsi, %rsi
je 0x59e57
movq 0x8(%r12), %rdi
incq %rsi
xorl %edx, %edx
movl -0x8(%rdi,%rsi,4), %ecx
movl $0x1, %r8d
shlq %cl, %r8
cmpl $0x3f, %ecx
cmovgeq %r10, %r8
orq %r8, %rdx
decq %rsi
cmpq $0x1, %rsi
ja 0x59e08
notq %rdx
jmp 0x59e5e
movl $0x100, 0x28(%r14)      # imm = 0x100
testb %al, %al
movl -0x2c(%rbp), %eax
cmovnel %r10d, %eax
movb %al, 0x11(%r14)
movl -0x3c(%rbp), %edi
movl -0x30(%rbp), %esi
callq 0x5a50e
addl $0x10, %eax
movw %ax, 0x14(%r14)
jmp 0x59ed3
movq $-0x1, %rdx
xorl %ecx, %ecx
andq 0x50(%r15), %rdx
sete %cl
orl $0x8, %ecx
shll $0x6, %ecx
movl %ecx, 0x28(%r14)
testb %al, %al
je 0x59eac
testq %rdx, %rdx
jne 0x59eeb
testb $0x4, 0x57(%r12)
jne 0x59eeb
movq -0x58(%rbp), %rax
testb $0x4, 0x32(%rax)
jne 0x59eeb
cmpl $0x0, 0x3a57d(%rip)     # 0x94410
je 0x59eeb
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x40, 0x4c(%rax)
jne 0x59eeb
movb $0x0, 0x11(%r14)
jmp 0x59ebe
movl -0x2c(%rbp), %eax
movb %al, 0x11(%r14)
movq -0x48(%rbp), %rax
movl %eax, %ecx
testq %rdx, %rdx
jne 0x59ece
movl -0x3c(%rbp), %edi
movl -0x30(%rbp), %esi
callq 0x5a50e
movzwl %bx, %ecx
addl %eax, %ecx
movw %cx, 0x14(%r14)
movq (%r13), %rdi
movq 0x20(%r13), %rsi
movq %r14, %rdx
callq 0x5a131
testl %eax, %eax
jne 0x5a046
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x5a56e
movl %eax, %ebx
cmpq $0x0, 0x60(%r15)
jne 0x5a048
testl %ebx, %ebx
jne 0x5a048
movq 0x28(%r12), %r12
incl -0x2c(%rbp)
testq %r12, %r12
jne 0x59cb2
jmp 0x5a048
movw $0x9, %bx
xorl %eax, %eax
xorl %r10d, %r10d
jmp 0x59deb
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
testb $0x10, 0x2e(%rax)
je 0x59c9c
testq %rbx, %rbx
jne 0x59c9c
testb $0x7, 0x39(%r14)
jne 0x59c9c
movq -0x68(%rbp), %rax
movslq 0x14(%rax), %rax
testq %rax, %rax
jle 0x59c9c
movq %r12, -0x38(%rbp)
movq -0x68(%rbp), %rcx
movq 0x20(%rcx), %r12
imulq $0x38, %rax, %r15
addq %r12, %r15
movq -0x70(%rbp), %rax
movq -0x48(%rbp), %rcx
addl %ecx, %eax
addl $0x1c, %eax
movl %eax, -0x2c(%rbp)
addq $0x38, %r12
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
xorl %ebx, %ebx
testq %rax, -0x10(%r12)
jne 0x5a024
leaq -0x38(%r12), %r14
xorl %ebx, %ebx
movq %r14, %rdi
movq -0x60(%rbp), %rsi
xorl %edx, %edx
callq 0x5a4a4
testl %eax, %eax
je 0x5a024
movq -0x50(%rbp), %rbx
movl $0x1, 0x18(%rbx)
movq $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 -0x30(%rbp), %edi
movl $0x2b, %esi
callq 0x5a50e
movw %ax, 0x14(%rbx)
movl $0x4000, 0x28(%rbx)     # imm = 0x4000
movq -0x10(%r12), %rax
orq -0x88(%rbp), %rax
movq %rax, (%rbx)
movq (%r13), %rdi
movq 0x20(%r13), %rsi
movq %rbx, %rdx
callq 0x5a131
movl %eax, %ebx
testl %ebx, %ebx
jne 0x5a039
leaq 0x38(%r12), %rax
cmpq %r15, %r12
movq %rax, %r12
jb 0x59f94
testl %ebx, %ebx
movq -0x38(%rbp), %r12
jne 0x5a048
jmp 0x59c9c
movl %eax, %ebx
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq