// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: wherePathSolver
static int wherePathSolver(WhereInfo *pWInfo, WhereCost 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 */
  WhereCost rCost;             /* Cost of a path */
  WhereCost mxCost = 0;        /* Maximum cost of a set of paths */
  WhereCost 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==whereCost(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 N*log2(N) where N is the
    ** number of output rows. */
    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 = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
        rCost = whereCostAdd(rCost, pFrom->rCost);
        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 = whereCostAdd(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 ){
            testcase( jj==nTo-1 );
            break;
          }
        }
        if( jj>=nTo ){
          if( nTo>=mxChoice && rCost>=mxCost ){
#ifdef WHERETRACE_ENABLED
            if( sqlite3WhereTrace&0x4 ){
              sqlite3DebugPrintf("Skip   %s cost=%3d order=%c\n",
                  wherePathName(pFrom, iLoop, pWLoop), rCost,
                  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 */
            for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); }
          }
          pTo = &aTo[jj];
#ifdef WHERETRACE_ENABLED
          if( sqlite3WhereTrace&0x4 ){
            sqlite3DebugPrintf("New    %s cost=%-3d order=%c\n",
                wherePathName(pFrom, iLoop, pWLoop), rCost,
                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
          }
#endif
        }else{
          if( pTo->rCost<=rCost ){
#ifdef WHERETRACE_ENABLED
            if( sqlite3WhereTrace&0x4 ){
              sqlite3DebugPrintf(
                  "Skip   %s cost=%-3d order=%c",
                  wherePathName(pFrom, iLoop, pWLoop), rCost,
                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
              sqlite3DebugPrintf("   vs %s cost=%-3d order=%c\n",
                  wherePathName(pTo, iLoop+1, 0), pTo->rCost,
                  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
          if( sqlite3WhereTrace&0x4 ){
            sqlite3DebugPrintf(
                "Update %s cost=%-3d order=%c",
                wherePathName(pFrom, iLoop, pWLoop), rCost,
                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
            sqlite3DebugPrintf("  was %s cost=%-3d order=%c\n",
                wherePathName(pTo, iLoop+1, 0), pTo->rCost,
                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 = pFrom->nRow + pWLoop->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 ){
          mxCost = aTo[0].rCost;
          for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
            if( pTo->rCost>mxCost ) mxCost = pTo->rCost;
          }
        }
      }
    }

#ifdef WHERETRACE_ENABLED
    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;
  assert( nFrom==1 );
#if 0 /* The following is needed if nFrom is ever more than 1 */
  for(ii=1; ii<nFrom; ii++){
    if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
  }
#endif
  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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %esi, -0x3c(%rbp)
movq (%rdi), %rcx
movq %rdi, -0x70(%rbp)
movzbl 0x38(%rdi), %r12d
xorl %eax, %eax
cmpl $0x2, %r12d
setne %al
cmpl $0x1, %r12d
leal 0x5(%rax,%rax,4), %r13d
cmovel %r12d, %r13d
movq %rcx, -0x90(%rbp)
movq (%rcx), %r14
leal 0x20(,%r12,8), %esi
leal (,%r13,2), %ebx
imull %ebx, %esi
movq %r14, %rdi
callq 0x103b4
testq %rax, %rax
je 0x4e9a8
movq %rax, %r15
movq %r14, -0xa0(%rbp)
movl %r13d, %eax
shll $0x5, %eax
leaq (%r15,%rax), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15,%rax)
movups %xmm0, (%r15,%rax)
addq %r14, %rax
movq %r15, %rcx
addq $0x18, %rcx
leal (,%r12,8), %edx
testl %ebx, %ebx
jle 0x4e965
movq %rax, (%rcx)
decl %ebx
addq $0x20, %rcx
addq %rdx, %rax
jmp 0x4e953
movq -0x90(%rbp), %rax
movl 0x1e8(%rax), %eax
cmpl $0x2e, %eax
pushq $0x2e
popq %rcx
cmovbl %eax, %ecx
movw %cx, 0x10(%r14)
cmpw $0x0, -0x3c(%rbp)
movq -0x70(%rbp), %rbx
movq %r13, -0x50(%rbp)
je 0x4e9ba
cmpq $0x0, 0x10(%rbx)
je 0x4e9ba
movl -0x3c(%rbp), %r13d
movzwl %r13w, %edi
callq 0x516bf
addl %r13d, %eax
jmp 0x4e9c1
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x15(%r14)
xorl %eax, %eax
leaq 0x20(%rbx), %rcx
movq %rcx, -0xc0(%rbp)
movl %r12d, %ecx
movq %rcx, -0x98(%rbp)
pushq $0x1
popq %rcx
xorl %edi, %edi
movzwl %ax, %eax
movl %eax, -0x7c(%rbp)
xorl %ebx, %ebx
movq %r15, -0x48(%rbp)
movq %r15, -0xa8(%rbp)
movq %r12, -0xc8(%rbp)
cmpq -0x98(%rbp), %rdi
je 0x4ecb9
leaq (,%rdi,8), %rax
movq %rax, -0xd8(%rbp)
xorl %r8d, %r8d
testl %ecx, %ecx
cmovlel %r8d, %ecx
movl %ecx, -0x80(%rbp)
movq -0x48(%rbp), %rax
leaq -0xe(%rax), %rcx
movq %rcx, -0xb8(%rbp)
addq $0x32, %rax
movq %rax, -0xd0(%rbp)
movzwl %di, %eax
movl %eax, -0x84(%rbp)
movq %r14, -0xb0(%rbp)
movq %r14, %rsi
xorl %ecx, %ecx
movq %rdi, -0x78(%rbp)
cmpl -0x80(%rbp), %ecx
je 0x4ec98
movq -0xc0(%rbp), %r12
movq %rsi, -0x38(%rbp)
movl %ecx, -0x2c(%rbp)
movq (%r12), %r12
testq %r12, %r12
je 0x4ec86
andq $0x0, -0x68(%rbp)
movq (%rsi), %r14
movq %r14, %rax
notq %rax
testq %rax, (%r12)
jne 0x4ec40
movq 0x8(%r12), %r13
testq %r14, %r13
jne 0x4ec40
movq %r8, -0x60(%rbp)
movb 0x15(%rsi), %al
movl %eax, -0x88(%rbp)
movb 0x14(%rsi), %al
movl %eax, -0x54(%rbp)
movzwl 0x12(%r12), %edi
movzwl 0x10(%rsi), %eax
addw 0x14(%r12), %ax
movq %rsi, %r15
movzwl %ax, %esi
callq 0x51712
movzwl 0x12(%r15), %esi
movzwl %ax, %edi
callq 0x51712
movl -0x88(%rbp), %r10d
testb %r10b, %r10b
je 0x4eafa
movq -0x38(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, -0x68(%rbp)
movq -0x50(%rbp), %rcx
movq -0x60(%rbp), %r8
movl -0x54(%rbp), %edi
jmp 0x4eb59
movl %eax, %r15d
movq -0x70(%rbp), %rdi
movq 0x10(%rdi), %rsi
movzwl 0x32(%rdi), %ecx
leaq -0x68(%rbp), %rax
movq %rax, (%rsp)
movq -0x38(%rbp), %rdx
movl -0x84(%rbp), %r8d
movq %r12, %r9
callq 0x51cb4
testl %eax, %eax
je 0x4eb3c
cmpl $0x1, %eax
movl %r15d, %eax
jne 0x4ec72
movb $0x1, %r10b
movb $0x1, %dil
jmp 0x4eb4d
movzwl %r15w, %edi
movl -0x7c(%rbp), %esi
callq 0x51712
movb $0x1, %r10b
xorl %edi, %edi
movq -0x50(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x38(%rbp), %rsi
orq %r14, %r13
testl %r8d, %r8d
movl $0x0, %edx
cmovgl %r8d, %edx
movq -0x48(%rbp), %r14
subl $0x1, %edx
jb 0x4eb82
cmpq %r13, (%r14)
jne 0x4eb7c
cmpb %r10b, 0x15(%r14)
je 0x4eb97
addq $0x20, %r14
jmp 0x4eb6c
cmpl %ecx, %r8d
jl 0x4eb8c
cmpw %bx, %ax
jae 0x4eb9e
cmpl %ecx, %r8d
jge 0x4eba7
leal 0x1(%r8), %edx
jmp 0x4ebcd
cmpw %ax, 0x12(%r14)
ja 0x4ebdb
movq -0x78(%rbp), %rdi
jmp 0x4ec3d
movslq %r8d, %r9
shlq $0x5, %r9
addq -0xb8(%rbp), %r9
movl %r8d, %ecx
decl %ecx
leaq -0x20(%r9), %rdx
cmpw %bx, (%r9)
movq %rdx, %r9
jb 0x4ebb8
movl %r8d, %edx
movl %ecx, %r8d
movslq %r8d, %r14
shlq $0x5, %r14
addq -0x48(%rbp), %r14
movl %edx, %r8d
movq 0x8(%r12), %rcx
orq (%rsi), %rcx
movq %rcx, (%r14)
movq -0x68(%rbp), %rcx
movq %rcx, 0x8(%r14)
movzwl 0x16(%r12), %ecx
addw 0x10(%rsi), %cx
movw %cx, 0x10(%r14)
movw %ax, 0x12(%r14)
movb %r10b, 0x15(%r14)
movb %dil, 0x14(%r14)
movq 0x18(%r14), %rdi
movq 0x18(%rsi), %rsi
movq -0xd8(%rbp), %rdx
movq %r8, %r15
callq 0x82c0
movq %r15, %r8
movq -0x50(%rbp), %rsi
movq 0x18(%r14), %rax
movq -0x78(%rbp), %rdi
movq %r12, (%rax,%rdi,8)
cmpl %esi, %r8d
jge 0x4ec49
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %ecx
addq $0x38, %r12
jmp 0x4ea67
movq -0x48(%rbp), %rax
movzwl 0x12(%rax), %ebx
movq -0xd0(%rbp), %rax
pushq $0x1
popq %rcx
cmpl %esi, %ecx
jae 0x4ec39
movzwl (%rax), %edx
cmpw %bx, %dx
cmovbel %ebx, %edx
incl %ecx
addq $0x20, %rax
movl %edx, %ebx
jmp 0x4ec5b
xorl %r10d, %r10d
movq -0x50(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x38(%rbp), %rsi
jmp 0x4eaf5
incl %ecx
addq $0x20, %rsi
movq -0xc8(%rbp), %r12
jmp 0x4ea50
incq %rdi
movl %r8d, %ecx
movq -0x48(%rbp), %r14
movq -0xb0(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xa8(%rbp), %r15
jmp 0x4e9f5
movq %r14, %r13
testl %ecx, %ecx
movq -0xa0(%rbp), %r14
je 0x4ed79
movq -0x70(%rbp), %rbx
leaq 0x33c(%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, -0x98(%rbp)
je 0x4ed0b
movq 0x18(%r13), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, 0x3c(%rax)
movzbl 0x10(%rdx), %edx
movb %dl, 0x1c(%rax)
movq 0x8(%rbx), %rsi
imulq $0x68, %rdx, %rdx
movl 0x44(%rsi,%rdx), %edx
movl %edx, (%rax)
incq %rcx
addq $0x48, %rax
jmp 0x4ecd8
movzwl 0x32(%rbx), %eax
andl $0x600, %eax            # imm = 0x600
cmpw $0x400, %ax             # imm = 0x400
jne 0x4ed5e
cmpw $0x0, -0x3c(%rbp)
je 0x4ed5e
cmpb $0x0, 0x37(%rbx)
jne 0x4ed5e
movq 0x18(%rbx), %rsi
movq %r13, %rdx
movq 0x18(%r13), %rax
movq -0x8(%rax,%r12,8), %r9
leaq -0x1(%r12), %rax
leaq -0x68(%rbp), %rcx
movq %rcx, (%rsp)
movzwl %ax, %r8d
movq %rbx, %rdi
movl $0x200, %ecx            # imm = 0x200
callq 0x51cb4
cmpl $0x1, %eax
jne 0x4ed5e
movb $0x2, 0x37(%rbx)
cmpb $0x0, 0x14(%r13)
je 0x4ed94
testb $0x2, 0x33(%rbx)
jne 0x4ed90
movb $0x1, 0x34(%rbx)
movq 0x8(%r13), %rax
movq %rax, 0x28(%rbx)
jmp 0x4ed94
leaq 0x1eda7(%rip), %rsi     # 0x6db27
movq -0x90(%rbp), %rdi
xorl %eax, %eax
callq 0x104d6
jmp 0x4ed9d
movb $0x2, 0x37(%rbx)
movzwl 0x10(%r13), %eax
movw %ax, 0x30(%rbx)
movq %r14, %rdi
movq %r15, %rsi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x968b