// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movq (%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x8(%rax), %rax
movq 0x18(%rdi), %rcx
movq %rcx, -0x48(%rbp)
movzbl 0x10(%rcx), %ecx
imulq $0x68, %rcx, %rcx
leaq (%rax,%rcx), %r14
addq $0x8, %r14
movq %rdi, -0x88(%rbp)
movq 0x10(%rdi), %r11
movq %rdx, -0x40(%rbp)
movl 0x14(%rdx), %edx
xorl %r15d, %r15d
movl $0x0, %r12d
testl %edx, %edx
jle 0x59500
movq -0x40(%rbp), %rsi
movq 0x20(%rsi), %rsi
movl 0x3c(%r14), %edi
addq $0x18, %rsi
xorl %r12d, %r12d
cmpl %edi, -0xc(%rsi)
jne 0x594f8
movb (%rsi), %r8b
andb $-0x80, %r8b
cmpb $0x1, %r8b
adcl $0x0, %r12d
addq $0x38, %rsi
decl %edx
jne 0x594e4
movq -0x38(%rbp), %rdx
movq (%rdx), %rdi
testq %r11, %r11
je 0x59548
movl (%r11), %edx
xorl %r15d, %r15d
movl $0x0, %esi
testl %edx, %edx
jle 0x59542
movq 0x8(%r11), %r9
xorl %esi, %esi
movq (%r9), %r8
cmpb $-0x68, (%r8)
jne 0x59542
movl 0x2c(%r8), %r8d
cmpl 0x3c(%r14), %r8d
jne 0x59542
incq %rsi
addq $0x20, %r9
cmpq %rsi, %rdx
jne 0x59521
movl %edx, %esi
cmpl %edx, %esi
cmovel %edx, %r15d
movq %r11, -0x50(%rbp)
movq 0x28(%rax,%rcx), %rax
movq %rax, -0x68(%rbp)
leal (%r12,%r12,4), %eax
leal (,%r15,8), %ecx
leal (%rcx,%rax,4), %ebx
addl $0x48, %ebx
movq %rdi, -0x58(%rbp)
movl %ebx, %esi
callq 0x115e9
testq %rax, %rax
je 0x59669
movq %rax, %r13
movslq %ebx, %rdx
xorl %ebx, %ebx
movq %rax, %rdi
xorl %esi, %esi
callq 0x8200
movslq %r15d, %rdx
movslq %r12d, %rax
leaq 0x48(%r13), %rcx
leaq (%rax,%rax,2), %rax
leaq 0x48(%r13,%rax,4), %rsi
leaq (%rsi,%rdx,8), %rdx
movl %r12d, (%r13)
movq %r15, %r12
movl %r15d, 0x10(%r13)
movq %rcx, 0x8(%r13)
movq %rsi, 0x18(%r13)
movq %rdx, 0x20(%r13)
movq -0x40(%rbp), %rdx
movl 0x14(%rdx), %edx
testl %edx, %edx
jle 0x59624
movq -0x40(%rbp), %rsi
movq 0x20(%rsi), %rsi
movl 0x3c(%r14), %edi
addq $0x18, %rsi
xorl %r8d, %r8d
cmpl %edi, -0xc(%rsi)
jne 0x5961a
movzwl (%rsi), %r9d
testb %r9b, %r9b
js 0x5961a
movl -0x8(%rsi), %r11d
movslq %r8d, %r10
leaq (%r10,%r10,2), %r10
movl %r11d, (%rcx,%r10,4)
movl %ebx, 0x8(%rcx,%r10,4)
movl %r9d, %r14d
andl $0x7f, %r14d
movl $0x2, %r11d
cmpw $0x1, %r14w
je 0x59612
movl %r9d, %r11d
movb %r11b, 0x4(%rcx,%r10,4)
incl %r8d
incl %ebx
addq $0x38, %rsi
cmpl %ebx, %edx
jne 0x595d9
testl %r12d, %r12d
jle 0x5967e
movq -0x50(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %r12d, %edx
shlq $0x3, %rdx
leaq 0x4c(,%rax,4), %rax
addq %r13, %rax
xorl %esi, %esi
movq -0x48(%rbp), %r8
movq (%rcx,%rsi,4), %rdi
movswl 0x30(%rdi), %edi
movl %edi, -0x4(%rax,%rsi)
movb 0x18(%rcx,%rsi,4), %dil
movb %dil, (%rax,%rsi)
addq $0x8, %rsi
cmpq %rsi, %rdx
jne 0x59649
jmp 0x59682
leaq 0x200f4(%rip), %rsi     # 0x79764
xorl %r13d, %r13d
movq -0x38(%rbp), %rdi
xorl %eax, %eax
callq 0x11724
movq -0x48(%rbp), %r8
testq %r13, %r13
je 0x59b33
movq $0x0, (%r8)
xorl %eax, %eax
movw %ax, 0x12(%r8)
movl $0x400, 0x28(%r8)       # imm = 0x400
movw %ax, 0x2c(%r8)
movb $0x0, 0x1c(%r8)
movq 0x20(%r13), %r12
movl (%r13), %r15d
movq %r8, %rsi
movq -0x58(%rbp), %r14
movq %r14, %rdi
movl %r15d, %edx
callq 0x5a0cb
movl $0x7, %ebx
testl %eax, %eax
jne 0x59b6c
movslq %r15d, %rax
movq %rax, -0x98(%rbp)
leaq 0x30(%r13), %rax
movq %rax, -0x80(%rbp)
testl %r15d, %r15d
setg -0x29(%rbp)
movq %r15, %rax
negq %rax
movq %rax, -0x90(%rbp)
movl $0x1, %r10d
movq %r12, %rbx
xorl %r12d, %r12d
movl $0x0, -0x50(%rbp)
xorl %r14d, %r14d
movq %rbx, -0x78(%rbp)
movq %r15, -0x60(%rbp)
testl %r14d, %r14d
jne 0x59730
movl %r12d, %eax
andl $0x1, %eax
je 0x59730
cmpl $0x3, %r12d
je 0x59b2f
incl %r12d
cmpl $0x0, -0x50(%rbp)
sete %al
cmpl $0x2, %r12d
setge %cl
testb %cl, %al
jne 0x59b2f
movslq (%r13), %rdx
testq %rdx, %rdx
jle 0x597d3
movq 0x8(%r13), %rax
movq -0x40(%rbp), %rcx
movq 0x20(%rcx), %rcx
addq $0x5, %rax
movl %edx, %esi
movslq 0x3(%rax), %rdi
imulq $0x38, %rdi, %rdi
addq %rcx, %rdi
cmpl $0x2, %r12d
je 0x597bd
cmpl $0x1, %r12d
je 0x597b3
testl %r12d, %r12d
jne 0x597ae
movb $0x0, (%rax)
movb 0x18(%rdi), %r8b
testb $0x1, %r8b
cmovnel %r10d, %r14d
cmpq $0x0, 0x28(%rdi)
setne %r9b
movl -0x50(%rbp), %edi
cmovnel %r10d, %edi
orb %r8b, %r9b
testb $0x1, %r9b
je 0x597ae
movl %edi, -0x50(%rbp)
jmp 0x597cb
movb $0x1, (%rax)
jmp 0x597cb
cmpq $0x0, 0x28(%rdi)
sete (%rax)
jmp 0x597cb
movb 0x18(%rdi), %dil
notb %dil
andb $0x1, %dil
movb %dil, (%rax)
addq $0xc, %rax
decl %esi
jne 0x59765
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8200
cmpl $0x0, 0x38(%r13)
je 0x597f1
movq 0x30(%r13), %rdi
callq 0xa2e8
movl $0x0, 0x28(%r13)
movq -0x80(%rbp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movabsq $0x546d42aea2879f2e, %rax # imm = 0x546D42AEA2879F2E
movq %rax, 0x40(%r13)
movq -0x68(%rbp), %rax
movq 0x58(%rax), %rax
testq %rax, %rax
je 0x59835
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
cmpq %rcx, (%rax)
je 0x59835
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x59827
movq 0x10(%rax), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%rax)
testl %eax, %eax
leaq 0x2228b(%rip), %r15     # 0x7bad9
je 0x598b2
cmpl $0x7, %eax
jne 0x59862
movq -0x38(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x50(%rax)
jmp 0x598b2
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
jne 0x598a0
leaq 0x23a66(%rip), %rdx     # 0x7d2d8
cmpl $0x204, %eax            # imm = 0x204
je 0x598a0
movzbl %al, %eax
cmpl $0x1b, %eax
setae %cl
cmpl $0x2, %eax
sete %dl
orb %cl, %dl
leaq 0x23a39(%rip), %rdx     # 0x7d2ca
jne 0x598a0
movl %eax, %eax
leaq 0x39f64(%rip), %rcx     # 0x93800
movq (%rcx,%rax,8), %rdx
movq -0x38(%rbp), %rdi
leaq 0x1fe2e(%rip), %rsi     # 0x796d9
xorl %eax, %eax
callq 0x11724
movl %r14d, -0x6c(%rbp)
movq 0x10(%rbx), %rdi
callq 0xa2e8
movq $0x0, 0x10(%rbx)
cmpl $0x0, (%r13)
jle 0x59910
movl $0x5, %ebx
xorl %r14d, %r14d
movq 0x8(%r13), %rax
cmpb $0x0, (%rax,%rbx)
jne 0x59900
movq 0x20(%r13), %rax
cmpl $0x0, (%rax,%r14,8)
jle 0x59900
movq -0x68(%rbp), %rax
movq (%rax), %rdx
movq -0x38(%rbp), %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x11724
incq %r14
movslq (%r13), %rax
addq $0xc, %rbx
cmpq %rax, %r14
jl 0x598d6
movq -0x38(%rbp), %rax
movl 0x50(%rax), %ebx
testl %ebx, %ebx
jne 0x59b58
movq 0x8(%r13), %rax
movq -0x48(%rbp), %r14
movq $0x0, (%r14)
movq -0x60(%rbp), %r15
testl %r15d, %r15d
jle 0x5994d
xorl %ecx, %ecx
movq 0x30(%r14), %rdx
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %r15
jne 0x59939
movw $0x0, 0x1e(%r14)
testl %r15d, %r15d
movq -0x78(%rbp), %rbx
jle 0x59a4b
addq $0x8, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %edi
movb -0x29(%rbp), %sil
movl -0x8(%rbx,%rdi,8), %r8d
testl %r8d, %r8d
jle 0x59a1e
cmpl %r15d, %r8d
jg 0x59b3a
movl (%rax), %r9d
testl %r9d, %r9d
js 0x59b3a
movq -0x40(%rbp), %rcx
cmpl 0x14(%rcx), %r9d
jge 0x59b3a
leal -0x1(%r8), %ecx
movq 0x30(%r14), %r10
movl %ecx, %r11d
cmpq $0x0, (%r10,%r11,8)
jne 0x59b3a
movq -0x40(%rbp), %rbx
movq 0x20(%rbx), %rbx
imulq $0x38, %r9, %r14
leaq (%rbx,%r14), %r9
movq 0x28(%rbx,%r14), %rbx
movq -0x48(%rbp), %r14
orq %rbx, (%r14)
movq -0x78(%rbp), %rbx
movq %r9, (%r10,%r11,8)
cmpl %edx, %ecx
cmovgl %ecx, %edx
cmpl $0x10, %r8d
jg 0x599fe
cmpb $0x0, -0x4(%rbx,%rdi,8)
je 0x599fe
movl $0x1, %r8d
shll %cl, %r8d
orw %r8w, 0x1e(%r14)
testb $0x1, 0x18(%r9)
jne 0x59a0b
movq -0x60(%rbp), %r15
jmp 0x59a1e
cmpb $0x0, -0x4(%rbx,%rdi,8)
movq -0x60(%rbp), %r15
je 0x59a47
movl $0x0, 0x3c(%r13)
cmpq -0x98(%rbp), %rdi
setl %sil
movq -0x90(%rbp), %rcx
addq %rdi, %rcx
incq %rcx
incq %rdi
addq $0xc, %rax
cmpq $0x1, %rcx
jne 0x59973
incl %edx
jmp 0x59a51
xorl %edx, %edx
movb -0x29(%rbp), %sil
testb $0x1, %sil
jne 0x59b13
movw %dx, 0x2c(%r14)
movl 0x28(%r13), %eax
movl %eax, 0x18(%r14)
movb 0x38(%r13), %al
movb %al, 0x1c(%r14)
movl $0x0, 0x38(%r13)
movq 0x30(%r13), %rax
movq %rax, 0x20(%r14)
cmpl $0x0, 0x10(%r13)
je 0x59a91
cmpl $0x0, 0x3c(%r13)
setne %al
jmp 0x59a93
xorl %eax, %eax
movb %al, 0x1d(%r14)
movw $0x0, 0x12(%r14)
movsd 0x40(%r13), %xmm0
movsd 0x1f82c(%rip), %xmm1    # 0x792d8
ucomisd %xmm0, %xmm1
jae 0x59ad5
movsd 0x1f826(%rip), %xmm1    # 0x792e0
ucomisd %xmm0, %xmm1
jae 0x59ad9
movq %xmm0, %rax
shrq $0x34, %rax
addl %eax, %eax
leal (%rax,%rax,4), %eax
addl $0xffffd814, %eax       # imm = 0xFFFFD814
jmp 0x59ae3
xorl %eax, %eax
jmp 0x59ae3
cvttsd2si %xmm0, %rdi
callq 0x5a359
movw %ax, 0x14(%r14)
movw $0x2e, 0x16(%r14)
movq -0x88(%rbp), %rdi
movq %r14, %rsi
callq 0x5a153
cmpb $0x0, 0x1c(%r14)
je 0x59b13
movq 0x20(%r14), %rdi
callq 0xa2e8
movb $0x0, 0x1c(%r14)
leal 0x1(%r12), %eax
cmpl $0x3, %r12d
movl %eax, %r12d
movl $0x1, %r10d
movl -0x6c(%rbp), %r14d
jl 0x59716
xorl %ebx, %ebx
jmp 0x59b58
movl $0x7, %ebx
jmp 0x59b77
movq -0x68(%rbp), %rax
movq (%rax), %rdx
leaq 0x21f75(%rip), %rsi     # 0x7babd
movq -0x38(%rbp), %rdi
xorl %eax, %eax
callq 0x11724
movl $0x1, %ebx
cmpl $0x0, 0x38(%r13)
je 0x59b68
movq 0x30(%r13), %rdi
callq 0xa2e8
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x9c3a
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq