// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/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 nColumn;                /* Number of columns in the constructed index */
  WhereTerm *pTerm;           /* A single term of the WHERE clause */
  WhereTerm *pWCEnd;          /* End of pWC->a[] */
  int nByte;                  /* Byte of memory needed for pIdx */
  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 */
  KeyInfo *pKeyinfo;          /* Key information for the index */   
  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 */
  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 */
  nColumn = 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, nColumn+1) ) return;
        pLoop->aLTerm[nColumn++] = pTerm;
        idxCols |= cMask;
      }
    }
  }
  assert( nColumn>0 );
  pLoop->u.btree.nEq = pLoop->nLTerm = nColumn;
  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) ) nColumn++;
  }
  if( pSrc->colUsed & MASKBIT(BMS-1) ){
    nColumn += pTable->nCol - BMS + 1;
  }
  pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;

  /* Construct the Index object to describe this index */
  nByte = sizeof(Index);
  nByte += nColumn*sizeof(int);     /* Index.aiColumn */
  nByte += nColumn*sizeof(char*);   /* Index.azColl */
  nByte += nColumn;                 /* Index.aSortOrder */
  pIdx = sqlite3DbMallocZero(pParse->db, nByte);
  if( pIdx==0 ) return;
  pLoop->u.btree.pIndex = pIdx;
  pIdx->azColl = (char**)&pIdx[1];
  pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
  pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
  pIdx->zName = "auto-index";
  pIdx->nColumn = nColumn;
  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==nColumn );

  /* Create the automatic index */
  pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
  assert( pLevel->iIdxCur>=0 );
  pLevel->iIdxCur = pParse->nTab++;
  sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
                    (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
  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, 1, 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r15
movq %rcx, -0x78(%rbp)
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r12
movq 0x10(%rdi), %rax
movq %rax, -0x60(%rbp)
callq 0x45ece
movl 0x60(%r12), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%r12)
xorl %r14d, %r14d
movq %rax, %rdi
movl $0x26, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, -0x2c(%rbp)
movq %rbx, -0x58(%rbp)
movq 0x20(%rbx), %rax
movq %rax, -0x40(%rbp)
movq 0x20(%r13), %rbx
movq %r13, -0x88(%rbp)
movslq 0x14(%r13), %rax
imulq $0x38, %rax, %rcx
addq %rbx, %rcx
movq %rcx, -0x70(%rbp)
movq %r15, -0x68(%rbp)
movq 0x40(%r15), %r13
testq %rax, %rax
movq %r12, -0x48(%rbp)
jle 0x571d8
movq $0x0, -0x50(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %r13, -0x38(%rbp)
movq %rbx, %rdi
movq -0x58(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0x5a4a4
testl %eax, %eax
je 0x571b8
movslq 0x10(%rbx), %rcx
movl $0x1, %r13d
shlq %cl, %r13
cmpq $0x40, %rcx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmovgeq %rax, %r13
cmpb $0x0, -0x50(%rbp)
jne 0x57169
movq -0x40(%rbp), %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 0x249b7(%rip), %rsi     # 0x7bb19
xorl %eax, %eax
callq 0xb9c7
movb $0x1, %al
movq %rax, -0x50(%rbp)
testq %r15, %r13
je 0x5717a
movq -0x38(%rbp), %r13
jmp 0x571b8
movq (%r12), %rdi
leal 0x1(%r14), %edx
movq -0x38(%rbp), %r12
movq %r12, %rsi
movl %edx, -0x30(%rbp)
callq 0x5a0a9
testl %eax, %eax
jne 0x57554
movq 0x30(%r12), %rax
movslq %r14d, %rcx
movq %rbx, (%rax,%rcx,8)
orq %r13, %r15
movl -0x30(%rbp), %eax
movl %eax, %r14d
movq -0x48(%rbp), %rax
movq %r12, %r13
movq %rax, %r12
addq $0x38, %rbx
cmpq -0x70(%rbp), %rbx
jb 0x57102
notq %r15
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %r15
jmp 0x571df
movq $-0x1, %r15
movw %r14w, 0x2c(%r13)
movzwl %r14w, %eax
movl %eax, 0x18(%r13)
movq -0x58(%rbp), %rax
movq 0x50(%rax), %rbx
andq %rbx, %r15
movq -0x40(%rbp), %rax
movswl 0x42(%rax), %eax
cmpl $0x3f, %eax
movl $0x3f, %ecx
cmovll %eax, %ecx
movl %ecx, %r12d
movl %ecx, -0x30(%rbp)
testl %ecx, %ecx
jle 0x57226
xorl %ecx, %ecx
btq %rcx, %r15
adcl $0x0, %r14d
incq %rcx
cmpq %rcx, %r12
jne 0x57216
movq %r15, -0x50(%rbp)
addl $-0x3f, %eax
sarq $0x3f, %rbx
andl %eax, %ebx
addl %r14d, %ebx
movl $0x4241, 0x28(%r13)     # imm = 0x4241
leal (%rbx,%rbx,2), %eax
leal (%rbx,%rax,4), %r14d
addl $0x58, %r14d
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movl %r14d, %esi
callq 0x11721
testq %rax, %rax
je 0x57554
movq %r13, %rcx
movq %rbx, %r13
movq %rax, %rbx
movslq %r14d, %rdx
xorl %r15d, %r15d
movq %rax, %rdi
xorl %esi, %esi
movq %rcx, %r14
callq 0x8200
movslq %r13d, %rax
movq %rbx, 0x20(%r14)
leaq 0x58(%rbx), %rcx
movq %rcx, 0x40(%rbx)
leaq 0x58(%rbx,%rax,8), %rcx
movq %rcx, 0x8(%rbx)
leaq (%rcx,%rax,4), %rax
movq %rax, 0x38(%rbx)
leaq 0x2488f(%rip), %rax     # 0x7bb33
movq %rax, (%rbx)
movq %r13, -0x80(%rbp)
movw %r13w, 0x54(%rbx)
movq -0x40(%rbp), %rax
movq %rax, 0x18(%rbx)
movq -0x88(%rbp), %rax
movq 0x20(%rax), %r14
cmpq -0x70(%rbp), %r14
jae 0x57357
xorl %r15d, %r15d
movq $0x0, -0x38(%rbp)
movq %r14, %rdi
movq -0x58(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0x5a4a4
testl %eax, %eax
je 0x5734d
movl 0x10(%r14), %ecx
movl $0x1, %r13d
shlq %cl, %r13
cmpl $0x40, %ecx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmovgeq %rax, %r13
testq %r13, -0x38(%rbp)
jne 0x5734d
movq (%r14), %rax
movq 0x8(%rbx), %rdx
movslq %r15d, %r15
movl %ecx, (%rdx,%r15,4)
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
movq -0x48(%rbp), %rdi
callq 0x4ef2c
leaq 0x24208(%rip), %rcx     # 0x7b53e
testq %rax, %rax
je 0x5733e
movq (%rax), %rcx
orq %r13, -0x38(%rbp)
movq 0x40(%rbx), %rax
movq %rcx, (%rax,%r15,8)
incl %r15d
addq $0x38, %r14
cmpq -0x70(%rbp), %r14
jb 0x572d8
cmpl $0x0, -0x30(%rbp)
jle 0x5738e
xorl %eax, %eax
leaq 0x241d8(%rip), %rcx     # 0x7b53e
movq -0x50(%rbp), %rdx
btq %rax, %rdx
jae 0x57386
movq 0x8(%rbx), %rdx
movslq %r15d, %r15
movl %eax, (%rdx,%r15,4)
movq 0x40(%rbx), %rdx
movq %rcx, (%rdx,%r15,8)
incl %r15d
incq %rax
cmpq %rax, %r12
jne 0x57366
movq -0x58(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jns 0x573cd
movq -0x40(%rbp), %rax
movswl 0x42(%rax), %eax
cmpl $0x40, %eax
jl 0x573cd
movslq %r15d, %rcx
movl $0x3f, %edx
leaq 0x24189(%rip), %rsi     # 0x7b53e
movq 0x8(%rbx), %rdi
movl %edx, (%rdi,%rcx,4)
movq 0x40(%rbx), %rdi
movq %rsi, (%rdi,%rcx,8)
incq %rcx
incl %edx
cmpl %edx, %eax
jne 0x573b5
movq -0x48(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4c2fa
movq %rax, %r14
movl 0x54(%r12), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x54(%r12)
movq -0x68(%rbp), %r13
movl %edx, 0x8(%r13)
movq -0x80(%rbp), %rcx
incl %ecx
movq -0x60(%rbp), %r15
movq %r15, %rdi
movl $0x32, %esi
xorl %r8d, %r8d
callq 0x45f15
movq %r15, %rdi
movl %eax, %esi
movq %r14, %rdx
movl $0xfffffff0, %ecx       # imm = 0xFFFFFFF0
callq 0x11a9f
movl 0x4(%r13), %edx
movq %r15, %rdi
movl $0x65, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, %r15d
movb 0x1f(%r12), %al
testb %al, %al
je 0x57453
decb %al
movb %al, 0x1f(%r12)
movzbl %al, %eax
movl 0x28(%r12,%rax,4), %r14d
jmp 0x57460
movl 0x58(%r12), %r14d
incl %r14d
movl %r14d, 0x58(%r12)
movq -0x68(%rbp), %r13
movl 0x4(%r13), %edx
movq %r12, %rdi
movq %rbx, %rsi
movl %r14d, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x4c389
movl 0x8(%r13), %edx
movq -0x60(%rbp), %rbx
movq %rbx, %rdi
movl $0x67, %esi
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x574b0
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x10, -0x15(%rax,%rcx,8)
movq -0x68(%rbp), %rax
movl 0x4(%rax), %edx
leal 0x1(%r15), %ecx
movq %rbx, %rdi
movl $0x7, %esi
xorl %r8d, %r8d
callq 0x45f15
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x574e1
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x3, -0x15(%rax,%rcx,8)
testl %r15d, %r15d
js 0x574f9
movl 0x34(%rbx), %ecx
cmpl %r15d, %ecx
jbe 0x574f9
movl %r15d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %ecx, 0x8(%rax,%rdx,8)
testl %r14d, %r14d
je 0x57537
movzbl 0x1f(%r12), %ecx
cmpq $0x7, %rcx
ja 0x57537
xorl %edx, %edx
cmpl %r14d, 0x84(%r12,%rdx)
je 0x57531
addq $0x18, %rdx
cmpl $0xf0, %edx
jne 0x5750c
leal 0x1(%rcx), %edx
movb %dl, 0x1f(%r12)
movl %r14d, 0x28(%r12,%rcx,4)
jmp 0x57537
movb $0x1, 0x7c(%r12,%rdx)
cmpl $0x0, -0x2c(%rbp)
js 0x57554
movq -0x60(%rbp), %rcx
movl 0x34(%rcx), %ecx
cmpl -0x2c(%rbp), %ecx
jbe 0x57554
movl -0x2c(%rbp), %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