// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movq %rsi, %rbx
movq 0x20(%rdi), %rax
movq (%rax), %r13
movq 0x8(%rax), %rcx
movq 0x18(%rax), %r12
movzbl (%rsi), %edx
cmpl $0x9a, %edx
je 0x5bb69
xorl %r14d, %r14d
cmpl $0x99, %edx
je 0x5bba4
cmpl $0x98, %edx
jne 0x5bdbc
movl $0x1, %r14d
testq %rcx, %rcx
je 0x5bdbc
movzbl (%rcx), %edx
testq %rdx, %rdx
je 0x5bdbc
movl 0x2c(%rbx), %eax
imulq $0x68, %rdx, %rdx
xorl %esi, %esi
cmpl 0x44(%rcx,%rsi), %eax
je 0x5bcd1
addq $0x68, %rsi
cmpl %esi, %edx
jne 0x5bb8d
jmp 0x5bdbc
testb $0x8, 0x30(%rax)
jne 0x5bdbc
movzbl 0x37(%rbx), %eax
cmpl %eax, 0x18(%rdi)
jne 0x5bdbc
movq %r13, -0x38(%rbp)
movq 0x28(%r12), %rax
movq %rax, -0x30(%rbp)
movl 0x30(%r12), %r14d
testl %r14d, %r14d
jle 0x5bbfe
xorl %r15d, %r15d
movq -0x30(%rbp), %r13
movq (%r13), %rdi
movq %rbx, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4b798
testl %eax, %eax
je 0x5bcbd
incl %r15d
addq $0x18, %r13
cmpl %r15d, %r14d
jne 0x5bbd9
leaq 0x30(%r12), %rcx
movq -0x38(%rbp), %r13
movq (%r13), %rdi
movq 0x20(%rdi), %rax
movq 0x18(%rax), %rax
movb 0x71(%rax), %r14b
leaq -0x40(%rbp), %r15
movq -0x30(%rbp), %rsi
movl $0x18, %edx
movq %r15, %r8
callq 0x5bdd6
movq %rax, 0x28(%r12)
movslq (%r15), %r15
testq %r15, %r15
js 0x5bcbd
leaq (%r15,%r15,2), %rdi
movq %rbx, (%rax,%rdi,8)
movl 0x58(%r13), %ecx
incl %ecx
movl %ecx, 0x58(%r13)
movl %ecx, 0x10(%rax,%rdi,8)
movq 0x8(%rbx), %rsi
xorl %ecx, %ecx
movl $0x0, %edx
testq %rsi, %rsi
je 0x5bc7d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rsi, %r8
incl %edx
cmpb $0x0, (%r8)
leaq 0x1(%r8), %r8
jne 0x5bc6b
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movl %r14d, %r8d
leaq (%rax,%rdi,8), %r14
movq (%r13), %rdi
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x5bc93
movl (%rax), %ecx
movzbl %r8b, %r8d
xorl %r9d, %r9d
callq 0x15493
movq %rax, 0x8(%r14)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x10, 0x2(%rbx)
je 0x5bcb9
movl 0x54(%r13), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x54(%r13)
movl %eax, 0x14(%r14)
movw %r15w, 0x32(%rbx)
movq %r12, 0x38(%rbx)
movl $0x1, %r14d
jmp 0x5bdbc
movq 0x18(%r12), %rsi
movl 0x20(%r12), %edx
testl %edx, %edx
jle 0x5bd02
leaq 0xc(%rsi), %rdi
xorl %ecx, %ecx
cmpl %eax, -0x4(%rdi)
jne 0x5bcf8
movswl 0x30(%rbx), %r8d
cmpl %r8d, (%rdi)
je 0x5bdb1
incl %ecx
addq $0x20, %rdi
cmpl %ecx, %edx
jne 0x5bce5
leaq 0x20(%r12), %rcx
movq (%r13), %rdi
leaq -0x3c(%rbp), %r15
movl $0x20, %edx
movq %r15, %r8
callq 0x5bdd6
movq %rax, 0x18(%r12)
movslq (%r15), %rcx
testq %rcx, %rcx
js 0x5bdb1
movq %rax, %rdi
movq %rcx, %r8
shlq $0x5, %r8
addq %r8, %rax
movq 0x40(%rbx), %rdx
movq %rdx, (%rdi,%r8)
movl 0x2c(%rbx), %edx
movl %edx, 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 0x5bda1
movl (%rdi), %r8d
testl %r8d, %r8d
jle 0x5bda1
movq 0x8(%rdi), %r9
xorl %edi, %edi
movq (%r9), %r10
cmpb $-0x68, (%r10)
jne 0x5bd96
cmpl %edx, 0x2c(%r10)
jne 0x5bd96
cmpw %si, 0x30(%r10)
je 0x5bdae
incl %edi
addq $0x20, %r9
cmpl %edi, %r8d
jne 0x5bd80
movl 0xc(%r12), %edi
leal 0x1(%rdi), %edx
movl %edx, 0xc(%r12)
movl %edi, 0x10(%rax)
movq %r12, 0x38(%rbx)
movb $-0x66, (%rbx)
movw %cx, 0x32(%rbx)
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq