// File: /workspace/github_c_makefile_star2_O1/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 %rdx, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
movq (%rdi), %rax
movq (%rax), %rax
movq (%rax), %rax
movl $0x7, %r12d
cmpb $0x0, 0x50(%rax)
jne 0x5db1d
movq %rsi, %r14
movq %rax, 0x80(%rsp)
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rbx
movl $0x18, %eax
testb $0x20, 0x28(%rbx)
jne 0x5d5db
movq 0x10(%rsp), %rax
cmpl $0x0, 0x58(%rax)
jle 0x5d5d6
xorl %eax, %eax
testb $0x8, 0x38(%r14)
sete %al
shll $0x7, %eax
orl $0x3f, %eax
jmp 0x5d5db
movl $0x3f, %eax
movq %rcx, 0x58(%rsp)
movl %eax, %ebp
andl $-0x7d, %ebp
movq 0x10(%rsp), %rcx
testb $0x4, 0x63(%rcx)
cmovel %eax, %ebp
movzwl 0x18(%rbx), %eax
movq $0x0, 0x20(%rsp)
cmpw 0x5e(%rcx), %ax
jae 0x5d63d
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rdx
movswl (%rcx,%rax,2), %r13d
movl 0x4(%rdx,%rax,4), %edi
callq 0x3530e
movl %eax, %r12d
testw %ax, %ax
jne 0x5d638
cmpb $0x0, 0x62(%r15)
movl $0x1, %eax
cmovnel %r12d, %eax
movl %eax, %r12d
movl %r13d, %ecx
jmp 0x5d645
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rsi
movq %r14, 0x40(%rsp)
movl 0x3c(%r14), %edx
leaq 0x88(%rsp), %rdi
movl %ecx, 0x1c(%rsp)
movl %ebp, %r8d
movq 0x10(%rsp), %r14
movq %r14, %r9
callq 0x5bfa5
movq %rax, %rbp
movzwl 0x18(%rbx), %r13d
movzwl 0x1a(%rbx), %r15d
movq %r15, 0x70(%rsp)
movzwl 0x2c(%rbx), %eax
movw %ax, 0xe(%rsp)
movzwl %ax, %eax
movq %rax, 0x48(%rsp)
movl 0x28(%rbx), %eax
movl %eax, 0x18(%rsp)
movq (%rbx), %rax
movq %rax, 0x78(%rsp)
movzwl 0x16(%rbx), %eax
movw %ax, 0xc(%rsp)
movw $0x0, 0x12(%rbx)
movq 0x10(%r14), %r14
movl (%r14), %edi
callq 0x3530e
movswq %ax, %rdi
callq 0x3530e
leal -0x21(%rax), %ecx
cmpw $0x22, %ax
movq 0x20(%rsp), %rax
cmovgel %ecx, %eax
movq %rax, 0x20(%rsp)
testq %rbp, %rbp
sete %al
movq %r13, 0x50(%rsp)
cmpw %r15w, %r13w
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5d77f
movzwl 0x50(%rsp), %ecx
leal 0x1(%rcx), %eax
movq 0x10(%rsp), %rdx
movzwl 0x5e(%rdx), %edx
cmpl %edx, %eax
jae 0x5d77f
cmpl $0x12, 0x4(%r14,%rcx,4)
jb 0x5d77f
movq 0x50(%rsp), %rax
incl %eax
movw %ax, 0x18(%rbx)
movq 0x70(%rsp), %rax
incl %eax
movw %ax, 0x1a(%rbx)
movq 0x30(%rbx), %rax
movq 0x48(%rsp), %rsi
leal 0x1(%rsi), %edx
movw %dx, 0x2c(%rbx)
movq $0x0, (%rax,%rsi,8)
movl 0x18(%rsp), %eax
orl $0x8000, %eax           # imm = 0x8000
movl %eax, 0x28(%rbx)
movq 0x10(%rsp), %r14
movq 0x10(%r14), %rsi
movl (%rsi), %eax
xorl %edx, %edx
divl 0x4(%rsi,%rcx,4)
movq %rax, %rdi
callq 0x3530e
movswl %ax, %ecx
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x5d566
testq %rbp, %rbp
je 0x5dae5
movl 0x1c(%rsp), %eax
movq 0x48(%rsp), %rcx
incl %ecx
movq %rcx, 0x48(%rsp)
movzwl %r12w, %ecx
movq 0x58(%rsp), %rdx
movzwl %dx, %esi
addl %ecx, %esi
movl %esi, 0x6c(%rsp)
leal (%r12,%rdx), %ecx
movl %ecx, 0x64(%rsp)
leal (%r12,%rdx), %ecx
addl $0xa, %ecx
movl %ecx, 0x60(%rsp)
movq 0x20(%rsp), %rdx
leal -0x11(%rdx), %ecx
cmpw $0x1c, %dx
movl $0xa, %edx
cmovll %edx, %ecx
movq $0x0, 0x38(%rsp)
leaq (%rax,%rax,2), %r15
shlq $0x4, %r15
movswl %cx, %eax
movl %eax, 0x68(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x40(%rsp), %r14
movl 0x18(%rsp), %r13d
cmpw $0x80, 0x1a(%rbp)
jne 0x5d826
cmpl $0x0, 0x1c(%rsp)
js 0x5d8d9
movq 0x20(%r14), %rax
movq 0x8(%rax), %rax
xorl %r12d, %r12d
cmpb $0x0, 0x28(%rax,%r15)
jne 0x5d8dc
movq 0x8(%rbx), %rax
xorl %r12d, %r12d
testq %rax, 0x28(%rbp)
jne 0x5d8dc
movl %r13d, 0x28(%rbx)
movq 0x50(%rsp), %rax
movw %ax, 0x18(%rbx)
movzwl 0xe(%rsp), %eax
movw %ax, 0x2c(%rbx)
movq 0x80(%rsp), %rdi
movq %rbx, %rsi
movq 0x48(%rsp), %rdx
callq 0x5d023
movb $0x1, %cl
testl %eax, %eax
jne 0x5d8de
movq 0x30(%rbx), %rcx
movzwl 0x2c(%rbx), %edx
leal 0x1(%rdx), %eax
movw %ax, 0x2c(%rbx)
movq %rbp, (%rcx,%rdx,8)
movq 0x28(%rbp), %rcx
orq 0x78(%rsp), %rcx
movq 0x8(%rbx), %rdx
notq %rdx
andq %rcx, %rdx
movq %rdx, (%rbx)
movq 0x20(%rsp), %rcx
movw %cx, 0x14(%rbx)
movzwl 0x1a(%rbp), %ecx
testb $0x1, %cl
jne 0x5d90d
testb $0x2, %cl
jne 0x5d95b
testb %cl, %cl
js 0x5d9ac
movl 0x28(%rbx), %edx
testb $0x24, %cl
je 0x5daa7
orl $0x22, %edx
movl %edx, 0x28(%rbx)
movq $0x0, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
jmp 0x5d9a7
xorl %r12d, %r12d
xorl %ecx, %ecx
testb %cl, %cl
jne 0x5daed
leaq 0x88(%rsp), %rdi
callq 0x5c02f
testl %r12d, %r12d
jne 0x5daed
movq %rax, %rbp
testq %rax, %rax
jne 0x5d7fc
jmp 0x5daed
movq (%rbp), %rax
orl $0x4, 0x28(%rbx)
movl $0x2e, %r14d
testb $0x8, 0x5(%rax)
jne 0x5d93e
movq 0x20(%rax), %rax
xorl %r14d, %r14d
testq %rax, %rax
je 0x5d93e
movslq (%rax), %rdi
testq %rdi, %rdi
je 0x5d93e
callq 0x3530e
movzwl %ax, %r14d
movq 0x20(%rsp), %rax
addl %r14d, %eax
movw %ax, 0x14(%rbx)
incw 0x18(%rbx)
movl %r14d, %eax
addl 0x6c(%rsp), %eax
movw %ax, 0x16(%rbx)
jmp 0x5d9c1
movl 0x28(%rbx), %eax
movl %eax, %ecx
orl $0x1, %ecx
movl %ecx, 0x28(%rbx)
cmpl $0x0, 0x1c(%rsp)
js 0x5d993
cmpw $0x0, 0x58(%rsp)
jne 0x5d99b
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x62(%rcx)
je 0x5d99b
movzwl 0x18(%rbx), %ecx
movq 0x10(%rsp), %rdx
movzwl 0x5e(%rdx), %edx
decl %edx
cmpl %ecx, %edx
jne 0x5d99b
orl $0x1001, %eax           # imm = 0x1001
movl %eax, 0x28(%rbx)
incw 0x18(%rbx)
movl 0x64(%rsp), %eax
movw %ax, 0x16(%rbx)
xorl %r14d, %r14d
jmp 0x5d9c1
orb $0x8, 0x28(%rbx)
incw 0x18(%rbx)
movl 0x60(%rsp), %eax
movw %ax, 0x16(%rbx)
movw $0xa, %r14w
movl 0x28(%rbx), %eax
testb $0x2, %al
je 0x5da0e
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, %eax            # imm = 0x140
jne 0x5da26
movswl 0x14(%rbx), %edi
movl 0x68(%rsp), %esi
callq 0x5d44e
movw %ax, 0x14(%rbx)
movswl 0x14(%rbx), %edi
movswl 0x16(%rbx), %esi
callq 0x5d44e
movw %ax, 0x14(%rbx)
movq 0x28(%rsp), %r12
movq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x5d4a5
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5d0a1
movl %eax, %r12d
testb $0x10, 0x28(%rbx)
jne 0x5da92
movzwl 0x18(%rbx), %eax
movq 0x10(%rsp), %rcx
cmpq $0x1, (%rcx)
movzwl 0x5e(%rcx), %ecx
sbbl $-0x1, %ecx
cmpl %eax, %ecx
jbe 0x5da92
addl 0x58(%rsp), %r14d
movswl %r14w, %ecx
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x5d566
movzwl 0xc(%rsp), %eax
movw %ax, 0x16(%rbx)
xorl %ecx, %ecx
movq 0x40(%rsp), %r14
jmp 0x5d8de
movl %edx, %ecx
orl $0x12, %ecx
movl %ecx, 0x28(%rbx)
testb $0x20, %dl
jne 0x5dac7
movq $0x0, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
jmp 0x5d9a7
movq 0x30(%rbx), %rcx
movzwl %ax, %eax
movq -0x10(%rcx,%rax,8), %rax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
movq %rbp, 0x30(%rsp)
jmp 0x5d9c1
xorl %r12d, %r12d
movl 0x18(%rsp), %r13d
movq 0x78(%rsp), %rax
movq %rax, (%rbx)
movq 0x50(%rsp), %rax
movw %ax, 0x18(%rbx)
movq 0x70(%rsp), %rax
movw %ax, 0x1a(%rbx)
movl %r13d, 0x28(%rbx)
movzwl 0xc(%rsp), %eax
movw %ax, 0x16(%rbx)
movzwl 0xe(%rsp), %eax
movw %ax, 0x2c(%rbx)
movl %r12d, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq