// File: /workspace/github_c_makefile_star2_O3/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
movl $0x7, %r15d
cmpb $0x0, 0x50(%rax)
jne 0x5aa0b
movl %ecx, %r12d
movq %rsi, %r8
movq %rax, -0xa0(%rbp)
movq 0x18(%rdi), %rbx
movl $0x18, %eax
testb $0x20, 0x28(%rbx)
jne 0x5a5d4
cmpl $0x0, 0x50(%rdx)
jle 0x5a5cf
xorl %eax, %eax
testb $0x8, 0x38(%r8)
sete %al
shll $0x7, %eax
orl $0x3f, %eax
jmp 0x5a5d4
movl $0x3f, %eax
movq %rdi, -0x40(%rbp)
movl %eax, %r14d
andl $-0x7d, %r14d
testb $0x4, 0x57(%rdx)
cmovel %eax, %r14d
movslq 0x18(%rbx), %rax
movzwl 0x54(%rdx), %ecx
xorl %r15d, %r15d
cmpl %ecx, %eax
movq %rdx, -0x70(%rbp)
movq %r12, -0x68(%rbp)
movq %r8, -0x60(%rbp)
jge 0x5a639
movq %rdx, %r13
movq 0x8(%rdx), %rcx
movq 0x10(%rdx), %rdx
movl (%rcx,%rax,4), %ecx
movl %ecx, -0x30(%rbp)
movl 0x4(%rdx,%rax,4), %edi
callq 0x5a316
testw %ax, %ax
jne 0x5a62b
xorl %eax, %eax
cmpb $0x0, 0x56(%r13)
sete %al
movq %rax, -0x38(%rbp)
movq -0x60(%rbp), %r8
movl -0x30(%rbp), %r13d
jmp 0x5a647
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
movl 0x3c(%r8), %edx
leaq -0x120(%rbp), %rdi
movl %r13d, %ecx
movl %r14d, %r8d
movq -0x70(%rbp), %r12
movq %r12, %r9
callq 0x59176
movq %rax, %r14
movl 0x18(%rbx), %eax
movl %eax, -0x4c(%rbp)
movzwl 0x2c(%rbx), %eax
movw %ax, -0x2a(%rbp)
movl 0x28(%rbx), %eax
movl %eax, -0x48(%rbp)
movq (%rbx), %rax
movq %rax, -0x90(%rbp)
movzwl 0x16(%rbx), %eax
movl %eax, -0x44(%rbp)
movw $0x0, 0x12(%rbx)
movq 0x10(%r12), %rax
movl (%rax), %edi
callq 0x5a316
movzwl %ax, %edi
callq 0x5a316
movl %eax, %r12d
subw $0x21, %r12w
cmovbl %r15d, %r12d
testq %r14, %r14
je 0x5a9e6
movzwl -0x2a(%rbp), %eax
movl %r13d, %ecx
movq %rcx, -0x98(%rbp)
incl %eax
movl %eax, -0x84(%rbp)
movq -0x38(%rbp), %rsi
movzwl %si, %eax
movq -0x68(%rbp), %rcx
movzwl %cx, %edx
addl %eax, %edx
movl %edx, -0x80(%rbp)
leal (%rsi,%rcx), %eax
movl %eax, -0x78(%rbp)
leal (%rsi,%rcx), %eax
addl $0xa, %eax
movl %eax, -0x74(%rbp)
leal -0x11(%r12), %eax
cmpw $0x1c, %r12w
movl $0xa, %ecx
cmovbl %ecx, %eax
movzwl %ax, %eax
movl %eax, -0x7c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movl %r13d, -0x30(%rbp)
movq 0x8(%rbx), %rax
testq %rax, 0x28(%r14)
je 0x5a739
xorl %r15d, %r15d
jmp 0x5a9a5
cmpw $0x80, 0x18(%r14)
jne 0x5a769
testl %r13d, %r13d
js 0x5a731
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rax
movq -0x98(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x5a731
movl -0x48(%rbp), %eax
movl %eax, 0x28(%rbx)
movl -0x4c(%rbp), %eax
movl %eax, 0x18(%rbx)
movzwl -0x2a(%rbp), %eax
movw %ax, 0x2c(%rbx)
movq -0xa0(%rbp), %rdi
movq %rbx, %rsi
movl -0x84(%rbp), %edx
callq 0x5a0a9
testl %eax, %eax
jne 0x5a9e3
movq 0x30(%rbx), %rcx
movzwl 0x2c(%rbx), %edx
leal 0x1(%rdx), %eax
movw %ax, 0x2c(%rbx)
movq %r14, (%rcx,%rdx,8)
movq 0x28(%r14), %rcx
orq -0x90(%rbp), %rcx
movq 0x8(%rbx), %rdx
notq %rdx
andq %rcx, %rdx
movq %rdx, (%rbx)
movw %r12w, 0x14(%rbx)
movzwl 0x18(%r14), %ecx
testb $0x1, %cl
jne 0x5a80e
testb $0x2, %cl
jne 0x5a862
testb %cl, %cl
js 0x5a8b7
movl 0x28(%rbx), %edx
testb $0x24, %cl
je 0x5a8d7
orl $0x22, %edx
movl %edx, 0x28(%rbx)
movq $0x0, -0x58(%rbp)
movl %r12d, %eax
movl %edx, %r15d
movq %r14, -0x38(%rbp)
jmp 0x5a8fa
movq (%r14), %rax
movl 0x28(%rbx), %r15d
orl $0x4, %r15d
movl %r15d, 0x28(%rbx)
movl $0x2e, %r13d
testb $0x8, 0x3(%rax)
movq -0x40(%rbp), %r14
jne 0x5a848
movq 0x20(%rax), %rax
xorl %r13d, %r13d
testq %rax, %rax
je 0x5a848
movl (%rax), %edi
testl %edi, %edi
je 0x5a848
callq 0x5a316
movzwl %ax, %r13d
leal (%r12,%r13), %eax
movw %ax, 0x14(%rbx)
incl 0x18(%rbx)
movl %r13d, %ecx
addl -0x80(%rbp), %ecx
movw %cx, 0x16(%rbx)
jmp 0x5a901
movl 0x18(%rbx), %eax
movl 0x28(%rbx), %ecx
movl %ecx, %r15d
orl $0x1, %r15d
movl %r15d, 0x28(%rbx)
testl %r13d, %r13d
movq -0x40(%rbp), %r14
js 0x5a897
cmpw $0x0, -0x68(%rbp)
jne 0x5a8a3
movq -0x70(%rbp), %rdx
cmpb $0x0, 0x56(%rdx)
je 0x5a8a3
movzwl 0x54(%rdx), %edx
decl %edx
cmpl %edx, %eax
jne 0x5a8a3
orl $0x1001, %ecx           # imm = 0x1001
movl %ecx, 0x28(%rbx)
movl %ecx, %r15d
incl %eax
movl %eax, 0x18(%rbx)
movl -0x78(%rbp), %eax
movw %ax, 0x16(%rbx)
xorl %r13d, %r13d
movl %r12d, %eax
jmp 0x5a901
movl 0x28(%rbx), %r15d
orl $0x8, %r15d
movl %r15d, 0x28(%rbx)
incl 0x18(%rbx)
movl -0x74(%rbp), %eax
movw %ax, 0x16(%rbx)
movw $0xa, %r13w
movl %r12d, %eax
jmp 0x5a8fd
movl %edx, %r15d
orl $0x12, %r15d
movl %r15d, 0x28(%rbx)
testb $0x20, %dl
jne 0x5a9c4
movq $0x0, -0x38(%rbp)
movl %r12d, %eax
movq %r14, -0x58(%rbp)
xorl %r13d, %r13d
movq -0x40(%rbp), %r14
testb $0x2, %r15b
je 0x5a93d
cmpq $0x0, -0x38(%rbp)
movl $0x14, %ecx
movl $0x0, %edx
cmovel %edx, %ecx
leal 0x14(%rcx), %edx
cmpq $0x0, -0x58(%rbp)
cmovel %ecx, %edx
movl -0x44(%rbp), %esi
movl %esi, %ecx
subl %edx, %ecx
addl $0xa, %edx
cmpw %si, %dx
movl $0xa, %edx
cmovael %edx, %ecx
movw %cx, 0x16(%rbx)
testl $0x140, %r15d           # imm = 0x140
jne 0x5a951
movzwl %ax, %edi
movl -0x7c(%rbp), %esi
callq 0x5a50e
movzwl 0x16(%rbx), %esi
movzwl %ax, %edi
callq 0x5a50e
movw %ax, 0x14(%rbx)
movq (%r14), %rdi
movq 0x20(%r14), %rsi
movq %rbx, %rdx
callq 0x5a131
movl %eax, %r15d
testb $0x10, 0x28(%rbx)
jne 0x5a9a1
movq -0x70(%rbp), %rdx
movzwl 0x54(%rdx), %eax
cmpq $0x1, (%rdx)
sbbl $-0x1, %eax
cmpl %eax, 0x18(%rbx)
jge 0x5a9a1
addl -0x68(%rbp), %r13d
movzwl %r13w, %ecx
movq %r14, %rdi
movq -0x60(%rbp), %rsi
callq 0x5a56e
movl -0x30(%rbp), %r13d
leaq -0x120(%rbp), %rdi
callq 0x59205
testl %r15d, %r15d
jne 0x5a9e6
movq %rax, %r14
testq %rax, %rax
jne 0x5a727
jmp 0x5a9e6
movq 0x30(%rbx), %rcx
movzwl %ax, %eax
movq -0x10(%rcx,%rax,8), %rax
movq %rax, -0x38(%rbp)
xorl %r13d, %r13d
movl %r12d, %eax
movq %r14, -0x58(%rbp)
jmp 0x5a8fd
xorl %r15d, %r15d
movq -0x90(%rbp), %rax
movq %rax, (%rbx)
movl -0x4c(%rbp), %eax
movl %eax, 0x18(%rbx)
movl -0x48(%rbp), %eax
movl %eax, 0x28(%rbx)
movl -0x44(%rbp), %eax
movw %ax, 0x16(%rbx)
movzwl -0x2a(%rbp), %eax
movw %ax, 0x2c(%rbx)
movl %r15d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq