// File: /workspace/github_c_makefile_star2_O0/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
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x8, %rax
movq -0x88(%rbp), %rcx
movzbl 0x10(%rcx), %ecx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
callq 0x78c90
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x779b1
movl $0x7, -0x4(%rbp)
jmp 0x77f3a
movq -0x88(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rax
movw $0x0, 0x12(%rax)
movq -0x88(%rbp), %rax
movl $0x400, 0x28(%rax)      # imm = 0x400
movq -0x88(%rbp), %rax
movw $0x0, 0x2c(%rax)
movq -0x88(%rbp), %rax
movb $0x0, 0x1c(%rax)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x40(%rbp), %rdi
movq -0x88(%rbp), %rsi
movl -0x74(%rbp), %edx
callq 0x78fd0
cmpl $0x0, %eax
je 0x77a38
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x9cd0
movl $0x7, -0x4(%rbp)
jmp 0x77f3a
movl $0x0, -0x80(%rbp)
cmpl $0x3, -0x80(%rbp)
jg 0x77f0b
cmpl $0x0, -0x78(%rbp)
jne 0x77a70
movl -0x80(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x77a70
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
cmpl $0x3, -0x80(%rbp)
jle 0x77a6e
jmp 0x77f0b
jmp 0x77a70
cmpl $0x0, -0x7c(%rbp)
jne 0x77a81
cmpl $0x1, -0x80(%rbp)
jle 0x77a81
jmp 0x77f0b
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x77ba0
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x68(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x90(%rbp)
testl %eax, %eax
je 0x77af0
jmp 0x77ad3
movl -0x90(%rbp), %eax
subl $0x1, %eax
je 0x77b3f
jmp 0x77ae0
movl -0x90(%rbp), %eax
subl $0x2, %eax
je 0x77b5b
jmp 0x77b7c
movq -0x50(%rbp), %rax
movb $0x0, 0x5(%rax)
movq -0x60(%rbp), %rax
movzwl 0x18(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x77b0f
movl $0x1, -0x78(%rbp)
movq -0x60(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x77b23
movl $0x1, -0x7c(%rbp)
jmp 0x77b3d
movq -0x60(%rbp), %rax
movzwl 0x18(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x77b3b
movq -0x50(%rbp), %rax
movb $0x1, 0x5(%rax)
jmp 0x77b3d
jmp 0x77b84
movq -0x60(%rbp), %rax
cmpq $0x0, 0x28(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, 0x5(%rax)
jmp 0x77b84
movq -0x60(%rbp), %rax
movzwl 0x18(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movb %cl, 0x5(%rax)
jmp 0x77b84
movq -0x50(%rbp), %rax
movb $0x1, 0x5(%rax)
jmp 0x77b86
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x50(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x50(%rbp)
jmp 0x77a94
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movslq (%rax), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x8200
movq -0x48(%rbp), %rax
cmpl $0x0, 0x38(%rax)
je 0x77bcd
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0xa420
movq -0x48(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x38(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x3c(%rax)
movq -0x48(%rbp), %rax
movsd 0x3624a(%rip), %xmm0    # 0xade50
movsd %xmm0, 0x40(%rax)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x790a0
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
je 0x77c30
jmp 0x77f0d
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x88(%rbp), %rax
movq $0x0, (%rax)
movl $0xffffffff, -0x70(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0x77c82
movq -0x88(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x64(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x77c58
movq -0x88(%rbp), %rax
movw $0x0, 0x1e(%rax)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0x77def
movq -0x58(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,8), %eax
subl $0x1, %eax
movl %eax, -0x6c(%rbp)
cmpl $0x0, %eax
jl 0x77dd3
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0x77cf6
cmpl $0x0, -0x68(%rbp)
jl 0x77cf6
movl -0x68(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x77cf6
movq -0x88(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x6c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x77d1e
movl $0x1, -0x8c(%rbp)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rdx
leaq 0x388a7(%rip), %rsi     # 0xb05b9
movb $0x0, %al
callq 0x136f0
jmp 0x77f0d
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x68(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x88(%rbp), %rax
orq (%rax), %rcx
movq %rcx, (%rax)
movq -0x60(%rbp), %rdx
movq -0x88(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x6c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x6c(%rbp), %eax
cmpl -0x70(%rbp), %eax
jle 0x77d6f
movl -0x6c(%rbp), %eax
movl %eax, -0x70(%rbp)
cmpl $0x10, -0x6c(%rbp)
jge 0x77da2
movq -0x58(%rbp), %rax
movslq -0x64(%rbp), %rcx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x0, %eax
je 0x77da2
movl -0x6c(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movq -0x88(%rbp), %rax
movzwl 0x1e(%rax), %ecx
orl %edx, %ecx
movw %cx, 0x1e(%rax)
movq -0x60(%rbp), %rax
movzwl 0x18(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x77dd1
movq -0x58(%rbp), %rax
movslq -0x64(%rbp), %rcx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x0, %eax
jne 0x77dc6
jmp 0x77def
movq -0x48(%rbp), %rax
movl $0x0, 0x3c(%rax)
jmp 0x77dd3
jmp 0x77dd5
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x50(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x50(%rbp)
jmp 0x77c96
movl -0x64(%rbp), %eax
cmpl -0x74(%rbp), %eax
jl 0x77efb
movl -0x70(%rbp), %eax
addl $0x1, %eax
movw %ax, %cx
movq -0x88(%rbp), %rax
movw %cx, 0x2c(%rax)
movq -0x48(%rbp), %rax
movl 0x28(%rax), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x48(%rbp), %rax
movl 0x38(%rax), %eax
movb %al, %cl
movq -0x88(%rbp), %rax
movb %cl, 0x1c(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x38(%rax)
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x10(%rcx)
movb %al, -0x91(%rbp)
je 0x77e74
movq -0x48(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
setne %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x88(%rbp), %rax
movb %cl, 0x1d(%rax)
movq -0x88(%rbp), %rax
movw $0x0, 0x12(%rax)
movq -0x48(%rbp), %rax
movsd 0x40(%rax), %xmm0
callq 0x791e0
movw %ax, %cx
movq -0x88(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x88(%rbp), %rax
movw $0x2e, 0x16(%rax)
movq -0x10(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x79260
movq -0x88(%rbp), %rax
cmpb $0x0, 0x1c(%rax)
je 0x77ef9
movq -0x88(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xa420
movq -0x88(%rbp), %rax
movb $0x0, 0x1c(%rax)
jmp 0x77efb
jmp 0x77efd
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x77a3f
jmp 0x77f0d
movq -0x48(%rbp), %rax
cmpl $0x0, 0x38(%rax)
je 0x77f24
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0xa420
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x9cd0
movl -0x8c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)