// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: pushOntoSorter
static void pushOntoSorter(
  Parse *pParse,         /* Parser context */
  SortCtx *pSort,        /* Information about the ORDER BY clause */
  Select *pSelect,       /* The whole SELECT statement */
  int regData,           /* First register holding data to be sorted */
  int regOrigData,       /* First register holding data before packing */
  int nData,             /* Number of elements in the regData data array */
  int nPrefixReg         /* No. of reg prior to regData available for use */
){
  Vdbe *v = pParse->pVdbe;                         /* Stmt under construction */
  int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
  int nExpr = pSort->pOrderBy->nExpr;              /* No. of ORDER BY terms */
  int nBase = nExpr + bSeq + nData;                /* Fields in sorter record */
  int regBase;                                     /* Regs for sorter record */
  int regRecord = 0;                               /* Assembled sorter record */
  int nOBSat = pSort->nOBSat;                      /* ORDER BY terms to skip */
  int op;                            /* Opcode to add sorter record to sorter */
  int iLimit;                        /* LIMIT counter */
  int iSkip = 0;                     /* End of the sorter insert loop */

  assert( bSeq==0 || bSeq==1 );

  /* Three cases:
  **   (1) The data to be sorted has already been packed into a Record
  **       by a prior OP_MakeRecord.  In this case nData==1 and regData
  **       will be completely unrelated to regOrigData.
  **   (2) All output columns are included in the sort record.  In that
  **       case regData==regOrigData.
  **   (3) Some output columns are omitted from the sort record due to
  **       the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
  **       SQLITE_ECEL_OMITREF optimization, or due to the
  **       SortCtx.pDeferredRowLoad optimiation.  In any of these cases
  **       regOrigData is 0 to prevent this routine from trying to copy
  **       values that might not yet exist.
  */
  assert( nData==1 || regData==regOrigData || regOrigData==0 );

  if( nPrefixReg ){
    assert( nPrefixReg==nExpr+bSeq );
    regBase = regData - nPrefixReg;
  }else{
    regBase = pParse->nMem + 1;
    pParse->nMem += nBase;
  }
  assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
  iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
  pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
  sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
                          SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
  if( bSeq ){
    sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
  }
  if( nPrefixReg==0 && nData>0 ){
    sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
  }
  if( nOBSat>0 ){
    int regPrevKey;   /* The first nOBSat columns of the previous row */
    int addrFirst;    /* Address of the OP_IfNot opcode */
    int addrJmp;      /* Address of the OP_Jump opcode */
    VdbeOp *pOp;      /* Opcode that opens the sorter */
    int nKey;         /* Number of sorting key columns, including OP_Sequence */
    KeyInfo *pKI;     /* Original KeyInfo on the sorter table */

    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
    regPrevKey = pParse->nMem+1;
    pParse->nMem += pSort->nOBSat;
    nKey = nExpr - pSort->nOBSat + bSeq;
    if( bSeq ){
      addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
    }else{
      addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
    }
    VdbeCoverage(v);
    sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
    pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
    if( pParse->db->mallocFailed ) return;
    pOp->p2 = nKey + nData;
    pKI = pOp->p4.pKeyInfo;
    memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
    sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
    testcase( pKI->nAllField > pKI->nKeyField+2 );
    pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
                                           pKI->nAllField-pKI->nKeyField-1);
    pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */
    addrJmp = sqlite3VdbeCurrentAddr(v);
    sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
    pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
    pSort->regReturn = ++pParse->nMem;
    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
    sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
    if( iLimit ){
      sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
      VdbeCoverage(v);
    }
    sqlite3VdbeJumpHere(v, addrFirst);
    sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
    sqlite3VdbeJumpHere(v, addrJmp);
  }
  if( iLimit ){
    /* At this point the values for the new sorter entry are stored
    ** in an array of registers. They need to be composed into a record
    ** and inserted into the sorter if either (a) there are currently
    ** less than LIMIT+OFFSET items or (b) the new record is smaller than
    ** the largest record currently in the sorter. If (b) is true and there
    ** are already LIMIT+OFFSET items in the sorter, delete the largest
    ** entry before inserting the new one. This way there are never more
    ** than LIMIT+OFFSET items in the sorter.
    **
    ** If the new record does not need to be inserted into the sorter,
    ** jump to the next iteration of the loop. If the pSort->labelOBLopt
    ** value is not zero, then it is a label of where to jump.  Otherwise,
    ** just bypass the row insert logic.  See the header comment on the
    ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
    */
    int iCsr = pSort->iECursor;
    sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
    VdbeCoverage(v);
    sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
    iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
                                 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
    VdbeCoverage(v);
    sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
  }
  if( regRecord==0 ){
    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
  }
  if( pSort->sortFlags & SORTFLAG_UseSorter ){
    op = OP_SorterInsert;
  }else{
    op = OP_IdxInsert;
  }
  sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
                       regBase+nOBSat, nBase-nOBSat);
  if( iSkip ){
    sqlite3VdbeChangeP2(v, iSkip,
         pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, %eax
movl %ecx, %r8d
movq %rsi, %rbx
movq %rdi, %r13
movl 0x80(%rsp), %r14d
movzbl 0x24(%rsi), %r12d
andl $0x1, %r12d
movl %r12d, %ebp
xorl $0x1, %ebp
movq (%rsi), %rsi
movl (%rsi), %edi
movq %rdi, 0x30(%rsp)
addl %edi, %ebp
leal (%r9,%rbp), %edi
movl 0x8(%rbx), %ecx
movq %rcx, 0x28(%rsp)
testl %r14d, %r14d
je 0x27e11c
movl %r8d, %r15d
subl %r14d, %r15d
jmp 0x27e12a
movl 0x38(%r13), %ecx
leal 0x1(%rcx), %r15d
addl %edi, %ecx
movl %ecx, 0x38(%r13)
movl 0xc(%rdx), %ecx
testl %ecx, %ecx
movq %rdx, 0x38(%rsp)
movl %edi, 0x4(%rsp)
movl %r8d, 0x8(%rsp)
movq %r9, 0x10(%rsp)
je 0x27e148
incl %ecx
jmp 0x27e14b
movl 0x8(%rdx), %ecx
movl %ecx, 0xc(%rsp)
movq 0x10(%r13), %rcx
movq %rcx, 0x20(%rsp)
movl 0x44(%r13), %ecx
decl %ecx
movl %ecx, 0x44(%r13)
movl %ecx, 0x1c(%rbx)
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
leal 0x1(,%rcx,4), %r8d
movq %r13, %rdi
movl %r15d, %edx
movl %eax, %ecx
callq 0x2664ba
testb %r12b, %r12b
jne 0x27e19f
movl 0xc(%rbx), %edx
movq 0x30(%rsp), %rax
leal (%r15,%rax), %ecx
pushq $0x77
popq %rsi
movq 0x20(%rsp), %rdi
callq 0x24d51a
testl %r14d, %r14d
setne %al
movq 0x10(%rsp), %rcx
testl %ecx, %ecx
setle %dl
orb %al, %dl
jne 0x27e1c4
addl %r15d, %ebp
movq %r13, %rdi
movl 0x8(%rsp), %esi
movl %ebp, %edx
callq 0x275256
movl $0x0, 0x10(%rsp)
movl $0x0, %ecx
cmpl $0x0, 0x28(%rsp)
movq 0x38(%rsp), %rdx
jle 0x27e34e
movq %r13, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl 0x4(%rsp), %r8d
callq 0x27e437
movl %eax, 0x8(%rsp)
movl 0x38(%r13), %r14d
movl 0x8(%rbx), %ebp
leal (%r14,%rbp), %eax
incl %r14d
movl %eax, 0x38(%r13)
testb %r12b, %r12b
je 0x27e216
movl 0xc(%rbx), %edx
pushq $0x71
jmp 0x27e221
movq 0x30(%rsp), %rax
leal (%r15,%rax), %edx
pushq $0x14
popq %rsi
movq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x265d5c
movl %eax, 0x1c(%rsp)
movl 0x8(%rbx), %r8d
pushq $0x56
popq %rsi
movq %r12, %rdi
movl %r14d, %edx
movl %r15d, %ecx
callq 0x24d51f
movq (%r13), %rax
cmpb $0x0, 0x61(%rax)
jne 0x27e428
movl 0x18(%rbx), %esi
movq %r12, %rdi
callq 0x26468f
movq %rax, 0x40(%rsp)
movl 0x4(%rsp), %ecx
subl %ebp, %ecx
movl %ecx, 0x8(%rax)
movq 0x10(%rax), %rbp
movq 0x18(%rbp), %rdi
movzwl 0x6(%rbp), %edx
xorl %esi, %esi
callq 0x17380
pushq $-0x1
popq %rsi
pushq $-0x9
popq %rcx
movq %r12, %rdi
movq %rbp, %rdx
callq 0x2270ee
movq (%rbx), %rsi
movzwl 0x8(%rbp), %eax
movzwl 0x6(%rbp), %ecx
notl %ecx
addl %eax, %ecx
movq %r13, %rdi
movq 0x28(%rsp), %rdx
callq 0x270980
movq 0x40(%rsp), %rcx
movq %rax, 0x10(%rcx)
movl 0x90(%r12), %ebp
leal 0x1(%rbp), %edx
pushq $0x10
popq %rsi
movq %r12, %rdi
xorl %ecx, %ecx
movl %edx, %r8d
callq 0x24d51f
movl 0x44(%r13), %ecx
decl %ecx
movl %ecx, 0x44(%r13)
movl %ecx, 0x14(%rbx)
movl 0x38(%r13), %edx
incl %edx
movl %edx, 0x38(%r13)
movl %edx, 0x10(%rbx)
pushq $0xc
popq %rsi
movq %r12, %rdi
callq 0x24d51a
movl 0xc(%rbx), %edx
movq %r12, %rdi
movl $0x8a, %esi
callq 0x265d5c
movl 0xc(%rsp), %edx
testl %edx, %edx
je 0x27e31e
movl 0x1c(%rbx), %ecx
pushq $0x14
popq %rsi
movq %r12, %rdi
callq 0x24d51a
movq %r12, %rdi
movl 0x1c(%rsp), %esi
callq 0x2645f8
movl 0x8(%rbx), %ecx
movq %r13, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x275256
movq %r12, %rdi
movl %ebp, %esi
callq 0x2645f8
movq 0x38(%rsp), %rdx
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %eax
testl %eax, %eax
movq 0x20(%rsp), %r14
je 0x27e3c0
movl 0xc(%rbx), %ebp
movl %ecx, %r12d
movl 0x90(%r14), %ecx
addl $0x4, %ecx
pushq $0x31
popq %rsi
movq %r14, %rdi
movl %eax, %edx
callq 0x24d51a
pushq $0x21
popq %rsi
movq %r14, %rdi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x24d51a
movq 0x28(%rsp), %rax
leal (%r15,%rax), %r8d
movq 0x30(%rsp), %r9
subl %eax, %r9d
pushq $0x26
popq %rsi
movq %r14, %rdi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x226fc2
movl %eax, 0x10(%rsp)
pushq $0x7a
popq %rsi
movq %r14, %rdi
movl %ebp, %edx
callq 0x265d5c
movl %r12d, %ecx
movq 0x38(%rsp), %rdx
testl %ecx, %ecx
jne 0x27e3d9
movq %r13, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
movl 0x4(%rsp), %r8d
callq 0x27e437
movl %eax, %ecx
movzbl 0x24(%rbx), %esi
andl $0x1, %esi
orl $0x82, %esi
movl 0xc(%rbx), %edx
movq 0x28(%rsp), %rax
addl %eax, %r15d
movl 0x4(%rsp), %r9d
subl %eax, %r9d
movq %r14, %rdi
movl %r15d, %r8d
callq 0x226fc2
cmpl $0x0, 0x10(%rsp)
je 0x27e428
movl 0x20(%rbx), %ebx
testl %ebx, %ebx
jne 0x27e419
movl 0x90(%r14), %ebx
movq %r14, %rdi
movl 0x10(%rsp), %esi
callq 0x26468f
movl %ebx, 0x8(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq