// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: resolveSelectStep
static int resolveSelectStep(Walker *pWalker, Select *p){
  NameContext *pOuterNC;  /* Context that contains this SELECT */
  NameContext sNC;        /* Name context of this SELECT */
  int isCompound;         /* True if p is a compound select */
  int nCompound;          /* Number of compound terms processed so far */
  Parse *pParse;          /* Parsing context */
  int i;                  /* Loop counter */
  ExprList *pGroupBy;     /* The GROUP BY clause */
  Select *pLeftmost;      /* Left-most of SELECT of a compound */
  sqlite3 *db;            /* Database connection */


  assert( p!=0 );
  if( p->selFlags & SF_Resolved ){
    return WRC_Prune;
  }
  pOuterNC = pWalker->u.pNC;
  pParse = pWalker->pParse;
  db = pParse->db;

  /* Normally sqlite3SelectExpand() will be called first and will have
  ** already expanded this SELECT.  However, if this is a subquery within
  ** an expression, sqlite3ResolveExprNames() will be called without a
  ** prior call to sqlite3SelectExpand().  When that happens, let
  ** sqlite3SelectPrep() do all of the processing for this SELECT.
  ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
  ** this routine in the correct order.
  */
  if( (p->selFlags & SF_Expanded)==0 ){
    sqlite3SelectPrep(pParse, p, pOuterNC);
    return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
  }

  isCompound = p->pPrior!=0;
  nCompound = 0;
  pLeftmost = p;
  while( p ){
    assert( (p->selFlags & SF_Expanded)!=0 );
    assert( (p->selFlags & SF_Resolved)==0 );
    p->selFlags |= SF_Resolved;

    /* Resolve the expressions in the LIMIT and OFFSET clauses. These
    ** are not allowed to refer to any names, so pass an empty NameContext.
    */
    memset(&sNC, 0, sizeof(sNC));
    sNC.pParse = pParse;
    sNC.pWinSelect = p;
    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
      return WRC_Abort;
    }

    /* If the SF_Converted flags is set, then this Select object was
    ** was created by the convertCompoundSelectToSubquery() function.
    ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
    ** as if it were part of the sub-query, not the parent. This block
    ** moves the pOrderBy down to the sub-query. It will be moved back
    ** after the names have been resolved.  */
    if( p->selFlags & SF_Converted ){
      Select *pSub = p->pSrc->a[0].pSelect;
      assert( p->pSrc->nSrc==1 && p->pOrderBy );
      assert( pSub->pPrior && pSub->pOrderBy==0 );
      pSub->pOrderBy = p->pOrderBy;
      p->pOrderBy = 0;
    }

    /* Recursively resolve names in all subqueries
    */
    for(i=0; i<p->pSrc->nSrc; i++){
      struct SrcList_item *pItem = &p->pSrc->a[i];
      if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
        NameContext *pNC;         /* Used to iterate name contexts */
        int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
        const char *zSavedContext = pParse->zAuthContext;

        /* Count the total number of references to pOuterNC and all of its
        ** parent contexts. After resolving references to expressions in
        ** pItem->pSelect, check if this value has changed. If so, then
        ** SELECT statement pItem->pSelect must be correlated. Set the
        ** pItem->fg.isCorrelated flag if this is the case. */
        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;

        if( pItem->zName ) pParse->zAuthContext = pItem->zName;
        sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
        pParse->zAuthContext = zSavedContext;
        if( pParse->nErr || db->mallocFailed ) return WRC_Abort;

        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
        assert( pItem->fg.isCorrelated==0 && nRef<=0 );
        pItem->fg.isCorrelated = (nRef!=0);
      }
    }

    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
    ** resolve the result-set expression list.
    */
    sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
    sNC.pSrcList = p->pSrc;
    sNC.pNext = pOuterNC;

    /* Resolve names in the result set. */
    if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
    sNC.ncFlags &= ~NC_AllowWin;

    /* If there are no aggregate functions in the result-set, and no GROUP BY
    ** expression, do not allow aggregates in any of the other expressions.
    */
    assert( (p->selFlags & SF_Aggregate)==0 );
    pGroupBy = p->pGroupBy;
    if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
      assert( NC_MinMaxAgg==SF_MinMaxAgg );
      p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
    }else{
      sNC.ncFlags &= ~NC_AllowAgg;
    }

    /* If a HAVING clause is present, then there must be a GROUP BY clause.
    */
    if( p->pHaving && !pGroupBy ){
      sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
      return WRC_Abort;
    }

    /* Add the output column list to the name-context before parsing the
    ** other expressions in the SELECT statement. This is so that
    ** expressions in the WHERE clause (etc.) can refer to expressions by
    ** aliases in the result set.
    **
    ** Minor point: If this is the case, then the expression will be
    ** re-evaluated for each reference to it.
    */
    assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
    sNC.uNC.pEList = p->pEList;
    sNC.ncFlags |= NC_UEList;
    if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
    if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;

    /* Resolve names in table-valued-function arguments */
    for(i=0; i<p->pSrc->nSrc; i++){
      struct SrcList_item *pItem = &p->pSrc->a[i];
      if( pItem->fg.isTabFunc
       && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
      ){
        return WRC_Abort;
      }
    }

    /* The ORDER BY and GROUP BY clauses may not refer to terms in
    ** outer queries
    */
    sNC.pNext = 0;
    sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;

    /* If this is a converted compound query, move the ORDER BY clause from
    ** the sub-query back to the parent query. At this point each term
    ** within the ORDER BY clause has been transformed to an integer value.
    ** These integers will be replaced by copies of the corresponding result
    ** set expressions by the call to resolveOrderGroupBy() below.  */
    if( p->selFlags & SF_Converted ){
      Select *pSub = p->pSrc->a[0].pSelect;
      p->pOrderBy = pSub->pOrderBy;
      pSub->pOrderBy = 0;
    }

    /* Process the ORDER BY clause for singleton SELECT statements.
    ** The ORDER BY clause for compounds SELECT statements is handled
    ** below, after all of the result-sets for all of the elements of
    ** the compound have been resolved.
    **
    ** If there is an ORDER BY clause on a term of a compound-select other
    ** than the right-most term, then that is a syntax error.  But the error
    ** is not detected until much later, and so we need to go ahead and
    ** resolve those symbols on the incorrect ORDER BY for consistency.
    */
    if( isCompound<=nCompound  /* Defer right-most ORDER BY of a compound */
     && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
    ){
      return WRC_Abort;
    }
    if( db->mallocFailed ){
      return WRC_Abort;
    }
    sNC.ncFlags &= ~NC_AllowWin;

    /* Resolve the GROUP BY clause.  At the same time, make sure
    ** the GROUP BY clause does not contain aggregate functions.
    */
    if( pGroupBy ){
      struct ExprList_item *pItem;

      if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
        return WRC_Abort;
      }
      for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
        if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
          sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
              "the GROUP BY clause");
          return WRC_Abort;
        }
      }
    }

#ifndef SQLITE_OMIT_WINDOWFUNC
    if( IN_RENAME_OBJECT ){
      Window *pWin;
      for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
        if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
         || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
        ){
          return WRC_Abort;
        }
      }
    }
#endif

    /* If this is part of a compound SELECT, check that it has the right
    ** number of expressions in the select list. */
    if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
      sqlite3SelectWrongNumTermsError(pParse, p->pNext);
      return WRC_Abort;
    }

    /* Advance to the next term of the compound
    */
    p = p->pPrior;
    nCompound++;
  }

  /* Resolve the ORDER BY on a compound SELECT after all terms of
  ** the compound have been resolved.
  */
  if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
    return WRC_Abort;
  }

  return WRC_Prune;
}

// Assembly:
movl 0x4(%rsi), %eax
movl $0x1, %ecx
testb $0x4, %al
jne 0x2cfe3d
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq (%rdi), %r14
movq 0x28(%rdi), %r12
movq (%r14), %rcx
movq %rcx, (%rsp)
testb $0x40, %al
jne 0x2cfa3f
movq %r14, %rdi
movq %r12, %rdx
callq 0x2d0563
movl $0x2, %ecx
cmpl $0x0, 0x30(%r14)
jne 0x2cfe2c
movq (%rsp), %rax
cmpb $0x1, 0x61(%rax)
sbbl $0x0, %ecx
jmp 0x2cfe2c
movq 0x50(%rsi), %rax
movq %rax, 0x50(%rsp)
testq %rsi, %rsi
je 0x2cfdf6
movq $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r13
movq %rsi, %r15
movq %r12, 0x58(%rsp)
movq %rsi, 0x60(%rsp)
orb $0x4, 0x4(%r15)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %r14, 0x10(%rsp)
movq %r15, 0x40(%rsp)
movq 0x60(%r15), %rsi
movq %r13, %rdi
callq 0x2ce0ea
testl %eax, %eax
jne 0x2cfe27
testb $0x1, 0x6(%r15)
je 0x2cfac0
movq 0x28(%r15), %rax
movq 0x48(%r15), %rcx
movq 0x30(%rax), %rax
movq %rcx, 0x48(%rax)
movq $0x0, 0x48(%r15)
movq 0x28(%r15), %rax
cmpl $0x0, (%rax)
jle 0x2cfbbd
xorl %r13d, %r13d
imulq $0x70, %r13, %rcx
movq 0x30(%rax,%rcx), %rsi
testq %rsi, %rsi
je 0x2cfbaa
testb $0x4, 0x4(%rsi)
jne 0x2cfbaa
xorl %ebp, %ebp
testq %r12, %r12
je 0x2cfaff
addl 0x20(%r12), %ebp
movq 0x18(%r12), %r12
jmp 0x2cfaee
leaq (%rax,%rcx), %rbx
addq $0x8, %rbx
movq 0x160(%r14), %r12
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x2cfb1e
movq %rax, 0x160(%r14)
leaq -0x1823(%rip), %rax     # 0x2ce302
movq %rax, 0x70(%rsp)
leaq -0x150(%rip), %rax      # 0x2cf9e1
movq %rax, 0x78(%rsp)
movq $0x0, 0x80(%rsp)
movq %r14, 0x68(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x2ca2ea
movq %r12, 0x160(%r14)
cmpl $0x0, 0x30(%r14)
jne 0x2cfe27
movq (%rsp), %rax
cmpb $0x0, 0x61(%rax)
jne 0x2cfe27
movq 0x58(%rsp), %r12
testq %r12, %r12
je 0x2cfb97
movq %r12, %rax
subl 0x20(%rax), %ebp
movq 0x18(%rax), %rax
testq %rax, %rax
jne 0x2cfb8b
testl %ebp, %ebp
setne %al
movb 0x3d(%rbx), %cl
shlb $0x3, %al
andb $-0x9, %cl
orb %al, %cl
movb %cl, 0x3d(%rbx)
incq %r13
movq 0x28(%r15), %rax
movslq (%rax), %rcx
cmpq %rcx, %r13
jl 0x2cfad0
movl $0x4001, 0x38(%rsp)     # imm = 0x4001
movq %rax, 0x18(%rsp)
movq %r12, 0x28(%rsp)
movq 0x20(%r15), %rsi
leaq 0x10(%rsp), %rdi
callq 0x2ce1c1
testl %eax, %eax
jne 0x2cfe27
movl 0x38(%rsp), %eax
movl %eax, %ecx
andl $0xffffbfff, %ecx       # imm = 0xFFFFBFFF
movl %ecx, 0x38(%rsp)
movq 0x38(%r15), %r13
testq %r13, %r13
setne %cl
movl %eax, %edx
andl $0x10, %edx
shrl $0x4, %edx
orb %cl, %dl
cmpb $0x1, %dl
jne 0x2cfc20
andl $0x1000, %eax           # imm = 0x1000
orl 0x4(%r15), %eax
orl $0x8, %eax
movl %eax, 0x4(%r15)
jmp 0x2cfc29
andl $0xffffbfee, %eax       # imm = 0xFFFFBFEE
movl %eax, 0x38(%rsp)
testq %r13, %r13
setne %al
movq 0x40(%r15), %rsi
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0x2cfe40
movq 0x20(%r15), %rax
movq %rax, 0x20(%rsp)
orb $-0x80, 0x38(%rsp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ce0ea
testl %eax, %eax
jne 0x2cfe27
movq 0x30(%r15), %rsi
movq %rbx, %rdi
callq 0x2ce0ea
testl %eax, %eax
jne 0x2cfe27
movq 0x28(%r15), %rax
cmpl $0x0, (%rax)
jle 0x2cfcb8
movl $0x68, %ebx
xorl %ebp, %ebp
testb $0x4, -0x23(%rax,%rbx)
je 0x2cfca5
movq (%rax,%rbx), %rsi
leaq 0x10(%rsp), %rdi
callq 0x2ce1c1
testl %eax, %eax
jne 0x2cfe27
incq %rbp
movq 0x28(%r15), %rax
movslq (%rax), %rcx
addq $0x70, %rbx
cmpq %rcx, %rbp
jl 0x2cfc88
movq $0x0, 0x28(%rsp)
orl $0x4001, 0x38(%rsp)     # imm = 0x4001
testb $0x1, 0x6(%r15)
je 0x2cfce4
movq 0x30(%rax), %rax
movq 0x48(%rax), %rcx
movq %rcx, 0x48(%r15)
movq $0x0, 0x48(%rax)
cmpq $0x0, 0x50(%rsp)
setne %al
cmpl $0x0, 0x8(%rsp)
sete %cl
testb %cl, %al
jne 0x2cfd19
movq 0x48(%r15), %rdx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
leaq 0x1116ec(%rip), %rcx    # 0x3e13f8
callq 0x2d0676
testl %eax, %eax
jne 0x2cfe27
movq (%rsp), %rax
cmpb $0x0, 0x61(%rax)
jne 0x2cfe27
andb $-0x41, 0x39(%rsp)
testq %r13, %r13
je 0x2cfd80
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
leaq 0x1116bb(%rip), %rcx    # 0x3e13fe
callq 0x2d0676
testl %eax, %eax
jne 0x2cfe27
movq (%rsp), %rax
cmpb $0x0, 0x61(%rax)
jne 0x2cfe27
movl (%r13), %eax
testl %eax, %eax
jle 0x2cfd80
addq $0x8, %r13
movq (%r13), %rcx
testb $0x10, 0x4(%rcx)
jne 0x2cfe16
addq $0x18, %r13
decl %eax
jne 0x2cfd6a
cmpb $0x2, 0x11c(%r14)
leaq 0x10(%rsp), %r13
jb 0x2cfdbe
movq 0x78(%r15), %rbx
testq %rbx, %rbx
je 0x2cfdbe
movq 0x18(%rbx), %rsi
movq %r13, %rdi
callq 0x2ce1c1
testl %eax, %eax
jne 0x2cfe27
movq 0x10(%rbx), %rsi
movq %r13, %rdi
callq 0x2ce1c1
testl %eax, %eax
jne 0x2cfe27
movq 0x40(%rbx), %rbx
jmp 0x2cfd93
movq 0x58(%r15), %rsi
testq %rsi, %rsi
je 0x2cfdd5
movq 0x20(%r15), %rax
movl (%rax), %eax
movq 0x20(%rsi), %rcx
cmpl (%rcx), %eax
jne 0x2cfe49
movq 0x50(%r15), %r15
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
testq %r15, %r15
movq 0x60(%rsp), %rsi
xorps %xmm0, %xmm0
jne 0x2cfa6f
cmpq $0x0, 0x50(%rsp)
je 0x2cfe0f
movq %r14, %rdi
callq 0x2d08d1
movl $0x2, %ecx
testl %eax, %eax
jne 0x2cfe2c
movl $0x1, %ecx
jmp 0x2cfe2c
leaq 0x1115e7(%rip), %rsi    # 0x3e1404
movq %r14, %rdi
xorl %eax, %eax
callq 0x284eb8
movl $0x2, %ecx
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl %ecx, %eax
retq
leaq 0x111585(%rip), %rsi    # 0x3e13cc
jmp 0x2cfe1d
movq %r14, %rdi
callq 0x2d0890
jmp 0x2cfe27