// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/sqlite3.c
// Function: whereLoopAddBtreeIndex
static int whereLoopAddBtreeIndex(
  WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
  struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
  Index *pProbe,                  /* An index on pSrc */
  LogEst nInMul                   /* log(Number of iterations due to IN) */
){
  WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
  Parse *pParse = pWInfo->pParse;        /* Parsing context */
  sqlite3 *db = pParse->db;       /* Database connection malloc context */
  WhereLoop *pNew;                /* Template WhereLoop under construction */
  WhereTerm *pTerm;               /* A WhereTerm under consideration */
  int opMask;                     /* Valid operators for constraints */
  WhereScan scan;                 /* Iterator for WHERE terms */
  Bitmask saved_prereq;           /* Original value of pNew->prereq */
  u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
  u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
  u16 saved_nSkip;                /* Original value of pNew->u.btree.nSkip */
  u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
  LogEst saved_nOut;              /* Original value of pNew->nOut */
  int iCol;                       /* Index of the column in the table */
  int rc = SQLITE_OK;             /* Return code */
  LogEst nRowEst;                 /* Estimated index selectivity */
  LogEst rLogSize;                /* Logarithm of table size */
  WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */

  pNew = pBuilder->pNew;
  if( db->mallocFailed ) return SQLITE_NOMEM;

  assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
  assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
  if( pNew->wsFlags & WHERE_BTM_LIMIT ){
    opMask = WO_LT|WO_LE;
  }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
  }else{
    opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
  }
  if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);

  assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
  if( pNew->u.btree.nEq < pProbe->nKeyCol ){
    iCol = pProbe->aiColumn[pNew->u.btree.nEq];
    nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
    if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
  }else{
    iCol = -1;
    nRowEst = 0;
  }
  pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
                        opMask, pProbe);
  saved_nEq = pNew->u.btree.nEq;
  saved_nSkip = pNew->u.btree.nSkip;
  saved_nLTerm = pNew->nLTerm;
  saved_wsFlags = pNew->wsFlags;
  saved_prereq = pNew->prereq;
  saved_nOut = pNew->nOut;
  pNew->rSetup = 0;
  rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));

  /* Consider using a skip-scan if there are no WHERE clause constraints
  ** available for the left-most terms of the index, and if the average
  ** number of repeats in the left-most terms is at least 18.  The magic
  ** number 18 was found by experimentation to be the payoff point where
  ** skip-scan become faster than a full-scan.
  */
  if( pTerm==0
   && saved_nEq==saved_nSkip
   && saved_nEq+1<pProbe->nKeyCol
   && pProbe->aiRowEst[saved_nEq+1]>=18  /* TUNING: Minimum for skip-scan */
  ){
    LogEst nIter;
    pNew->u.btree.nEq++;
    pNew->u.btree.nSkip++;
    pNew->aLTerm[pNew->nLTerm++] = 0;
    pNew->wsFlags |= WHERE_SKIPSCAN;
    nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
    whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter);
  }
  for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
    int nIn = 0;
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
    int nRecValid = pBuilder->nRecValid;
#endif
    if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
     && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
    ){
      continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
    }
    if( pTerm->prereqRight & pNew->maskSelf ) continue;

    assert( pNew->nOut==saved_nOut );

    pNew->wsFlags = saved_wsFlags;
    pNew->u.btree.nEq = saved_nEq;
    pNew->nLTerm = saved_nLTerm;
    if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
    pNew->aLTerm[pNew->nLTerm++] = pTerm;
    pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
    pNew->rRun = rLogSize; /* Baseline cost is log2(N).  Adjustments below */
    if( pTerm->eOperator & WO_IN ){
      Expr *pExpr = pTerm->pExpr;
      pNew->wsFlags |= WHERE_COLUMN_IN;
      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
        /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
        nIn = 46;  assert( 46==sqlite3LogEst(25) );
      }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
        /* "x IN (value, value, ...)" */
        nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
      }
      pNew->rRun += nIn;
      pNew->u.btree.nEq++;
      pNew->nOut = nRowEst + nInMul + nIn;
    }else if( pTerm->eOperator & (WO_EQ) ){
      assert(
        (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
        || nInMul==0
      );
      pNew->wsFlags |= WHERE_COLUMN_EQ;
      if( iCol<0  
       || (pProbe->onError!=OE_None && nInMul==0
           && pNew->u.btree.nEq==pProbe->nKeyCol-1)
      ){
        assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
        pNew->wsFlags |= WHERE_ONEROW;
      }
      pNew->u.btree.nEq++;
      pNew->nOut = nRowEst + nInMul;
    }else if( pTerm->eOperator & (WO_ISNULL) ){
      pNew->wsFlags |= WHERE_COLUMN_NULL;
      pNew->u.btree.nEq++;
      /* TUNING: IS NULL selects 2 rows */
      nIn = 10;  assert( 10==sqlite3LogEst(2) );
      pNew->nOut = nRowEst + nInMul + nIn;
    }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
      testcase( pTerm->eOperator & WO_GT );
      testcase( pTerm->eOperator & WO_GE );
      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
      pBtm = pTerm;
      pTop = 0;
    }else{
      assert( pTerm->eOperator & (WO_LT|WO_LE) );
      testcase( pTerm->eOperator & WO_LT );
      testcase( pTerm->eOperator & WO_LE );
      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
      pTop = pTerm;
      pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
                     pNew->aLTerm[pNew->nLTerm-2] : 0;
    }
    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
      /* Adjust nOut and rRun for STAT3 range values */
      assert( pNew->nOut==saved_nOut );
      whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
    }
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
    if( nInMul==0 
     && pProbe->nSample 
     && pNew->u.btree.nEq<=pProbe->nSampleCol
     && OptimizationEnabled(db, SQLITE_Stat3) 
    ){
      Expr *pExpr = pTerm->pExpr;
      tRowcnt nOut = 0;
      if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
        testcase( pTerm->eOperator & WO_EQ );
        testcase( pTerm->eOperator & WO_ISNULL );
        rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
      }else if( (pTerm->eOperator & WO_IN)
             &&  !ExprHasProperty(pExpr, EP_xIsSelect)  ){
        rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
      }
      assert( nOut==0 || rc==SQLITE_OK );
      if( nOut ){
        pNew->nOut = sqlite3LogEst(nOut);
        if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
      }
    }
#endif
    if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
      /* Each row involves a step of the index, then a binary search of
      ** the main table */
      pNew->rRun =  sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
    }
    /* Step cost for each output row */
    pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
    whereLoopOutputAdjust(pBuilder->pWC, pNew);
    rc = whereLoopInsert(pBuilder, pNew);
    if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
     && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
    ){
      whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
    }
    pNew->nOut = saved_nOut;
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
    pBuilder->nRecValid = nRecValid;
#endif
  }
  pNew->prereq = saved_prereq;
  pNew->u.btree.nEq = saved_nEq;
  pNew->u.btree.nSkip = saved_nSkip;
  pNew->wsFlags = saved_wsFlags;
  pNew->nOut = saved_nOut;
  pNew->nLTerm = saved_nLTerm;
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq (%rdi), %rax
movq (%rax), %rax
movq (%rax), %rax
cmpb $0x0, 0x50(%rax)
je 0x53e13
pushq $0x7
popq %rbp
jmp 0x5436b
movq %rdi, %r12
movq 0x18(%rdi), %r13
testb $0x20, 0x28(%r13)
movq %rcx, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rax, 0x70(%rsp)
jne 0x53e47
cmpl $0x0, 0x58(%rdx)
jle 0x53e4b
xorl %eax, %eax
testb $0x8, 0x38(%rsi)
sete %al
shll $0x7, %eax
orl $0x3f, %eax
jmp 0x53e4e
pushq $0x18
jmp 0x53e4d
pushq $0x3f
popq %rax
movl %eax, %ebx
andl $-0x7d, %ebx
testb $0x4, 0x63(%rdx)
cmovel %eax, %ebx
movzwl 0x18(%r13), %eax
cmpw 0x5e(%rdx), %ax
movq %rdx, 0x18(%rsp)
jae 0x53e9e
movq 0x8(%rdx), %rcx
movq %rdx, %r14
movq 0x10(%rdx), %rdx
movswl (%rcx,%rax,2), %ecx
movl %ecx, (%rsp)
movl 0x4(%rdx,%rax,4), %edi
callq 0x30110
testw %ax, %ax
jne 0x53e94
xorl %eax, %eax
cmpb $0x0, 0x62(%r14)
sete %al
movq %rax, 0x20(%rsp)
movl (%rsp), %ecx
jmp 0x53eaa
pushq $-0x1
popq %rcx
movq $0x0, 0x20(%rsp)
movq 0x8(%r12), %rsi
movq 0x48(%rsp), %rax
movl 0x3c(%rax), %edx
leaq 0x78(%rsp), %rdi
movl %ecx, (%rsp)
movl %ebx, %r8d
movq 0x18(%rsp), %rbx
movq %rbx, %r9
callq 0x52ad1
movzwl 0x18(%r13), %r15d
movzwl 0x1a(%r13), %ebp
movzwl 0x2c(%r13), %ecx
movl 0x28(%r13), %edx
movl %edx, 0x10(%rsp)
movq (%r13), %rdx
movq %rdx, 0x68(%rsp)
movzwl 0x16(%r13), %edx
movw %dx, 0x4(%rsp)
andw $0x0, 0x12(%r13)
movq %rax, %r14
movw %cx, 0x6(%rsp)
movzwl %cx, %eax
movq %rax, 0x40(%rsp)
movq 0x10(%rbx), %rbx
movl (%rbx), %edi
callq 0x30110
movswl %ax, %edi
callq 0x53cac
movq %rax, 0x8(%rsp)
testq %r14, %r14
sete %al
movw %bp, 0x16(%rsp)
movq %r15, 0x38(%rsp)
cmpw %bp, %r15w
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x53fc1
movzwl 0x38(%rsp), %ecx
leal 0x1(%rcx), %eax
movq 0x18(%rsp), %rdx
movzwl 0x5e(%rdx), %edx
cmpl %edx, %eax
jae 0x53fc1
cmpl $0x12, 0x4(%rbx,%rcx,4)
jb 0x53fc1
movq 0x38(%rsp), %rax
incl %eax
movw %ax, 0x18(%r13)
movw %ax, 0x1a(%r13)
movq 0x30(%r13), %rax
movq 0x40(%rsp), %rsi
leal 0x1(%rsi), %edx
movw %dx, 0x2c(%r13)
andq $0x0, (%rax,%rsi,8)
movl 0x10(%rsp), %eax
orl $0x8000, %eax           # imm = 0x8000
movl %eax, 0x28(%r13)
movq 0x18(%rsp), %rbx
movq 0x10(%rbx), %rsi
movl (%rsi), %eax
xorl %edx, %edx
divl 0x4(%rsi,%rcx,4)
movq %rax, %rdi
callq 0x30110
movswl %ax, %ecx
movq %r12, %rdi
movq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x53deb
movl (%rsp), %eax
movq 0x40(%rsp), %rcx
incl %ecx
movq %rcx, 0x40(%rsp)
movq 0x20(%rsp), %rdi
movzwl %di, %ecx
movq 0x50(%rsp), %rsi
movzwl %si, %edx
addl %ecx, %edx
movl %edx, 0x64(%rsp)
leal (%rdi,%rsi), %ecx
movl %ecx, 0x5c(%rsp)
movq 0x8(%rsp), %rdx
leal -0x11(%rdx), %ecx
cmpw $0x1c, %dx
pushq $0xa
popq %rdx
cmovll %edx, %ecx
leal (%rdi,%rsi), %edx
addl $0xa, %edx
movl %edx, 0x58(%rsp)
xorl %ebp, %ebp
imulq $0x30, %rax, %rax
movq %rax, 0x20(%rsp)
leaq 0x78(%rsp), %r15
movswl %cx, %eax
movl %eax, 0x60(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
testl %ebp, %ebp
jne 0x54332
testq %r14, %r14
je 0x54332
cmpw $0x80, 0x1a(%r14)
jne 0x54075
xorl %ebx, %ebx
cmpl $0x0, (%rsp)
js 0x542fe
movq 0x48(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rax
movq 0x20(%rsp), %rcx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x542fe
movq 0x8(%r13), %rax
xorl %ebx, %ebx
testq %rax, 0x28(%r14)
jne 0x542fe
movl 0x10(%rsp), %eax
movl %eax, 0x28(%r13)
movq 0x38(%rsp), %rax
movw %ax, 0x18(%r13)
movzwl 0x6(%rsp), %eax
movw %ax, 0x2c(%r13)
movq 0x70(%rsp), %rdi
movq %r13, %rsi
movq 0x40(%rsp), %rdx
callq 0x53924
testl %eax, %eax
jne 0x54332
movq 0x30(%r13), %rcx
movzwl 0x2c(%r13), %edx
leal 0x1(%rdx), %eax
movw %ax, 0x2c(%r13)
movq %r14, (%rcx,%rdx,8)
movq 0x28(%r14), %rcx
orq 0x68(%rsp), %rcx
movq 0x8(%r13), %rdx
notq %rdx
andq %rcx, %rdx
movq %rdx, (%r13)
movq 0x8(%rsp), %rcx
movw %cx, 0x14(%r13)
movzwl 0x1a(%r14), %ecx
testb $0x1, %cl
jne 0x5413a
testb $0x2, %cl
jne 0x5418f
testb %cl, %cl
js 0x541e6
movl 0x28(%r13), %edx
testb $0x24, %cl
je 0x5420a
orl $0x22, %edx
movl %edx, 0x28(%r13)
movq $0x0, 0x28(%rsp)
movq 0x8(%rsp), %rax
movl %edx, %ebx
movq %r14, 0x30(%rsp)
jmp 0x5422f
movq (%r14), %rax
movl 0x28(%r13), %ebx
orl $0x4, %ebx
movl %ebx, 0x28(%r13)
testb $0x8, 0x5(%rax)
pushq $0x2e
popq %rcx
movl %ecx, %ebp
jne 0x5416e
movq 0x20(%rax), %rax
xorl %ebp, %ebp
testq %rax, %rax
je 0x5416e
movslq (%rax), %rdi
testq %rdi, %rdi
je 0x5416e
callq 0x30110
movzwl %ax, %ebp
movq 0x8(%rsp), %rax
addl %ebp, %eax
movw %ax, 0x14(%r13)
incw 0x18(%r13)
movl %ebp, %ecx
addl 0x64(%rsp), %ecx
movw %cx, 0x16(%r13)
jmp 0x54231
movl 0x28(%r13), %ecx
movl %ecx, %ebx
orl $0x1, %ebx
movl %ebx, 0x28(%r13)
movzwl 0x18(%r13), %eax
cmpl $0x0, (%rsp)
movq 0x50(%rsp), %rdx
js 0x541c6
testw %dx, %dx
jne 0x541d2
movq 0x18(%rsp), %rdx
cmpb $0x0, 0x62(%rdx)
je 0x541d2
movzwl 0x5e(%rdx), %edx
decl %edx
cmpl %eax, %edx
jne 0x541d2
orl $0x1001, %ecx           # imm = 0x1001
movl %ecx, 0x28(%r13)
movl %ecx, %ebx
incl %eax
movw %ax, 0x18(%r13)
movl 0x5c(%rsp), %eax
movw %ax, 0x16(%r13)
xorl %ebp, %ebp
jmp 0x54203
movl 0x28(%r13), %ebx
orl $0x8, %ebx
movl %ebx, 0x28(%r13)
incw 0x18(%r13)
movl 0x58(%rsp), %eax
movw %ax, 0x16(%r13)
movw $0xa, %bp
movq 0x8(%rsp), %rax
jmp 0x54231
movl %edx, %ebx
orl $0x12, %ebx
movl %ebx, 0x28(%r13)
testb $0x20, %dl
jne 0x54310
movq $0x0, 0x30(%rsp)
movq 0x8(%rsp), %rax
movq %r14, 0x28(%rsp)
xorl %ebp, %ebp
testb $0x2, %bl
je 0x5427c
movswl 0x16(%r13), %ecx
leal -0x14(%rcx), %edx
xorl %esi, %esi
cmpq $0x0, 0x30(%rsp)
setne %sil
cmovel %ecx, %edx
leal -0x14(%rdx), %edi
xorl %r8d, %r8d
cmpq $0x0, 0x28(%rsp)
setne %r8b
cmovel %edx, %edi
addl %r8d, %esi
subl %esi, %ecx
cmpw $0xb, %di
pushq $0xa
popq %rdx
cmovll %edx, %edi
movzwl %di, %edx
cmpl %edx, %ecx
cmovgel %edx, %ecx
movw %cx, 0x16(%r13)
testw $0x140, %bx             # imm = 0x140
jne 0x5428f
movswl %ax, %edi
movl 0x60(%rsp), %esi
callq 0x53cfe
movswl 0x16(%r13), %esi
movswl %ax, %edi
callq 0x53cfe
movw %ax, 0x14(%r13)
movq 0x8(%r12), %rdi
movq %r13, %rsi
callq 0x53d55
movq %r12, %rdi
movq %r13, %rsi
callq 0x539a0
movl %eax, %ebx
testb $0x10, 0x28(%r13)
jne 0x542f4
movzwl 0x18(%r13), %eax
movq 0x18(%rsp), %rcx
cmpq $0x1, (%rcx)
movzwl 0x5e(%rcx), %ecx
sbbl $-0x1, %ecx
cmpl %eax, %ecx
jbe 0x542f4
addl 0x50(%rsp), %ebp
movswl %bp, %ecx
movq %r12, %rdi
movq 0x48(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x53deb
movzwl 0x4(%rsp), %eax
movw %ax, 0x16(%r13)
movq %r15, %rdi
callq 0x52b4b
movq %rax, %r14
movl %ebx, %ebp
jmp 0x54032
movq 0x30(%r13), %rcx
movzwl %ax, %eax
movq -0x10(%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movq %r14, 0x28(%rsp)
jmp 0x54231
movq 0x68(%rsp), %rax
movq %rax, (%r13)
movq 0x38(%rsp), %rax
movw %ax, 0x18(%r13)
movzwl 0x16(%rsp), %eax
movw %ax, 0x1a(%r13)
movl 0x10(%rsp), %eax
movl %eax, 0x28(%r13)
movzwl 0x4(%rsp), %eax
movw %ax, 0x16(%r13)
movzwl 0x6(%rsp), %eax
movw %ax, 0x2c(%r13)
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq