// File: /workspace/github_c_makefile_star2_O1/skeeto[P]flappy/sqlite3.c
// Function: constructAutomaticIndex
static void constructAutomaticIndex(
  Parse *pParse,              /* The parsing context */
  WhereClause *pWC,           /* The WHERE clause */
  struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
  Bitmask notReady,           /* Mask of cursors that are not available */
  WhereLevel *pLevel          /* Write new index here */
){
  int nKeyCol;                /* Number of columns in the constructed index */
  WhereTerm *pTerm;           /* A single term of the WHERE clause */
  WhereTerm *pWCEnd;          /* End of pWC->a[] */
  Index *pIdx;                /* Object describing the transient index */
  Vdbe *v;                    /* Prepared statement under construction */
  int addrInit;               /* Address of the initialization bypass jump */
  Table *pTable;              /* The table being indexed */
  int addrTop;                /* Top of the index fill loop */
  int regRecord;              /* Register holding an index record */
  int n;                      /* Column counter */
  int i;                      /* Loop counter */
  int mxBitCol;               /* Maximum column in pSrc->colUsed */
  CollSeq *pColl;             /* Collating sequence to on a column */
  WhereLoop *pLoop;           /* The Loop object */
  char *zNotUsed;             /* Extra space on the end of pIdx */
  Bitmask idxCols;            /* Bitmap of columns used for indexing */
  Bitmask extraCols;          /* Bitmap of additional columns */
  u8 sentWarning = 0;         /* True if a warnning has been issued */

  /* Generate code to skip over the creation and initialization of the
  ** transient index on 2nd and subsequent iterations of the loop. */
  v = pParse->pVdbe;
  assert( v!=0 );
  addrInit = sqlite3CodeOnce(pParse);

  /* Count the number of columns that will be added to the index
  ** and used to match WHERE clause constraints */
  nKeyCol = 0;
  pTable = pSrc->pTab;
  pWCEnd = &pWC->a[pWC->nTerm];
  pLoop = pLevel->pWLoop;
  idxCols = 0;
  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
      int iCol = pTerm->u.leftColumn;
      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
      testcase( iCol==BMS );
      testcase( iCol==BMS-1 );
      if( !sentWarning ){
        sqlite3_log(SQLITE_WARNING_AUTOINDEX,
            "automatic index on %s(%s)", pTable->zName,
            pTable->aCol[iCol].zName);
        sentWarning = 1;
      }
      if( (idxCols & cMask)==0 ){
        if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return;
        pLoop->aLTerm[nKeyCol++] = pTerm;
        idxCols |= cMask;
      }
    }
  }
  assert( nKeyCol>0 );
  pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
  pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
                     | WHERE_AUTO_INDEX;

  /* Count the number of additional columns needed to create a
  ** covering index.  A "covering index" is an index that contains all
  ** columns that are needed by the query.  With a covering index, the
  ** original table never needs to be accessed.  Automatic indices must
  ** be a covering index because the index will not be updated if the
  ** original table changes and the index and table cannot both be used
  ** if they go out of sync.
  */
  extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
  mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
  testcase( pTable->nCol==BMS-1 );
  testcase( pTable->nCol==BMS-2 );
  for(i=0; i<mxBitCol; i++){
    if( extraCols & MASKBIT(i) ) nKeyCol++;
  }
  if( pSrc->colUsed & MASKBIT(BMS-1) ){
    nKeyCol += pTable->nCol - BMS + 1;
  }
  pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;

  /* Construct the Index object to describe this index */
  pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
  if( pIdx==0 ) return;
  pLoop->u.btree.pIndex = pIdx;
  pIdx->zName = "auto-index";
  pIdx->pTable = pTable;
  n = 0;
  idxCols = 0;
  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
      int iCol = pTerm->u.leftColumn;
      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
      testcase( iCol==BMS-1 );
      testcase( iCol==BMS );
      if( (idxCols & cMask)==0 ){
        Expr *pX = pTerm->pExpr;
        idxCols |= cMask;
        pIdx->aiColumn[n] = pTerm->u.leftColumn;
        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
        pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
        n++;
      }
    }
  }
  assert( (u32)n==pLoop->u.btree.nEq );

  /* Add additional columns needed to make the automatic index into
  ** a covering index */
  for(i=0; i<mxBitCol; i++){
    if( extraCols & MASKBIT(i) ){
      pIdx->aiColumn[n] = i;
      pIdx->azColl[n] = "BINARY";
      n++;
    }
  }
  if( pSrc->colUsed & MASKBIT(BMS-1) ){
    for(i=BMS-1; i<pTable->nCol; i++){
      pIdx->aiColumn[n] = i;
      pIdx->azColl[n] = "BINARY";
      n++;
    }
  }
  assert( n==nKeyCol );
  pIdx->aiColumn[n] = -1;
  pIdx->azColl[n] = "BINARY";

  /* Create the automatic index */
  assert( pLevel->iIdxCur>=0 );
  pLevel->iIdxCur = pParse->nTab++;
  sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
  sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  VdbeComment((v, "for %s", pTable->zName));

  /* Fill the automatic index with content */
  addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
  regRecord = sqlite3GetTempReg(pParse);
  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0);
  sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
  sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
  sqlite3VdbeJumpHere(v, addrTop);
  sqlite3ReleaseTempReg(pParse, regRecord);
  
  /* Jump here when skipping the initialization */
  sqlite3VdbeJumpHere(v, addrInit);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r14
