// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movw $0xffff, -0x92(%rbp)    # imm = 0xFFFF
movl $0x0, -0xb4(%rbp)
movl $0x1, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
addq $0x8, %rax
movq -0xb0(%rbp), %rcx
movzbl 0x10(%rcx), %ecx
movslq %ecx, %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0xa8(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x7ece3
movq -0xa8(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x7eda4
movq -0xd0(%rbp), %rax
movzbl 0x48(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x7ed0a
movq -0xd0(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x7eda2
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x71a0
movw $0x1, -0x2a(%rbp)
leaq -0x92(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0x78(%rbp)
movb $0x5, -0x26(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xd0(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x90(%rbp)
movl $0x1, -0x8c(%rbp)
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0xd8(%rbp)
movq -0xa8(%rbp), %rax
movb 0x39(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7ed97
movq -0xd8(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x7eda4
movq -0xd0(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, %edi
callq 0x4b340
movw %ax, -0xbe(%rbp)
movswl -0xbe(%rbp), %edi
callq 0x806a0
movw %ax, -0xc0(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x7f016
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movl 0x2c(%rax), %eax
andl $0x100000, %eax         # imm = 0x100000
cmpl $0x0, %eax
je 0x7f016
movq -0xa8(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x7f016
movq -0xa8(%rbp), %rax
movb 0x39(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7f016
movq -0xa8(%rbp), %rax
movb 0x39(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7f016
movq -0xd0(%rbp), %rax
movzbl 0x48(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x7f016
movq -0xa8(%rbp), %rax
movb 0x39(%rax), %al
shrb %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7f016
movq -0xc8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xc8(%rbp), %rcx
movslq 0x14(%rcx), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xe8(%rbp)
movq -0xc8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xe0(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xb4(%rbp)
movb %al, -0xf1(%rbp)
jne 0x7eecd
movq -0xe0(%rbp), %rax
cmpq -0xe8(%rbp), %rax
setb %al
movb %al, -0xf1(%rbp)
movb -0xf1(%rbp), %al
testb $0x1, %al
jne 0x7eedc
jmp 0x7f014
movq -0xe0(%rbp), %rax
movq 0x28(%rax), %rax
movq -0xb0(%rbp), %rcx
andq 0x8(%rcx), %rax
cmpq $0x0, %rax
je 0x7eefd
jmp 0x7effd
movq -0xe0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x806f0
cmpl $0x0, %eax
je 0x7effb
movq -0xb0(%rbp), %rax
movw $0x1, 0x18(%rax)
movq -0xb0(%rbp), %rax
movw $0x0, 0x1a(%rax)
movq -0xb0(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xb0(%rbp), %rax
movw $0x1, 0x2c(%rax)
movq -0xe0(%rbp), %rcx
movq -0xb0(%rbp), %rax
movq 0x30(%rax), %rax
movq %rcx, (%rax)
movswl -0xc0(%rbp), %eax
movswl -0xbe(%rbp), %ecx
addl %ecx, %eax
addl $0x1c, %eax
movw %ax, %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x12(%rax)
movq -0xb0(%rbp), %rax
movw $0x2b, 0x16(%rax)
movw -0xc0(%rbp), %cx
movq -0xb0(%rbp), %rax
movswl %cx, %edi
movswl 0x16(%rax), %esi
callq 0x807c0
movw %ax, %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0xb0(%rbp), %rax
movl $0x4000, 0x28(%rax)     # imm = 0x4000
movq -0x10(%rbp), %rcx
movq -0xe0(%rbp), %rax
orq 0x28(%rax), %rcx
movq -0xb0(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x80060
movl %eax, -0xb4(%rbp)
jmp 0x7effd
movq -0xe0(%rbp), %rax
addq $0x38, %rax
movq %rax, -0xe0(%rbp)
jmp 0x7eea5
jmp 0x7f016
jmp 0x7f018
xorl %eax, %eax
cmpl $0x0, -0xb4(%rbp)
movb %al, -0xf2(%rbp)
jne 0x7f037
cmpq $0x0, -0x20(%rbp)
setne %al
movb %al, -0xf2(%rbp)
movb -0xf2(%rbp), %al
testb $0x1, %al
jne 0x7f046
jmp 0x7f430
movq -0x20(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x7f07a
movq -0xb0(%rbp), %rax
movzbl 0x10(%rax), %edi
movq -0xc8(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdx
callq 0x808a0
cmpl $0x0, %eax
jne 0x7f07a
jmp 0x7f410
movq -0xb0(%rbp), %rax
movw $0x0, 0x18(%rax)
movq -0xb0(%rbp), %rax
movw $0x0, 0x1a(%rax)
movq -0xb0(%rbp), %rax
movw $0x0, 0x2c(%rax)
movq -0xb0(%rbp), %rax
movb $0x0, 0x11(%rax)
movq -0xb0(%rbp), %rax
movw $0x0, 0x12(%rax)
movq -0x10(%rbp), %rcx
movq -0xb0(%rbp), %rax
movq %rcx, (%rax)
movw -0xbe(%rbp), %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x16(%rax)
movq -0x20(%rbp), %rcx
movq -0xb0(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0xa8(%rbp), %rax
movl 0x3c(%rax), %edx
callq 0x80920
movl %eax, -0xbc(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x58(%rax)
jg 0x7f1ca
movq -0xb0(%rbp), %rax
movl $0x100, 0x28(%rax)      # imm = 0x100
cmpl $0x0, -0xbc(%rbp)
je 0x7f138
movl -0xb8(%rbp), %eax
movl %eax, -0xf8(%rbp)
jmp 0x7f142
xorl %eax, %eax
movl %eax, -0xf8(%rbp)
jmp 0x7f142
movl -0xf8(%rbp), %eax
movb %al, %cl
movq -0xb0(%rbp), %rax
movb %cl, 0x11(%rax)
movw -0xbe(%rbp), %ax
movswl %ax, %edi
movswl -0xc0(%rbp), %esi
callq 0x807c0
cwtl
addl $0x10, %eax
movw %ax, %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0xc8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x80a40
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x80060
movl %eax, -0xb4(%rbp)
movw -0xbe(%rbp), %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x16(%rax)
cmpl $0x0, -0xb4(%rbp)
je 0x7f1c5
jmp 0x7f430
jmp 0x7f3e2
movq -0x20(%rbp), %rax
movb 0x63(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x7f1f9
movq -0xb0(%rbp), %rax
movl $0x240, 0x28(%rax)      # imm = 0x240
movq $0x0, -0xf0(%rbp)
jmp 0x7f24e
movq -0xa8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x20(%rbp), %rdi
callq 0x7e510
movq %rax, %rcx
movq -0x100(%rbp), %rax
xorq $-0x1, %rcx
andq %rcx, %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rdx
movl $0x200, %ecx            # imm = 0x200
movl $0x240, %eax            # imm = 0x240
cmpq $0x0, %rdx
cmovel %eax, %ecx
movq -0xb0(%rbp), %rax
movl %ecx, 0x28(%rax)
cmpl $0x0, -0xbc(%rbp)
jne 0x7f2ea
movq -0xd0(%rbp), %rax
movzbl 0x48(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x7f2ea
cmpq $0x0, -0xf0(%rbp)
jne 0x7f3e0
movq -0x20(%rbp), %rax
movb 0x63(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7f3e0
movq -0x20(%rbp), %rax
movswl 0x5c(%rax), %eax
movq -0xd0(%rbp), %rcx
movswl 0x46(%rcx), %ecx
cmpl %ecx, %eax
jge 0x7f3e0
movq -0x18(%rbp), %rax
movzwl 0x32(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x7f3e0
cmpl $0x0, 0x33016(%rip)     # 0xb22e0
je 0x7f3e0
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movzwl 0x4c(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0x7f3e0
cmpl $0x0, -0xbc(%rbp)
je 0x7f301
movl -0xb8(%rbp), %eax
movl %eax, -0x104(%rbp)
jmp 0x7f30b
xorl %eax, %eax
movl %eax, -0x104(%rbp)
jmp 0x7f30b
movl -0x104(%rbp), %eax
movb %al, %cl
movq -0xb0(%rbp), %rax
movb %cl, 0x11(%rax)
cmpq $0x0, -0xf0(%rbp)
jne 0x7f37a
movw -0xbe(%rbp), %ax
movswl %ax, %edi
movswl -0xc0(%rbp), %esi
callq 0x807c0
cwtl
addl $0x1, %eax
movl %eax, -0x108(%rbp)
movq -0x20(%rbp), %rax
movswl 0x5c(%rax), %eax
imull $0xf, %eax, %eax
movq -0xd0(%rbp), %rcx
movswl 0x46(%rcx), %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x108(%rbp), %eax
addl %ecx, %eax
movw %ax, %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x14(%rax)
jmp 0x7f398
movswl -0xbe(%rbp), %eax
movswl -0xc0(%rbp), %ecx
addl %ecx, %eax
movw %ax, %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0xc8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x80a40
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x80060
movl %eax, -0xb4(%rbp)
movw -0xbe(%rbp), %cx
movq -0xb0(%rbp), %rax
movw %cx, 0x16(%rax)
cmpl $0x0, -0xb4(%rbp)
je 0x7f3de
jmp 0x7f430
jmp 0x7f3e0
jmp 0x7f3e2
movq -0x8(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0x80ba0
movl %eax, -0xb4(%rbp)
movq -0xa8(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x7f40e
jmp 0x7f430
jmp 0x7f410
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x7f018
movl -0xb4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop