// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xffffffff, -0x84(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xa4(%rbp)
movl $0x1, -0xa8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
addq $0x8, %rax
movq -0xa0(%rbp), %rcx
movzbl 0x10(%rcx), %ecx
movslq %ecx, %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x98(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x78002
movq -0x98(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x78093
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x58, %edx
callq 0x8200
movw $0x1, -0x24(%rbp)
leaq -0x84(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
movb $0x5, -0x22(%rbp)
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x38(%rax), %eax
movl %eax, -0x80(%rbp)
movl $0x1, -0x7c(%rbp)
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x98(%rbp), %rax
movb 0x39(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7808b
movq -0xc0(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x38(%rax), %edi
callq 0x794f0
movw %ax, -0xae(%rbp)
movzwl -0xae(%rbp), %edi
callq 0x798f0
movw %ax, -0xb0(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x782e4
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movl 0x2c(%rax), %eax
andl $0x100000, %eax         # imm = 0x100000
cmpl $0x0, %eax
je 0x782e4
movq -0x98(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x782e4
movq -0x98(%rbp), %rax
movb 0x39(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x782e4
movq -0x98(%rbp), %rax
movb 0x39(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x782e4
movq -0x98(%rbp), %rax
movb 0x39(%rax), %al
shrb %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x782e4
movq -0xb8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xb8(%rbp), %rcx
movslq 0x14(%rcx), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xb8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xc8(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xa4(%rbp)
movb %al, -0xd9(%rbp)
jne 0x781a7
movq -0xc8(%rbp), %rax
cmpq -0xd0(%rbp), %rax
setb %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x781b6
jmp 0x782e2
movq -0xc8(%rbp), %rax
movq 0x28(%rax), %rax
movq -0xa0(%rbp), %rcx
andq 0x8(%rcx), %rax
cmpq $0x0, %rax
je 0x781d7
jmp 0x782cb
movq -0xc8(%rbp), %rdi
movq -0x98(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x79940
cmpl $0x0, %eax
je 0x782c9
movq -0xa0(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0xa0(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xa0(%rbp), %rax
movw $0x1, 0x2c(%rax)
movq -0xc8(%rbp), %rcx
movq -0xa0(%rbp), %rax
movq 0x30(%rax), %rax
movq %rcx, (%rax)
movzwl -0xb0(%rbp), %eax
movzwl -0xae(%rbp), %ecx
addl %ecx, %eax
addl $0x1c, %eax
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x12(%rax)
movq -0xa0(%rbp), %rax
movw $0x2b, 0x16(%rax)
movw -0xb0(%rbp), %cx
movq -0xa0(%rbp), %rax
movzwl %cx, %edi
movzwl 0x16(%rax), %esi
callq 0x79a10
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0xa0(%rbp), %rax
movl $0x4000, 0x28(%rax)     # imm = 0x4000
movq -0x10(%rbp), %rcx
movq -0xc8(%rbp), %rax
orq 0x28(%rax), %rcx
movq -0xa0(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x79260
movl %eax, -0xa4(%rbp)
jmp 0x782cb
movq -0xc8(%rbp), %rax
addq $0x38, %rax
movq %rax, -0xc8(%rbp)
jmp 0x7817f
jmp 0x782e4
jmp 0x782e6
xorl %eax, %eax
cmpl $0x0, -0xa4(%rbp)
movb %al, -0xda(%rbp)
jne 0x78305
cmpq $0x0, -0x20(%rbp)
setne %al
movb %al, -0xda(%rbp)
movb -0xda(%rbp), %al
testb $0x1, %al
jne 0x78314
jmp 0x78628
movq -0x20(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x78348
movq -0xa0(%rbp), %rax
movzbl 0x10(%rax), %edi
movq -0xb8(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdx
callq 0x79af0
cmpl $0x0, %eax
jne 0x78348
jmp 0x78608
movq -0xa0(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0xa0(%rbp), %rax
movw $0x0, 0x2c(%rax)
movq -0xa0(%rbp), %rax
movb $0x0, 0x11(%rax)
movq -0xa0(%rbp), %rax
movw $0x0, 0x12(%rax)
movq -0x10(%rbp), %rcx
movq -0xa0(%rbp), %rax
movq %rcx, (%rax)
movw -0xae(%rbp), %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x16(%rax)
movq -0x20(%rbp), %rcx
movq -0xa0(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x98(%rbp), %rax
movl 0x3c(%rax), %edx
callq 0x79b70
movl %eax, -0xac(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jg 0x78469
movq -0xa0(%rbp), %rax
movl $0x100, 0x28(%rax)      # imm = 0x100
cmpl $0x0, -0xac(%rbp)
je 0x783fa
movl -0xa8(%rbp), %eax
movl %eax, -0xe0(%rbp)
jmp 0x78404
xorl %eax, %eax
movl %eax, -0xe0(%rbp)
jmp 0x78404
movl -0xe0(%rbp), %eax
movb %al, %cl
movq -0xa0(%rbp), %rax
movb %cl, 0x11(%rax)
movw -0xae(%rbp), %ax
movzwl %ax, %edi
movzwl -0xb0(%rbp), %esi
callq 0x79a10
movzwl %ax, %eax
addl $0x10, %eax
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x79260
movl %eax, -0xa4(%rbp)
cmpl $0x0, -0xa4(%rbp)
je 0x78464
jmp 0x78628
jmp 0x785da
movq -0x98(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0xe8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x77880
movq %rax, %rcx
movq -0xe8(%rbp), %rax
xorq $-0x1, %rcx
andq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdx
movl $0x200, %ecx            # imm = 0x200
movl $0x240, %eax            # imm = 0x240
cmpq $0x0, %rdx
cmovel %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, 0x28(%rax)
cmpl $0x0, -0xac(%rbp)
jne 0x78528
cmpq $0x0, -0xd8(%rbp)
jne 0x785d8
movq -0x20(%rbp), %rax
movb 0x57(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x785d8
movq -0x18(%rbp), %rax
movzwl 0x32(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x785d8
cmpl $0x0, 0x57f98(%rip)     # 0xd04a0
je 0x785d8
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movzwl 0x4c(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0x785d8
cmpl $0x0, -0xac(%rbp)
je 0x7853f
movl -0xa8(%rbp), %eax
movl %eax, -0xec(%rbp)
jmp 0x78549
xorl %eax, %eax
movl %eax, -0xec(%rbp)
jmp 0x78549
movl -0xec(%rbp), %eax
movb %al, %cl
movq -0xa0(%rbp), %rax
movb %cl, 0x11(%rax)
cmpq $0x0, -0xd8(%rbp)
jne 0x78597
movw -0xae(%rbp), %ax
movzwl %ax, %edi
movzwl -0xb0(%rbp), %esi
callq 0x79a10
movzwl %ax, %eax
addl $0xa, %eax
subl -0xac(%rbp), %eax
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x14(%rax)
jmp 0x785b5
movzwl -0xae(%rbp), %eax
movzwl -0xb0(%rbp), %ecx
addl %ecx, %eax
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x79260
movl %eax, -0xa4(%rbp)
cmpl $0x0, -0xa4(%rbp)
je 0x785d6
jmp 0x78628
jmp 0x785d8
jmp 0x785da
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0x79c90
movl %eax, -0xa4(%rbp)
movq -0x98(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x78606
jmp 0x78628
jmp 0x78608
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x782e6
movl -0xa4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)