// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: resolveOrderGroupBy
static int resolveOrderGroupBy(
  NameContext *pNC,     /* The name context of the SELECT statement */
  Select *pSelect,      /* The SELECT statement holding pOrderBy */
  ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
  const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
){
  int i, j;                      /* Loop counters */
  int iCol;                      /* Column number */
  struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
  Parse *pParse;                 /* Parsing context */
  int nResult;                   /* Number of terms in the result set */

  if( pOrderBy==0 ) return 0;
  nResult = pSelect->pEList->nExpr;
  pParse = pNC->pParse;
  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
    Expr *pE = pItem->pExpr;
    Expr *pE2 = sqlite3ExprSkipCollate(pE);
    if( zType[0]!='G' ){
      iCol = resolveAsName(pParse, pSelect->pEList, pE2);
      if( iCol>0 ){
        /* If an AS-name match is found, mark this ORDER BY column as being
        ** a copy of the iCol-th result-set column.  The subsequent call to
        ** sqlite3ResolveOrderGroupBy() will convert the expression to a
        ** copy of the iCol-th result-set expression. */
        pItem->iOrderByCol = (u16)iCol;
        continue;
      }
    }
    if( sqlite3ExprIsInteger(pE2, &iCol) ){
      /* The ORDER BY term is an integer constant.  Again, set the column
      ** number so that sqlite3ResolveOrderGroupBy() will convert the
      ** order-by term to a copy of the result-set expression */
      if( iCol<1 || iCol>0xffff ){
        resolveOutOfRangeError(pParse, zType, i+1, nResult);
        return 1;
      }
      pItem->iOrderByCol = (u16)iCol;
      continue;
    }

    /* Otherwise, treat the ORDER BY term as an ordinary expression */
    pItem->iOrderByCol = 0;
    if( sqlite3ResolveExprNames(pNC, pE) ){
      return 1;
    }
    for(j=0; j<pSelect->pEList->nExpr; j++){
      if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
        pItem->iOrderByCol = j+1;
      }
    }
  }
  return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, -0x40(%rbp)
movq %rsi, -0x38(%rbp)
testq %rdx, %rdx
je 0x49054
movq %rdi, -0x60(%rbp)
movq (%rdi), %rax
movq %rax, -0x50(%rbp)
cmpl $0x0, (%rdx)
jle 0x49035
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x44(%rbp)
movq 0x8(%rdx), %r14
xorl %ebx, %ebx
movq %rdx, -0x68(%rbp)
movq (%r14), %r12
testq %r12, %r12
je 0x48f5f
movq %r12, %r13
movzbl (%r13), %eax
cmpl $0x5c, %eax
je 0x48f56
cmpl $0x18, %eax
jne 0x48f62
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x48f47
xorl %r13d, %r13d
movq -0x40(%rbp), %rax
cmpb $0x47, (%rax)
movq %rbx, -0x70(%rbp)
movq %r14, -0x58(%rbp)
je 0x48f89
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq %r13, %rsi
callq 0x4b708
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jg 0x48fac
movq %r13, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x4ac9f
testl %eax, %eax
je 0x48fcd
movl -0x2c(%rbp), %eax
movl $0xffff0000, %edx       # imm = 0xFFFF0000
leal (%rax,%rdx), %ecx
cmpl %edx, %ecx
jbe 0x49058
movw %ax, 0x1a(%r14)
movq -0x70(%rbp), %rbx
incl %ebx
movq -0x58(%rbp), %r14
addq $0x20, %r14
movq -0x68(%rbp), %rdx
cmpl (%rdx), %ebx
jl 0x48f3c
jmp 0x49035
movw $0x0, 0x1a(%r14)
movq -0x60(%rbp), %rdi
movq %r12, %rsi
callq 0x46b2f
testl %eax, %eax
jne 0x49076
movq -0x38(%rbp), %rax
movq (%rax), %rax
movslq (%rax), %r13
testq %r13, %r13
jle 0x48fb1
movq 0x8(%rax), %r14
shlq $0x5, %r13
movw $0x1, %r15w
xorl %ebx, %ebx
movq (%r14,%rbx), %rsi
movq %r12, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x4b798
testl %eax, %eax
jne 0x49024
movq -0x58(%rbp), %rax
movw %r15w, 0x1a(%rax)
incl %r15d
addq $0x20, %rbx
cmpq %rbx, %r13
jne 0x49006
jmp 0x48fb1
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4b972
xorl %eax, %eax
jmp 0x4907b
incl %ebx
leaq 0x32236(%rip), %rsi     # 0x7b297
movq -0x50(%rbp), %rdi
movl %ebx, %edx
movq -0x40(%rbp), %rcx
movl -0x44(%rbp), %r8d
xorl %eax, %eax
callq 0x11842
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq