// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rcx, -0x88(%rbp)
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movq 0x10(%rdi), %rax
movq %rax, -0x80(%rbp)
callq 0x45b57
movl 0x60(%r15), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%r15)
xorl %r14d, %r14d
movq %rax, %rdi
movl $0x26, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %eax, -0x2c(%rbp)
movq %rbx, -0x38(%rbp)
movq 0x20(%rbx), %rax
movq %rax, -0x58(%rbp)
movq 0x20(%r13), %rbx
movq %r13, -0x90(%rbp)
movslq 0x14(%r13), %rax
imulq $0x38, %rax, %rcx
addq %rbx, %rcx
movq %rcx, -0x70(%rbp)
movq %r12, -0x60(%rbp)
movq 0x40(%r12), %rdi
testq %rax, %rax
movq %r15, -0x48(%rbp)
movq %rdi, -0x40(%rbp)
jle 0x571c5
movq $0x0, -0x50(%rbp)
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
xorl %r14d, %r14d
xorl %edx, %edx
movq %rdx, %r15
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0x5a4e7
testl %eax, %eax
je 0x57151
movslq 0x10(%rbx), %rcx
movl $0x1, %r13d
shlq %cl, %r13
cmpq $0x40, %rcx
cmovgeq %r12, %r13
cmpb $0x0, -0x50(%rbp)
jne 0x5713f
movq -0x58(%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 0x249e1(%rip), %rsi     # 0x7bb19
xorl %eax, %eax
callq 0xb8cc
movb $0x1, %al
movq %rax, -0x50(%rbp)
testq %r15, %r13
je 0x57156
movq %r15, %rdx
movb $0x1, %al
jmp 0x5719f
movq %r15, %rdx
jmp 0x571a7
movq -0x48(%rbp), %rax
movq (%rax), %rdi
leal 0x1(%r14), %edx
movq -0x40(%rbp), %r12
movq %r12, %rsi
movl %edx, -0x68(%rbp)
callq 0x5a0cb
testl %eax, %eax
je 0x5717b
xorl %eax, %eax
movq %r15, %rdx
jmp 0x57195
movq 0x30(%r12), %rax
movslq %r14d, %rcx
movq %rbx, (%rax,%rcx,8)
movq %r15, %rdx
orq %r13, %rdx
movb $0x1, %al
movl -0x68(%rbp), %ecx
movl %ecx, %r14d
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
testb %al, %al
je 0x57543
addq $0x38, %rbx
cmpq -0x70(%rbp), %rbx
jb 0x570e0
notq %rdx
orq %r12, %rdx
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %rdi
jmp 0x571cc
movq $-0x1, %rdx
movw %r14w, 0x2c(%rdi)
movzwl %r14w, %eax
movl %eax, 0x18(%rdi)
movl $0x4241, 0x28(%rdi)     # imm = 0x4241
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rsi
andq %rsi, %rdx
movq -0x58(%rbp), %rax
movswl 0x42(%rax), %eax
cmpl $0x3f, %eax
movl $0x3f, %ecx
cmovll %eax, %ecx
movl %ecx, %r12d
movl %ecx, -0x74(%rbp)
testl %ecx, %ecx
jle 0x57219
xorl %ecx, %ecx
btq %rcx, %rdx
adcl $0x0, %r14d
incq %rcx
cmpq %rcx, %r12
jne 0x57209
addl $-0x3f, %eax
sarq $0x3f, %rsi
andl %eax, %esi
addl %r14d, %esi
movl $0x4241, 0x28(%rdi)     # imm = 0x4241
leal (%rsi,%rsi,2), %eax
leal (%rsi,%rax,4), %r14d
addl $0x58, %r14d
movq (%r15), %rdi
movq %rdx, -0x68(%rbp)
movq %rsi, %r13
movl %r14d, %esi
callq 0x115e9
testq %rax, %rax
je 0x57543
movq %rax, %rbx
movslq %r14d, %rdx
xorl %r15d, %r15d
movq %rax, %rdi
xorl %esi, %esi
callq 0x8200
movslq %r13d, %rax
movq -0x40(%rbp), %rcx
movq %rbx, 0x20(%rcx)
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 0x248a3(%rip), %rax     # 0x7bb33
movq %rax, (%rbx)
movq %r13, -0x40(%rbp)
movw %r13w, 0x54(%rbx)
movq -0x58(%rbp), %rax
movq %rax, 0x18(%rbx)
movq -0x90(%rbp), %rax
movq 0x20(%rax), %r14
cmpq -0x70(%rbp), %r14
jae 0x5734a
xorl %r15d, %r15d
movq $0x0, -0x50(%rbp)
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0x5a4e7
testl %eax, %eax
je 0x5733c
movl 0x10(%r14), %ecx
movl $0x1, %r13d
shlq %cl, %r13
cmpl $0x40, %ecx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmovgeq %rax, %r13
testq %r13, -0x50(%rbp)
jne 0x5733c
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 0x4eea5
leaq 0x24219(%rip), %rcx     # 0x7b53e
testq %rax, %rax
je 0x5732d
movq (%rax), %rcx
orq %r13, -0x50(%rbp)
movq 0x40(%rbx), %rax
movq %rcx, (%rax,%r15,8)
incl %r15d
addq $0x38, %r14
cmpq -0x70(%rbp), %r14
jb 0x572c4
cmpl $0x0, -0x74(%rbp)
movq -0x68(%rbp), %rsi
jle 0x57381
xorl %eax, %eax
leaq 0x241e1(%rip), %rcx     # 0x7b53e
btq %rax, %rsi
jae 0x57379
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 0x5735d
movq -0x38(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jns 0x573c0
movq -0x58(%rbp), %rax
movswl 0x42(%rax), %eax
cmpl $0x40, %eax
jl 0x573c0
movslq %r15d, %rcx
movl $0x3f, %edx
leaq 0x24196(%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 0x573a8
movq -0x48(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4c1d4
movq %rax, %r14
movl 0x54(%r13), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x54(%r13)
movq -0x60(%rbp), %r15
movl %edx, 0x8(%r15)
movq -0x40(%rbp), %rcx
incl %ecx
movq -0x80(%rbp), %r12
movq %r12, %rdi
movl $0x32, %esi
xorl %r8d, %r8d
callq 0x45b9e
movq %r12, %rdi
movl %eax, %esi
movq %r14, %rdx
movl $0xfffffff0, %ecx       # imm = 0xFFFFFFF0
callq 0x11975
movl 0x4(%r15), %edx
movq %r12, %rdi
movl $0x65, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq %rax, -0x38(%rbp)
movb 0x1f(%r13), %al
testb %al, %al
je 0x57443
decb %al
movb %al, 0x1f(%r13)
movzbl %al, %eax
movl 0x28(%r13,%rax,4), %r14d
jmp 0x5744e
movl 0x58(%r13), %r14d
incl %r14d
movl %r14d, 0x58(%r13)
movq -0x60(%rbp), %r15
movl 0x4(%r15), %edx
movq %r13, %rdi
movq %rbx, %rsi
movl %r14d, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x4c263
movl 0x8(%r15), %edx
movq %r12, %rdi
movl $0x67, %esi
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x5749c
movslq 0x34(%r12), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x10, -0x15(%rax,%rcx,8)
movq -0x60(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x38(%rbp), %rbx
leal 0x1(%rbx), %ecx
movq %r12, %rdi
movl $0x7, %esi
xorl %r8d, %r8d
callq 0x45b9e
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x574d2
movslq 0x34(%r12), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x3, -0x15(%rax,%rcx,8)
testl %ebx, %ebx
js 0x574e9
movl 0x34(%r12), %ecx
cmpl %ebx, %ecx
jbe 0x574e9
movl %ebx, %edx
leaq (%rdx,%rdx,2), %rdx
movl %ecx, 0x8(%rax,%rdx,8)
testl %r14d, %r14d
movq -0x48(%rbp), %rsi
je 0x57526
movzbl 0x1f(%rsi), %ecx
cmpq $0x7, %rcx
ja 0x57526
xorl %edx, %edx
cmpl %r14d, 0x84(%rsi,%rdx)
je 0x57521
addq $0x18, %rdx
cmpl $0xf0, %edx
jne 0x574fe
leal 0x1(%rcx), %edx
movb %dl, 0x1f(%rsi)
movl %r14d, 0x28(%rsi,%rcx,4)
jmp 0x57526
movb $0x1, 0x7c(%rsi,%rdx)
cmpl $0x0, -0x2c(%rbp)
js 0x57543
movq -0x80(%rbp), %rcx
movl 0x34(%rcx), %ecx
cmpl -0x2c(%rbp), %ecx
jbe 0x57543
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