// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %rbx
movq 0x20(%rdi), %rdx
movq (%rdx), %r13
movq 0x8(%rdx), %r9
movq 0x18(%rdx), %r11
movzbl (%rsi), %ecx
cmpl $0x9a, %ecx
je 0x5bc2a
xorl %eax, %eax
cmpl $0x99, %ecx
je 0x5bdbb
cmpl $0x98, %ecx
jne 0x5bf14
movl $0x1, %eax
testq %r9, %r9
je 0x5bf14
cmpb $0x0, (%r9)
je 0x5bf14
leaq 0x8(%r9), %r15
leaq 0x20(%r11), %rax
movq %rax, -0x48(%rbp)
xorl %r12d, %r12d
movq %rbx, -0x40(%rbp)
movq %r9, -0x30(%rbp)
movl 0x2c(%rbx), %r14d
movl 0x3c(%r15), %r10d
cmpl %r10d, %r14d
jne 0x5bda2
movq 0x18(%r11), %rsi
movl 0x20(%r11), %eax
testl %eax, %eax
setg %dl
jle 0x5bca9
leaq 0xc(%rsi), %rdi
xorl %ecx, %ecx
cmpl %r14d, -0x4(%rdi)
jne 0x5bc8f
movswl 0x30(%rbx), %r8d
cmpl %r8d, (%rdi)
je 0x5bca0
incl %ecx
cmpl %eax, %ecx
setl %dl
addq $0x20, %rdi
cmpl %ecx, %eax
jne 0x5bc7f
jmp 0x5bca9
testb $0x1, %dl
jne 0x5bd8e
movl %r10d, -0x38(%rbp)
movq (%r13), %rdi
movl $0x20, %edx
movq -0x48(%rbp), %rcx
leaq -0x4c(%rbp), %r8
movq %r13, %rbx
movq %r11, %r13
callq 0x5bf2b
movq %r13, %r11
movq %rax, 0x18(%r13)
movslq -0x4c(%rbp), %rcx
testq %rcx, %rcx
js 0x5bd65
movq %rax, %rdi
movq %rcx, %r8
shlq $0x5, %r8
addq %r8, %rax
movq -0x40(%rbp), %r10
movq 0x40(%r10), %rdx
movq %rdx, (%rdi,%r8)
movl 0x2c(%r10), %edx
movl %edx, 0x8(%rdi,%r8)
movswl 0x30(%r10), %esi
movl %esi, 0xc(%rdi,%r8)
movl 0x58(%rbx), %r9d
incl %r9d
movl %r9d, 0x58(%rbx)
movl %r9d, 0x14(%rdi,%r8)
movl $0xffffffff, 0x10(%rdi,%r8) # imm = 0xFFFFFFFF
movq %r10, 0x18(%rdi,%r8)
movq 0x10(%r11), %r8
testq %r8, %r8
movq %rbx, %r13
je 0x5bd6e
movl (%r8), %edi
testl %edi, %edi
jle 0x5bd6e
movq 0x8(%r8), %r9
xorl %r8d, %r8d
movq (%r9), %r10
cmpb $-0x68, (%r10)
jne 0x5bd57
cmpl %edx, 0x2c(%r10)
jne 0x5bd57
cmpw %si, 0x30(%r10)
je 0x5bd6a
incl %r8d
addq $0x20, %r9
cmpl %r8d, %edi
jne 0x5bd41
jmp 0x5bd6e
movq %rbx, %r13
jmp 0x5bd82
movl %r8d, 0x10(%rax)
cmpl $0x0, 0x10(%rax)
jns 0x5bd82
movl 0xc(%r11), %edx
leal 0x1(%rdx), %esi
movl %esi, 0xc(%r11)
movl %edx, 0x10(%rax)
movq -0x40(%rbp), %rbx
movq -0x30(%rbp), %r9
movl -0x38(%rbp), %r10d
movq %r11, 0x38(%rbx)
movb $-0x66, (%rbx)
movw %cx, 0x32(%rbx)
cmpl %r10d, %r14d
je 0x5bf0f
incl %r12d
addq $0x68, %r15
movzbl (%r9), %eax
cmpl %eax, %r12d
jb 0x5bc59
jmp 0x5bf0f
testb $0x8, 0x30(%rdx)
jne 0x5bf14
movzbl 0x37(%rbx), %ecx
cmpl %ecx, 0x18(%rdi)
jne 0x5bf14
movq %r13, -0x48(%rbp)
movq 0x28(%r11), %rax
movq %rax, -0x30(%rbp)
movq %r11, -0x38(%rbp)
movl 0x30(%r11), %r12d
testl %r12d, %r12d
setg %r14b
jle 0x5be48
movq -0x30(%rbp), %r15
movq (%r15), %rdi
movq %rbx, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4b67e
testl %eax, %eax
je 0x5be3c
leal -0x1(%r12), %r13d
leaq 0x18(%r15), %r14
xorl %r15d, %r15d
cmpl %r15d, %r13d
je 0x5be48
movq (%r14), %rdi
movq %rbx, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4b67e
addq $0x18, %r14
incl %r15d
testl %eax, %eax
jne 0x5be13
cmpl %r12d, %r15d
setl %r14b
jmp 0x5be3f
xorl %r15d, %r15d
testb %r14b, %r14b
jne 0x5bf02
movq -0x38(%rbp), %r14
leaq 0x30(%r14), %rcx
movq -0x48(%rbp), %r13
movq (%r13), %rdi
movq 0x20(%rdi), %rax
movq 0x18(%rax), %rax
movb 0x71(%rax), %r12b
leaq -0x50(%rbp), %r15
movq -0x30(%rbp), %rsi
movl $0x18, %edx
movq %r15, %r8
callq 0x5bf2b
movq %rax, 0x28(%r14)
movslq (%r15), %r15
testq %r15, %r15
js 0x5bf02
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 0x5bec5
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rsi, %r8
incl %edx
cmpb $0x0, (%r8)
leaq 0x1(%r8), %r8
jne 0x5beb3
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
leaq (%rax,%rdi,8), %r14
movq (%r13), %rdi
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x5bed8
movl (%rax), %ecx
movzbl %r12b, %r8d
xorl %r9d, %r9d
callq 0x154a0
movq %rax, 0x8(%r14)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x10, 0x2(%rbx)
je 0x5befe
movl 0x54(%r13), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x54(%r13)
movl %eax, 0x14(%r14)
movw %r15w, 0x32(%rbx)
movq -0x38(%rbp), %rax
movq %rax, 0x38(%rbx)
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq