// File: /workspace/github_c_makefile_star2_O1/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), %r12d
xorl %eax, %eax
cmpl $0x2, %r12d
setne %al
cmpl $0x1, %r12d
leal 0x5(%rax,%rax,4), %r13d
cmovel %r12d, %r13d
movq %rcx, -0x98(%rbp)
movq (%rcx), %rbx
leal 0x20(,%r12,8), %esi
imull %r13d, %esi
addl %esi, %esi
movq %rbx, %rdi
callq 0x115e9
testq %rax, %rax
je 0x56b5e
movq %rax, %r15
movl %r13d, %eax
shll $0x5, %eax
leaq (%r15,%rax), %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15,%rax)
movups %xmm0, (%r15,%rax)
movq %rcx, -0x38(%rbp)
addq %rcx, %rax
movq %r13, -0x68(%rbp)
leal 0x1(,%r13,2), %ecx
movq %r12, -0x40(%rbp)
leal (,%r12,8), %edx
movq %r15, %rsi
addq $0x18, %rsi
movq %rax, (%rsi)
decl %ecx
addq %rdx, %rax
addq $0x20, %rsi
cmpl $0x1, %ecx
jg 0x56b04
movq -0x98(%rbp), %rax
movl 0x1e8(%rax), %eax
cmpl $0x2e, %eax
movl $0x2e, %ecx
cmovbl %eax, %ecx
movq -0x38(%rbp), %rax
movw %cx, 0x10(%rax)
cmpw $0x0, -0x30(%rbp)
je 0x56b70
cmpq $0x0, 0x10(%r14)
je 0x56b70
movl -0x30(%rbp), %r12d
movzwl %r12w, %edi
callq 0x5a359
xorl %ecx, %ecx
subw $0x21, %ax
cmovbl %ecx, %eax
addl %r12d, %eax
jmp 0x56b7a
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %rax
movb $0x1, 0x15(%rax)
xorl %eax, %eax
movq -0x68(%rbp), %rcx
cmpq $0x0, -0x40(%rbp)
movq %r15, -0x90(%rbp)
je 0x56eb0
movq %rbx, -0xa8(%rbp)
movl %ecx, %ecx
cmpq $0x3, %rcx
movl $0x2, %edx
cmovaeq %rcx, %rdx
shll $0x5, %edx
addq $-0x20, %rdx
movl $0x1, %r8d
movq $0x0, -0x60(%rbp)
movzwl %ax, %eax
movl %eax, -0x7c(%rbp)
xorl %ebx, %ebx
movq %r14, -0xa0(%rbp)
movq %rdx, -0xb8(%rbp)
movq %r15, %rdi
testl %r8d, %r8d
jg 0x56be1
xorl %r9d, %r9d
jmp 0x56e7e
movl %r8d, -0x80(%rbp)
movq -0x60(%rbp), %rax
leaq (,%rax,8), %rcx
movq %rcx, -0xc0(%rbp)
leaq -0xe(%rdi), %rcx
movq %rcx, -0xb0(%rbp)
movl $0x0, -0x54(%rbp)
movzwl %ax, %eax
movl %eax, -0x84(%rbp)
movq -0x38(%rbp), %r15
xorl %r9d, %r9d
movq %rdi, -0x48(%rbp)
movq 0x20(%r14), %r14
testq %r14, %r14
je 0x56e62
movq %r15, -0x50(%rbp)
movq $0x0, -0x78(%rbp)
movq (%r15), %r13
movq %r13, %rax
notq %rax
testq %rax, (%r14)
jne 0x56e43
movq 0x8(%r14), %r12
testq %r13, %r12
jne 0x56e43
movq %r9, -0x70(%rbp)
movb 0x15(%r15), %al
movl %eax, -0x2c(%rbp)
movb 0x14(%r15), %al
movl %eax, -0x58(%rbp)
movzwl 0x12(%r14), %edi
movzwl 0x10(%r15), %eax
addw 0x14(%r14), %ax
movzwl %ax, %esi
callq 0x5a551
movzwl 0x12(%r15), %esi
movzwl %ax, %edi
callq 0x5a551
movl -0x2c(%rbp), %r11d
testb %r11b, %r11b
je 0x56cab
movq 0x8(%r15), %rcx
movq %rcx, -0x78(%rbp)
movq -0x48(%rbp), %rdi
movq -0x70(%rbp), %r9
movl -0x58(%rbp), %r8d
jmp 0x56d0f
movw %ax, -0x2c(%rbp)
movq -0xa0(%rbp), %rdi
movq 0x10(%rdi), %rsi
movzwl 0x32(%rdi), %ecx
leaq -0x78(%rbp), %rax
movq %rax, (%rsp)
movq %r15, %rdx
movl -0x84(%rbp), %r8d
movq %r14, %r9
callq 0x5aafb
testl %eax, %eax
je 0x56cf1
cmpl $0x1, %eax
movzwl -0x2c(%rbp), %eax
jne 0x56e52
movb $0x1, %r11b
movb $0x1, %r8b
jmp 0x56d03
movzwl -0x2c(%rbp), %edi
movl -0x7c(%rbp), %esi
callq 0x5a551
movb $0x1, %r11b
xorl %r8d, %r8d
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %r15
movq -0x70(%rbp), %r9
testl %r9d, %r9d
setg %r10b
jle 0x56d5f
orq %r13, %r12
movl %r9d, %ecx
negl %ecx
movl $0x1, %edx
movq %rdi, %r13
cmpq %r12, (%r13)
jne 0x56d34
cmpb %r11b, 0x15(%r13)
je 0x56d4d
addq $0x20, %r13
cmpl %r9d, %edx
setl %r10b
leal (%rcx,%rdx), %esi
incl %esi
incl %edx
cmpl $0x1, %esi
jne 0x56d28
jmp 0x56d5f
testb $0x1, %r10b
je 0x56d5f
cmpw %ax, 0x12(%r13)
ja 0x56dae
jmp 0x56e43
movq -0x68(%rbp), %rcx
cmpl %ecx, %r9d
jl 0x56d71
cmpw %bx, %ax
jae 0x56e43
cmpl %ecx, %r9d
jge 0x56d7c
leal 0x1(%r9), %edx
jmp 0x56da1
movslq %r9d, %rsi
shlq $0x5, %rsi
addq -0xb0(%rbp), %rsi
movl %r9d, %ecx
decl %ecx
leaq -0x20(%rsi), %rdx
cmpw %bx, (%rsi)
movq %rdx, %rsi
jb 0x56d8d
movl %r9d, %edx
movl %ecx, %r9d
movslq %r9d, %r13
shlq $0x5, %r13
addq %rdi, %r13
movl %edx, %r9d
movq 0x8(%r14), %rcx
orq (%r15), %rcx
movq %rcx, (%r13)
movq -0x78(%rbp), %rcx
movq %rcx, 0x8(%r13)
movzwl 0x16(%r14), %ecx
addw 0x10(%r15), %cx
movw %cx, 0x10(%r13)
movw %ax, 0x12(%r13)
movb %r11b, 0x15(%r13)
movb %r8b, 0x14(%r13)
movq 0x18(%r13), %rdi
movq 0x18(%r15), %rsi
movq -0xc0(%rbp), %rdx
movq %r9, %r15
callq 0x82f0
movq %r15, %r9
movq 0x18(%r13), %rax
movq -0x60(%rbp), %rcx
movq %r14, (%rax,%rcx,8)
cmpl -0x68(%rbp), %r9d
jge 0x56e13
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %r15
jmp 0x56e43
movq -0x48(%rbp), %rdi
movzwl 0x12(%rdi), %ebx
cmpl $0x1, -0x40(%rbp)
movq -0xb8(%rbp), %rdx
movq -0x50(%rbp), %r15
je 0x56e43
xorl %eax, %eax
movzwl 0x32(%rdi,%rax), %ecx
cmpw %bx, %cx
cmovbel %ebx, %ecx
movl %ecx, %ebx
addq $0x20, %rax
cmpl %eax, %edx
jne 0x56e2e
movq 0x38(%r14), %r14
testq %r14, %r14
jne 0x56c2f
jmp 0x56e62
xorl %r11d, %r11d
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %r15
jmp 0x56ca1
movl -0x54(%rbp), %eax
incl %eax
addq $0x20, %r15
movl %eax, -0x54(%rbp)
cmpl -0x80(%rbp), %eax
movq -0xa0(%rbp), %r14
jne 0x56c1e
movq -0x60(%rbp), %rsi
incq %rsi
movq -0x38(%rbp), %r15
movq %rdi, -0x38(%rbp)
movl %r9d, %r8d
movq %rsi, -0x60(%rbp)
cmpq -0x40(%rbp), %rsi
jne 0x56bd1
testl %r9d, %r9d
sete %al
movq %rdi, %r12
movq -0xa8(%rbp), %rbx
jmp 0x56eb6
xorl %eax, %eax
movq -0x38(%rbp), %r12
testb %al, %al
movq -0x40(%rbp), %rax
je 0x56edf
leaq 0x24c42(%rip), %rsi     # 0x7bb07
movq -0x98(%rbp), %rdi
xorl %eax, %eax
callq 0x11724
movq -0x90(%rbp), %r15
jmp 0x56fbe
testq %rax, %rax
movq -0x90(%rbp), %r15
je 0x56f2d
movq 0x18(%r12), %rax
movq 0x8(%r14), %rcx
addq $0x8, %rcx
movl -0x40(%rbp), %edx
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, %rdx
jne 0x56f04
movzwl 0x32(%r14), %eax
andl $0x600, %eax            # imm = 0x600
cmpl $0x400, %eax            # imm = 0x400
jne 0x56f88
cmpw $0x0, -0x30(%rbp)
je 0x56f88
cmpb $0x0, 0x37(%r14)
jne 0x56f88
movq 0x18(%r14), %rsi
movq %r12, %rdx
movq 0x18(%r12), %rax
movq -0x40(%rbp), %rcx
movq -0x8(%rax,%rcx,8), %r9
leaq -0x1(%rcx), %rax
leaq -0x78(%rbp), %rcx
movq %rcx, (%rsp)
movzwl %ax, %r8d
movq %r14, %rdi
movl $0x200, %ecx            # imm = 0x200
callq 0x5aafb
cmpl $0x1, %eax
jne 0x56f88
movb $0x2, 0x37(%r14)
movq %r12, %rcx
cmpb $0x0, 0x14(%r12)
je 0x56fb5
testb $0x2, 0x33(%r14)
jne 0x56fad
movb $0x1, 0x34(%r14)
movq %r12, %rcx
movq 0x8(%r12), %rax
movq %rax, 0x28(%r14)
jmp 0x56fb5
movb $0x2, 0x37(%r14)
movq %r12, %rcx
movzwl 0x10(%rcx), %eax
movw %ax, 0x30(%r14)
movq %rbx, %rdi
movq %r15, %rsi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9c3a