// File: /workspace/github_c_makefile_star2_O0/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
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movb $0x0, -0xa9(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
callq 0x6aa40
movl %eax, -0x5c(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x14(%rcx), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x72e7d
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x79940
cmpl $0x0, %eax
je 0x72e6a
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xb0(%rbp)
cmpl $0x40, -0xb0(%rbp)
jl 0x72d93
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
movq %rax, -0xd8(%rbp)
jmp 0x72daa
movl -0xb0(%rbp), %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpb $0x0, -0xa9(%rbp)
jne 0x72dfb
movq -0x68(%rbp), %rax
movq (%rax), %rdx
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0xb0(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movl $0x11c, %edi            # imm = 0x11C
leaq 0x3d828(%rip), %rsi     # 0xb0615
movb $0x0, %al
callq 0xc5f0
movb $0x1, -0xa9(%rbp)
movq -0xa0(%rbp), %rax
andq -0xb8(%rbp), %rax
cmpq $0x0, %rax
jne 0x72e68
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x98(%rbp), %rsi
movl -0x2c(%rbp), %edx
addl $0x1, %edx
callq 0x78fd0
cmpl $0x0, %eax
je 0x72e32
jmp 0x733a8
movq -0x38(%rbp), %rdx
movq -0x98(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xb8(%rbp), %rax
orq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x72e6a
jmp 0x72e6c
movq -0x38(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x38(%rbp)
jmp 0x72d42
movl -0x2c(%rbp), %eax
movq -0x98(%rbp), %rcx
movw %ax, 0x2c(%rcx)
movzwl %ax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x98(%rbp), %rax
movl $0x4241, 0x28(%rax)     # imm = 0x4241
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq -0xa0(%rbp), %rcx
xorq $-0x1, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
orq %rdx, %rcx
andq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x68(%rbp), %rax
movswl 0x42(%rax), %eax
cmpl $0x3f, %eax
jl 0x72eea
movl $0x3f, %eax
movl %eax, -0xdc(%rbp)
jmp 0x72ef8
movq -0x68(%rbp), %rax
movswl 0x42(%rax), %eax
movl %eax, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
movl %eax, -0x84(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x72f4a
movq -0xa8(%rbp), %rax
movl -0x80(%rbp), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x72f3d
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x72f3f
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x72f0b
movq -0x18(%rbp), %rcx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
andq 0x50(%rcx), %rax
cmpq $0x0, %rax
je 0x72f76
movq -0x68(%rbp), %rax
movswl 0x42(%rax), %eax
subl $0x40, %eax
addl $0x1, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x98(%rbp), %rax
movl 0x28(%rax), %ecx
orl $0x41, %ecx
movl %ecx, 0x28(%rax)
movl $0x58, -0x44(%rbp)
movslq -0x2c(%rbp), %rcx
shlq $0x2, %rcx
movslq -0x44(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x44(%rbp)
movslq -0x2c(%rbp), %rcx
shlq $0x3, %rcx
movslq -0x44(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x44(%rbp)
movl -0x2c(%rbp), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x44(%rbp), %esi
callq 0x11ca0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x72fd9
jmp 0x733a8
movq -0x50(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rcx
addq $0x58, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rcx
movslq -0x2c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
movslq -0x2c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x50(%rbp), %rax
leaq 0x3d5f6(%rip), %rcx     # 0xb062f
movq %rcx, (%rax)
movl -0x2c(%rbp), %eax
movw %ax, %cx
movq -0x50(%rbp), %rax
movw %cx, 0x54(%rax)
movq -0x68(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x7c(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x731c3
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x79940
cmpl $0x0, %eax
je 0x731b0
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xbc(%rbp)
cmpl $0x40, -0xbc(%rbp)
jl 0x730c5
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
movq %rax, -0xe8(%rbp)
jmp 0x730dc
movl -0xbc(%rbp), %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xa0(%rbp), %rax
andq -0xc8(%rbp), %rax
cmpq $0x0, %rax
jne 0x731ae
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
orq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x38(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x7c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0xd0(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x6aa80
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0x7317e
movq -0x90(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
jmp 0x7318e
leaq 0x3ceb5(%rip), %rax     # 0xb003a
movq %rax, -0xf0(%rbp)
jmp 0x7318e
movq -0xf0(%rbp), %rdx
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x7c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x731b0
jmp 0x731b2
movq -0x38(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x38(%rbp)
jmp 0x73074
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x73232
movq -0xa8(%rbp), %rax
movl -0x80(%rbp), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x73225
movl -0x80(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x7c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x7c(%rbp), %rcx
leaq 0x3ce22(%rip), %rdx     # 0xb003a
movq %rdx, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x73227
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x731ca
movq -0x18(%rbp), %rcx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
andq 0x50(%rcx), %rax
cmpq $0x0, %rax
je 0x7329f
movl $0x3f, -0x80(%rbp)
movl -0x80(%rbp), %eax
movq -0x68(%rbp), %rcx
movswl 0x42(%rcx), %ecx
cmpl %ecx, %eax
jge 0x7329d
movl -0x80(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x7c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x7c(%rbp), %rcx
leaq 0x3cdb5(%rip), %rdx     # 0xb003a
movq %rdx, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x73251
jmp 0x7329f
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x67540
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movl 0x54(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x54(%rax)
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movq -0x70(%rbp), %r9
movl $0x32, %esi
xorl %r8d, %r8d
movl $0xfffffff0, (%rsp)     # imm = 0xFFFFFFF0
callq 0x5e1b0
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl $0x65, %esi
callq 0x5ecf0
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rdi
callq 0x66a70
movl %eax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl -0x78(%rbp), %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x67610
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x78(%rbp), %ecx
movl $0x67, %esi
callq 0x5e0c0
movq -0x58(%rbp), %rdi
movl $0x10, %esi
callq 0x5e540
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl -0x74(%rbp), %ecx
addl $0x1, %ecx
movl $0x7, %esi
callq 0x5e0c0
movq -0x58(%rbp), %rdi
movl $0x3, %esi
callq 0x5e540
movq -0x58(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x5e090
movq -0x8(%rbp), %rdi
movl -0x78(%rbp), %esi
callq 0x66ac0
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x5e090
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)