// File: /workspace/github_c_makefile_star2_O1/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 0x470a7
movq 0x10(%rdi), %r14
movq 0x20(%rdi), %rdx
movq (%r14), %rcx
movq %rcx, -0x38(%rbp)
testb $0x10, %al
jne 0x46d97
movq %r14, %rdi
callq 0x48ae3
movl $0x2, %ebx
cmpl $0x0, 0x50(%r14)
jne 0x470a7
movq -0x38(%rbp), %rax
cmpb $0x1, 0x50(%rax)
sbbl $0x0, %ebx
jmp 0x470a7
movq 0x50(%rsi), %r15
testq %rsi, %rsi
je 0x47085
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %r12
movq %rsi, %r8
movq %rsi, -0x88(%rbp)
movq %rdx, -0x40(%rbp)
movq %r15, -0x48(%rbp)
orb $0x2, 0xa(%r8)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x50(%rbp)
movq %r14, -0x80(%rbp)
movq 0x68(%r8), %rsi
movq %r12, %rdi
movq %r8, %r13
callq 0x467fe
movl $0x2, %ebx
testl %eax, %eax
jne 0x470a7
movq 0x70(%r13), %rsi
movq %r12, %rdi
callq 0x467fe
testl %eax, %eax
jne 0x470a7
movq 0x28(%r13), %rax
cmpb $0x0, (%rax)
movq %r13, %rdx
movq %r13, -0x30(%rbp)
je 0x46f28
xorl %r13d, %r13d
movq -0x40(%rbp), %rdi
xorps %xmm0, %xmm0
imulq $0x68, %r13, %rcx
movq 0x30(%rax,%rcx), %rsi
testq %rsi, %rsi
je 0x46f13
xorl %ebx, %ebx
testq %rdi, %rdi
je 0x46e50
movq %rdi, %rdx
addl 0x28(%rdx), %ebx
movq 0x20(%rdx), %rdx
testq %rdx, %rdx
jne 0x46e44
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movq 0x248(%r14), %r12
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x46e6f
movq %rax, 0x248(%r14)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xa0(%rbp)
leaq -0x599(%rip), %rax      # 0x468eb
movq %rax, -0xb0(%rbp)
leaq -0x15b(%rip), %rax      # 0x46d37
movq %rax, -0xa8(%rbp)
movq %r14, -0xa0(%rbp)
movq %rdi, -0x90(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x47c6e
movq %r12, 0x248(%r14)
cmpl $0x0, 0x50(%r14)
jne 0x470a2
movq -0x38(%rbp), %rax
cmpb $0x0, 0x50(%rax)
movq -0x30(%rbp), %rdx
jne 0x470a2
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x46ef8
movq %rdi, %rax
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %r12
subl 0x28(%rax), %ebx
movq 0x20(%rax), %rax
testq %rax, %rax
jne 0x46eea
jmp 0x46eff
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %r12
testl %ebx, %ebx
setne %al
movb 0x39(%r15), %cl
addb %al, %al
andb $-0x3, %cl
orb %al, %cl
movb %cl, 0x39(%r15)
incq %r13
movq 0x28(%rdx), %rax
movzbl (%rax), %ecx
cmpq %rcx, %r13
jb 0x46e28
jmp 0x46f2c
movq -0x40(%rbp), %rdi
movb $0x1, -0x50(%rbp)
movq %rax, -0x78(%rbp)
movq %rdi, -0x60(%rbp)
movq (%rdx), %rbx
cmpl $0x0, (%rbx)
jle 0x46f6d
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq (%rax,%r15), %rsi
movq %r12, %rdi
callq 0x467fe
testl %eax, %eax
jne 0x470a2
incq %r13
movslq (%rbx), %rax
addq $0x20, %r15
cmpq %rax, %r13
jl 0x46f46
movq -0x30(%rbp), %rdx
movq 0x38(%rdx), %r15
testq %r15, %r15
jne 0x46f88
movb -0x50(%rbp), %al
testb $0x2, %al
jne 0x46f88
andb $-0x4, %al
movb %al, -0x50(%rbp)
jmp 0x46f8c
orb $0x4, 0xa(%rdx)
movl $0x2, %ebx
testq %r15, %r15
setne %al
movq 0x40(%rdx), %rsi
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0x470ce
movq (%rdx), %rax
movq %rax, -0x70(%rbp)
movq %r12, %rdi
movq %rdx, %r13
callq 0x467fe
testl %eax, %eax
jne 0x470a7
movq 0x30(%r13), %rsi
movq %r12, %rdi
callq 0x467fe
testl %eax, %eax
jne 0x470a7
orb $0x1, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x47009
movq -0x30(%rbp), %rsi
movq 0x48(%rsi), %rdx
movq %r12, %rdi
leaq 0x340cc(%rip), %rcx     # 0x7b0c8
callq 0x48c0a
testl %eax, %eax
jne 0x470a7
movq -0x38(%rbp), %rax
cmpb $0x0, 0x50(%rax)
jne 0x470a7
movq %r15, %r13
testq %r15, %r15
xorps %xmm0, %xmm0
movq -0x30(%rbp), %r8
movq -0x48(%rbp), %r15
je 0x47071
movq %r12, %rdi
movq %r8, %rsi
movq %r13, %rdx
leaq 0x34094(%rip), %rcx     # 0x7b0ce
callq 0x48c0a
testl %eax, %eax
jne 0x470a7
movq -0x38(%rbp), %rax
cmpb $0x0, 0x50(%rax)
jne 0x470a7
movl (%r13), %eax
testl %eax, %eax
xorps %xmm0, %xmm0
movq -0x30(%rbp), %r8
jle 0x47071
movq 0x8(%r13), %rcx
movq (%rcx), %rsi
testb $0x2, 0x2(%rsi)
jne 0x470bb
addq $0x20, %rcx
decl %eax
jne 0x47060
movq 0x50(%r8), %r8
testq %r8, %r8
movq -0x88(%rbp), %rsi
jne 0x46dbd
testq %r15, %r15
je 0x4709b
movq %r14, %rdi
callq 0x48dc0
movl $0x2, %ebx
testl %eax, %eax
jne 0x470a7
movl $0x1, %ebx
jmp 0x470a7
movl $0x2, %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x34012(%rip), %rsi     # 0x7b0d4
movq %r14, %rdi
xorl %eax, %eax
callq 0x11724
jmp 0x470a7
leaq 0x33fc7(%rip), %rsi     # 0x7b09c
jmp 0x470c2