// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: whereLoopAddVirtual
static int whereLoopAddVirtual(
  WhereLoopBuilder *pBuilder   /* WHERE clause information */
){
  WhereInfo *pWInfo;           /* WHERE analysis context */
  Parse *pParse;               /* The parsing context */
  WhereClause *pWC;            /* The WHERE clause */
  struct SrcList_item *pSrc;   /* The FROM clause term to search */
  Table *pTab;
  sqlite3 *db;
  sqlite3_index_info *pIdxInfo;
  struct sqlite3_index_constraint *pIdxCons;
  struct sqlite3_index_constraint_usage *pUsage;
  WhereTerm *pTerm;
  int i, j;
  int iTerm, mxTerm;
  int nConstraint;
  int seenIn = 0;              /* True if an IN operator is seen */
  int seenVar = 0;             /* True if a non-constant constraint is seen */
  int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
  WhereLoop *pNew;
  int rc = SQLITE_OK;

  pWInfo = pBuilder->pWInfo;
  pParse = pWInfo->pParse;
  db = pParse->db;
  pWC = pBuilder->pWC;
  pNew = pBuilder->pNew;
  pSrc = &pWInfo->pTabList->a[pNew->iTab];
  pTab = pSrc->pTab;
  assert( IsVirtual(pTab) );
  pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
  if( pIdxInfo==0 ) return SQLITE_NOMEM;
  pNew->prereq = 0;
  pNew->rSetup = 0;
  pNew->wsFlags = WHERE_VIRTUALTABLE;
  pNew->nLTerm = 0;
  pNew->u.vtab.needFree = 0;
  pUsage = pIdxInfo->aConstraintUsage;
  nConstraint = pIdxInfo->nConstraint;
  if( whereLoopResize(db, pNew, nConstraint) ){
    sqlite3DbFree(db, pIdxInfo);
    return SQLITE_NOMEM;
  }

  for(iPhase=0; iPhase<=3; iPhase++){
    if( !seenIn && (iPhase&1)!=0 ){
      iPhase++;
      if( iPhase>3 ) break;
    }
    if( !seenVar && iPhase>1 ) break;
    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
      j = pIdxCons->iTermOffset;
      pTerm = &pWC->a[j];
      switch( iPhase ){
        case 0:    /* Constants without IN operator */
          pIdxCons->usable = 0;
          if( (pTerm->eOperator & WO_IN)!=0 ){
            seenIn = 1;
          }
          if( pTerm->prereqRight!=0 ){
            seenVar = 1;
          }else if( (pTerm->eOperator & WO_IN)==0 ){
            pIdxCons->usable = 1;
          }
          break;
        case 1:    /* Constants with IN operators */
          assert( seenIn );
          pIdxCons->usable = (pTerm->prereqRight==0);
          break;
        case 2:    /* Variables without IN */
          assert( seenVar );
          pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
          break;
        default:   /* Variables with IN */
          assert( seenVar && seenIn );
          pIdxCons->usable = 1;
          break;
      }
    }
    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
    if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
    pIdxInfo->idxStr = 0;
    pIdxInfo->idxNum = 0;
    pIdxInfo->needToFreeIdxStr = 0;
    pIdxInfo->orderByConsumed = 0;
    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
    rc = vtabBestIndex(pParse, pTab, pIdxInfo);
    if( rc ) goto whereLoopAddVtab_exit;
    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
    pNew->prereq = 0;
    mxTerm = -1;
    assert( pNew->nLSlot>=nConstraint );
    for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
    pNew->u.vtab.omitMask = 0;
    for(i=0; i<nConstraint; i++, pIdxCons++){
      if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
        j = pIdxCons->iTermOffset;
        if( iTerm>=nConstraint
         || j<0
         || j>=pWC->nTerm
         || pNew->aLTerm[iTerm]!=0
        ){
          rc = SQLITE_ERROR;
          sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
          goto whereLoopAddVtab_exit;
        }
        testcase( iTerm==nConstraint-1 );
        testcase( j==0 );
        testcase( j==pWC->nTerm-1 );
        pTerm = &pWC->a[j];
        pNew->prereq |= pTerm->prereqRight;
        assert( iTerm<pNew->nLSlot );
        pNew->aLTerm[iTerm] = pTerm;
        if( iTerm>mxTerm ) mxTerm = iTerm;
        testcase( iTerm==15 );
        testcase( iTerm==16 );
        if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
        if( (pTerm->eOperator & WO_IN)!=0 ){
          if( pUsage[i].omit==0 ){
            /* Do not attempt to use an IN constraint if the virtual table
            ** says that the equivalent EQ constraint cannot be safely omitted.
            ** If we do attempt to use such a constraint, some rows might be
            ** repeated in the output. */
            break;
          }
          /* A virtual table that is constrained by an IN clause may not
          ** consume the ORDER BY clause because (1) the order of IN terms
          ** is not necessarily related to the order of output terms and
          ** (2) Multiple outputs from a single IN value will not merge
          ** together.  */
          pIdxInfo->orderByConsumed = 0;
        }
      }
    }
    if( i>=nConstraint ){
      pNew->nLTerm = mxTerm+1;
      assert( pNew->nLTerm<=pNew->nLSlot );
      pNew->u.vtab.idxNum = pIdxInfo->idxNum;
      pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
      pIdxInfo->needToFreeIdxStr = 0;
      pNew->u.vtab.idxStr = pIdxInfo->idxStr;
      pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
                                     && pIdxInfo->orderByConsumed);
      pNew->rSetup = 0;
      pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost);
      /* TUNING: Every virtual table query returns 25 rows */
      pNew->nOut = 46;  assert( 46==whereCost(25) );
      whereLoopInsert(pBuilder, pNew);
      if( pNew->u.vtab.needFree ){
        sqlite3_free(pNew->u.vtab.idxStr);
        pNew->u.vtab.needFree = 0;
      }
    }
  }  

whereLoopAddVtab_exit:
  if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
  sqlite3DbFree(db, pIdxInfo);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rdx
movq (%rdi), %rax
movq 0x8(%rdi), %rsi
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, -0x30(%rbp)
movq (%rcx), %rdi
movq 0x18(%rdx), %rcx
movq %rcx, -0x58(%rbp)
movzbl 0x10(%rcx), %ecx
imulq $0x68, %rcx, %rcx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movq 0x20(%r15), %rax
movq %rax, -0x60(%rbp)
movq %rdx, -0x80(%rbp)
movq 0x10(%rdx), %r14
movq 0x20(%rsi), %rax
movq %rsi, -0x48(%rbp)
movl 0x14(%rsi), %ecx
xorl %r13d, %r13d
testl %ecx, %ecx
cmovlel %r13d, %ecx
addq $0x18, %rax
subl $0x1, %ecx
jb 0x5085e
movl -0xc(%rax), %edx
cmpl 0x3c(%r15), %edx
jne 0x50858
movb (%rax), %dl
andb $-0x80, %dl
cmpb $0x1, %dl
adcl $0x0, %r13d
addq $0x38, %rax
jmp 0x5083e
testq %r14, %r14
je 0x508b0
movl (%r14), %eax
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, %rsi
shlq $0x5, %rsi
xorl %r9d, %r9d
cmpq %rdx, %rsi
je 0x508a5
movq 0x8(%r14), %r8
movq (%r8,%rdx), %r8
cmpb $-0x68, (%r8)
jne 0x508a2
movl 0x2c(%r8), %r8d
cmpl 0x3c(%r15), %r8d
jne 0x508a2
incl %r9d
addq $0x20, %rdx
jmp 0x5087c
movl %r9d, %ecx
xorl %r12d, %r12d
cmpl %eax, %ecx
cmovel %eax, %r12d
jmp 0x508b3
xorl %r12d, %r12d
imull $0x14, %r13d, %eax
leal (%rax,%r12,8), %esi
addl $0x48, %esi
movq %rdi, -0x78(%rbp)
callq 0xf4ae
testq %rax, %rax
je 0x509f2
movq %rax, %rbx
movslq %r12d, %rdx
movslq %r13d, %rax
leaq 0x48(%rbx), %rcx
imulq $0xc, %rax, %rax
movq %rax, -0x50(%rbp)
leaq (%rbx,%rax), %rsi
addq $0x48, %rsi
leaq (%rsi,%rdx,8), %rdx
movl %r13d, (%rbx)
movl %r12d, 0x10(%rbx)
movq %rcx, 0x8(%rbx)
movq %rsi, 0x18(%rbx)
movq %rdx, 0x20(%rbx)
movq -0x48(%rbp), %rdi
movl 0x14(%rdi), %edx
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
movq 0x20(%rdi), %rdi
addq $0x18, %rdi
pushq $0x2
popq %r8
xorl %r9d, %r9d
movq -0x58(%rbp), %rsi
cmpl %r9d, %edx
je 0x50974
movl -0xc(%rdi), %r10d
cmpl 0x3c(%r15), %r10d
jne 0x5096b
movzwl (%rdi), %r10d
testb %r10b, %r10b
js 0x5096b
movl -0x8(%rdi), %r11d
cltq
imulq $0xc, %rax, %r13
movl %r11d, (%rcx,%r13)
movl %r9d, 0x8(%rcx,%r13)
movl %r10d, %r11d
andl $0x7f, %r11d
cmpw $0x1, %r11w
cmovel %r8d, %r10d
movb %r10b, 0x4(%rcx,%r13)
movq -0x58(%rbp), %rsi
incl %eax
incl %r9d
addq $0x38, %rdi
jmp 0x50925
xorl %ecx, %ecx
testl %r12d, %r12d
cmovlel %ecx, %r12d
movq -0x50(%rbp), %rax
addq %rbx, %rax
addq $0x4c, %rax
shlq $0x3, %r12
movq -0x48(%rbp), %r15
cmpq %rcx, %r12
je 0x509b2
movq 0x8(%r14), %rdx
movq (%rdx,%rcx,4), %rdi
movswl 0x30(%rdi), %edi
movl %edi, -0x4(%rax,%rcx)
movb 0x18(%rdx,%rcx,4), %dl
movb %dl, (%rax,%rcx)
addq $0x8, %rcx
jmp 0x50990
andq $0x0, (%rsi)
andw $0x0, 0x12(%rsi)
movl $0x400, 0x28(%rsi)      # imm = 0x400
andw $0x0, 0x2c(%rsi)
movb $0x0, 0x1c(%rsi)
movq 0x20(%rbx), %r13
movl (%rbx), %r12d
movq -0x78(%rbp), %r14
movq %r14, %rdi
movl %r12d, %edx
callq 0x512f5
testl %eax, %eax
je 0x50a0d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x968b
jmp 0x50a04
leaq 0x1ad8b(%rip), %rsi     # 0x6b784
movq -0x30(%rbp), %rdi
xorl %eax, %eax
callq 0x104d6
pushq $0x7
popq %r15
jmp 0x50e21
movq %rbx, %rax
addq $0x30, %rax
movq %rax, -0x70(%rbp)
testl %r12d, %r12d
movl $0x0, %r11d
movl %r12d, -0x64(%rbp)
cmovgl %r12d, %r11d
xorl %r12d, %r12d
movl $0x0, -0x34(%rbp)
xorl %r14d, %r14d
movq %r13, -0x40(%rbp)
movq %r11, -0x50(%rbp)
cmpl $0x3, %r14d
jg 0x50e33
testl %r12d, %r12d
jne 0x50a6c
movl %r14d, %eax
andl $0x1, %eax
pushq $0x1
popq %r10
movl -0x34(%rbp), %r13d
je 0x50a74
cmpl $0x3, %r14d
je 0x50e33
incl %r14d
jmp 0x50a74
pushq $0x1
popq %r10
movl -0x34(%rbp), %r13d
testl %r13d, %r13d
sete %al
cmpl $0x2, %r14d
setge %cl
testb %cl, %al
jne 0x50e33
movq 0x8(%rbx), %rax
movslq (%rbx), %rdx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
addq $0x5, %rax
subl $0x1, %ecx
jb 0x50b1e
movslq 0x3(%rax), %rsi
imulq $0x38, %rsi, %rdi
addq 0x20(%r15), %rdi
cmpl $0x2, %r14d
je 0x50afe
cmpl $0x1, %r14d
je 0x50af4
testl %r14d, %r14d
jne 0x50aef
movb $0x0, (%rax)
movb 0x18(%rdi), %r8b
testb $0x1, %r8b
movl %r12d, %esi
cmovnel %r10d, %esi
cmpq $0x0, 0x28(%rdi)
setne %r9b
cmovel %r10d, %esi
movl %r13d, %edi
cmovnel %r10d, %edi
orb %r8b, %r9b
testb $0x1, %r9b
jne 0x50b12
movb $0x1, (%rax)
jmp 0x50b0c
cmpq $0x0, 0x28(%rdi)
sete (%rax)
jmp 0x50b0c
movb 0x18(%rdi), %sil
notb %sil
andb $0x1, %sil
movb %sil, (%rax)
movl %r12d, %esi
movl %r13d, %edi
addq $0xc, %rax
movl %esi, %r12d
movl %edi, %r13d
jmp 0x50a9e
shlq $0x3, %rdx
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x81e0
cmpl $0x0, 0x38(%rbx)
movq -0x70(%rbp), %r15
je 0x50b3f
movq (%r15), %rdi
callq 0x9c7b
andl $0x0, 0x28(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
movabsq $0x546d42aea2879f2e, %rax # imm = 0x546D42AEA2879F2E
movq %rax, 0x40(%rbx)
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x60(%rbp), %rsi
callq 0x3095c
movq 0x10(%rax), %r15
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x18(%rax)
testl %eax, %eax
je 0x50bc2
cmpl $0x7, %eax
jne 0x50b90
movq -0x30(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x50(%rax)
jmp 0x50bc2
movq 0x10(%r15), %rdx
testq %rdx, %rdx
je 0x50ba6
movq -0x30(%rbp), %rdi
leaq 0x1ab55(%rip), %rsi     # 0x6b6f9
jmp 0x50bbb
movl %eax, %edi
callq 0xe5c3
movq -0x30(%rbp), %rdi
leaq 0x1ab41(%rip), %rsi     # 0x6b6f9
movq %rax, %rdx
xorl %eax, %eax
callq 0x104d6
movl %r13d, -0x34(%rbp)
movq 0x10(%r15), %rdi
callq 0x9c7b
andq $0x0, 0x10(%r15)
pushq $0x5
popq %r15
xorl %r13d, %r13d
movq -0x50(%rbp), %r11
movslq (%rbx), %rax
cmpq %rax, %r13
jge 0x50c23
movq 0x8(%rbx), %rax
cmpb $0x0, (%rax,%r15)
jne 0x50c1a
movq 0x20(%rbx), %rax
cmpl $0x0, (%rax,%r13,8)
jle 0x50c1a
movq -0x60(%rbp), %rax
movq (%rax), %rdx
movq -0x30(%rbp), %rdi
leaq 0x1ceea(%rip), %rsi     # 0x6daf9
xorl %eax, %eax
callq 0x104d6
movq -0x50(%rbp), %r11
incq %r13
addq $0xc, %r15
jmp 0x50bdf
movq -0x30(%rbp), %rax
movl 0x50(%rax), %r15d
testl %r15d, %r15d
jne 0x50e03
movq 0x8(%rbx), %rax
movq -0x58(%rbp), %r13
andq $0x0, (%r13)
xorl %ecx, %ecx
movq -0x48(%rbp), %r15
cmpq %rcx, %r11
je 0x50c5a
movq 0x30(%r13), %rdx
andq $0x0, (%rdx,%rcx,8)
incq %rcx
jmp 0x50c47
andw $0x0, 0x1e(%r13)
addq $0x8, %rax
xorl %esi, %esi
xorl %edi, %edi
pushq $-0x1
popq %rdx
movq -0x40(%rbp), %rcx
cmpq %rdi, %r11
je 0x50d33
movl (%rcx,%rdi,8), %r8d
testl %r8d, %r8d
jle 0x50d27
cmpl -0x64(%rbp), %r8d
jg 0x50de6
movl (%rax), %r9d
testl %r9d, %r9d
js 0x50de6
cmpl 0x14(%r15), %r9d
jge 0x50de6
leal -0x1(%r8), %ecx
movq 0x30(%r13), %r10
movl %ecx, %r11d
cmpq $0x0, (%r10,%r11,8)
jne 0x50de6
movq 0x20(%r15), %r15
imulq $0x38, %r9, %r13
leaq (%r15,%r13), %r9
orq 0x28(%r15,%r13), %rsi
movq -0x58(%rbp), %r13
movq %rsi, (%r13)
movq %r9, (%r10,%r11,8)
cmpl %edx, %ecx
cmovgl %ecx, %edx
cmpl $0x10, %r8d
ja 0x50cfb
movq -0x40(%rbp), %r8
cmpb $0x0, 0x4(%r8,%rdi,8)
je 0x50cfb
pushq $0x1
popq %r8
shll %cl, %r8d
orw %r8w, 0x1e(%r13)
testb $0x1, 0x18(%r9)
movq -0x50(%rbp), %r11
jne 0x50d10
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %rcx
jmp 0x50d27
movq -0x40(%rbp), %rcx
cmpb $0x0, 0x4(%rcx,%rdi,8)
movq -0x48(%rbp), %r15
je 0x50dde
andl $0x0, 0x3c(%rbx)
incq %rdi
addq $0xc, %rax
jmp 0x50c6f
incl %edx
movw %dx, 0x2c(%r13)
movl 0x28(%rbx), %eax
movl %eax, 0x18(%r13)
movb 0x38(%rbx), %al
movb %al, 0x1c(%r13)
andl $0x0, 0x38(%rbx)
movq 0x30(%rbx), %rax
movq %rax, 0x20(%r13)
cmpl $0x0, 0x10(%rbx)
je 0x50d63
cmpl $0x0, 0x3c(%rbx)
setne %al
jmp 0x50d65
xorl %eax, %eax
movb %al, 0x1d(%r13)
andw $0x0, 0x12(%r13)
movsd 0x40(%rbx), %xmm0
movsd 0x1a56c(%rip), %xmm1    # 0x6b2e8
ucomisd %xmm0, %xmm1
jae 0x50da3
movsd 0x1a566(%rip), %xmm1    # 0x6b2f0
ucomisd %xmm0, %xmm1
jae 0x50da7
movq %xmm0, %rax
shrq $0x34, %rax
imull $0xa, %eax, %eax
addl $0xffffd814, %eax       # imm = 0xFFFFD814
jmp 0x50db1
xorl %eax, %eax
jmp 0x50db1
cvttsd2si %xmm0, %rdi
callq 0x5154b
movw %ax, 0x14(%r13)
movw $0x2e, 0x16(%r13)
movq -0x80(%rbp), %rdi
movq %r13, %rsi
callq 0x5137b
cmpb $0x0, 0x1c(%r13)
je 0x50dde
movq 0x20(%r13), %rdi
callq 0x9c7b
movb $0x0, 0x1c(%r13)
incl %r14d
jmp 0x50a3e
movq -0x60(%rbp), %rax
movq (%rax), %rdx
leaq 0x1cce9(%rip), %rsi     # 0x6dadd
movq -0x30(%rbp), %rdi
xorl %eax, %eax
callq 0x104d6
pushq $0x1
popq %r15
cmpl $0x0, 0x38(%rbx)
je 0x50e15
movq -0x70(%rbp), %rax
movq (%rax), %rdi
callq 0x9c7b
movq -0x78(%rbp), %rdi
movq %rbx, %rsi
callq 0x968b
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
jmp 0x50e03