// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: isDistinctRedundant
static int isDistinctRedundant(
  Parse *pParse,            /* Parsing context */
  SrcList *pTabList,        /* The FROM clause */
  WhereClause *pWC,         /* The WHERE clause */
  ExprList *pDistinct       /* The result set that needs to be DISTINCT */
){
  Table *pTab;
  Index *pIdx;
  int i;                          
  int iBase;

  /* If there is more than one table or sub-select in the FROM clause of
  ** this query, then it will not be possible to show that the DISTINCT 
  ** clause is redundant. */
  if( pTabList->nSrc!=1 ) return 0;
  iBase = pTabList->a[0].iCursor;
  pTab = pTabList->a[0].pTab;

  /* If any of the expressions is an IPK column on table iBase, then return 
  ** true. Note: The (p->iTable==iBase) part of this test may be false if the
  ** current SELECT is a correlated sub-query.
  */
  for(i=0; i<pDistinct->nExpr; i++){
    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
    if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
  }

  /* Loop through all indices on the table, checking each to see if it makes
  ** the DISTINCT qualifier redundant. It does so if:
  **
  **   1. The index is itself UNIQUE, and
  **
  **   2. All of the columns in the index are either part of the pDistinct
  **      list, or else the WHERE clause contains a term of the form "col=X",
  **      where X is a constant value. The collation sequences of the
  **      comparison and select-list expressions must match those of the index.
  **
  **   3. All of those index columns for which the WHERE clause does not
  **      contain a "col=X" term are subject to a NOT NULL constraint.
  */
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
    if( pIdx->onError==OE_None ) continue;
    for(i=0; i<pIdx->nColumn; i++){
      int iCol = pIdx->aiColumn[i];
      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
        int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
        if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){
          break;
        }
      }
    }
    if( i==pIdx->nColumn ){
      /* This index implies that the DISTINCT qualifier is redundant. */
      return 1;
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x71e20
movl $0x0, -0x4(%rbp)
jmp 0x71fb5
movq -0x18(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x71ea7
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x648b0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x98, %eax
jne 0x71e9a
movq -0x48(%rbp), %rax
movl 0x2c(%rax), %eax
cmpl -0x40(%rbp), %eax
jne 0x71e9a
movq -0x48(%rbp), %rax
movswl 0x30(%rax), %eax
cmpl $0x0, %eax
jge 0x71e9a
movl $0x1, -0x4(%rbp)
jmp 0x71fb5
jmp 0x71e9c
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x71e3d
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x71fae
movq -0x38(%rbp), %rax
movzbl 0x56(%rax), %eax
cmpl $0x0, %eax
jne 0x71ed0
jmp 0x71f9d
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
movzwl 0x54(%rcx), %ecx
cmpl %ecx, %eax
jge 0x71f83
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rdi
movl -0x40(%rbp), %esi
movl -0x4c(%rbp), %edx
movq -0x38(%rbp), %r9
movq $-0x1, %rcx
movl $0x2, %r8d
callq 0x77230
movq %rax, %rcx
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x71f73
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x40(%rbp), %edx
movq -0x38(%rbp), %rcx
movl -0x3c(%rbp), %r8d
callq 0x77300
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jl 0x71f6f
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movzbl 0x28(%rax), %eax
cmpl $0x0, %eax
jne 0x71f71
jmp 0x71f83
jmp 0x71f73
jmp 0x71f75
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x71ed7
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
movzwl 0x54(%rcx), %ecx
cmpl %ecx, %eax
jne 0x71f9b
movl $0x1, -0x4(%rbp)
jmp 0x71fb5
jmp 0x71f9d
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x71eb3
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop