// File: /workspace/github_c_makefile_star2_O1/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, %ebx
cmpb $0x0, 0x50(%rax)
jne 0x5aa3e
movq %rdx, %r15
movq %rax, -0xa0(%rbp)
movq %rsi, -0x60(%rbp)
movq %rcx, -0x68(%rbp)
movq 0x18(%rdi), %r14
movl $0x18, %eax
testb $0x20, 0x28(%r14)
jne 0x5a620
cmpl $0x0, 0x50(%r15)
jle 0x5a61b
xorl %eax, %eax
movq -0x60(%rbp), %rcx
testb $0x8, 0x38(%rcx)
sete %al
shll $0x7, %eax
orl $0x3f, %eax
jmp 0x5a620
movl $0x3f, %eax
movl %eax, %r13d
andl $-0x7d, %r13d
testb $0x4, 0x57(%r15)
cmovel %eax, %r13d
movslq 0x18(%r14), %rax
movzwl 0x54(%r15), %ecx
xorl %ebx, %ebx
cmpl %ecx, %eax
movq %rdi, -0x58(%rbp)
jge 0x5a67c
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rdx
movl (%rcx,%rax,4), %ecx
movl %ecx, -0x30(%rbp)
movl 0x4(%rdx,%rax,4), %edi
callq 0x5a359
movl %eax, %r12d
testw %ax, %ax
jne 0x5a673
cmpb $0x0, 0x56(%r15)
movl $0x1, %eax
cmovnel %r12d, %eax
movl %eax, %r12d
movq -0x58(%rbp), %rdi
movl -0x30(%rbp), %ecx
jmp 0x5a684
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movq 0x8(%rdi), %rsi
movq -0x60(%rbp), %rax
movl 0x3c(%rax), %edx
leaq -0x120(%rbp), %rdi
movl %ecx, -0x30(%rbp)
movl %r13d, %r8d
movq %r15, %r9
callq 0x5917f
movq %rax, %r13
movl 0x18(%r14), %eax
movl %eax, -0x3c(%rbp)
movzwl 0x2c(%r14), %eax
movw %ax, -0x2a(%rbp)
movl 0x28(%r14), %eax
movl %eax, -0x38(%rbp)
movq (%r14), %rax
movq %rax, -0x90(%rbp)
movzwl 0x16(%r14), %eax
movl %eax, -0x34(%rbp)
movw $0x0, 0x12(%r14)
movq 0x10(%r15), %rax
movl (%rax), %edi
callq 0x5a359
movzwl %ax, %edi
callq 0x5a359
subw $0x21, %ax
cmovbl %ebx, %eax
movq %rax, -0x70(%rbp)
testq %r13, %r13
je 0x5aa15
movzwl -0x2a(%rbp), %eax
movl -0x30(%rbp), %ecx
movq %rcx, -0x98(%rbp)
incl %eax
movl %eax, -0x84(%rbp)
movzwl %r12w, %eax
movq -0x68(%rbp), %rcx
movzwl %cx, %edx
addl %eax, %edx
movl %edx, -0x80(%rbp)
leal (%r12,%rcx), %eax
movl %eax, -0x78(%rbp)
leal (%r12,%rcx), %eax
addl $0xa, %eax
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rcx
leal -0x11(%rcx), %eax
cmpw $0x1c, %cx
movl $0xa, %ecx
cmovbl %ecx, %eax
movzwl %ax, %eax
movl %eax, -0x7c(%rbp)
leaq -0x120(%rbp), %r12
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq 0x8(%r14), %rax
testq %rax, 0x28(%r13)
je 0x5a798
xorl %ebx, %ebx
xorl %ecx, %ecx
testb %cl, %cl
jne 0x5aa15
movq %r12, %rdi
callq 0x5920e
testl %ebx, %ebx
jne 0x5aa15
movq %rax, %r13
testq %rax, %rax
jne 0x5a765
jmp 0x5aa15
cmpw $0x80, 0x18(%r13)
jne 0x5a7cb
cmpl $0x0, -0x30(%rbp)
js 0x5a76f
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rax
movq -0x98(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
xorl %ebx, %ebx
cmpb $0x0, 0x28(%rax,%rcx)
jne 0x5a771
movl -0x38(%rbp), %eax
movl %eax, 0x28(%r14)
movl -0x3c(%rbp), %eax
movl %eax, 0x18(%r14)
movzwl -0x2a(%rbp), %eax
movw %ax, 0x2c(%r14)
movq -0xa0(%rbp), %rdi
movq %r14, %rsi
movl -0x84(%rbp), %edx
callq 0x5a0cb
movb $0x1, %cl
testl %eax, %eax
je 0x5a804
xorl %ebx, %ebx
jmp 0x5a773
movq 0x30(%r14), %rcx
movzwl 0x2c(%r14), %edx
leal 0x1(%rdx), %eax
movw %ax, 0x2c(%r14)
movq %r13, (%rcx,%rdx,8)
movq 0x28(%r13), %rcx
orq -0x90(%rbp), %rcx
movq 0x8(%r14), %rdx
notq %rdx
andq %rcx, %rdx
movq %rdx, (%r14)
movq -0x70(%rbp), %rcx
movw %cx, 0x14(%r14)
movzwl 0x18(%r13), %ecx
testb $0x1, %cl
jne 0x5a876
testb $0x2, %cl
jne 0x5a8c3
testb %cl, %cl
js 0x5a90b
movl 0x28(%r14), %edx
testb $0x24, %cl
je 0x5a9da
orl $0x22, %edx
movl %edx, 0x28(%r14)
movq $0x0, -0x48(%rbp)
movq %r13, -0x50(%rbp)
jmp 0x5a906
movq (%r13), %rax
orl $0x4, 0x28(%r14)
movl $0x2e, %r12d
testb $0x8, 0x3(%rax)
jne 0x5a8a6
movq 0x20(%rax), %rax
xorl %r12d, %r12d
testq %rax, %rax
je 0x5a8a6
movl (%rax), %edi
testl %edi, %edi
je 0x5a8a6
callq 0x5a359
movzwl %ax, %r12d
movq -0x70(%rbp), %rax
addl %r12d, %eax
movw %ax, 0x14(%r14)
incl 0x18(%r14)
movl %r12d, %eax
addl -0x80(%rbp), %eax
movw %ax, 0x16(%r14)
jmp 0x5a921
movl 0x28(%r14), %eax
movl %eax, %ecx
orl $0x1, %ecx
movl %ecx, 0x28(%r14)
cmpl $0x0, -0x30(%rbp)
js 0x5a8f1
cmpw $0x0, -0x68(%rbp)
jne 0x5a8fa
cmpb $0x0, 0x56(%r15)
je 0x5a8fa
movzwl 0x54(%r15), %ecx
decl %ecx
cmpl %ecx, 0x18(%r14)
jne 0x5a8fa
orl $0x1001, %eax           # imm = 0x1001
movl %eax, 0x28(%r14)
incl 0x18(%r14)
movl -0x78(%rbp), %eax
movw %ax, 0x16(%r14)
xorl %r12d, %r12d
jmp 0x5a921
orb $0x8, 0x28(%r14)
incl 0x18(%r14)
movl -0x74(%rbp), %eax
movw %ax, 0x16(%r14)
movw $0xa, %r12w
movl 0x28(%r14), %eax
testb $0x2, %al
je 0x5a960
cmpq $0x0, -0x50(%rbp)
movl $0x14, %ecx
movl $0x0, %edx
cmovel %edx, %ecx
leal 0x14(%rcx), %edx
cmpq $0x0, -0x48(%rbp)
cmovel %ecx, %edx
movl -0x34(%rbp), %esi
movl %esi, %ecx
subl %edx, %ecx
addl $0xa, %edx
cmpw %si, %dx
movl $0xa, %edx
cmovael %edx, %ecx
movw %cx, 0x16(%r14)
testl $0x140, %eax            # imm = 0x140
jne 0x5a979
movzwl 0x14(%r14), %edi
movl -0x7c(%rbp), %esi
callq 0x5a551
movw %ax, 0x14(%r14)
movzwl 0x14(%r14), %edi
movzwl 0x16(%r14), %esi
callq 0x5a551
movw %ax, 0x14(%r14)
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x5a153
movl %eax, %ebx
testb $0x10, 0x28(%r14)
jne 0x5a9cc
movzwl 0x54(%r15), %eax
cmpq $0x1, (%r15)
sbbl $-0x1, %eax
cmpl %eax, 0x18(%r14)
jge 0x5a9cc
addl -0x68(%rbp), %r12d
movzwl %r12w, %ecx
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq %r15, %rdx
callq 0x5a5b1
xorl %ecx, %ecx
leaq -0x120(%rbp), %r12
jmp 0x5a773
movl %edx, %ecx
orl $0x12, %ecx
movl %ecx, 0x28(%r14)
testb $0x20, %dl
jne 0x5a9f9
movq $0x0, -0x50(%rbp)
movq %r13, -0x48(%rbp)
jmp 0x5a906
movq 0x30(%r14), %rcx
movzwl %ax, %eax
movq -0x10(%rcx,%rax,8), %rax
movq %rax, -0x50(%rbp)
xorl %r12d, %r12d
movq %r13, -0x48(%rbp)
jmp 0x5a921
movq -0x90(%rbp), %rax
movq %rax, (%r14)
movl -0x3c(%rbp), %eax
movl %eax, 0x18(%r14)
movl -0x38(%rbp), %eax
movl %eax, 0x28(%r14)
movl -0x34(%rbp), %eax
movw %ax, 0x16(%r14)
movzwl -0x2a(%rbp), %eax
movw %ax, 0x2c(%r14)
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq