// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %rcx, 0x8(%rsp)
movq %rdx, %r15
movq 0x10(%rdi), %rbx
movl 0x1c(%rdx), %ebp
movl 0x44(%rdi), %r13d
decl %r13d
movl %r13d, 0x44(%rdi)
movq (%rdx), %r14
movzbl (%r8), %edx
movq %r8, 0x30(%rsp)
movl 0x4(%r8), %eax
movl %eax, 0x14(%rsp)
movq %rsi, 0x48(%rsp)
movq 0x20(%rsi), %r12
movl 0x14(%r15), %ecx
testl %ecx, %ecx
movq %rdi, 0x38(%rsp)
movq %rdx, 0x60(%rsp)
je 0x2db9a6
movl 0x10(%r15), %edx
movq %rbx, %rdi
movl $0xc, %esi
xorl %r8d, %r8d
callq 0x2b200c
movq %rbx, %rdi
movl $0xb, %esi
xorl %edx, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x2b200c
movl 0x14(%r15), %esi
movq %rbx, %rdi
callq 0x2c91a7
movq 0x60(%rsp), %rdx
movq 0x38(%rsp), %rdi
movl 0xc(%r15), %eax
movl %eax, 0x2c(%rsp)
cmpl $0xd, %edx
ja 0x2dba86
movl $0x2600, %eax           # imm = 0x2600
btl %edx, %eax
jae 0x2dba86
movq 0x30(%rsp), %rax
movl 0xc(%rax), %eax
movq %rax, 0x18(%rsp)
movl $0x0, 0x24(%rsp)
movl (%r14), %eax
subl 0x8(%r15), %eax
movq %rax, 0x58(%rsp)
testb $0x1, 0x24(%r15)
movq %r15, 0x70(%rsp)
movl %r13d, 0x44(%rsp)
jne 0x2dba45
movq %rbx, %rdi
movl $0x24, %esi
movl 0x2c(%rsp), %r14d
movl %r14d, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x2b200c
movl %eax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x1, %r15d
testl %edx, %edx
jle 0x2dba3d
movq %rbx, %rdi
movl $0x30, %esi
movl %r13d, %ecx
movl $0x1, %r8d
callq 0x2b200c
movl %r14d, %edx
jmp 0x2dbb6e
movl 0x34(%rdi), %r9d
movl 0x38(%rdi), %r13d
incl %r13d
movl %r13d, 0x38(%rdi)
leal 0x1(%r9), %eax
movl %eax, 0x34(%rdi)
cmpl $0x0, 0x14(%r15)
movl 0x2c(%rsp), %r14d
je 0x2dbaa9
movq %rbx, %rdi
movl $0x11, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r9, %r15
callq 0x2b200c
movq %r15, %r9
movl %eax, %r15d
jmp 0x2dbaac
movb 0x1f(%rdi), %al
testb %al, %al
je 0x2dbe59
decb %al
movb %al, 0x1f(%rdi)
movzbl %al, %eax
movl 0xd4(%rdi,%rax,4), %eax
movl %eax, 0x24(%rsp)
jmp 0x2dbe65
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movq 0x58(%rsp), %rcx
leal (%rax,%rcx), %r8d
incl %r8d
movq %rbx, %rdi
movl $0x72, %esi
movq %r9, 0x50(%rsp)
movl %r9d, %edx
movl %r13d, %ecx
callq 0x2b200c
testl %r15d, %r15d
je 0x2dbb11
movl 0x90(%rbx), %eax
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
je 0x2dbaf2
leaq 0x1ee398(%rip), %rcx    # 0x4c9e88
jmp 0x2dbb0e
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)
xorl %r15d, %r15d
movq %rbx, %rdi
movl $0x23, %esi
movl %r14d, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x2b200c
movl %eax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movl 0xc(%rax), %edx
testl %edx, %edx
jle 0x2dbb50
movq %rbx, %rdi
movl $0x30, %esi
movl 0x44(%rsp), %ecx
movl $0x1, %r8d
callq 0x2b200c
movq %rbx, %rdi
movl $0x7d, %esi
movl %r14d, %edx
movl %r13d, %ecx
movq 0x50(%rsp), %r14
movl %r14d, %r8d
callq 0x2b200c
movq %r14, %rdx
movq 0x58(%rsp), %rax
movq %r15, 0x50(%rsp)
leal (%r15,%rax), %r13d
decl %r13d
movq 0x8(%rsp), %rsi
movl %esi, %r15d
testl %esi, %esi
movl %ebp, 0x6c(%rsp)
movq %rbx, %r14
jle 0x2dbbb4
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
cmpw $0x1, 0x1c(%r12,%rcx)
adcl $0x0, %r13d
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x2dbba0
testl %esi, %esi
jle 0x2dbc14
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x48(%rsp)
leaq (%r15,%r15,2), %rax
leaq (%r12,%rax,8), %r12
addq $0x4, %r12
leaq -0x1(%r15), %rbx
movzwl (%r12), %eax
leal -0x1(%rax), %ecx
xorl %esi, %esi
testl %eax, %eax
sete %sil
cmovel %r13d, %ecx
subl %esi, %r13d
movq 0x48(%rsp), %rax
leal (%rax,%r15), %r8d
movq %r14, %rdi
movl $0x59, %esi
movq %rdx, %rbp
callq 0x2b200c
movq %rbp, %rdx
addq $-0x18, %r12
cmpq $0x1, %r15
movq %rbx, %r15
jg 0x2dbbd0
movq 0x60(%rsp), %rax
addl $-0x9, %eax
cmpl $0x6, %eax
movq %r14, %rbx
movl 0x44(%rsp), %r12d
movl 0x24(%rsp), %r13d
movq 0x18(%rsp), %r15
ja 0x2dbd5a
leaq 0xfec72(%rip), %rcx     # 0x3da8b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x50(%rsp), %rcx
addl 0x58(%rsp), %ecx
movq %rbx, %rdi
movl $0x59, %esi
movl %r15d, %r8d
callq 0x2b200c
movq %rbx, %rdi
movl $0x78, %esi
movl 0x14(%rsp), %ebp
movl %ebp, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x2b200c
movq %rbx, %rdi
movl $0x79, %esi
movl %ebp, %edx
movl %r15d, %ecx
movl %r13d, %r8d
callq 0x2b200c
movslq 0x90(%rbx), %rax
testq %rax, %rax
jle 0x2dbd8b
movq 0x88(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movw $0x8, -0x16(%rcx,%rax,8)
jmp 0x2dbd8b
movq 0x30(%rsp), %rax
movl 0x8(%rax), %r14d
movq 0x38(%rsp), %rcx
movb 0x1f(%rcx), %al
testb %al, %al
je 0x2dbeaf
decb %al
movb %al, 0x1f(%rcx)
movzbl %al, %eax
movl 0xd4(%rcx,%rax,4), %ebp
jmp 0x2dbeb7
movq 0x30(%rsp), %rax
movq 0x18(%rax), %r15
movq %rbx, %rdi
movl $0x5b, %esi
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %r14
movl %r14d, %ecx
movl %r13d, %r8d
callq 0x2b200c
movq %rbx, %rdi
movl %eax, %esi
movq %r15, %rdx
movq 0x18(%rsp), %r15
movl %r14d, %ecx
callq 0x285198
movq %rbx, %rdi
movl $0x82, %esi
movl 0x14(%rsp), %edx
movl %r13d, %ecx
movl %r15d, %r8d
callq 0x2b200c
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2dbd8b
movq 0x88(%rbx), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movq 0x8(%rsp), %rdx
movl %edx, 0x10(%rcx,%rax,8)
jmp 0x2dbd8b
movq 0x30(%rsp), %rax
movl 0x4(%rax), %edx
movq %rbx, %rdi
movl $0xe, %esi
xorl %ecx, %ecx
jmp 0x2dbd83
movq 0x30(%rsp), %rax
movl 0xc(%rax), %edx
movq %rbx, %rdi
movl $0x50, %esi
movq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x2b200c
incl 0x28(%rsp)
testl %r13d, %r13d
movl 0x6c(%rsp), %ebp
movq 0x70(%rsp), %r14
movq 0x38(%rsp), %rdx
je 0x2dbdfb
cmpl $0xb, 0x60(%rsp)
jne 0x2dbdb3
movq 0x8(%rsp), %rax
cmpl $0x1, %eax
jne 0x2dbdd4
testl %r15d, %r15d
je 0x2dbde3
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x2dbde3
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
leaq (%rdx,%rax,4), %rax
addq $0xd4, %rax
jmp 0x2dbde0
cmpl %eax, 0x28(%rdx)
jge 0x2dbde3
movl %eax, 0x28(%rdx)
leaq 0x2c(%rdx), %rax
movl %r15d, (%rax)
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x2dbdfb
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
movl %r13d, 0xd4(%rdx,%rax,4)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x2c91a7
xorl %eax, %eax
testb $0x1, 0x24(%r14)
sete %al
leal 0x3(%rax,%rax), %esi
movq %rbx, %rdi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %ecx
xorl %r8d, %r8d
callq 0x2b200c
movl 0x10(%r14), %edx
testl %edx, %edx
je 0x2dbe41
movq %rbx, %rdi
movl $0x41, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2b200c
movq %rbx, %rdi
movl %ebp, %esi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c91a7
movl 0x38(%rdi), %eax
incl %eax
movl %eax, 0x24(%rsp)
movl %eax, 0x38(%rdi)
movl %edx, %eax
andb $-0x3, %al
cmpb $0xc, %al
jne 0x2dbe91
movb 0x1f(%rdi), %al
testb %al, %al
je 0x2dbf30
decb %al
movb %al, 0x1f(%rdi)
movzbl %al, %eax
movl 0xd4(%rdi,%rax,4), %eax
movq %rax, 0x18(%rsp)
jmp 0x2dbf3d
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2c8275
movq 0x38(%rsp), %rdi
movq %rax, 0x18(%rsp)
jmp 0x2db9da
movl 0x38(%rcx), %ebp
incl %ebp
movl %ebp, 0x38(%rcx)
movq 0x8(%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 0x2b200c
movq %rbx, %rdi
testl %r14d, %r14d
js 0x2dbf1d
movl $0x82, %esi
movl 0x14(%rsp), %edx
movl %ebp, %ecx
movl %r15d, %r8d
callq 0x2b200c
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2dbd8b
movq 0x88(%rbx), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movl %r14d, 0x10(%rcx,%rax,8)
jmp 0x2dbd8b
movl $0x79, %esi
movl 0x14(%rsp), %edx
movl %ebp, %ecx
movl %r15d, %r8d
jmp 0x2dbd86
movl 0x38(%rdi), %eax
incl %eax
movq %rax, 0x18(%rsp)
movl %eax, 0x38(%rdi)
movq $0x0, 0x8(%rsp)
jmp 0x2db9da