// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: generateSortTail
static void generateSortTail(
  Parse *pParse,    /* Parsing context */
  Select *p,        /* The SELECT statement */
  SortCtx *pSort,   /* Information on the ORDER BY clause */
  int nColumn,      /* Number of columns of data */
  SelectDest *pDest /* Write the sorted results here */
){
  Vdbe *v = pParse->pVdbe;                     /* The prepared statement */
  int addrBreak = pSort->labelDone;            /* Jump here to exit loop */
  int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
  int addr;                       /* Top of output loop. Jump for Next. */
  int addrOnce = 0;
  int iTab;
  ExprList *pOrderBy = pSort->pOrderBy;
  int eDest = pDest->eDest;
  int iParm = pDest->iSDParm;
  int regRow;
  int regRowid;
  int iCol;
  int nKey;                       /* Number of key columns in sorter record */
  int iSortTab;                   /* Sorter cursor to read from */
  int i;
  int bSeq;                       /* True if sorter record includes seq. no. */
  int nRefKey = 0;
  struct ExprList_item *aOutEx = p->pEList->a;

  assert( addrBreak<0 );
  if( pSort->labelBkOut ){
    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
    sqlite3VdbeGoto(v, addrBreak);
    sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
  }

#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  /* Open any cursors needed for sorter-reference expressions */
  for(i=0; i<pSort->nDefer; i++){
    Table *pTab = pSort->aDefer[i].pTab;
    int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
    sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
    nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
  }
#endif

  iTab = pSort->iECursor;
  if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
    regRowid = 0;
    regRow = pDest->iSdst;
  }else{
    regRowid = sqlite3GetTempReg(pParse);
    if( eDest==SRT_EphemTab || eDest==SRT_Table ){
      regRow = sqlite3GetTempReg(pParse);
      nColumn = 0;
    }else{
      regRow = sqlite3GetTempRange(pParse, nColumn);
    }
  }
  nKey = pOrderBy->nExpr - pSort->nOBSat;
  if( pSort->sortFlags & SORTFLAG_UseSorter ){
    int regSortOut = ++pParse->nMem;
    iSortTab = pParse->nTab++;
    if( pSort->labelBkOut ){
      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
    }
    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
        nKey+1+nColumn+nRefKey);
    if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
    VdbeCoverage(v);
    codeOffset(v, p->iOffset, addrContinue);
    sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
    bSeq = 0;
  }else{
    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
    codeOffset(v, p->iOffset, addrContinue);
    iSortTab = iTab;
    bSeq = 1;
  }
  for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
    if( aOutEx[i].bSorterRef ) continue;
#endif
    if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
  }
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  if( pSort->nDefer ){
    int iKey = iCol+1;
    int regKey = sqlite3GetTempRange(pParse, nRefKey);

    for(i=0; i<pSort->nDefer; i++){
      int iCsr = pSort->aDefer[i].iCsr;
      Table *pTab = pSort->aDefer[i].pTab;
      int nKey = pSort->aDefer[i].nKey;

      sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
      if( HasRowid(pTab) ){
        sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
        sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
            sqlite3VdbeCurrentAddr(v)+1, regKey);
      }else{
        int k;
        int iJmp;
        assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
        for(k=0; k<nKey; k++){
          sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
        }
        iJmp = sqlite3VdbeCurrentAddr(v);
        sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
        sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
        sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
      }
    }
    sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
  }
#endif
  for(i=nColumn-1; i>=0; i--){
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
    if( aOutEx[i].bSorterRef ){
      sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
    }else
#endif
    {
      int iRead;
      if( aOutEx[i].u.x.iOrderByCol ){
        iRead = aOutEx[i].u.x.iOrderByCol-1;
      }else{
        iRead = iCol--;
      }
      sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
      VdbeComment((v, "%s", aOutEx[i].zEName));
    }
  }
  switch( eDest ){
    case SRT_Table:
    case SRT_EphemTab: {
      sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow);
      sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
      sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
      break;
    }
#ifndef SQLITE_OMIT_SUBQUERY
    case SRT_Set: {
      assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
                        pDest->zAffSdst, nColumn);
      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
      break;
    }
    case SRT_Mem: {
      /* The LIMIT clause will terminate the loop for us */
      break;
    }
#endif
    case SRT_Upfrom: {
      int i2 = pDest->iSDParm2;
      int r1 = sqlite3GetTempReg(pParse);
      sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
      if( i2<0 ){
        sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, regRow);
      }else{
        sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regRow, i2);
      }
      break;
    }
    default: {
      assert( eDest==SRT_Output || eDest==SRT_Coroutine );
      testcase( eDest==SRT_Output );
      testcase( eDest==SRT_Coroutine );
      if( eDest==SRT_Output ){
        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
      }else{
        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
      }
      break;
    }
  }
  if( regRowid ){
    if( eDest==SRT_Set ){
      sqlite3ReleaseTempRange(pParse, regRow, nColumn);
    }else{
      sqlite3ReleaseTempReg(pParse, regRow);
    }
    sqlite3ReleaseTempReg(pParse, regRowid);
  }
  /* The bottom of the loop
  */
  sqlite3VdbeResolveLabel(v, addrContinue);
  if( pSort->sortFlags & SORTFLAG_UseSorter ){
    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
  }else{
    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
  }
  if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
  sqlite3VdbeResolveLabel(v, addrBreak);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, (%rsp)
movq %rdx, %r15
movq 0x10(%rdi), %rbx
movl 0x1c(%rdx), %r14d
movl 0x44(%rdi), %ebp
decl %ebp
movl %ebp, 0x44(%rdi)
movq (%rdx), %r9
movzbl (%r8), %r13d
movq %r8, 0x30(%rsp)
movl 0x4(%r8), %eax
movl %eax, 0xc(%rsp)
movq %rsi, 0x28(%rsp)
movq 0x20(%rsi), %r12
movl 0x14(%rdx), %ecx
testl %ecx, %ecx
movq %rdi, 0x38(%rsp)
movl %ebp, 0x18(%rsp)
je 0x2d219f
movl 0x10(%r15), %edx
movq %rbx, %rdi
movl $0xc, %esi
xorl %r8d, %r8d
movq %r9, %rbp
callq 0x2a83e1
movq %rbx, %rdi
movl $0xb, %esi
xorl %edx, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movl 0x14(%r15), %esi
movq %rbx, %rdi
callq 0x2bfa39
movq %rbp, %r9
movl 0x18(%rsp), %ebp
movq 0x38(%rsp), %rdi
movl %r14d, 0x24(%rsp)
movl 0xc(%r15), %eax
movl %eax, 0x20(%rsp)
cmpl $0xd, %r13d
movq %r13, 0x40(%rsp)
ja 0x2d2284
movl $0x2600, %eax           # imm = 0x2600
btl %r13d, %eax
jae 0x2d2284
movq 0x30(%rsp), %rax
movl 0xc(%rax), %eax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movl (%r9), %eax
subl 0x8(%r15), %eax
movq %rax, 0x48(%rsp)
testb $0x1, 0x24(%r15)
movq %rbx, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movl %r13d, 0x54(%rsp)
jne 0x2d224b
movq %rbx, %rdi
movl $0x24, %esi
movl 0x20(%rsp), %r13d
movl %r13d, %edx
movl 0x24(%rsp), %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movl %eax, 0x1c(%rsp)
movq 0x28(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x1, %r14d
testl %edx, %edx
jle 0x2d2243
movq %rbx, %rdi
movl $0x30, %esi
movl %ebp, %ecx
movl $0x1, %r8d
callq 0x2a83e1
movl %r13d, %edx
jmp 0x2d2394
movl 0x34(%rdi), %r9d
movl 0x38(%rdi), %ebp
incl %ebp
movl %ebp, 0x38(%rdi)
leal 0x1(%r9), %eax
movl %eax, 0x34(%rdi)
cmpl $0x0, 0x14(%r15)
je 0x2d22d2
movq %rbx, %rdi
movl $0x11, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r9, %r14
callq 0x2a83e1
movq %r14, %r9
movl %eax, %r15d
jmp 0x2d22d5
movb 0x1f(%rdi), %al
testb %al, %al
je 0x2d2680
leal -0x1(%rax), %ecx
movb %cl, 0x1f(%rdi)
movzbl %cl, %edx
movl 0xd4(%rdi,%rdx,4), %r13d
movq 0x40(%rsp), %rdx
andb $-0x3, %dl
cmpb $0xc, %dl
jne 0x2d26b0
testb %cl, %cl
je 0x2d2696
addb $-0x2, %al
movb %al, 0x1f(%rdi)
movzbl %al, %eax
movl 0xd4(%rdi,%rax,4), %eax
movq %rax, 0x10(%rsp)
jmp 0x2d26a3
xorl %r15d, %r15d
movq (%rsp), %rax
movq 0x48(%rsp), %rcx
leal (%rax,%rcx), %r8d
incl %r8d
movq %rbx, %rdi
movl $0x72, %esi
movq %r9, %r14
movl %r9d, %edx
movl %ebp, %ecx
callq 0x2a83e1
testl %r15d, %r15d
je 0x2d2336
movl 0x90(%rbx), %eax
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
je 0x2d2317
leaq 0x1ecb73(%rip), %rcx    # 0x4bee88
jmp 0x2d2333
leal -0x1(%rax), %ecx
testl %r15d, %r15d
cmovnsl %r15d, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x88(%rbx), %rcx
movl %eax, 0x8(%rcx)
movq %rbx, %rdi
movl $0x23, %esi
movl 0x20(%rsp), %r15d
movl %r15d, %edx
movl 0x24(%rsp), %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movl %eax, 0x1c(%rsp)
movq 0x28(%rsp), %rax
movl 0xc(%rax), %edx
testl %edx, %edx
jle 0x2d2379
movq %rbx, %rdi
movl $0x30, %esi
movl 0x18(%rsp), %ecx
movl $0x1, %r8d
callq 0x2a83e1
movq %rbx, %rdi
movl $0x7d, %esi
movl %r15d, %edx
movl %ebp, %ecx
movl %r14d, %r8d
callq 0x2a83e1
movq %r14, %rdx
xorl %r14d, %r14d
addl 0x48(%rsp), %r14d
movq %r14, 0x28(%rsp)
cmpl $0x0, (%rsp)
movq 0x58(%rsp), %r13
jle 0x2d2440
movq 0x28(%rsp), %rax
leal -0x1(%rax), %ebp
movq (%rsp), %rsi
movl %esi, %r14d
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
cmpw $0x1, 0x1c(%r12,%rcx)
adcl $0x0, %ebp
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x2d23ca
testl %esi, %esi
movq 0x58(%rsp), %r13
jle 0x2d2440
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x48(%rsp)
leaq (%r14,%r14,2), %rax
leaq (%r12,%rax,8), %r12
addq $0x4, %r12
leaq -0x1(%r14), %rbx
movzwl (%r12), %eax
leal -0x1(%rax), %ecx
xorl %esi, %esi
testl %eax, %eax
sete %sil
cmovel %ebp, %ecx
subl %esi, %ebp
movq 0x48(%rsp), %rax
leal (%rax,%r14), %r8d
movq %r13, %rdi
movl $0x59, %esi
movq %rdx, %r15
callq 0x2a83e1
movq %r15, %rdx
addq $-0x18, %r12
cmpq $0x1, %r14
movq %rbx, %r14
ja 0x2d23fe
movq 0x40(%rsp), %rax
addl $-0x9, %eax
cmpl $0x6, %eax
movq %r13, %rbx
movl 0x24(%rsp), %r12d
movl 0x54(%rsp), %r13d
movq 0x10(%rsp), %r15
ja 0x2d2580
leaq 0xfecce(%rip), %rcx     # 0x3d1138
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movl $0x59, %esi
movq 0x28(%rsp), %rcx
movl %r15d, %r8d
callq 0x2a83e1
movq %rbx, %rdi
movl $0x78, %esi
movl 0xc(%rsp), %ebp
movl %ebp, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movq %rbx, %rdi
movl $0x79, %esi
movl %ebp, %edx
movl %r15d, %ecx
movl %r13d, %r8d
callq 0x2a83e1
movslq 0x90(%rbx), %rax
testq %rax, %rax
jle 0x2d25b0
movq 0x88(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movw $0x8, -0x16(%rcx,%rax,8)
jmp 0x2d25b0
movq 0x30(%rsp), %rax
movl 0x8(%rax), %r14d
movq 0x38(%rsp), %rcx
movb 0x1f(%rcx), %al
testb %al, %al
je 0x2d26d9
decb %al
movb %al, 0x1f(%rcx)
movzbl %al, %eax
movl 0xd4(%rcx,%rax,4), %ebp
jmp 0x2d26e1
movq 0x30(%rsp), %rax
movq 0x18(%rax), %r15
movq %rbx, %rdi
movl $0x5b, %esi
movq 0x10(%rsp), %rdx
movq (%rsp), %r14
movl %r14d, %ecx
movl %r13d, %r8d
callq 0x2a83e1
movq %rbx, %rdi
movl %eax, %esi
movq %r15, %rdx
movq 0x10(%rsp), %r15
movl %r14d, %ecx
callq 0x27b778
movq %rbx, %rdi
movl $0x82, %esi
movl 0xc(%rsp), %edx
movl %r13d, %ecx
movl %r15d, %r8d
callq 0x2a83e1
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2d25b0
movq 0x88(%rbx), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movq (%rsp), %rdx
movl %edx, 0x10(%rcx,%rax,8)
jmp 0x2d25b0
movq 0x30(%rsp), %rax
movl 0x4(%rax), %edx
movq %rbx, %rdi
movl $0xe, %esi
xorl %ecx, %ecx
jmp 0x2d25a8
movq 0x30(%rsp), %rax
movl 0xc(%rax), %edx
movq %rbx, %rdi
movl $0x50, %esi
movq (%rsp), %rcx
xorl %r8d, %r8d
callq 0x2a83e1
incl 0x1c(%rsp)
testl %r13d, %r13d
movq 0x60(%rsp), %r14
movl 0x18(%rsp), %esi
movq 0x38(%rsp), %rdx
movl 0x20(%rsp), %ebp
je 0x2d2623
cmpl $0xb, 0x40(%rsp)
jne 0x2d25db
movq (%rsp), %rax
cmpl $0x1, %eax
jne 0x2d25fc
testl %r15d, %r15d
je 0x2d260b
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x2d260b
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
leaq (%rdx,%rax,4), %rax
addq $0xd4, %rax
jmp 0x2d2608
cmpl %eax, 0x28(%rdx)
jge 0x2d260b
movl %eax, 0x28(%rdx)
leaq 0x2c(%rdx), %rax
movl %r15d, (%rax)
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x2d2623
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
movl %r13d, 0xd4(%rdx,%rax,4)
movq %rbx, %rdi
callq 0x2bfa39
xorl %eax, %eax
testb $0x1, 0x24(%r14)
sete %al
leal 0x3(,%rax,2), %esi
movq %rbx, %rdi
movl %ebp, %edx
movl 0x1c(%rsp), %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movl 0x10(%r14), %edx
testl %edx, %edx
je 0x2d2667
movq %rbx, %rdi
movl $0x41, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movq %rbx, %rdi
movl %r12d, %esi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2bfa39
movl 0x38(%rdi), %r13d
incl %r13d
movl %r13d, 0x38(%rdi)
movq 0x40(%rsp), %rax
andb $-0x3, %al
cmpb $0xc, %al
jne 0x2d26b0
movl 0x38(%rdi), %eax
incl %eax
movq %rax, 0x10(%rsp)
movl %eax, 0x38(%rdi)
movq $0x0, (%rsp)
jmp 0x2d21da
movq 0x38(%rsp), %rdi
movq (%rsp), %rsi
movq %rbx, %r14
movq %r9, %rbx
callq 0x2beb15
movq %rbx, %r9
movq %r14, %rbx
movq 0x38(%rsp), %rdi
movq %rax, 0x10(%rsp)
jmp 0x2d21da
movl 0x38(%rcx), %ebp
incl %ebp
movl %ebp, 0x38(%rcx)
movq (%rsp), %rax
movl %r14d, %edx
shrl $0x1f, %edx
movl %eax, %ecx
subl %edx, %ecx
addl %r15d, %edx
movq %rbx, %rdi
movl $0x5b, %esi
movl %ebp, %r8d
callq 0x2a83e1
movq %rbx, %rdi
testl %r14d, %r14d
js 0x2d2746
movl $0x82, %esi
movl 0xc(%rsp), %edx
movl %ebp, %ecx
movl %r15d, %r8d
callq 0x2a83e1
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2d25b0
movq 0x88(%rbx), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movl %r14d, 0x10(%rcx,%rax,8)
jmp 0x2d25b0
movl $0x79, %esi
movl 0xc(%rsp), %edx
movl %ebp, %ecx
movl %r15d, %r8d
jmp 0x2d25ab