// File: /workspace/github_c_makefile_star2_O3/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), %rax
movq 0x8(%rdi), %r11
movq (%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x8(%rax), %rax
movq 0x18(%rdi), %r9
movzbl 0x10(%r9), %ecx
imulq $0x68, %rcx, %rcx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movq %rdi, -0x80(%rbp)
movq 0x10(%rdi), %r13
movl 0x14(%r11), %edx
xorl %r12d, %r12d
movl $0x0, %r14d
testl %edx, %edx
jle 0x594fe
movq 0x20(%r11), %rsi
movl 0x3c(%r15), %edi
addq $0x18, %rsi
xorl %r14d, %r14d
cmpl %edi, -0xc(%rsi)
jne 0x594f6
movb (%rsi), %r8b
andb $-0x80, %r8b
cmpb $0x1, %r8b
adcl $0x0, %r14d
addq $0x38, %rsi
decl %edx
jne 0x594e2
movq %r11, -0x50(%rbp)
movq -0x38(%rbp), %rdx
movq (%rdx), %rdx
movq %rdx, -0x58(%rbp)
testq %r13, %r13
je 0x5954f
movl (%r13), %edx
xorl %r12d, %r12d
movl $0x0, %esi
testl %edx, %edx
jle 0x59549
movq 0x8(%r13), %rdi
xorl %esi, %esi
movq (%rdi), %r8
cmpb $-0x68, (%r8)
jne 0x59549
movl 0x2c(%r8), %r8d
cmpl 0x3c(%r15), %r8d
jne 0x59549
incq %rsi
addq $0x20, %rdi
cmpq %rsi, %rdx
jne 0x59528
movl %edx, %esi
cmpl %edx, %esi
cmovel %edx, %r12d
movq %r9, -0x68(%rbp)
movq 0x28(%rax,%rcx), %rax
movq %rax, -0x70(%rbp)
leal (%r14,%r14,4), %eax
leal (,%r12,8), %ecx
leal (%rcx,%rax,4), %ebx
addl $0x48, %ebx
movq -0x58(%rbp), %rdi
movl %ebx, %esi
callq 0x11721
testq %rax, %rax
je 0x59637
movq %r13, -0x48(%rbp)
movslq %ebx, %rdx
xorl %ebx, %ebx
movq %rax, %rdi
movq %rax, %r13
xorl %esi, %esi
callq 0x8200
movslq %r12d, %rdx
movslq %r14d, %rax
leaq 0x48(%r13), %rcx
leaq (%rax,%rax,2), %rax
leaq 0x48(%r13,%rax,4), %rsi
leaq (%rsi,%rdx,8), %rdx
movl %r14d, (%r13)
movl %r12d, 0x10(%r13)
movq %rcx, 0x8(%r13)
movq %rsi, 0x18(%r13)
movq %r13, -0x30(%rbp)
movq %rdx, 0x20(%r13)
movq -0x50(%rbp), %rsi
movl 0x14(%rsi), %edx
testl %edx, %edx
jle 0x5964e
movq 0x20(%rsi), %rsi
movl 0x3c(%r15), %edi
addq $0x18, %rsi
xorl %r8d, %r8d
movq -0x58(%rbp), %r15
cmpl %edi, -0xc(%rsi)
jne 0x59626
movzwl (%rsi), %r9d
testb %r9b, %r9b
js 0x59626
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 0x5961e
movl %r9d, %r11d
movb %r11b, 0x4(%rcx,%r10,4)
incl %r8d
incl %ebx
addq $0x38, %rsi
cmpl %ebx, %edx
jne 0x595e5
testl %r12d, %r12d
jg 0x59657
jmp 0x5968f
leaq 0x20126(%rip), %rsi     # 0x79764
movq -0x38(%rbp), %rdi
xorl %eax, %eax
callq 0x11842
jmp 0x596d4
testl %r12d, %r12d
movq -0x58(%rbp), %r15
jle 0x5968f
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl %r12d, %edx
movq -0x30(%rbp), %rsi
leaq (%rsi,%rax,4), %rax
addq $0x4c, %rax
xorl %esi, %esi
movq (%rcx), %rdi
movswl 0x30(%rdi), %edi
movl %edi, -0x4(%rax,%rsi,8)
movb 0x18(%rcx), %dil
movb %dil, (%rax,%rsi,8)
incq %rsi
addq $0x20, %rcx
cmpq %rsi, %rdx
jne 0x59670
movq -0x68(%rbp), %rsi
movq $0x0, (%rsi)
xorl %eax, %eax
movw %ax, 0x12(%rsi)
movl $0x400, 0x28(%rsi)      # imm = 0x400
movw %ax, 0x2c(%rsi)
movb $0x0, 0x1c(%rsi)
movq -0x30(%rbp), %rbx
movq 0x20(%rbx), %r14
movl (%rbx), %r13d
movq %r15, %rdi
movl %r13d, %edx
callq 0x5a0a9
testl %eax, %eax
je 0x596de
movq %r15, %rdi
movq %rbx, %rsi
callq 0x9ca4
movl $0x7, %ebx
jmp 0x59b72
movq %r14, -0x40(%rbp)
movq %rbx, %rax
addq $0x30, %rax
movq %rax, -0x60(%rbp)
movq -0x70(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x78(%rbp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %rbx, %rsi
movq %r13, -0x48(%rbp)
movq -0x50(%rbp), %r13
testl %r14d, %r14d
jne 0x59727
movl %r12d, %eax
andl $0x1, %eax
je 0x59727
cmpl $0x3, %r12d
je 0x59b83
incl %r12d
testl %r15d, %r15d
sete %al
cmpl $0x2, %r12d
setge %cl
xorl %ebx, %ebx
testb %cl, %al
jne 0x59b53
movslq (%rsi), %rdx
testq %rdx, %rdx
jle 0x597ea
movq 0x8(%rsi), %rax
movq 0x20(%r13), %rcx
addq $0x5, %rax
movl %edx, %esi
movl %r15d, %edi
movl %r14d, %r8d
movq -0x60(%rbp), %rbx
movslq 0x3(%rax), %r9
imulq $0x38, %r9, %r9
addq %rcx, %r9
cmpl $0x2, %r12d
je 0x597c2
cmpl $0x1, %r12d
je 0x597b8
testl %r12d, %r12d
jne 0x597b3
movb $0x0, (%rax)
movb 0x18(%r9), %r10b
testb $0x1, %r10b
movl %r8d, %r14d
movl $0x1, %r11d
cmovnel %r11d, %r14d
cmpq $0x0, 0x28(%r9)
setne %r9b
cmovel %r11d, %r14d
movl %edi, %r15d
cmovnel %r11d, %r15d
orb %r10b, %r9b
testb $0x1, %r9b
jne 0x597d6
movb $0x1, (%rax)
jmp 0x597d0
cmpq $0x0, 0x28(%r9)
sete (%rax)
jmp 0x597d0
movb 0x18(%r9), %r9b
notb %r9b
andb $0x1, %r9b
movb %r9b, (%rax)
movl %r8d, %r14d
movl %edi, %r15d
addq $0xc, %rax
movl %r15d, %edi
movl %r14d, %r8d
decl %esi
jne 0x59762
jmp 0x597ee
movq -0x60(%rbp), %rbx
shlq $0x3, %rdx
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x8200
movq -0x30(%rbp), %rsi
cmpl $0x0, 0x38(%rsi)
je 0x59813
movq (%rbx), %rdi
callq 0xa3c7
movq -0x30(%rbp), %rsi
movl $0x0, 0x28(%rsi)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movabsq $0x546d42aea2879f2e, %rax # imm = 0x546D42AEA2879F2E
movq %rax, 0x40(%rsi)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rcx), %rdx
leaq 0x28(%rdx), %rcx
cmpq %rax, (%rdx)
jne 0x5983b
movq 0x10(%rdx), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
testl %eax, %eax
je 0x598ba
cmpl $0x7, %eax
jne 0x5986a
movq -0x38(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x50(%rax)
jmp 0x598ba
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
jne 0x598a8
leaq 0x23a5e(%rip), %rdx     # 0x7d2d8
cmpl $0x204, %eax            # imm = 0x204
je 0x598a8
movzbl %al, %eax
cmpl $0x1b, %eax
setae %cl
cmpl $0x2, %eax
sete %dl
orb %cl, %dl
leaq 0x23a31(%rip), %rdx     # 0x7d2ca
jne 0x598a8
movl %eax, %eax
leaq 0x39f5c(%rip), %rcx     # 0x93800
movq (%rcx,%rax,8), %rdx
movq -0x38(%rbp), %rdi
leaq 0x1fe26(%rip), %rsi     # 0x796d9
xorl %eax, %eax
callq 0x11842
movq 0x10(%rbx), %rdi
callq 0xa3c7
movq $0x0, 0x10(%rbx)
movq -0x30(%rbp), %rsi
movl (%rsi), %eax
testl %eax, %eax
movq -0x48(%rbp), %r9
jle 0x59928
movl $0x5, %ebx
xorl %r13d, %r13d
movq 0x8(%rsi), %rcx
cmpb $0x0, (%rcx,%rbx)
jne 0x59919
movq 0x20(%rsi), %rcx
cmpl $0x0, (%rcx,%r13,8)
jle 0x59919
movq -0x70(%rbp), %rax
movq (%rax), %rdx
movq -0x38(%rbp), %rdi
leaq 0x221d1(%rip), %rsi     # 0x7bad9
xorl %eax, %eax
callq 0x11842
movq -0x48(%rbp), %r9
movq -0x30(%rbp), %rsi
movl (%rsi), %eax
incq %r13
movslq %eax, %rcx
addq $0xc, %rbx
cmpq %rcx, %r13
jl 0x598e1
movq -0x38(%rbp), %rax
movl 0x50(%rax), %ebx
testl %ebx, %ebx
movq -0x50(%rbp), %r13
jne 0x59b53
movq 0x8(%rsi), %rax
movq -0x68(%rbp), %rbx
movq $0x0, (%rbx)
testl %r9d, %r9d
jle 0x59a5f
xorl %ecx, %ecx
movq 0x30(%rbx), %rdx
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %r9
jne 0x59955
movw $0x0, 0x1e(%rbx)
testl %r9d, %r9d
jle 0x59a65
addq $0x8, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %esi, %esi
xorl %edi, %edi
movq -0x40(%rbp), %rcx
movl (%rcx,%rdi,8), %r8d
testl %r8d, %r8d
jle 0x59a47
cmpl %r9d, %r8d
jg 0x59b31
movl (%rax), %r9d
testl %r9d, %r9d
js 0x59b31
cmpl 0x14(%r13), %r9d
jge 0x59b31
leal -0x1(%r8), %ecx
movq 0x30(%rbx), %r10
movl %ecx, %r11d
cmpq $0x0, (%r10,%r11,8)
jne 0x59b31
movq 0x20(%r13), %rbx
imulq $0x38, %r9, %r13
leaq (%rbx,%r13), %r9
orq 0x28(%rbx,%r13), %rsi
movq -0x68(%rbp), %rbx
movq %rsi, (%rbx)
movq %r9, (%r10,%r11,8)
cmpl %edx, %ecx
cmovgl %ecx, %edx
cmpl $0x10, %r8d
ja 0x59a0c
movq -0x40(%rbp), %r8
cmpb $0x0, 0x4(%r8,%rdi,8)
je 0x59a0c
movl $0x1, %r8d
shll %cl, %r8d
orw %r8w, 0x1e(%rbx)
testb $0x1, 0x18(%r9)
jne 0x59a21
movq -0x50(%rbp), %r13
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r9
jmp 0x59a47
movq -0x40(%rbp), %rcx
cmpb $0x0, 0x4(%rcx,%rdi,8)
movq -0x50(%rbp), %r13
movq -0x48(%rbp), %r9
je 0x59b14
movq -0x30(%rbp), %rcx
movl $0x0, 0x3c(%rcx)
movq -0x40(%rbp), %rcx
incq %rdi
addq $0xc, %rax
cmpq %rdi, %r9
jne 0x59989
incl %edx
movq -0x30(%rbp), %rsi
jmp 0x59a67
movw $0x0, 0x1e(%rbx)
xorl %edx, %edx
movw %dx, 0x2c(%rbx)
movl 0x28(%rsi), %eax
movl %eax, 0x18(%rbx)
movb 0x38(%rsi), %al
movb %al, 0x1c(%rbx)
movl $0x0, 0x38(%rsi)
movq 0x30(%rsi), %rax
movq %rax, 0x20(%rbx)
cmpl $0x0, 0x10(%rsi)
je 0x59a95
cmpl $0x0, 0x3c(%rsi)
setne %al
jmp 0x59a97
xorl %eax, %eax
movb %al, 0x1d(%rbx)
movw $0x0, 0x12(%rbx)
movsd 0x40(%rsi), %xmm0
movsd 0x1f82b(%rip), %xmm1    # 0x792d8
ucomisd %xmm0, %xmm1
jae 0x59ad6
movsd 0x1f825(%rip), %xmm1    # 0x792e0
ucomisd %xmm0, %xmm1
jae 0x59ada
movq %xmm0, %rax
shrq $0x34, %rax
addl %eax, %eax
leal (%rax,%rax,4), %eax
addl $0xffffd814, %eax       # imm = 0xFFFFD814
jmp 0x59ae4
xorl %eax, %eax
jmp 0x59ae4
cvttsd2si %xmm0, %rdi
callq 0x5a316
movw %ax, 0x14(%rbx)
movw $0x2e, 0x16(%rbx)
movq -0x80(%rbp), %rax
movq (%rax), %rdi
movq 0x20(%rax), %rsi
movq %rbx, %rdx
callq 0x5a131
cmpb $0x0, 0x1c(%rbx)
je 0x59b14
movq 0x20(%rbx), %rdi
callq 0xa3c7
movb $0x0, 0x1c(%rbx)
leal 0x1(%r12), %eax
movl $0x0, %ebx
cmpl $0x3, %r12d
movl %eax, %r12d
movq -0x30(%rbp), %rsi
jl 0x5970d
jmp 0x59b53
movq -0x70(%rbp), %rax
movq (%rax), %rdx
leaq 0x21f7e(%rip), %rsi     # 0x7babd
movq -0x38(%rbp), %rdi
xorl %eax, %eax
callq 0x11842
movl $0x1, %ebx
movq -0x30(%rbp), %rsi
cmpl $0x0, 0x38(%rsi)
je 0x59b69
movq -0x60(%rbp), %rax
movq (%rax), %rdi
callq 0xa3c7
movq -0x30(%rbp), %rsi
movq -0x58(%rbp), %rdi
callq 0x9ca4
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x59b53