// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: wherePathSatisfiesOrderBy
static int wherePathSatisfiesOrderBy(
  WhereInfo *pWInfo,    /* The WHERE clause */
  ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
  WherePath *pPath,     /* The WherePath to check */
  u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
  u16 nLoop,            /* Number of entries in pPath->aLoop[] */
  WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
  Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
){
  u8 revSet;            /* True if rev is known */
  u8 rev;               /* Composite sort order */
  u8 revIdx;            /* Index sort order */
  u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
  u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
  u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
  u16 nColumn;          /* Number of columns in pIndex */
  u16 nOrderBy;         /* Number terms in the ORDER BY clause */
  int iLoop;            /* Index of WhereLoop in pPath being processed */
  int i, j;             /* Loop counters */
  int iCur;             /* Cursor number for current WhereLoop */
  int iColumn;          /* A column number within table iCur */
  WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
  WhereTerm *pTerm;     /* A single term of the WHERE clause */
  Expr *pOBExpr;        /* An expression from the ORDER BY clause */
  CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
  Index *pIndex;        /* The index associated with pLoop */
  sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
  Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
  Bitmask obDone;       /* Mask of all ORDER BY terms */
  Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
  Bitmask ready;              /* Mask of inner loops */

  /*
  ** We say the WhereLoop is "one-row" if it generates no more than one
  ** row of output.  A WhereLoop is one-row if all of the following are true:
  **  (a) All index columns match with WHERE_COLUMN_EQ.
  **  (b) The index is unique
  ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
  ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
  **
  ** We say the WhereLoop is "order-distinct" if the set of columns from
  ** that WhereLoop that are in the ORDER BY clause are different for every
  ** row of the WhereLoop.  Every one-row WhereLoop is automatically
  ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
  ** is not order-distinct. To be order-distinct is not quite the same as being
  ** UNIQUE since a UNIQUE column or index can have multiple rows that 
  ** are NULL and NULL values are equivalent for the purpose of order-distinct.
  ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
  **
  ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
  ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
  ** automatically order-distinct.
  */

  assert( pOrderBy!=0 );

  /* Sortability of virtual tables is determined by the xBestIndex method
  ** of the virtual table itself */
  if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
    testcase( nLoop>0 );  /* True when outer loops are one-row and match 
                          ** no ORDER BY terms */
    return pLast->u.vtab.isOrdered;
  }
  if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;

  nOrderBy = pOrderBy->nExpr;
  testcase( nOrderBy==BMS-1 );
  if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
  isOrderDistinct = 1;
  obDone = MASKBIT(nOrderBy)-1;
  orderDistinctMask = 0;
  ready = 0;
  for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
    if( iLoop>0 ) ready |= pLoop->maskSelf;
    pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
    assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
    iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;

    /* Mark off any ORDER BY term X that is a column in the table of
    ** the current loop for which there is term in the WHERE
    ** clause of the form X IS NULL or X=? that reference only outer
    ** loops.
    */
    for(i=0; i<nOrderBy; i++){
      if( MASKBIT(i) & obSat ) continue;
      pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
      if( pOBExpr->op!=TK_COLUMN ) continue;
      if( pOBExpr->iTable!=iCur ) continue;
      pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
                       ~ready, WO_EQ|WO_ISNULL, 0);
      if( pTerm==0 ) continue;
      if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
        const char *z1, *z2;
        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
        if( !pColl ) pColl = db->pDfltColl;
        z1 = pColl->zName;
        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
        if( !pColl ) pColl = db->pDfltColl;
        z2 = pColl->zName;
        if( sqlite3StrICmp(z1, z2)!=0 ) continue;
      }
      obSat |= MASKBIT(i);
    }

    if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
      if( pLoop->wsFlags & WHERE_IPK ){
        pIndex = 0;
        nColumn = 0;
      }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
        return 0;
      }else{
        nColumn = pIndex->nColumn;
        isOrderDistinct = pIndex->onError!=OE_None;
      }

      /* Loop through all columns of the index and deal with the ones
      ** that are not constrained by == or IN.
      */
      rev = revSet = 0;
      distinctColumns = 0;
      for(j=0; j<=nColumn; j++){
        u8 bOnce;   /* True to run the ORDER BY search loop */

        /* Skip over == and IS NULL terms */
        if( j<pLoop->u.btree.nEq
         && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
        ){
          if( i & WO_ISNULL ){
            testcase( isOrderDistinct );
            isOrderDistinct = 0;
          }
          continue;  
        }

        /* Get the column number in the table (iColumn) and sort order
        ** (revIdx) for the j-th column of the index.
        */
        if( j<nColumn ){
          /* Normal index columns */
          iColumn = pIndex->aiColumn[j];
          revIdx = pIndex->aSortOrder[j];
          if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
        }else{
          /* The ROWID column at the end */
          assert( j==nColumn );
          iColumn = -1;
          revIdx = 0;
        }

        /* An unconstrained column that might be NULL means that this
        ** WhereLoop is not well-ordered 
        */
        if( isOrderDistinct
         && iColumn>=0
         && j>=pLoop->u.btree.nEq
         && pIndex->pTable->aCol[iColumn].notNull==0
        ){
          isOrderDistinct = 0;
        }

        /* Find the ORDER BY term that corresponds to the j-th column
        ** of the index and and mark that ORDER BY term off 
        */
        bOnce = 1;
        isMatch = 0;
        for(i=0; bOnce && i<nOrderBy; i++){
          if( MASKBIT(i) & obSat ) continue;
          pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
          testcase( wctrlFlags & WHERE_GROUPBY );
          testcase( wctrlFlags & WHERE_DISTINCTBY );
          if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
          if( pOBExpr->op!=TK_COLUMN ) continue;
          if( pOBExpr->iTable!=iCur ) continue;
          if( pOBExpr->iColumn!=iColumn ) continue;
          if( iColumn>=0 ){
            pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
            if( !pColl ) pColl = db->pDfltColl;
            if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
          }
          isMatch = 1;
          break;
        }
        if( isMatch ){
          if( iColumn<0 ){
            testcase( distinctColumns==0 );
            distinctColumns = 1;
          }
          obSat |= MASKBIT(i);
          if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
            /* Make sure the sort order is compatible in an ORDER BY clause.
            ** Sort order is irrelevant for a GROUP BY clause. */
            if( revSet ){
              if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0;
            }else{
              rev = revIdx ^ pOrderBy->a[i].sortOrder;
              if( rev ) *pRevMask |= MASKBIT(iLoop);
              revSet = 1;
            }
          }
        }else{
          /* No match found */
          if( j==0 || j<nColumn ){
            testcase( isOrderDistinct!=0 );
            isOrderDistinct = 0;
          }
          break;
        }
      } /* end Loop over all index columns */
      if( distinctColumns ){
        testcase( isOrderDistinct==0 );
        isOrderDistinct = 1;
      }
    } /* end-if not one-row */

    /* Mark off any other ORDER BY terms that reference pLoop */
    if( isOrderDistinct ){
      orderDistinctMask |= pLoop->maskSelf;
      for(i=0; i<nOrderBy; i++){
        Expr *p;
        if( MASKBIT(i) & obSat ) continue;
        p = pOrderBy->a[i].pExpr;
        if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){
          obSat |= MASKBIT(i);
        }
      }
    }
  } /* End the loop over all WhereLoops from outer-most down to inner-most */
  if( obSat==obDone ) return 1;
  if( !isOrderDistinct ) return 0;
  return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %ecx, -0x8c(%rbp)
movq %rdx, -0xe0(%rbp)
movq %rsi, -0x58(%rbp)
testb $0x4, 0x29(%r9)
jne 0x51d04
movq (%rdi), %rax
movq (%rax), %rcx
testw %r8w, %r8w
je 0x51cf2
testb $-0x80, 0x4c(%rcx)
jne 0x51d00
movq -0x58(%rbp), %rax
movzwl (%rax), %r10d
cmpl $0x3f, %r10d
jbe 0x51d1b
xorl %eax, %eax
jmp 0x51d09
movzbl 0x1d(%r9), %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, -0xa8(%rbp)
pushq $-0x1
popq %rax
movl %r10d, %ecx
shlq %cl, %rax
notq %rax
movq %rax, -0xc0(%rbp)
leaq 0x150(%rdi), %rax
movq %rax, -0x100(%rbp)
leaq 0x4c(%rdi), %rax
movq %rax, -0x108(%rbp)
movzwl %r8w, %eax
movb $0x1, %bl
pushq $0x1
popq %r8
andl $0x300, -0x8c(%rbp)     # imm = 0x300
xorl %ecx, %ecx
xorl %esi, %esi
xorl %r11d, %r11d
movq $0x0, -0xc8(%rbp)
xorl %edx, %edx
movq %rdi, -0x30(%rbp)
movq %r10, -0x38(%rbp)
movq %r9, -0xf0(%rbp)
movq %rax, -0xe8(%rbp)
movl %ebx, -0x88(%rbp)
testb %bl, %bl
je 0x5231c
cmpq -0xc0(%rbp), %r11
jae 0x5231c
cmpq %rax, %rcx
ja 0x5231c
testq %rcx, %rcx
je 0x51db9
orq 0x8(%rsi), %rdx
cmpq %rax, %rcx
jae 0x51dcd
movq -0xe0(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rcx,8), %r9
movq %rcx, -0xd0(%rbp)
movq 0x8(%rdi), %rax
movq %r9, -0x98(%rbp)
movzbl 0x10(%r9), %ecx
imulq $0x68, %rcx, %rcx
movl 0x44(%rax,%rcx), %eax
movl %eax, -0x3c(%rbp)
movq %rdx, -0xf8(%rbp)
notq %rdx
movq %rdx, -0x70(%rbp)
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpq %r14, %r10
je 0x51f1a
movq %r8, %r15
movl %r14d, %ecx
shlq %cl, %r15
btq %r14, %r11
jb 0x51f0e
movq %r11, %r13
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r12), %rdi
callq 0x45e0b
cmpb $-0x68, (%rax)
jne 0x51eff
movq %rax, %rbx
movl -0x3c(%rbp), %esi
cmpl %esi, 0x2c(%rax)
jne 0x51eff
movswl 0x30(%rbx), %edx
movq -0x100(%rbp), %rdi
movq -0x70(%rbp), %rcx
movl $0x82, %r8d
xorl %r9d, %r9d
callq 0x504dd
testq %rax, %rax
je 0x51eff
testb $0x2, 0x18(%rax)
movq -0x38(%rbp), %r10
pushq $0x1
popq %r8
movq %r13, %r11
je 0x51ef6
cmpw $0x0, 0x30(%rbx)
js 0x51ef6
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rbx
movq (%rbx), %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r12), %rsi
callq 0x466ee
testq %rax, %rax
jne 0x51eb5
movq -0xa8(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq (%rbx), %rdi
movq -0x50(%rbp), %rax
movq (%rax), %rsi
callq 0x466ee
testq %rax, %rax
jne 0x51edb
movq -0xa8(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rsi
movq -0x48(%rbp), %rdi
callq 0xb1bf
testl %eax, %eax
movq -0x38(%rbp), %r10
pushq $0x1
popq %r8
movq %r13, %r11
jne 0x51ef9
orq %r15, %r11
movq -0x30(%rbp), %rdi
jmp 0x51f0e
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %r10
pushq $0x1
popq %r8
movq %r13, %r11
incq %r14
addq $0x20, %r12
jmp 0x51e03
movq -0x98(%rbp), %rsi
movl 0x28(%rsi), %eax
btl $0xc, %eax
jb 0x51f60
btl $0x8, %eax
movq -0xd0(%rbp), %rcx
jb 0x51f6b
movq 0x20(%rsi), %r14
xorl %eax, %eax
testq %r14, %r14
je 0x51d09
testb $0x4, 0x57(%r14)
jne 0x51d09
movzwl 0x54(%r14), %r15d
cmpb $0x0, 0x56(%r14)
setne %al
jmp 0x51f77
movl -0x88(%rbp), %eax
jmp 0x52276
xorl %r15d, %r15d
xorl %r14d, %r14d
movl -0x88(%rbp), %eax
movq %r8, %rdx
shlq %cl, %rdx
movq %rdx, -0xd8(%rbp)
movl %r15d, %r9d
incl %r15d
xorl %edx, %edx
movl $0x0, -0x60(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq %r14, -0x80(%rbp)
movq %r15, -0xb8(%rbp)
movq %r9, -0x78(%rbp)
cmpq %r15, %rdx
je 0x5226e
movslq 0x18(%rsi), %rcx
movq %rdx, %r13
movl %eax, %ebx
movzbl %al, %eax
cmpq %rcx, %rdx
jge 0x52002
movq 0x30(%rsi), %rdx
movq (%rdx,%r13,8), %rdx
movzwl 0x18(%rdx), %edx
testb $-0x7e, %dl
je 0x52002
testb %dl, %dl
movl $0x0, %ecx
cmovsl %ecx, %eax
movq %r11, %r12
movq %r13, %rdx
incq %rdx
movq %r12, %r11
movq -0x98(%rbp), %rsi
jmp 0x51fb8
movl $0x0, -0x64(%rbp)
cmpq %r9, %r13
movq %r13, -0x48(%rbp)
jae 0x52077
movq 0x8(%r14), %rdx
movq 0x18(%r14), %rsi
movl (%rdx,%r13,4), %r9d
movq 0x38(%r14), %rdi
movswl 0x40(%rsi), %edx
cmpl %edx, %r9d
movl %r9d, -0x50(%rbp)
movl %r9d, %edx
pushq $-0x1
popq %r9
cmovel %r9d, %edx
movb (%rdi,%r13), %dil
testl %edx, %edx
setns %r9b
movl %r9d, -0x64(%rbp)
testb %bl, %bl
movl %edi, -0x5c(%rbp)
je 0x52086
testl %edx, %edx
js 0x52086
movb $0x1, %r9b
movl %r9d, -0x64(%rbp)
cmpq %rcx, -0x48(%rbp)
jl 0x52089
movq 0x8(%rsi), %rcx
movl %edx, %edx
imulq $0x30, %rdx, %rdx
cmpb $0x0, 0x28(%rcx,%rdx)
movl $0x0, %ecx
cmovel %ecx, %eax
jmp 0x5208b
movl $0x0, -0x5c(%rbp)
pushq $-0x1
popq %rax
movl %eax, -0x50(%rbp)
jmp 0x52089
movl %edx, -0x50(%rbp)
movl %ebx, %eax
movb $0x1, %r15b
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r11, -0x70(%rbp)
testb %r15b, %r15b
je 0x52246
cmpq %r10, %rbx
jae 0x52246
movq %r8, %r12
movl %ebx, %ecx
shlq %cl, %r12
btq %rbx, %r11
jb 0x5216d
movl %eax, %r14d
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r13), %rdi
movq %rdi, -0x88(%rbp)
callq 0x45e0b
movl -0x8c(%rbp), %ecx
testl %ecx, %ecx
movzbl %r15b, %r15d
cmovel %ecx, %r15d
cmpb $-0x68, (%rax)
jne 0x5215e
movl -0x3c(%rbp), %ecx
cmpl %ecx, 0x2c(%rax)
jne 0x5215e
movswl 0x30(%rax), %eax
cmpl %eax, -0x50(%rbp)
movq -0x38(%rbp), %r10
pushq $0x1
popq %r8
movq -0x70(%rbp), %r11
jne 0x5216a
cmpb $0x0, -0x64(%rbp)
je 0x52179
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x88(%rbp), %rsi
callq 0x466ee
testq %rax, %rax
jne 0x52131
movq -0xa8(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rdi
movq -0x80(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0xb1bf
testl %eax, %eax
movq -0x38(%rbp), %r10
pushq $0x1
popq %r8
movq -0x70(%rbp), %r11
movl %r14d, %eax
jne 0x5216d
jmp 0x52179
movq -0x38(%rbp), %r10
pushq $0x1
popq %r8
movq -0x70(%rbp), %r11
movl %r14d, %eax
incq %rbx
addq $0x20, %r13
jmp 0x52097
cmpl $0x0, -0x50(%rbp)
movl -0x60(%rbp), %ecx
movzbl %cl, %ecx
pushq $0x1
popq %rax
cmovsl %eax, %ecx
orq %r11, %r12
movq -0x30(%rbp), %rdi
testb $0x1, 0x33(%rdi)
movl %ecx, -0x60(%rbp)
jne 0x521e3
cmpb $0x0, -0xa0(%rbp)
movq -0xb8(%rbp), %r15
movq -0x48(%rbp), %rdx
je 0x521fe
movl -0x5c(%rbp), %esi
xorb -0xb0(%rbp), %sil
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movb $0x1, %cl
movq %rcx, -0xa0(%rbp)
cmpb 0x18(%rax,%r13), %sil
movl %r14d, %eax
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r9
je 0x51ff3
jmp 0x51d00
movl %r14d, %eax
movq -0x80(%rbp), %r14
movq -0xb8(%rbp), %r15
movq -0x78(%rbp), %r9
movq -0x48(%rbp), %rdx
jmp 0x51ff3
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movb 0x18(%rax,%r13), %al
movl %eax, %ecx
movl -0x5c(%rbp), %esi
xorb %sil, %cl
movq %rcx, -0xb0(%rbp)
movb $0x1, %cl
movq %rcx, -0xa0(%rbp)
cmpb %sil, %al
je 0x52236
movq 0x10(%rbp), %rax
movq -0xd8(%rbp), %rcx
orq %rcx, (%rax)
movl %r14d, %eax
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r9
jmp 0x51ff3
movq -0x48(%rbp), %rdx
cmpq -0x78(%rbp), %rdx
movzbl %al, %eax
movl %eax, %ecx
movl $0x0, %eax
cmovbl %eax, %ecx
testq %rdx, %rdx
cmovel %eax, %ecx
movl %ecx, %eax
movq -0x30(%rbp), %rdi
movq -0x98(%rbp), %rsi
movb $0x1, %cl
cmpb $0x0, -0x60(%rbp)
jne 0x5227c
testb %al, %al
je 0x522f6
movl %eax, %ecx
movl %ecx, -0x3c(%rbp)
movq -0xc8(%rbp), %r15
orq 0x8(%rsi), %r15
movq %r15, -0xc8(%rbp)
notq %r15
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq %rbx, %r10
je 0x522e6
movq %r8, %r13
movl %ebx, %ecx
shlq %cl, %r13
btq %rbx, %r11
jb 0x522dd
movq %r11, %r14
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r12), %rsi
movq -0x108(%rbp), %rdi
callq 0x5034c
testq %r15, %rax
je 0x522cf
xorl %r13d, %r13d
movq %r14, %r11
orq %r13, %r11
movq -0x38(%rbp), %r10
pushq $0x1
popq %r8
incq %rbx
addq $0x20, %r12
jmp 0x52299
movq -0x30(%rbp), %rdi
movq -0x98(%rbp), %rsi
movl -0x3c(%rbp), %ebx
jmp 0x522f8
xorl %ebx, %ebx
movq -0xd0(%rbp), %rcx
incq %rcx
movq -0xf0(%rbp), %r9
movq -0xe8(%rbp), %rax
movq -0xf8(%rbp), %rdx
jmp 0x51d8c
pushq $0x1
popq %rax
cmpq -0xc0(%rbp), %r11
je 0x51d09
xorl %eax, %eax
movl -0x88(%rbp), %ecx
negb %cl
sbbl %eax, %eax
jmp 0x51d09