// File: /workspace/github_c_makefile_star2_O2/skeeto[P]flappy/sqlite3.c
// Function: wherePathSolver
static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
  int mxChoice;             /* Maximum number of simultaneous paths tracked */
  int nLoop;                /* Number of terms in the join */
  Parse *pParse;            /* Parsing context */
  sqlite3 *db;              /* The database connection */
  int iLoop;                /* Loop counter over the terms of the join */
  int ii, jj;               /* Loop counters */
  int mxI = 0;              /* Index of next entry to replace */
  LogEst rCost;             /* Cost of a path */
  LogEst nOut;              /* Number of outputs */
  LogEst mxCost = 0;        /* Maximum cost of a set of paths */
  LogEst mxOut = 0;         /* Maximum nOut value on the set of paths */
  LogEst rSortCost;         /* Cost to do a sort */
  int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
  WherePath *aFrom;         /* All nFrom paths at the previous level */
  WherePath *aTo;           /* The nTo best paths at the current level */
  WherePath *pFrom;         /* An element of aFrom[] that we are working on */
  WherePath *pTo;           /* An element of aTo[] that we are working on */
  WhereLoop *pWLoop;        /* One of the WhereLoop objects */
  WhereLoop **pX;           /* Used to divy up the pSpace memory */
  char *pSpace;             /* Temporary memory used by this routine */

  pParse = pWInfo->pParse;
  db = pParse->db;
  nLoop = pWInfo->nLevel;
  /* TUNING: For simple queries, only the best path is tracked.
  ** For 2-way joins, the 5 best paths are followed.
  ** For joins of 3 or more tables, track the 10 best paths */
  mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10);
  assert( nLoop<=pWInfo->pTabList->nSrc );
  WHERETRACE(0x002, ("---- begin solver\n"));

  /* Allocate and initialize space for aTo and aFrom */
  ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
  pSpace = sqlite3DbMallocRaw(db, ii);
  if( pSpace==0 ) return SQLITE_NOMEM;
  aTo = (WherePath*)pSpace;
  aFrom = aTo+mxChoice;
  memset(aFrom, 0, sizeof(aFrom[0]));
  pX = (WhereLoop**)(aFrom+mxChoice);
  for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
    pFrom->aLoop = pX;
  }

  /* Seed the search with a single WherePath containing zero WhereLoops.
  **
  ** TUNING: Do not let the number of iterations go above 25.  If the cost
  ** of computing an automatic index is not paid back within the first 25
  ** rows, then do not use the automatic index. */
  aFrom[0].nRow = MIN(pParse->nQueryLoop, 46);  assert( 46==sqlite3LogEst(25) );
  nFrom = 1;

  /* Precompute the cost of sorting the final result set, if the caller
  ** to sqlite3WhereBegin() was concerned about sorting */
  rSortCost = 0;
  if( pWInfo->pOrderBy==0 || nRowEst==0 ){
    aFrom[0].isOrderedValid = 1;
  }else{
    /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
    ** number of output rows. The 48 is the expected size of a row to sort. 
    ** FIXME:  compute a better estimate of the 48 multiplier based on the
    ** result set expressions. */
    rSortCost = nRowEst + estLog(nRowEst);
    WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
  }

  /* Compute successively longer WherePaths using the previous generation
  ** of WherePaths as the basis for the next.  Keep track of the mxChoice
  ** best paths at each generation */
  for(iLoop=0; iLoop<nLoop; iLoop++){
    nTo = 0;
    for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
      for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
        Bitmask maskNew;
        Bitmask revMask = 0;
        u8 isOrderedValid = pFrom->isOrderedValid;
        u8 isOrdered = pFrom->isOrdered;
        if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
        if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
        /* At this point, pWLoop is a candidate to be the next loop. 
        ** Compute its cost */
        rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
        rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
        nOut = pFrom->nRow + pWLoop->nOut;
        maskNew = pFrom->maskLoop | pWLoop->maskSelf;
        if( !isOrderedValid ){
          switch( wherePathSatisfiesOrderBy(pWInfo,
                       pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
                       iLoop, pWLoop, &revMask) ){
            case 1:  /* Yes.  pFrom+pWLoop does satisfy the ORDER BY clause */
              isOrdered = 1;
              isOrderedValid = 1;
              break;
            case 0:  /* No.  pFrom+pWLoop will require a separate sort */
              isOrdered = 0;
              isOrderedValid = 1;
              rCost = sqlite3LogEstAdd(rCost, rSortCost);
              break;
            default: /* Cannot tell yet.  Try again on the next iteration */
              break;
          }
        }else{
          revMask = pFrom->revLoop;
        }
        /* Check to see if pWLoop should be added to the mxChoice best so far */
        for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
          if( pTo->maskLoop==maskNew
           && pTo->isOrderedValid==isOrderedValid
           && ((pTo->rCost<=rCost && pTo->nRow<=nOut) ||
                (pTo->rCost>=rCost && pTo->nRow>=nOut))
          ){
            testcase( jj==nTo-1 );
            break;
          }
        }
        if( jj>=nTo ){
          if( nTo>=mxChoice && rCost>=mxCost ){
#ifdef WHERETRACE_ENABLED /* 0x4 */
            if( sqlite3WhereTrace&0x4 ){
              sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
            }
#endif
            continue;
          }
          /* Add a new Path to the aTo[] set */
          if( nTo<mxChoice ){
            /* Increase the size of the aTo set by one */
            jj = nTo++;
          }else{
            /* New path replaces the prior worst to keep count below mxChoice */
            jj = mxI;
          }
          pTo = &aTo[jj];
#ifdef WHERETRACE_ENABLED /* 0x4 */
          if( sqlite3WhereTrace&0x4 ){
            sqlite3DebugPrintf("New    %s cost=%-3d,%3d order=%c\n",
                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
          }
#endif
        }else{
          if( pTo->rCost<=rCost && pTo->nRow<=nOut ){
#ifdef WHERETRACE_ENABLED /* 0x4 */
            if( sqlite3WhereTrace&0x4 ){
              sqlite3DebugPrintf(
                  "Skip   %s cost=%-3d,%3d order=%c",
                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
              sqlite3DebugPrintf("   vs %s cost=%-3d,%d order=%c\n",
                  wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
                  pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
            }
#endif
            testcase( pTo->rCost==rCost );
            continue;
          }
          testcase( pTo->rCost==rCost+1 );
          /* A new and better score for a previously created equivalent path */
#ifdef WHERETRACE_ENABLED /* 0x4 */
          if( sqlite3WhereTrace&0x4 ){
            sqlite3DebugPrintf(
                "Update %s cost=%-3d,%3d order=%c",
                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
            sqlite3DebugPrintf("  was %s cost=%-3d,%3d order=%c\n",
                wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
                pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
          }
#endif
        }
        /* pWLoop is a winner.  Add it to the set of best so far */
        pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
        pTo->revLoop = revMask;
        pTo->nRow = nOut;
        pTo->rCost = rCost;
        pTo->isOrderedValid = isOrderedValid;
        pTo->isOrdered = isOrdered;
        memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
        pTo->aLoop[iLoop] = pWLoop;
        if( nTo>=mxChoice ){
          mxI = 0;
          mxCost = aTo[0].rCost;
          mxOut = aTo[0].nRow;
          for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
            if( pTo->rCost>mxCost || (pTo->rCost==mxCost && pTo->nRow>mxOut) ){
              mxCost = pTo->rCost;
              mxOut = pTo->nRow;
              mxI = jj;
            }
          }
        }
      }
    }

#ifdef WHERETRACE_ENABLED  /* >=2 */
    if( sqlite3WhereTrace>=2 ){
      sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
      for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
        sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
           wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
           pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
        if( pTo->isOrderedValid && pTo->isOrdered ){
          sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
        }else{
          sqlite3DebugPrintf("\n");
        }
      }
    }
#endif

    /* Swap the roles of aFrom and aTo for the next generation */
    pFrom = aTo;
    aTo = aFrom;
    aFrom = pFrom;
    nFrom = nTo;
  }

  if( nFrom==0 ){
    sqlite3ErrorMsg(pParse, "no query solution");
    sqlite3DbFree(db, pSpace);
    return SQLITE_ERROR;
  }
  
  /* Find the lowest cost path.  pFrom will be left pointing to that path */
  pFrom = aFrom;
  for(ii=1; ii<nFrom; ii++){
    if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
  }
  assert( pWInfo->nLevel==nLoop );
  /* Load the lowest cost path into pWInfo */
  for(iLoop=0; iLoop<nLoop; iLoop++){
    WhereLevel *pLevel = pWInfo->a + iLoop;
    pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
    pLevel->iFrom = pWLoop->iTab;
    pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
  }
  if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
   && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
   && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
   && nRowEst
  ){
    Bitmask notUsed;
    int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
                 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
    if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
  }
  if( pFrom->isOrdered ){
    if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
    }else{
      pWInfo->bOBSat = 1;
      pWInfo->revMask = pFrom->revLoop;
    }
  }
  pWInfo->nRowOut = pFrom->nRow;

  /* Free temporary memory and return success */
  sqlite3DbFree(db, pSpace);
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %esi, 0x1c(%rsp)
movq %rdi, %rbx
movq (%rdi), %rcx
movzbl 0x38(%rdi), %r15d
xorl %eax, %eax
cmpl $0x2, %r15d
setne %al
cmpl $0x1, %r15d
leal 0x5(%rax,%rax,4), %r12d
cmovel %r15d, %r12d
movq %rcx, 0x70(%rsp)
movq (%rcx), %rdi
leal 0x20(,%r15,8), %esi
leal (%r12,%r12), %ebp
imull %ebp, %esi
movq %rdi, 0x80(%rsp)
callq 0x10a37
testq %rax, %rax
je 0x50f05
movq %rax, %r14
movq %rbx, 0x40(%rsp)
movl %r12d, %eax
shll $0x5, %eax
leaq (%r14,%rax), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14,%rax)
movups %xmm0, (%r14,%rax)
addq %rbx, %rax
movq %r14, %rcx
addq $0x18, %rcx
leal (,%r15,8), %edx
testl %ebp, %ebp
jle 0x50ec4
movq %rax, (%rcx)
decl %ebp
addq $0x20, %rcx
addq %rdx, %rax
jmp 0x50eb2
movq 0x70(%rsp), %rax
movl 0x1f8(%rax), %eax
cmpl $0x2e, %eax
pushq $0x2e
popq %rcx
cmovbl %eax, %ecx
movw %cx, 0x10(%rbx)
cmpw $0x0, 0x1c(%rsp)
movq %r12, 0x28(%rsp)
je 0x50f17
movq 0x40(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x50f17
movl 0x1c(%rsp), %ebp
movswl %bp, %edi
callq 0x53cac
addl %ebp, %eax
jmp 0x50f1d
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x15(%rbx)
xorl %eax, %eax
movq 0x40(%rsp), %rcx
addq $0x20, %rcx
movq %rcx, 0x98(%rsp)
movl %r15d, %ecx
movq %rcx, 0x78(%rsp)
pushq $0x1
popq %rcx
xorl %r9d, %r9d
cwtl
movl %eax, 0x64(%rsp)
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %r14, 0x20(%rsp)
movq %r14, 0x88(%rsp)
movq %r15, 0xa0(%rsp)
cmpq 0x78(%rsp), %r9
je 0x5127e
leaq (,%r9,8), %rax
movq %rax, 0xb0(%rsp)
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, 0x10(%rsp)
cmovlel %eax, %ecx
movl %ecx, 0x68(%rsp)
movq 0x20(%rsp), %rax
addq $0x30, %rax
movq %rax, 0xa8(%rsp)
movzwl %r9w, %eax
movl %eax, 0x6c(%rsp)
movq %rbx, 0x90(%rsp)
movq %rbx, %r14
xorl %ebx, %ebx
movq %r9, 0x50(%rsp)
cmpl 0x68(%rsp), %ebx
je 0x51258
movq 0x98(%rsp), %r15
movq %r14, 0x58(%rsp)
movl %ebx, 0x14(%rsp)
movq (%r15), %r15
testq %r15, %r15
je 0x51245
andq $0x0, 0x48(%rsp)
movq (%r14), %r13
movq %r13, %rax
notq %rax
testq %rax, (%r15)
jne 0x511c6
movq 0x8(%r15), %rbp
testq %r13, %rbp
jne 0x511c6
movq %r11, 0x30(%rsp)
movl %r10d, 0x18(%rsp)
movb 0x15(%r14), %r12b
movb 0x14(%r14), %al
movl %eax, 0x3c(%rsp)
movswl 0x12(%r15), %edi
movzwl 0x10(%r14), %ebx
movzwl 0x14(%r15), %eax
addw %bx, %ax
movswl %ax, %esi
callq 0x53cfe
movswl 0x12(%r14), %esi
movswl %ax, %edi
callq 0x53cfe
movq %r14, %rdx
movl %eax, %r14d
addw 0x16(%r15), %bx
testb %r12b, %r12b
je 0x5106c
movq 0x8(%rdx), %rax
movq %rax, 0x48(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %r11
movl 0x10(%rsp), %edi
movl 0x3c(%rsp), %r8d
jmp 0x510c8
movq 0x40(%rsp), %rdi
movq 0x10(%rdi), %rsi
movzwl 0x32(%rdi), %ecx
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
movl 0x6c(%rsp), %r8d
movq %r15, %r9
callq 0x543fc
testl %eax, %eax
je 0x510a4
cmpl $0x1, %eax
jne 0x51223
movb $0x1, %r12b
movb $0x1, %r8b
jmp 0x510ba
movswl %r14w, %edi
movl 0x64(%rsp), %esi
callq 0x53cfe
movl %eax, %r14d
movb $0x1, %r12b
xorl %r8d, %r8d
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %r11
movl 0x10(%rsp), %edi
orq %r13, %rbp
testl %edi, %edi
movl $0x0, %eax
cmovgl %edi, %eax
movq 0x20(%rsp), %r13
subl $0x1, %eax
jb 0x5111e
cmpq %rbp, (%r13)
jne 0x51118
cmpb %r12b, 0x15(%r13)
jne 0x51118
movzwl 0x12(%r13), %ecx
cmpw %r14w, %cx
jg 0x51104
movzwl 0x10(%r13), %edx
cmpw %bx, %dx
jle 0x51235
cmpw %r14w, %cx
jl 0x51118
movzwl 0x10(%r13), %edx
cmpw %bx, %dx
jge 0x5122b
addq $0x20, %r13
jmp 0x510da
cmpl %esi, %edi
jl 0x5113c
cmpw %r11w, %r14w
jl 0x5113c
movq 0x50(%rsp), %r9
movl 0x18(%rsp), %r10d
movq 0x58(%rsp), %r14
jmp 0x511c2
xorl %eax, %eax
cmpl %esi, %edi
setl %al
movl 0x18(%rsp), %ecx
cmovll %edi, %ecx
addl %eax, %edi
movslq %ecx, %r13
shlq $0x5, %r13
addq 0x20(%rsp), %r13
movl %edi, %ebp
movq 0x8(%r15), %rax
movq 0x58(%rsp), %rcx
orq (%rcx), %rax
movq %rax, (%r13)
movq 0x48(%rsp), %rax
movq %rax, 0x8(%r13)
movw %bx, 0x10(%r13)
movw %r14w, 0x12(%r13)
movq %rcx, %r14
movb %r12b, 0x15(%r13)
movb %r8b, 0x14(%r13)
movq 0x18(%r13), %rdi
movq 0x18(%rcx), %rsi
movq 0xb0(%rsp), %rdx
callq 0x7240
movq 0x28(%rsp), %r8
movq 0x18(%r13), %rax
movq 0x50(%rsp), %r9
movq %r15, (%rax,%r9,8)
movl %ebp, 0x10(%rsp)
cmpl %r8d, %ebp
jge 0x511cf
movl 0x18(%rsp), %r10d
movq 0x30(%rsp), %r11
movl 0x14(%rsp), %ebx
addq $0x38, %r15
jmp 0x50fd0
movq 0x20(%rsp), %rax
movzwl 0x10(%rax), %ecx
movzwl 0x12(%rax), %r11d
xorl %r10d, %r10d
movq 0xa8(%rsp), %rax
pushq $0x1
popq %rdx
movl 0x14(%rsp), %ebx
cmpl %r8d, %edx
jae 0x511c6
movzwl 0x2(%rax), %esi
cmpw %r11w, %si
jle 0x51209
movzwl (%rax), %ecx
movl %edx, %r10d
movl %esi, %r11d
jmp 0x5121b
jne 0x5121b
movzwl (%rax), %edi
cmpw %cx, %di
jle 0x5121b
movl %edx, %r10d
movl %esi, %r11d
movl %edi, %ecx
incl %edx
addq $0x20, %rax
jmp 0x511ef
xorl %r12d, %r12d
jmp 0x51057
cmpw %r14w, %cx
jg 0x51158
cmpw %bx, %dx
movl %edi, %ebp
jle 0x51128
jmp 0x5115a
incl %ebx
addq $0x20, %r14
movq 0xa0(%rsp), %r15
jmp 0x50fb5
incq %r9
movl 0x10(%rsp), %ecx
movq 0x20(%rsp), %rbx
movq 0x90(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x88(%rsp), %r14
jmp 0x50f5c
movq %rbx, %r12
testl %ecx, %ecx
movq 0x40(%rsp), %rbx
je 0x51369
movslq %ecx, %rax
leaq 0x20(%r12), %rcx
pushq $0x1
popq %rdx
cmpq %rax, %rdx
jge 0x512bc
movzwl 0x12(%r12), %edi
movq %rcx, %rsi
cmpw 0x12(%rcx), %di
jg 0x512b0
movq %r12, %rsi
incq %rdx
addq $0x20, %rcx
movq %rsi, %r12
jmp 0x51299
leaq 0x380(%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, 0x78(%rsp)
je 0x512f7
movq 0x18(%r12), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, (%rax)
movzbl 0x10(%rdx), %edx
movb %dl, -0x1c(%rax)
movq 0x8(%rbx), %rsi
imulq $0x68, %rdx, %rdx
movl 0x44(%rsi,%rdx), %edx
movl %edx, -0x3c(%rax)
incq %rcx
addq $0x50, %rax
jmp 0x512c5
movzwl 0x32(%rbx), %eax
andl $0x600, %eax            # imm = 0x600
cmpw $0x400, %ax             # imm = 0x400
jne 0x5134c
cmpw $0x0, 0x1c(%rsp)
je 0x5134c
cmpb $0x0, 0x37(%rbx)
jne 0x5134c
movq 0x18(%rbx), %rsi
movq %r12, %rdx
movq 0x18(%r12), %rax
movq -0x8(%rax,%r15,8), %r9
leaq -0x1(%r15), %rax
leaq 0x48(%rsp), %rcx
movq %rcx, (%rsp)
movzwl %ax, %r8d
movq %rbx, %rdi
movl $0x200, %ecx            # imm = 0x200
callq 0x543fc
cmpl $0x1, %eax
jne 0x5134c
movb $0x2, 0x37(%rbx)
cmpb $0x0, 0x14(%r12)
je 0x51382
testb $0x2, 0x33(%rbx)
jne 0x5137e
movb $0x1, 0x34(%rbx)
movq 0x8(%r12), %rax
movq %rax, 0x28(%rbx)
jmp 0x51382
leaq 0x146d0(%rip), %rsi     # 0x65a40
movq 0x70(%rsp), %rdi
xorl %eax, %eax
callq 0x10b53
jmp 0x5138c
movb $0x2, 0x37(%rbx)
movzwl 0x10(%r12), %eax
movw %ax, 0x30(%rbx)
movq 0x80(%rsp), %rdi
movq %r14, %rsi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9805