movq %rcx, 0x50(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movq 0x10(%rdi), %rax
movq %rax, 0x28(%rsp)
callq 0x47f6a
movl 0x60(%rbx), %edx
leal 0x1(%rdx), %ecx
movq %rbx, 0x18(%rsp)
movl %ecx, 0x60(%rbx)
xorl %r15d, %r15d
movq %rax, %rdi
movl $0x28, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
movl %eax, 0x4(%rsp)
movq %r12, 0x20(%rsp)
movq 0x20(%r12), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%r13), %rbx
movq %r13, 0x58(%rsp)
movslq 0x14(%r13), %rax
imulq $0x38, %rax, %rcx
addq %rbx, %rcx
movq %rcx, 0x38(%rsp)
movq %r14, 0x48(%rsp)
movq 0x40(%r14), %rcx
movq %rcx, 0x30(%rsp)
testq %rax, %rax
jle 0x5a0db
xorl %r13d, %r13d
movabsq $-0x8000000000000000, %rbp # imm = 0x8000000000000000
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x5d3ee
testl %eax, %eax
je 0x5a0c4
movslq 0x10(%rbx), %rcx
movl $0x1, %r12d
shlq %cl, %r12
cmpq $0x40, %rcx
cmovgeq %rbp, %r12
testb %r13b, %r13b
jne 0x5a061
movq 0x8(%rsp), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rax
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rcx
movl $0x11c, %edi            # imm = 0x11C
leaq 0x189d8(%rip), %rsi     # 0x72a32
xorl %eax, %eax
callq 0xb94a
movb $0x1, %r13b
testq %r14, %r12
je 0x5a06d
movb $0x1, %al
jmp 0x5a0bc
movq %r14, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rdi
leal 0x1(%r15), %ebp
movq 0x30(%rsp), %r14
movq %r14, %rsi
movl %ebp, %edx
callq 0x5d023
testl %eax, %eax
je 0x5a09a
xorl %eax, %eax
movq 0x10(%rsp), %r14
jmp 0x5a0b2
movq 0x30(%r14), %rax
movslq %r15d, %rcx
movq %rbx, (%rax,%rcx,8)
movq 0x10(%rsp), %r14
orq %r12, %r14
movb $0x1, %al
movl %ebp, %r15d
movabsq $-0x8000000000000000, %rbp # imm = 0x8000000000000000
testb %al, %al
je 0x5a465
addq $0x38, %rbx
cmpq 0x38(%rsp), %rbx
jb 0x5a002
notq %r14
orq %rbp, %r14
jmp 0x5a0e2
movq $-0x1, %r14
movq 0x30(%rsp), %rsi
movw %r15w, 0x2c(%rsi)
movw %r15w, 0x18(%rsi)
movl $0x4241, 0x28(%rsi)     # imm = 0x4241
movq 0x20(%rsp), %rax
movq 0x50(%rax), %rcx
andq %rcx, %r14
movq 0x8(%rsp), %rax
movswl 0x42(%rax), %eax
cmpl $0x3f, %eax
movl $0x3f, %edx
cmovll %eax, %edx
movl %edx, %r13d
movl %edx, 0x40(%rsp)
testl %edx, %edx
jle 0x5a135
xorl %edx, %edx
btq %rdx, %r14
adcl $0x0, %r15d
incq %rdx
cmpq %rdx, %r13
jne 0x5a125
addl $-0x3e, %eax
testq %rcx, %rcx
movl $0x4241, 0x28(%rsi)     # imm = 0x4241
movq 0x18(%rsp), %rcx
movq (%rcx), %rdi
movl $0x1, %ecx
cmovsl %eax, %ecx
addl %r15d, %ecx
movl %ecx, 0x44(%rsp)
movswl %cx, %esi
xorl %ebx, %ebx
leaq 0x60(%rsp), %rcx
xorl %edx, %edx
callq 0x51ad0
testq %rax, %rax
je 0x5a465
movq %rax, %rbp
movq 0x30(%rsp), %rax
movq %rbp, 0x20(%rax)
leaq 0x188c6(%rip), %rax     # 0x72a4c
movq %rax, (%rbp)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rbp)
movq 0x58(%rsp), %rax
movq 0x20(%rax), %r15
cmpq 0x38(%rsp), %r15
jae 0x5a23a
xorl %ebx, %ebx
movq $0x0, 0x10(%rsp)
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x5d3ee
testl %eax, %eax
je 0x5a22b
movl 0x10(%r15), %ecx
movl $0x1, %r12d
shlq %cl, %r12
cmpl $0x40, %ecx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmovgeq %rax, %r12
testq %r12, 0x10(%rsp)
jne 0x5a22b
movq (%r15), %rax
movq 0x8(%rbp), %rdx
movslq %ebx, %rbx
movw %cx, (%rdx,%rbx,2)
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
movq 0x18(%rsp), %rdi
callq 0x4c059
leaq 0x168ca(%rip), %rcx     # 0x70ade
testq %rax, %rax
je 0x5a21c
movq (%rax), %rcx
orq %r12, 0x10(%rsp)
movq 0x40(%rbp), %rax
movq %rcx, (%rax,%rbx,8)
incl %ebx
addq $0x38, %r15
cmpq 0x38(%rsp), %r15
jb 0x5a1b2
cmpl $0x0, 0x40(%rsp)
jle 0x5a274
xorl %eax, %eax
leaq 0x16894(%rip), %rcx     # 0x70ade
movq 0x8(%rsp), %rsi
btq %rax, %r14
jae 0x5a26a
movq 0x8(%rbp), %rdx
movslq %ebx, %rbx
movw %ax, (%rdx,%rbx,2)
movq 0x40(%rbp), %rdx
movq %rcx, (%rdx,%rbx,8)
incl %ebx
incq %rax
cmpq %rax, %r13
jne 0x5a24f
jmp 0x5a279
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rax
cmpq $0x0, 0x50(%rax)
js 0x5a296
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %r12
movq 0x48(%rsp), %r13
jmp 0x5a2d8
cmpw $0x40, 0x42(%rsi)
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %r12
movq 0x48(%rsp), %r13
jl 0x5a2d8
movslq %ebx, %rbx
movl $0x3f, %eax
leaq 0x16823(%rip), %rcx     # 0x70ade
movq 0x8(%rbp), %rdx
movw %ax, (%rdx,%rbx,2)
movq 0x40(%rbp), %rdx
movq %rcx, (%rdx,%rbx,8)
incq %rbx
incl %eax
movswl 0x42(%rsi), %edx
cmpl %edx, %eax
jl 0x5a2bb
movq 0x8(%rbp), %rax
movslq %ebx, %rcx
movw $0xffff, (%rax,%rcx,2)  # imm = 0xFFFF
movq 0x40(%rbp), %rax
leaq 0x167ee(%rip), %rdx     # 0x70ade
movq %rdx, (%rax,%rcx,8)
movl 0x54(%r12), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x54(%r12)
movl %edx, 0x8(%r13)
movq %r14, %rdi
movl $0x34, %esi
movl 0x44(%rsp), %ecx
xorl %r8d, %r8d
callq 0x47fb1
movq 0x10(%r12), %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x48266
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rax, %rdx
movl $0xfffffffa, %ecx       # imm = 0xFFFFFFFA
callq 0x11ea3
movl 0x4(%r13), %edx
movq %r14, %rdi
movl $0x67, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
movl %eax, %r15d
movb 0x1f(%r12), %al
testb %al, %al
je 0x5a371
decb %al
movb %al, 0x1f(%r12)
movzbl %al, %eax
movl 0x28(%r12,%rax,4), %ebx
jmp 0x5a37d
movl 0x58(%r12), %ebx
incl %ebx
movl %ebx, 0x58(%r12)
movl 0x4(%r13), %edx
movq %r12, %rdi
movq %rbp, %rsi
movl %ebx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x52128
movl 0x8(%r13), %edx
movq %r14, %rdi
movl $0x69, %esi
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x47fb1
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x5a3c0
movslq 0x34(%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x10, -0x15(%rax,%rcx,8)
movl 0x4(%r13), %edx
leal 0x1(%r15), %ecx
movq %r14, %rdi
movl $0x9, %esi
xorl %r8d, %r8d
callq 0x47fb1
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x5a3ee
movslq 0x34(%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x3, -0x15(%rax,%rcx,8)
testl %r15d, %r15d
js 0x5a407
movl 0x34(%r14), %ecx
cmpl %r15d, %ecx
jbe 0x5a407
movl %r15d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %ecx, 0x8(%rax,%rdx,8)
testl %ebx, %ebx
je 0x5a444
movzbl 0x1f(%r12), %ecx
cmpq $0x7, %rcx
ja 0x5a444
xorl %edx, %edx
cmpl %ebx, 0x84(%r12,%rdx)
je 0x5a43e
addq $0x18, %rdx
cmpl $0xf0, %edx
jne 0x5a419
leal 0x1(%rcx), %edx
movb %dl, 0x1f(%r12)
movl %ebx, 0x28(%r12,%rcx,4)
jmp 0x5a444
movb $0x1, 0x7c(%r12,%rdx)
cmpl $0x0, 0x4(%rsp)
js 0x5a465
movq 0x28(%rsp), %rcx
movl 0x34(%rcx), %ecx
cmpl 0x4(%rsp), %ecx
jbe 0x5a465
movl 0x4(%rsp), %edx
leaq (%rdx,%rdx,2), %rdx
movl %ecx, 0x8(%rax,%rdx,8)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq