// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/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 */
  WhereCost 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 */
  int saved_nEq;                  /* Original value of pNew->u.btree.nEq */
  u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
  WhereCost saved_nOut;           /* Original value of pNew->nOut */
  int iCol;                       /* Index of the column in the table */
  int rc = SQLITE_OK;             /* Return code */
  WhereCost nRowEst;              /* Estimated index selectivity */
  WhereCost 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->nColumn );
  if( pNew->u.btree.nEq < pProbe->nColumn ){
    iCol = pProbe->aiColumn[pNew->u.btree.nEq];
    nRowEst = whereCost(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_nLTerm = pNew->nLTerm;
  saved_wsFlags = pNew->wsFlags;
  saved_prereq = pNew->prereq;
  saved_nOut = pNew->nOut;
  pNew->rSetup = 0;
  rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
  for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
    int nIn = 0;
    if( pTerm->prereqRight & pNew->maskSelf ) continue;
    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 */
    }
    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==whereCost(25) );
      }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
        /* "x IN (value, value, ...)" */
        nIn = whereCost(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))!=0
                  || nInMul==0 );
      pNew->wsFlags |= WHERE_COLUMN_EQ;
      if( iCol<0  
       || (pProbe->onError!=OE_None && nInMul==0
           && pNew->u.btree.nEq==pProbe->nColumn-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==whereCost(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 */
      WhereCost rDiv;
      whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
                        pBtm, pTop, &rDiv);
      pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10;
    }
#ifdef SQLITE_ENABLE_STAT3
    if( pNew->u.btree.nEq==1 && pProbe->nSample
     &&  OptimizationEnabled(db, SQLITE_Stat3) ){
      tRowcnt nOut = 0;
      if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
        testcase( pTerm->eOperator & WO_EQ );
        testcase( pTerm->eOperator & WO_ISNULL );
        rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
      }else if( (pTerm->eOperator & WO_IN)
             &&  !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)  ){
        rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
      }
      assert( nOut==0 || rc==SQLITE_OK );
      if( nOut ) pNew->nOut = whereCost(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 =  whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10);
    }
    /* Step cost for each output row */
    pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut);
    /* TBD: Adjust nOut for additional constraints */
    rc = whereLoopInsert(pBuilder, pNew);
    if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
     && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
    ){
      whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
    }
  }
  pNew->prereq = saved_prereq;
  pNew->u.btree.nEq = saved_nEq;
  pNew->wsFlags = saved_wsFlags;
  pNew->nOut = saved_nOut;
  pNew->nLTerm = saved_nLTerm;
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %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 0x5179e
pushq $0x7
popq %r14
jmp 0x51c18
movq %rdx, %r12
movq %rsi, %rdx
movq %rcx, -0x68(%rbp)
movq 0x18(%rdi), %r13
testb $0x20, 0x28(%r13)
movq %rax, -0xa0(%rbp)
jne 0x517d3
cmpl $0x0, 0x50(%r12)
jle 0x517d7
xorl %eax, %eax
testb $0x8, 0x38(%rdx)
sete %al
shll $0x7, %eax
orl $0x3f, %eax
jmp 0x517da
pushq $0x18
jmp 0x517d9
pushq $0x3f
popq %rax
movq %rdi, -0x70(%rbp)
movl %eax, %ebx
andl $-0x7d, %ebx
testb $0x4, 0x57(%r12)
cmovel %eax, %ebx
movslq 0x18(%r13), %rax
movzwl 0x54(%r12), %ecx
cmpl %ecx, %eax
movq %rdx, -0x60(%rbp)
jge 0x5181f
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rdx
movl (%rcx,%rax,4), %r14d
movl 0x4(%rdx,%rax,4), %edi
callq 0x5154b
testw %ax, %ax
je 0x51828
movl %eax, %r15d
jmp 0x51835
pushq $-0x1
popq %r14
xorl %r15d, %r15d
jmp 0x51839
xorl %r15d, %r15d
cmpb $0x0, 0x56(%r12)
sete %r15b
movq -0x60(%rbp), %rdx
movl %r14d, -0x30(%rbp)
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rsi
movl 0x3c(%rdx), %edx
leaq -0x120(%rbp), %rdi
movl %r14d, %ecx
movl %ebx, %r8d
movq %rdi, %rbx
movq %r12, %r9
callq 0x50547
movl 0x18(%r13), %ecx
movl %ecx, -0x44(%rbp)
movzwl 0x2c(%r13), %r14d
movl 0x28(%r13), %ecx
movl %ecx, -0x40(%rbp)
movq (%r13), %rcx
movq %rcx, -0x98(%rbp)
movzwl 0x16(%r13), %ecx
movl %ecx, -0x3c(%rbp)
andw $0x0, 0x12(%r13)
movq %r12, %rcx
movq %rax, %r12
movq %rcx, -0x38(%rbp)
movq 0x10(%rcx), %rax
movl (%rax), %edi
callq 0x5154b
movzwl %ax, %edi
callq 0x516bf
movl -0x30(%rbp), %ecx
movq %rcx, -0x90(%rbp)
movl %r14d, -0x48(%rbp)
incl %r14d
movl %r14d, -0x84(%rbp)
movzwl %r15w, %edx
movq -0x68(%rbp), %rcx
movzwl %cx, %esi
addl %edx, %esi
movl %esi, -0x80(%rbp)
leal (%r15,%rcx), %edx
movl %edx, -0x78(%rbp)
addl %r15d, %ecx
addl $0xa, %ecx
movl %ecx, -0x74(%rbp)
leal -0x11(%rax), %ecx
movq %rax, -0x30(%rbp)
cmpw $0x1c, %ax
pushq $0xa
popq %rax
cmovbl %eax, %ecx
movzwl %cx, %eax
movl %eax, -0x7c(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
testl %r14d, %r14d
jne 0x51bef
testq %r12, %r12
je 0x51bef
movq 0x8(%r13), %rax
xorl %r15d, %r15d
testq %rax, 0x28(%r12)
jne 0x51bbd
cmpw $0x80, 0x18(%r12)
jne 0x51963
movq -0x90(%rbp), %rcx
testl %ecx, %ecx
js 0x51bbd
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rax
imulq $0x30, %rcx, %rcx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x51bbd
movl -0x40(%rbp), %eax
movl %eax, 0x28(%r13)
movl -0x44(%rbp), %eax
movl %eax, 0x18(%r13)
movl -0x48(%rbp), %eax
movw %ax, 0x2c(%r13)
movq -0xa0(%rbp), %rdi
movq %r13, %rsi
movl -0x84(%rbp), %edx
callq 0x512f5
testl %eax, %eax
jne 0x51bef
movq 0x30(%r13), %rcx
movzwl 0x2c(%r13), %edx
leal 0x1(%rdx), %eax
movw %ax, 0x2c(%r13)
movq %r12, (%rcx,%rdx,8)
movq 0x28(%r12), %rcx
orq -0x98(%rbp), %rcx
movq 0x8(%r13), %rdx
notq %rdx
andq %rcx, %rdx
movq %rdx, (%r13)
movq -0x30(%rbp), %rcx
movw %cx, 0x14(%r13)
movzwl 0x18(%r12), %ecx
testb $0x1, %cl
jne 0x51a16
testb $0x2, %cl
jne 0x51a6c
testb %cl, %cl
js 0x51aca
movl 0x28(%r13), %edx
testb $0x24, %cl
je 0x51aec
orl $0x22, %edx
movl %edx, 0x28(%r13)
movq $0x0, -0x50(%rbp)
movq -0x30(%rbp), %rax
movl %edx, %r14d
movq %r12, -0x58(%rbp)
jmp 0x51b10
movq (%r12), %rax
movl 0x28(%r13), %r14d
orl $0x4, %r14d
movl %r14d, 0x28(%r13)
testb $0x8, 0x3(%rax)
pushq $0x2e
popq %rcx
movl %ecx, %ebx
movq -0x38(%rbp), %r12
jne 0x51a4e
movq 0x20(%rax), %rax
xorl %ebx, %ebx
testq %rax, %rax
je 0x51a4e
movl (%rax), %edi
testl %edi, %edi
je 0x51a4e
callq 0x5154b
movzwl %ax, %ebx
movq -0x30(%rbp), %rax
addl %ebx, %eax
movw %ax, 0x14(%r13)
incl 0x18(%r13)
movl %ebx, %ecx
addl -0x80(%rbp), %ecx
movw %cx, 0x16(%r13)
jmp 0x51b16
movl 0x18(%r13), %eax
movl 0x28(%r13), %ecx
movl %ecx, %r14d
orl $0x1, %r14d
movl %r14d, 0x28(%r13)
cmpl $0x0, -0x90(%rbp)
movq -0x38(%rbp), %r12
js 0x51aa7
cmpw $0x0, -0x68(%rbp)
jne 0x51ab4
cmpb $0x0, 0x56(%r12)
je 0x51ab4
movzwl 0x54(%r12), %edx
decl %edx
cmpl %edx, %eax
jne 0x51ab4
orl $0x1001, %ecx           # imm = 0x1001
movl %ecx, 0x28(%r13)
movl %ecx, %r14d
incl %eax
movl %eax, 0x18(%r13)
movl -0x78(%rbp), %eax
movw %ax, 0x16(%r13)
xorl %ebx, %ebx
movq -0x30(%rbp), %rax
jmp 0x51b16
movl 0x28(%r13), %r14d
orl $0x8, %r14d
movl %r14d, 0x28(%r13)
incl 0x18(%r13)
movl -0x74(%rbp), %eax
movw %ax, 0x16(%r13)
movw $0xa, %bx
movq -0x30(%rbp), %rax
jmp 0x51b12
movl %edx, %r14d
orl $0x12, %r14d
movl %r14d, 0x28(%r13)
testb $0x20, %dl
jne 0x51bd0
movq $0x0, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq %r12, -0x50(%rbp)
xorl %ebx, %ebx
movq -0x38(%rbp), %r12
testb $0x2, %r14b
je 0x51b4f
cmpq $0x0, -0x58(%rbp)
pushq $0x14
popq %rcx
movl $0x0, %edx
cmovel %edx, %ecx
leal 0x14(%rcx), %edx
cmpq $0x0, -0x50(%rbp)
cmovel %ecx, %edx
movl -0x3c(%rbp), %esi
movl %esi, %ecx
subl %edx, %ecx
addl $0xa, %edx
cmpw %si, %dx
pushq $0xa
popq %rdx
cmovael %edx, %ecx
movw %cx, 0x16(%r13)
testw $0x140, %r14w           # imm = 0x140
jne 0x51b62
movzwl %ax, %edi
movl -0x7c(%rbp), %esi
callq 0x51712
movq -0x70(%rbp), %r14
movzwl 0x16(%r13), %esi
movzwl %ax, %edi
callq 0x51712
movw %ax, 0x14(%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x5137b
movl %eax, %r15d
testb $0x10, 0x28(%r13)
jne 0x51bb6
movzwl 0x54(%r12), %eax
cmpq $0x1, (%r12)
sbbl $-0x1, %eax
cmpl %eax, 0x18(%r13)
jge 0x51bb6
addl -0x68(%rbp), %ebx
movzwl %bx, %ecx
movq %r14, %rdi
movq -0x60(%rbp), %rsi
movq %r12, %rdx
callq 0x51772
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
callq 0x505c9
movq %rax, %r12
movl %r15d, %r14d
jmp 0x5190b
movq 0x30(%r13), %rcx
movzwl %ax, %eax
movq -0x10(%rcx,%rax,8), %rax
movq %rax, -0x58(%rbp)
xorl %ebx, %ebx
movq -0x30(%rbp), %rax
movq %r12, -0x50(%rbp)
jmp 0x51b12
movq -0x98(%rbp), %rax
movq %rax, (%r13)
movl -0x44(%rbp), %eax
movl %eax, 0x18(%r13)
movl -0x40(%rbp), %eax
movl %eax, 0x28(%r13)
movl -0x3c(%rbp), %eax
movw %ax, 0x16(%r13)
movl -0x48(%rbp), %eax
movw %ax, 0x2c(%r13)
movl %r14d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq