// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: analyzeAggregate
static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
  int i;
  NameContext *pNC = pWalker->u.pNC;
  Parse *pParse = pNC->pParse;
  SrcList *pSrcList = pNC->pSrcList;
  AggInfo *pAggInfo = pNC->pAggInfo;

  switch( pExpr->op ){
    case TK_AGG_COLUMN:
    case TK_COLUMN: {
      testcase( pExpr->op==TK_AGG_COLUMN );
      testcase( pExpr->op==TK_COLUMN );
      /* Check to see if the column is in one of the tables in the FROM
      ** clause of the aggregate query */
      if( ALWAYS(pSrcList!=0) ){
        struct SrcList_item *pItem = pSrcList->a;
        for(i=0; i<pSrcList->nSrc; i++, pItem++){
          struct AggInfo_col *pCol;
          assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
          if( pExpr->iTable==pItem->iCursor ){
            /* If we reach this point, it means that pExpr refers to a table
            ** that is in the FROM clause of the aggregate query.  
            **
            ** Make an entry for the column in pAggInfo->aCol[] if there
            ** is not an entry there already.
            */
            int k;
            pCol = pAggInfo->aCol;
            for(k=0; k<pAggInfo->nColumn; k++, pCol++){
              if( pCol->iTable==pExpr->iTable &&
                  pCol->iColumn==pExpr->iColumn ){
                break;
              }
            }
            if( (k>=pAggInfo->nColumn)
             && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
            ){
              pCol = &pAggInfo->aCol[k];
              pCol->pTab = pExpr->pTab;
              pCol->iTable = pExpr->iTable;
              pCol->iColumn = pExpr->iColumn;
              pCol->iMem = ++pParse->nMem;
              pCol->iSorterColumn = -1;
              pCol->pExpr = pExpr;
              if( pAggInfo->pGroupBy ){
                int j, n;
                ExprList *pGB = pAggInfo->pGroupBy;
                struct ExprList_item *pTerm = pGB->a;
                n = pGB->nExpr;
                for(j=0; j<n; j++, pTerm++){
                  Expr *pE = pTerm->pExpr;
                  if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
                      pE->iColumn==pExpr->iColumn ){
                    pCol->iSorterColumn = j;
                    break;
                  }
                }
              }
              if( pCol->iSorterColumn<0 ){
                pCol->iSorterColumn = pAggInfo->nSortingColumn++;
              }
            }
            /* There is now an entry for pExpr in pAggInfo->aCol[] (either
            ** because it was there before or because we just created it).
            ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
            ** pAggInfo->aCol[] entry.
            */
            ExprSetIrreducible(pExpr);
            pExpr->pAggInfo = pAggInfo;
            pExpr->op = TK_AGG_COLUMN;
            pExpr->iAgg = (i16)k;
            break;
          } /* endif pExpr->iTable==pItem->iCursor */
        } /* end loop over pSrcList */
      }
      return WRC_Prune;
    }
    case TK_AGG_FUNCTION: {
      if( (pNC->ncFlags & NC_InAggFunc)==0
       && pWalker->walkerDepth==pExpr->op2
      ){
        /* Check to see if pExpr is a duplicate of another aggregate 
        ** function that is already in the pAggInfo structure
        */
        struct AggInfo_func *pItem = pAggInfo->aFunc;
        for(i=0; i<pAggInfo->nFunc; i++, pItem++){
          if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
            break;
          }
        }
        if( i>=pAggInfo->nFunc ){
          /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
          */
          u8 enc = ENC(pParse->db);
          i = addAggInfoFunc(pParse->db, pAggInfo);
          if( i>=0 ){
            assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
            pItem = &pAggInfo->aFunc[i];
            pItem->pExpr = pExpr;
            pItem->iMem = ++pParse->nMem;
            assert( !ExprHasProperty(pExpr, EP_IntValue) );
            pItem->pFunc = sqlite3FindFunction(pParse->db,
                   pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
                   pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
            if( pExpr->flags & EP_Distinct ){
              pItem->iDistinct = pParse->nTab++;
            }else{
              pItem->iDistinct = -1;
            }
          }
        }
        /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
        */
        assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
        ExprSetIrreducible(pExpr);
        pExpr->iAgg = (i16)i;
        pExpr->pAggInfo = pAggInfo;
        return WRC_Prune;
      }else{
        return WRC_Continue;
      }
    }
  }
  return WRC_Continue;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq 0x20(%rdi), %rcx
movq (%rcx), %r13
movq 0x8(%rcx), %rax
movq 0x18(%rcx), %r12
movzbl (%rsi), %edx
cmpl $0x9a, %edx
je 0x52a3d
xorl %r14d, %r14d
cmpl $0x99, %edx
je 0x52a6a
cmpl $0x98, %edx
jne 0x52c97
pushq $0x1
popq %r14
testq %rax, %rax
je 0x52c97
movzbl (%rax), %ecx
imulq $0x68, %rcx, %rcx
xorl %edx, %edx
cmpl %edx, %ecx
je 0x52c97
movl 0x2c(%rbx), %edi
cmpl 0x44(%rax,%rdx), %edi
je 0x52ad7
addq $0x68, %rdx
jmp 0x52a53
testb $0x8, 0x30(%rcx)
jne 0x52c97
movzbl 0x37(%rbx), %eax
cmpl %eax, 0x18(%rdi)
jne 0x52c97
movq %r13, -0x58(%rbp)
movq 0x28(%r12), %r13
leaq 0x30(%r12), %rax
movq %rax, -0x40(%rbp)
movq %r12, -0x48(%rbp)
movl 0x30(%r12), %r12d
xorl %r15d, %r15d
testl %r12d, %r12d
cmovlel %r15d, %r12d
pushq $-0x1
popq %r14
movq %r13, -0x38(%rbp)
cmpl %r15d, %r12d
je 0x52bcd
movq (%r13), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x46bdc
testl %eax, %eax
je 0x52c4d
incl %r15d
addq $0x18, %r13
jmp 0x52aae
movq 0x18(%r12), %rsi
leaq 0x20(%r12), %rcx
movl 0x20(%r12), %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
leaq 0xc(%rsi), %r8
cmpl %edx, %eax
je 0x52b11
cmpl %edi, -0x4(%r8)
jne 0x52b09
movswl 0x30(%rbx), %r9d
cmpl %r9d, (%r8)
je 0x52bbd
incl %edx
addq $0x20, %r8
jmp 0x52af1
movq (%r13), %rdi
pushq $0x20
popq %rdx
leaq -0x5c(%rbp), %r15
movq %r15, %r8
callq 0x52cb1
movq %rax, 0x18(%r12)
movslq (%r15), %rdx
testq %rdx, %rdx
js 0x52bbd
movq %rax, %rdi
movq %rdx, %r8
shlq $0x5, %r8
addq %r8, %rax
movq 0x40(%rbx), %rcx
movq %rcx, (%rdi,%r8)
movl 0x2c(%rbx), %ecx
movl %ecx, 0x8(%rdi,%r8)
movswl 0x30(%rbx), %esi
movl %esi, 0xc(%rdi,%r8)
movl 0x58(%r13), %r9d
incl %r9d
movl %r9d, 0x58(%r13)
movl %r9d, 0x14(%rdi,%r8)
movq %rbx, 0x18(%rdi,%r8)
movq 0x10(%r12), %rdi
testq %rdi, %rdi
je 0x52bad
movq 0x8(%rdi), %r8
movl (%rdi), %r9d
xorl %edi, %edi
testl %r9d, %r9d
cmovlel %edi, %r9d
cmpl %edi, %r9d
je 0x52bad
movq (%r8), %r10
cmpb $-0x68, (%r10)
jne 0x52ba5
cmpl %ecx, 0x2c(%r10)
jne 0x52ba5
cmpw %si, 0x30(%r10)
je 0x52bba
incl %edi
addq $0x20, %r8
jmp 0x52b8a
movl 0xc(%r12), %edi
leal 0x1(%rdi), %ecx
movl %ecx, 0xc(%r12)
movl %edi, 0x10(%rax)
movq %r12, 0x38(%rbx)
movb $-0x66, (%rbx)
movw %dx, 0x32(%rbx)
jmp 0x52c97
movq -0x58(%rbp), %r13
movq (%r13), %rdi
movq 0x20(%rdi), %rax
movq 0x18(%rax), %rax
movb 0x71(%rax), %al
movb %al, -0x29(%rbp)
pushq $0x18
popq %rdx
leaq -0x60(%rbp), %r15
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
movq %r15, %r8
callq 0x52cb1
movq -0x48(%rbp), %r12
movq %rax, 0x28(%r12)
movslq (%r15), %r15
testq %r15, %r15
js 0x52c8a
movq %rax, %rsi
imulq $0x18, %r15, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq %rbx, (%rsi,%rcx)
movl 0x58(%r13), %edx
incl %edx
movl %edx, 0x58(%r13)
movl %edx, 0x10(%rsi,%rcx)
movq (%r13), %rax
movq %rax, -0x38(%rbp)
movq 0x8(%rbx), %rdi
movq %rdi, -0x50(%rbp)
callq 0x8f8e
movq 0x20(%rbx), %rcx
testq %rcx, %rcx
je 0x52c53
movl (%rcx), %ecx
jmp 0x52c55
movq -0x48(%rbp), %r12
jmp 0x52c8a
xorl %ecx, %ecx
movzbl -0x29(%rbp), %r8d
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl %eax, %edx
xorl %r9d, %r9d
callq 0x13a4e
movq -0x40(%rbp), %rcx
movq %rax, 0x8(%rcx)
testb $0x10, 0x2(%rbx)
je 0x52c86
movl 0x54(%r13), %r14d
leal 0x1(%r14), %eax
movl %eax, 0x54(%r13)
movl %r14d, 0x14(%rcx)
movw %r15w, 0x32(%rbx)
movq %r12, 0x38(%rbx)
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq