// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/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 */
  ExprList *pEList;       /* Result set expression list */
  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;
    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
      return WRC_Abort;
    }
  
    /* 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 ){
        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->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->isCorrelated==0 && nRef<=0 );
        pItem->isCorrelated = (nRef!=0);
      }
    }
  
    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
    ** resolve the result-set expression list.
    */
    sNC.ncFlags = NC_AllowAgg;
    sNC.pSrcList = p->pSrc;
    sNC.pNext = pOuterNC;
  
    /* Resolve names in the result set. */
    pEList = p->pEList;
    assert( pEList!=0 );
    for(i=0; i<pEList->nExpr; i++){
      Expr *pX = pEList->a[i].pExpr;
      if( sqlite3ResolveExprNames(&sNC, pX) ){
        return WRC_Abort;
      }
    }
  
    /* 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 ){
      p->selFlags |= SF_Aggregate;
    }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.
    */
    sNC.pEList = p->pEList;
    if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
    if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) 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;

    /* 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( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
      return WRC_Abort;
    }
    if( db->mallocFailed ){
      return WRC_Abort;
    }
  
    /* 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;
        }
      }
    }

    /* 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:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movzwl 0xa(%rsi), %eax
movl $0x1, %ebx
testb $0x2, %al
jne 0x4738b
movq 0x10(%rdi), %r14
movq 0x20(%rdi), %rdx
movq (%r14), %rcx
movq %rcx, -0x30(%rbp)
testb $0x10, %al
jne 0x470a7
movq %r14, %rdi
callq 0x48dde
movl $0x2, %ebx
cmpl $0x0, 0x50(%r14)
jne 0x4738b
movq -0x30(%rbp), %rax
cmpb $0x1, 0x50(%rax)
sbbl $0x0, %ebx
jmp 0x4738b
movq 0x50(%rsi), %rax
movq %rax, -0x40(%rbp)
leaq -0x80(%rbp), %rax
xorps %xmm0, %xmm0
leaq -0x88(%rbp), %r12
movq %rsi, -0x50(%rbp)
movq %rsi, %r15
movq %rdx, -0x38(%rbp)
orb $0x2, 0xa(%r15)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %r14, -0x88(%rbp)
movq 0x68(%r15), %rsi
movq %r12, %rdi
callq 0x46b2f
movl $0x2, %ebx
testl %eax, %eax
jne 0x4738b
movq 0x70(%r15), %rsi
movq %r12, %rdi
callq 0x46b2f
testl %eax, %eax
jne 0x4738b
movq 0x28(%r15), %rax
movb (%rax), %cl
testb %cl, %cl
movq %r15, -0x48(%rbp)
je 0x47224
xorl %r13d, %r13d
movq -0x38(%rbp), %rdi
imulq $0x68, %r13, %rdx
movq 0x30(%rax,%rdx), %rsi
testq %rsi, %rsi
je 0x47213
xorl %ebx, %ebx
testq %rdi, %rdi
je 0x4714d
movq %rdi, %rcx
addl 0x28(%rcx), %ebx
movq 0x20(%rcx), %rcx
testq %rcx, %rcx
jne 0x47141
leaq (%rax,%rdx), %r15
addq $0x8, %r15
movq 0x248(%r14), %r12
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x4716c
movq %rax, 0x248(%r14)
movq $0x0, -0x98(%rbp)
leaq -0x56f(%rip), %rax      # 0x46c0f
movq %rax, -0xb0(%rbp)
leaq -0x145(%rip), %rax      # 0x47047
movq %rax, -0xa8(%rbp)
movq %r14, -0xa0(%rbp)
movq %rdi, -0x90(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x47f1c
movq %r12, 0x248(%r14)
cmpl $0x0, 0x50(%r14)
jne 0x47386
movq -0x30(%rbp), %rax
cmpb $0x0, 0x50(%rax)
jne 0x47386
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x471ee
movq %rdi, %rax
leaq -0x88(%rbp), %r12
subl 0x28(%rax), %ebx
movq 0x20(%rax), %rax
testq %rax, %rax
jne 0x471e0
jmp 0x471f5
leaq -0x88(%rbp), %r12
testl %ebx, %ebx
setne %al
movb 0x39(%r15), %cl
addb %al, %al
andb $-0x3, %cl
orb %al, %cl
movb %cl, 0x39(%r15)
movq -0x48(%rbp), %r15
movq 0x28(%r15), %rax
movb (%rax), %cl
incq %r13
movzbl %cl, %edx
cmpq %rdx, %r13
jb 0x47125
jmp 0x47228
movq -0x38(%rbp), %rdi
movb $0x1, -0x58(%rbp)
movq %rax, -0x80(%rbp)
movq %rdi, -0x68(%rbp)
movq (%r15), %rbx
cmpl $0x0, (%rbx)
jle 0x47269
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq (%rax,%r15), %rsi
movq %r12, %rdi
callq 0x46b2f
testl %eax, %eax
jne 0x47386
incq %r13
movslq (%rbx), %rax
addq $0x20, %r15
cmpq %rax, %r13
jl 0x47242
movq -0x48(%rbp), %r15
movq 0x38(%r15), %r13
testq %r13, %r13
jne 0x47284
movb -0x58(%rbp), %al
testb $0x2, %al
jne 0x47284
andb $-0x4, %al
movb %al, -0x58(%rbp)
jmp 0x47289
orb $0x4, 0xa(%r15)
movl $0x2, %ebx
testq %r13, %r13
setne %al
movq 0x40(%r15), %rsi
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0x473b2
movq (%r15), %rax
movq %rax, -0x78(%rbp)
movq %r12, %rdi
callq 0x46b2f
testl %eax, %eax
jne 0x4738b
movq 0x30(%r15), %rsi
movq %r12, %rdi
callq 0x46b2f
testl %eax, %eax
jne 0x4738b
orb $0x1, -0x58(%rbp)
movq $0x0, -0x68(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x47302
movq 0x48(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
leaq 0x33dd3(%rip), %rcx     # 0x7b0c8
callq 0x48ef0
testl %eax, %eax
jne 0x4738b
movq -0x30(%rbp), %rax
cmpb $0x0, 0x50(%rax)
jne 0x4738b
testq %r13, %r13
xorps %xmm0, %xmm0
je 0x47357
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
leaq 0x33daa(%rip), %rcx     # 0x7b0ce
callq 0x48ef0
testl %eax, %eax
jne 0x4738b
movq -0x30(%rbp), %rax
cmpb $0x0, 0x50(%rax)
jne 0x4738b
movl (%r13), %eax
testl %eax, %eax
xorps %xmm0, %xmm0
jle 0x47357
movq 0x8(%r13), %rcx
movq (%rcx), %rsi
testb $0x2, 0x2(%rsi)
jne 0x4739f
addq $0x20, %rcx
decl %eax
jne 0x47346
movq 0x50(%r15), %r15
testq %r15, %r15
leaq -0x80(%rbp), %rax
jne 0x470c8
cmpq $0x0, -0x40(%rbp)
je 0x4737f
movq %r14, %rdi
movq -0x50(%rbp), %rsi
callq 0x4908a
testl %eax, %eax
jne 0x4738b
movl $0x1, %ebx
jmp 0x4738b
movl $0x2, %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x33d2e(%rip), %rsi     # 0x7b0d4
movq %r14, %rdi
xorl %eax, %eax
callq 0x11842
jmp 0x4738b
leaq 0x33ce3(%rip), %rsi     # 0x7b09c
jmp 0x473a6