// File: /workspace/github_c_makefile_star2_O3/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 $0x108, %rsp            # imm = 0x108
movq (%rdi), %rax
movq (%rax), %rax
movq (%rax), %rax
movl $0x7, %ebp
cmpb $0x0, 0x50(%rax)
jne 0x5e351
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
movq %rax, 0x80(%rsp)
movq 0x18(%rdi), %rbx
movl $0x18, %eax
testb $0x20, 0x28(%rbx)
jne 0x5de01
cmpl $0x0, 0x58(%r14)
jle 0x5ddfc
xorl %eax, %eax
testb $0x8, 0x38(%r13)
sete %al
shll $0x7, %eax
orl $0x3f, %eax
jmp 0x5de01
movl $0x3f, %eax
movq %rcx, 0x50(%rsp)
movl %eax, %r15d
andl $-0x7d, %r15d
testb $0x4, 0x63(%r14)
cmovel %eax, %r15d
movzwl 0x18(%rbx), %eax
movq $0x0, 0x10(%rsp)
cmpw 0x5e(%r14), %ax
jae 0x5de55
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rdx
movswl (%rcx,%rax,2), %ebp
movl 0x4(%rdx,%rax,4), %edi
callq 0x357b3
testw %ax, %ax
jne 0x5de4e
xorl %eax, %eax
cmpb $0x0, 0x62(%r14)
sete %al
movq %rax, 0x28(%rsp)
jmp 0x5de63
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq $0x0, 0x28(%rsp)
movq %r12, 0x70(%rsp)
movq 0x8(%r12), %rsi
movq %r13, 0x48(%rsp)
movl 0x3c(%r13), %edx
leaq 0x88(%rsp), %rdi
movl %ebp, 0x20(%rsp)
movl %ebp, %ecx
movl %r15d, %r8d
movq %r14, %r9
callq 0x5c805
movq %rax, %r15
movq %r14, %r12
movzwl 0x18(%rbx), %r14d
movzwl 0x1a(%rbx), %ebp
movzwl 0x2c(%rbx), %eax
movq %rax, 0x58(%rsp)
movzwl %ax, %eax
movq %rax, 0x40(%rsp)
movl 0x28(%rbx), %eax
movl %eax, 0x18(%rsp)
movq (%rbx), %rax
movq %rax, 0x78(%rsp)
movzwl 0x16(%rbx), %eax
movw %ax, 0xe(%rsp)
movw $0x0, 0x12(%rbx)
movq 0x10(%r12), %r13
movl (%r13), %edi
callq 0x357b3
movswq %ax, %rdi
callq 0x357b3
leal -0x21(%rax), %ecx
cmpw $0x22, %ax
movq 0x10(%rsp), %rax
cmovgel %ecx, %eax
movq %rax, 0x10(%rsp)
testq %r15, %r15
sete %al
movw %bp, 0x26(%rsp)
cmpw %bp, %r14w
sete %cl
andb %al, %cl
cmpb $0x1, %cl
movl %r14d, 0x1c(%rsp)
jne 0x5dfac
movzwl %r14w, %ecx
leal 0x1(%rcx), %eax
movzwl 0x5e(%r12), %edx
xorl %ebp, %ebp
cmpl %edx, %eax
jae 0x5e31f
movq %r12, %r14
cmpl $0x12, 0x4(%r13,%rcx,4)
movq 0x48(%rsp), %r15
movq 0x70(%rsp), %r12
jb 0x5e31f
movl 0x1c(%rsp), %eax
incl %eax
movw %ax, 0x18(%rbx)
movw %ax, 0x1a(%rbx)
movq 0x30(%rbx), %rax
movq 0x58(%rsp), %rdx
incl %edx
movw %dx, 0x2c(%rbx)
movq 0x40(%rsp), %rdx
movq $0x0, (%rax,%rdx,8)
movl 0x18(%rsp), %eax
orl $0x8000, %eax           # imm = 0x8000
movl %eax, 0x28(%rbx)
movq 0x10(%r14), %rsi
movl (%rsi), %eax
xorl %ebp, %ebp
xorl %edx, %edx
divl 0x4(%rsi,%rcx,4)
movq %rax, %rdi
callq 0x357b3
movswl %ax, %ecx
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5dd9a
jmp 0x5e31f
testq %r15, %r15
je 0x5e31d
movl 0x20(%rsp), %eax
movq 0x40(%rsp), %rcx
incl %ecx
movq %rcx, 0x40(%rsp)
movq 0x28(%rsp), %rdi
movzwl %di, %ecx
movq 0x50(%rsp), %rdx
movzwl %dx, %esi
addl %ecx, %esi
movl %esi, 0x6c(%rsp)
leal (%rdi,%rdx), %ecx
movl %ecx, 0x64(%rsp)
leal (%rdi,%rdx), %ecx
addl $0xa, %ecx
movl %ecx, 0x60(%rsp)
movq 0x10(%rsp), %rdx
leal -0x11(%rdx), %ecx
cmpw $0x1c, %dx
movl $0xa, %edx
cmovll %edx, %ecx
movq $0x0, 0x38(%rsp)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq %rax, 0x28(%rsp)
movswl %cx, %eax
movl %eax, 0x68(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x48(%rsp), %r13
movq %r12, %r14
movq 0x70(%rsp), %r12
cmpw $0x80, 0x1a(%r15)
jne 0x5e061
xorl %ebp, %ebp
cmpl $0x0, 0x20(%rsp)
js 0x5e2db
movq 0x20(%r13), %rax
movq 0x8(%rax), %rax
movq 0x28(%rsp), %rcx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x5e2db
movq 0x8(%rbx), %rax
xorl %ebp, %ebp
testq %rax, 0x28(%r15)
jne 0x5e2db
movl 0x18(%rsp), %eax
movl %eax, 0x28(%rbx)
movl 0x1c(%rsp), %eax
movw %ax, 0x18(%rbx)
movq 0x58(%rsp), %rax
movw %ax, 0x2c(%rbx)
movq 0x80(%rsp), %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
callq 0x5d88a
testl %eax, %eax
jne 0x5e31d
movq 0x30(%rbx), %rcx
movzwl 0x2c(%rbx), %edx
leal 0x1(%rdx), %eax
movw %ax, 0x2c(%rbx)
movq %r15, (%rcx,%rdx,8)
movq 0x28(%r15), %rcx
orq 0x78(%rsp), %rcx
movq 0x8(%rbx), %rdx
notq %rdx
andq %rcx, %rdx
movq %rdx, (%rbx)
movq 0x10(%rsp), %rcx
movw %cx, 0x14(%rbx)
movzwl 0x1a(%r15), %ecx
testb $0x1, %cl
jne 0x5e11f
testb $0x2, %cl
jne 0x5e174
testb %cl, %cl
js 0x5e1c2
movl 0x28(%rbx), %edx
testb $0x24, %cl
je 0x5e1e3
orl $0x22, %edx
movl %edx, 0x28(%rbx)
movq $0x0, 0x30(%rsp)
movq 0x10(%rsp), %rax
movl %edx, %ebp
movq %r15, 0x38(%rsp)
jmp 0x5e207
movq (%r15), %rax
movl 0x28(%rbx), %ebp
orl $0x4, %ebp
movl %ebp, 0x28(%rbx)
movl $0x2e, %r13d
testb $0x8, 0x5(%rax)
jne 0x5e154
movq 0x20(%rax), %rax
xorl %r13d, %r13d
testq %rax, %rax
je 0x5e154
movslq (%rax), %rdi
testq %rdi, %rdi
je 0x5e154
callq 0x357b3
movzwl %ax, %r13d
movq 0x10(%rsp), %rax
addl %r13d, %eax
movw %ax, 0x14(%rbx)
incw 0x18(%rbx)
movl %r13d, %ecx
addl 0x6c(%rsp), %ecx
movw %cx, 0x16(%rbx)
jmp 0x5e20a
movl 0x28(%rbx), %ecx
movl %ecx, %ebp
orl $0x1, %ebp
movl %ebp, 0x28(%rbx)
movzwl 0x18(%rbx), %eax
cmpl $0x0, 0x20(%rsp)
js 0x5e1a4
cmpw $0x0, 0x50(%rsp)
jne 0x5e1af
cmpb $0x0, 0x62(%r14)
je 0x5e1af
movzwl 0x5e(%r14), %edx
decl %edx
cmpl %eax, %edx
jne 0x5e1af
orl $0x1001, %ecx           # imm = 0x1001
movl %ecx, 0x28(%rbx)
movl %ecx, %ebp
incl %eax
movw %ax, 0x18(%rbx)
movl 0x64(%rsp), %eax
movw %ax, 0x16(%rbx)
xorl %r13d, %r13d
jmp 0x5e1dc
movl 0x28(%rbx), %ebp
orl $0x8, %ebp
movl %ebp, 0x28(%rbx)
incw 0x18(%rbx)
movl 0x60(%rsp), %eax
movw %ax, 0x16(%rbx)
movw $0xa, %r13w
movq 0x10(%rsp), %rax
jmp 0x5e20a
movl %edx, %ebp
orl $0x12, %ebp
movl %ebp, 0x28(%rbx)
testb $0x20, %dl
jne 0x5e2fa
movq $0x0, 0x38(%rsp)
movq 0x10(%rsp), %rax
movq %r15, 0x30(%rsp)
xorl %r13d, %r13d
testb $0x2, %bpl
je 0x5e256
movswl 0x16(%rbx), %ecx
leal -0x14(%rcx), %edx
xorl %esi, %esi
cmpq $0x0, 0x38(%rsp)
setne %sil
cmovel %ecx, %edx
leal -0x14(%rdx), %edi
xorl %r8d, %r8d
cmpq $0x0, 0x30(%rsp)
setne %r8b
cmovel %edx, %edi
addl %r8d, %esi
subl %esi, %ecx
cmpw $0xb, %di
movl $0xa, %edx
cmovll %edx, %edi
movzwl %di, %edx
cmpl %edx, %ecx
cmovgel %edx, %ecx
movw %cx, 0x16(%rbx)
testl $0x140, %ebp            # imm = 0x140
jne 0x5e26a
movswl %ax, %edi
movl 0x68(%rsp), %esi
callq 0x5dc9d
movswl 0x16(%rbx), %esi
movswl %ax, %edi
callq 0x5dc9d
movw %ax, 0x14(%rbx)
movq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x5dcf4
movq (%r12), %rdi
movq 0x20(%r12), %rsi
movq %rbx, %rdx
callq 0x5d908
movl %eax, %ebp
testb $0x10, 0x28(%rbx)
jne 0x5e2cd
movzwl 0x18(%rbx), %eax
cmpq $0x1, (%r14)
movzwl 0x5e(%r14), %ecx
sbbl $-0x1, %ecx
cmpl %eax, %ecx
jbe 0x5e2cd
addl 0x50(%rsp), %r13d
movswl %r13w, %ecx
movq %r12, %rdi
movq 0x48(%rsp), %rsi
movq %r14, %rdx
callq 0x5dd9a
movzwl 0xe(%rsp), %eax
movw %ax, 0x16(%rbx)
movq 0x48(%rsp), %r13
leaq 0x88(%rsp), %rdi
callq 0x5c88f
testl %ebp, %ebp
jne 0x5e31f
movq %rax, %r15
testq %rax, %rax
jne 0x5e033
jmp 0x5e31f
movq 0x30(%rbx), %rcx
movzwl %ax, %eax
movq -0x10(%rcx,%rax,8), %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
movq %r15, 0x30(%rsp)
jmp 0x5e20a
xorl %ebp, %ebp
movq 0x78(%rsp), %rax
movq %rax, (%rbx)
movl 0x1c(%rsp), %eax
movw %ax, 0x18(%rbx)
movzwl 0x26(%rsp), %eax
movw %ax, 0x1a(%rbx)
movl 0x18(%rsp), %eax
movl %eax, 0x28(%rbx)
movzwl 0xe(%rsp), %eax
movw %ax, 0x16(%rbx)
movq 0x58(%rsp), %rax
movw %ax, 0x2c(%rbx)
movl %ebp, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq