// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movl %esi, -0x30(%rbp)
movq %rdi, %r14
movq (%rdi), %rcx
movzbl 0x38(%rdi), %r13d
xorl %eax, %eax
cmpl $0x2, %r13d
setne %al
cmpl $0x1, %r13d
leal 0x5(%rax,%rax,4), %r15d
cmovel %r13d, %r15d
movq %rcx, -0x70(%rbp)
movq (%rcx), %rdi
leal 0x20(,%r13,8), %esi
leal (%r15,%r15), %ebx
imull %ebx, %esi
movq %rdi, -0x68(%rbp)
callq 0x11721
testq %rax, %rax
je 0x56b82
movq %rax, %r12
movq %r15, -0x78(%rbp)
movl %r15d, %eax
shll $0x5, %eax
leaq (%r12,%rax), %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12,%rax)
movups %xmm0, (%r12,%rax)
addq %rdx, %rax
incl %ebx
leal (,%r13,8), %ecx
movq %r12, %rsi
addq $0x18, %rsi
movq %rax, (%rsi)
decl %ebx
addq %rcx, %rax
addq $0x20, %rsi
cmpl $0x1, %ebx
ja 0x56b25
movq -0x70(%rbp), %rdi
movl 0x1e8(%rdi), %eax
cmpl $0x2e, %eax
movl $0x2e, %ecx
cmovbl %eax, %ecx
movw %cx, 0x10(%rdx)
cmpw $0x0, -0x30(%rbp)
je 0x56b94
cmpq $0x0, 0x10(%r14)
je 0x56b94
movl -0x30(%rbp), %r15d
movzwl %r15w, %edi
movq %rdx, %rbx
callq 0x5a316
movq %rbx, %rdx
xorl %ecx, %ecx
subw $0x21, %ax
cmovbl %ecx, %eax
addl %r15d, %eax
movq -0x70(%rbp), %rdi
jmp 0x56b9a
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x15(%rdx)
xorl %eax, %eax
movq -0x68(%rbp), %r15
testq %r13, %r13
je 0x56f54
movq %r13, -0x90(%rbp)
movq %r13, %rsi
negq %rsi
movl -0x78(%rbp), %ecx
cmpq $0x3, %rcx
movl $0x2, %r8d
cmovaeq %rcx, %r8
shll $0x5, %r8d
addq $-0x20, %r8
movq %r8, -0xb8(%rbp)
movl $0x1, %r10d
xorl %ecx, %ecx
movzwl %ax, %eax
movl %eax, -0x7c(%rbp)
movq %r12, -0x60(%rbp)
xorl %r13d, %r13d
leaq 0x1(%rcx), %rbx
shlq $0x3, %rcx
movq %rcx, %r9
movq %rdx, %r8
movl %r10d, %r11d
movq %r12, %rdx
testl %r11d, %r11d
jg 0x56c24
addq $0x8, %r9
leaq (%rsi,%rbx), %rcx
incq %rcx
incq %rbx
xorl %r11d, %r11d
movq %r8, %r12
movq %rdx, %r8
cmpq $0x1, %rcx
jne 0x56bfa
jmp 0x56fc9
movq %r9, -0xc8(%rbp)
movq %rsi, -0xb0(%rbp)
movq %rbx, -0x98(%rbp)
leaq -0x1(%rbx), %rax
leaq -0xe(%rdx), %rcx
movq %rcx, -0xc0(%rbp)
xorl %ecx, %ecx
movzwl %ax, %eax
movl %eax, -0x84(%rbp)
movq %r8, -0xa8(%rbp)
movq %r8, %r12
xorl %r10d, %r10d
movq %r14, -0xa0(%rbp)
movq %rdx, -0x38(%rbp)
movl %r11d, -0x80(%rbp)
movl %ecx, -0x88(%rbp)
movq 0x20(%r14), %r15
testq %r15, %r15
je 0x56eb1
movq %r12, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq (%r12), %r14
movq %r14, %rax
notq %rax
testq %rax, (%r15)
jne 0x56ca6
movq 0x8(%r15), %rbx
testq %r14, %rbx
je 0x56cb4
movq 0x38(%r15), %r15
testq %r15, %r15
jne 0x56c86
jmp 0x56eb1
movq %r10, -0x50(%rbp)
movb 0x15(%r12), %al
movl %eax, -0x2c(%rbp)
movb 0x14(%r12), %al
movl %eax, -0x44(%rbp)
movzwl 0x12(%r15), %edi
movzwl 0x10(%r12), %eax
addw 0x14(%r15), %ax
movzwl %ax, %esi
callq 0x5a50e
movzwl 0x12(%r12), %esi
movzwl %ax, %edi
callq 0x5a50e
movl -0x2c(%rbp), %r9d
testb %r9b, %r9b
je 0x56d0d
movq 0x8(%r12), %rcx
movq %rcx, -0x58(%rbp)
movq -0x38(%rbp), %rdx
movq -0x50(%rbp), %r10
movl -0x44(%rbp), %edi
jmp 0x56d70
movw %ax, -0x2c(%rbp)
movq -0xa0(%rbp), %rdi
movq 0x10(%rdi), %rsi
movzwl 0x32(%rdi), %ecx
leaq -0x58(%rbp), %rax
movq %rax, (%rsp)
movq %r12, %rdx
movl -0x84(%rbp), %r8d
movq %r15, %r9
callq 0x5aaae
testl %eax, %eax
je 0x56d53
cmpl $0x1, %eax
movzwl -0x2c(%rbp), %eax
jne 0x56e9d
movb $0x1, %r9b
movb $0x1, %dil
jmp 0x56d64
movzwl -0x2c(%rbp), %edi
movl -0x7c(%rbp), %esi
callq 0x5a50e
movb $0x1, %r9b
xorl %edi, %edi
movq -0x38(%rbp), %rdx
movq -0x50(%rbp), %r10
movq -0x40(%rbp), %r12
testl %r10d, %r10d
jle 0x56d91
orq %r14, %rbx
movl %r10d, %ecx
movq %rdx, %r14
cmpq %rbx, (%r14)
jne 0x56d89
cmpb %r9b, 0x15(%r14)
je 0x56daf
addq $0x20, %r14
decl %ecx
jne 0x56d7e
movq -0x78(%rbp), %rcx
cmpl %ecx, %r10d
jl 0x56da4
cmpw %r13w, %ax
jae 0x56ca6
cmpl %ecx, %r10d
jge 0x56dbc
leal 0x1(%r10), %esi
jmp 0x56de2
cmpw %ax, 0x12(%r14)
jbe 0x56ca6
jmp 0x56def
movslq %r10d, %r8
shlq $0x5, %r8
addq -0xc0(%rbp), %r8
movl %r10d, %ecx
decl %ecx
leaq -0x20(%r8), %rsi
cmpw %r13w, (%r8)
movq %rsi, %r8
jb 0x56dcd
movl %r10d, %esi
movl %ecx, %r10d
movslq %r10d, %r14
shlq $0x5, %r14
addq %rdx, %r14
movl %esi, %r10d
movq 0x8(%r15), %rcx
orq (%r12), %rcx
movq %rcx, (%r14)
movq -0x58(%rbp), %rcx
movq %rcx, 0x8(%r14)
movzwl 0x16(%r15), %ecx
addw 0x10(%r12), %cx
movw %cx, 0x10(%r14)
movw %ax, 0x12(%r14)
movb %r9b, 0x15(%r14)
movb %dil, 0x14(%r14)
movq 0x18(%r14), %rdi
movq 0x18(%r12), %rsi
movq -0xc8(%rbp), %rdx
movq %r10, %rbx
callq 0x82f0
movq %rbx, %r10
movq 0x18(%r14), %rax
movq -0x98(%rbp), %rcx
movq %r15, -0x8(%rax,%rcx,8)
cmpl -0x78(%rbp), %r10d
jge 0x56e5d
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %r12
jmp 0x56ca6
movq -0x38(%rbp), %rdx
movzwl 0x12(%rdx), %r13d
cmpl $0x1, -0x90(%rbp)
movq -0x40(%rbp), %r12
je 0x56ca6
xorl %eax, %eax
movq -0xb8(%rbp), %rsi
movzwl 0x32(%rdx,%rax), %ecx
cmpw %r13w, %cx
cmovbel %r13d, %ecx
movl %ecx, %r13d
addq $0x20, %rax
cmpl %eax, %esi
jne 0x56e80
jmp 0x56ca6
xorl %r9d, %r9d
movq -0x38(%rbp), %rdx
movq -0x50(%rbp), %r10
movq -0x40(%rbp), %r12
jmp 0x56d08
movl -0x88(%rbp), %ecx
incl %ecx
addq $0x20, %r12
movl -0x80(%rbp), %r11d
cmpl %r11d, %ecx
movq -0xa0(%rbp), %r14
jne 0x56c6f
movq -0xb0(%rbp), %rsi
movq %rsi, %rax
movq -0x98(%rbp), %rcx
addq %rcx, %rax
movq -0x68(%rbp), %r15
movq -0x70(%rbp), %rdi
movq -0xa8(%rbp), %r12
jne 0x56be9
testl %r10d, %r10d
je 0x56fc9
movq -0x90(%rbp), %r13
testq %r13, %r13
je 0x56f50
movq 0x18(%rdx), %rax
movq 0x8(%r14), %rcx
addq $0x8, %rcx
movl %r13d, %r9d
leaq 0x33c(%r14), %rsi
xorl %edi, %edi
movq (%rax,%rdi,8), %r8
movq %r8, 0x3c(%rsi)
movzbl 0x10(%r8), %r8d
movb %r8b, 0x1c(%rsi)
imulq $0x68, %r8, %r8
movl 0x3c(%rcx,%r8), %r8d
movl %r8d, (%rsi)
incq %rdi
addq $0x48, %rsi
cmpq %rdi, %r9
jne 0x56f27
movq -0x60(%rbp), %r12
movzwl 0x32(%r14), %eax
andl $0x600, %eax            # imm = 0x600
cmpl $0x400, %eax            # imm = 0x400
jne 0x56fad
cmpw $0x0, -0x30(%rbp)
je 0x56fad
cmpb $0x0, 0x37(%r14)
jne 0x56fad
movq 0x18(%r14), %rsi
movq 0x18(%rdx), %rax
movq -0x8(%rax,%r13,8), %r9
leaq -0x1(%r13), %rax
leaq -0x58(%rbp), %rcx
movq %rcx, (%rsp)
movzwl %ax, %r8d
movq %r14, %rdi
movq %rdx, %rbx
movl $0x200, %ecx            # imm = 0x200
callq 0x5aaae
cmpl $0x1, %eax
jne 0x56faa
movb $0x2, 0x37(%r14)
movq %rbx, %rdx
cmpb $0x0, 0x14(%rdx)
je 0x56fe2
testb $0x2, 0x33(%r14)
jne 0x56fdd
movb $0x1, 0x34(%r14)
movq 0x8(%rdx), %rax
movq %rax, 0x28(%r14)
jmp 0x56fe2
leaq 0x24b37(%rip), %rsi     # 0x7bb07
xorl %eax, %eax
callq 0x11842
movq -0x60(%rbp), %r12
jmp 0x56feb
movb $0x2, 0x37(%r14)
movzwl 0x10(%rdx), %eax
movw %ax, 0x30(%r14)
movq %r15, %rdi
movq %r12, %rsi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9ca4