// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %r8, %r13
movq %rcx, 0x58(%rsp)
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r12
movq 0x10(%rdi), %rax
movq %rax, 0x28(%rsp)
callq 0x48791
movl 0x60(%r12), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%r12)
xorl %r15d, %r15d
movq %rax, %rdi
movl $0x28, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
movl %eax, 0xc(%rsp)
movq 0x20(%rbp), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%r14), %rbx
movq %r14, 0x68(%rsp)
movslq 0x14(%r14), %rax
imulq $0x38, %rax, %rcx
addq %rbx, %rcx
movq %rcx, 0x38(%rsp)
movq 0x40(%r13), %rcx
movq %rcx, 0x50(%rsp)
testq %rax, %rax
movq %rbp, 0x30(%rsp)
movq %r12, 0x18(%rsp)
movq %r13, 0x48(%rsp)
jle 0x5a84c
movq $0x0, 0x20(%rsp)
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x58(%rsp), %rdx
callq 0x5dc3d
testl %eax, %eax
je 0x5a830
movslq 0x10(%rbx), %rcx
movl $0x1, %r12d
shlq %cl, %r12
cmpq $0x40, %rcx
cmovgeq %r13, %r12
cmpb $0x0, 0x20(%rsp)
jne 0x5a7db
movq 0x10(%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 0x192fe(%rip), %rsi     # 0x73ad2
xorl %eax, %eax
callq 0xba6d
movb $0x1, %al
movq %rax, 0x20(%rsp)
testq %r14, %r12
jne 0x5a830
movq %r14, %r13
movq 0x18(%rsp), %rax
movq (%rax), %rdi
leal 0x1(%r15), %ebp
movq 0x50(%rsp), %r14
movq %r14, %rsi
movl %ebp, %edx
callq 0x5d88a
testl %eax, %eax
jne 0x5acfe
movq 0x30(%r14), %rax
movslq %r15d, %rcx
movq %rbx, (%rax,%rcx,8)
movq %r13, %r14
orq %r12, %r14
movl %ebp, %r15d
movq 0x30(%rsp), %rbp
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
addq $0x38, %rbx
cmpq 0x38(%rsp), %rbx
jb 0x5a77c
notq %r14
orq %r13, %r14
movq 0x18(%rsp), %r12
jmp 0x5a853
movq $-0x1, %r14
movq 0x50(%rsp), %rbx
movw %r15w, 0x2c(%rbx)
movw %r15w, 0x18(%rbx)
movq 0x50(%rbp), %rcx
andq %rcx, %r14
movq 0x10(%rsp), %rax
movswl 0x42(%rax), %eax
cmpl $0x3f, %eax
movq %r15, %r8
movl $0x3f, %r15d
cmovll %eax, %r15d
movl %r15d, %edi
testl %r15d, %r15d
movq %rdi, 0x20(%rsp)
jle 0x5a9d5
leal 0x3(%r15), %edx
andl $-0x4, %edx
leaq -0x1(%rdi), %rsi
movd %r8d, %xmm0
movq %rsi, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
movq %r14, %xmm2
pshufd $0x44, %xmm2, %xmm5     # xmm5 = xmm2[0,1,0,1]
movdqa 0x16831(%rip), %xmm6    # 0x710f0
movdqa 0x16839(%rip), %xmm7    # 0x71100
movdqa 0x16890(%rip), %xmm8    # 0x71160
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x1685d(%rip), %xmm11   # 0x71140
movdqa %xmm0, %xmm2
movdqa %xmm6, %xmm4
movdqa %xmm7, %xmm3
movdqa %xmm8, %xmm12
psllq %xmm7, %xmm12
pshufd $0xee, %xmm7, %xmm13    # xmm13 = xmm7[2,3,2,3]
movdqa %xmm8, %xmm0
psllq %xmm13, %xmm0
movsd %xmm12, %xmm0           # xmm0 = xmm12[0],xmm0[1]
movdqa %xmm8, %xmm12
psllq %xmm6, %xmm12
pshufd $0xee, %xmm6, %xmm13    # xmm13 = xmm6[2,3,2,3]
movdqa %xmm8, %xmm14
psllq %xmm13, %xmm14
movsd %xmm12, %xmm14          # xmm14 = xmm12[0],xmm14[1]
andpd %xmm5, %xmm14
andpd %xmm5, %xmm0
pcmpeqd %xmm9, %xmm0
pcmpeqd %xmm9, %xmm14
movdqa %xmm0, %xmm12
shufps $0xdd, %xmm14, %xmm12   # xmm12 = xmm12[1,3],xmm14[1,3]
shufps $0x88, %xmm14, %xmm0    # xmm0 = xmm0[0,2],xmm14[0,2]
andps %xmm12, %xmm0
paddd %xmm2, %xmm0
psubd %xmm10, %xmm0
paddq %xmm11, %xmm7
paddq %xmm11, %xmm6
addq $-0x4, %rdx
jne 0x5a8e3
movdqa 0x16798(%rip), %xmm5    # 0x71110
pxor %xmm5, %xmm1
pxor %xmm5, %xmm3
movdqa %xmm3, %xmm6
pcmpgtd %xmm1, %xmm6
pxor %xmm5, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm1, %xmm5
movdqa %xmm6, %xmm7
shufps $0x88, %xmm5, %xmm7     # xmm7 = xmm7[0,2],xmm5[0,2]
pcmpeqd %xmm1, %xmm3
pcmpeqd %xmm1, %xmm4
shufps $0xdd, %xmm4, %xmm3     # xmm3 = xmm3[1,3],xmm4[1,3]
andps %xmm7, %xmm3
shufps $0xdd, %xmm5, %xmm6     # xmm6 = xmm6[1,3],xmm5[1,3]
orps %xmm3, %xmm6
pand %xmm6, %xmm2
pandn %xmm0, %xmm6
por %xmm2, %xmm6
pshufd $0xee, %xmm6, %xmm0     # xmm0 = xmm6[2,3,2,3]
paddd %xmm6, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
paddd %xmm0, %xmm1
movd %xmm1, %r8d
addl $-0x3e, %eax
testq %rcx, %rcx
movl $0x4241, 0x28(%rbx)     # imm = 0x4241
movq (%r12), %rdi
movl $0x1, %r12d
cmovsl %eax, %r12d
addl %r8d, %r12d
movswl %r12w, %esi
xorl %ebp, %ebp
leaq 0x70(%rsp), %rcx
xorl %edx, %edx
callq 0x52098
testq %rax, %rax
je 0x5acfe
movq %rbx, %rcx
movq %rax, %rbx
movl %r12d, 0x44(%rsp)
movq %r15, 0x60(%rsp)
movq %rax, 0x20(%rcx)
leaq 0x190c3(%rip), %rax     # 0x73aec
movq %rax, (%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rbx)
movq 0x68(%rsp), %rax
movq 0x20(%rax), %r15
cmpq 0x38(%rsp), %r15
jae 0x5aace
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x5dc3d
testl %eax, %eax
je 0x5aac3
movl 0x10(%r15), %ecx
movl $0x1, %r12d
shlq %cl, %r12
cmpl $0x40, %ecx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmovgeq %rax, %r12
testq %r13, %r12
jne 0x5aac3
movq (%r15), %rax
movq 0x8(%rbx), %rdx
movslq %ebp, %rbp
movw %cx, (%rdx,%rbp,2)
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
movq 0x18(%rsp), %rdi
callq 0x4c7f5
leaq 0x170d0(%rip), %rcx     # 0x71b7e
testq %rax, %rax
je 0x5aab6
movq (%rax), %rcx
orq %r12, %r13
movq 0x40(%rbx), %rax
movq %rcx, (%rax,%rbp,8)
incl %ebp
addq $0x38, %r15
cmpq 0x38(%rsp), %r15
jb 0x5aa4e
cmpl $0x0, 0x60(%rsp)
movq 0x20(%rsp), %rdi
jle 0x5ab17
xorl %eax, %eax
leaq 0x1709b(%rip), %rcx     # 0x71b7e
movq 0x18(%rsp), %r12
movq 0x48(%rsp), %r13
movq 0x10(%rsp), %rsi
btq %rax, %r14
jae 0x5ab0d
movq 0x8(%rbx), %rdx
movslq %ebp, %rbp
movw %ax, (%rdx,%rbp,2)
movq 0x40(%rbx), %rdx
movq %rcx, (%rdx,%rbp,8)
incl %ebp
incq %rax
cmpq %rax, %rdi
jne 0x5aaf2
jmp 0x5ab26
movq 0x18(%rsp), %r12
movq 0x48(%rsp), %r13
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rax
cmpq $0x0, 0x50(%rax)
js 0x5ab39
movq 0x28(%rsp), %r14
jmp 0x5ab71
cmpw $0x40, 0x42(%rsi)
movq 0x28(%rsp), %r14
jl 0x5ab71
movslq %ebp, %rbp
movl $0x3f, %eax
leaq 0x1702a(%rip), %rcx     # 0x71b7e
movq 0x8(%rbx), %rdx
movw %ax, (%rdx,%rbp,2)
movq 0x40(%rbx), %rdx
movq %rcx, (%rdx,%rbp,8)
incq %rbp
incl %eax
movswl 0x42(%rsi), %edx
cmpl %edx, %eax
jl 0x5ab54
movq 0x8(%rbx), %rax
movslq %ebp, %rcx
movw $0xffff, (%rax,%rcx,2)  # imm = 0xFFFF
movq 0x40(%rbx), %rax
leaq 0x16ff5(%rip), %rdx     # 0x71b7e
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 0x487d8
movq 0x10(%r12), %r15
movq %r12, %rdi
movq %rbx, %rsi
callq 0x48a99
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rax, %rdx
movl $0xfffffffa, %ecx       # imm = 0xFFFFFFFA
callq 0x12044
movl 0x4(%r13), %edx
movq %r14, %rdi
movl $0x67, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x487d8
movl %eax, %r15d
movb 0x1f(%r12), %al
testb %al, %al
je 0x5ac0a
decb %al
movb %al, 0x1f(%r12)
movzbl %al, %eax
movl 0x28(%r12,%rax,4), %ebp
jmp 0x5ac16
movl 0x58(%r12), %ebp
incl %ebp
movl %ebp, 0x58(%r12)
movl 0x4(%r13), %edx
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x526f0
movl 0x8(%r13), %edx
movq %r14, %rdi
movl $0x69, %esi
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x487d8
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x5ac59
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 0x487d8
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x5ac87
movslq 0x34(%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x3, -0x15(%rax,%rcx,8)
testl %r15d, %r15d
js 0x5aca0
movl 0x34(%r14), %ecx
cmpl %r15d, %ecx
jbe 0x5aca0
movl %r15d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %ecx, 0x8(%rax,%rdx,8)
testl %ebp, %ebp
je 0x5acdd
movzbl 0x1f(%r12), %ecx
cmpq $0x7, %rcx
ja 0x5acdd
xorl %edx, %edx
cmpl %ebp, 0x84(%r12,%rdx)
je 0x5acd7
addq $0x18, %rdx
cmpl $0xf0, %edx
jne 0x5acb2
leal 0x1(%rcx), %edx
movb %dl, 0x1f(%r12)
movl %ebp, 0x28(%r12,%rcx,4)
jmp 0x5acdd
movb $0x1, 0x7c(%r12,%rdx)
cmpl $0x0, 0xc(%rsp)
js 0x5acfe
movq 0x28(%rsp), %rcx
movl 0x34(%rcx), %ecx
cmpl 0xc(%rsp), %ecx
jbe 0x5acfe
movl 0xc(%rsp), %edx
leaq (%rdx,%rdx,2), %rdx
movl %ecx, 0x8(%rax,%rdx,8)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq