// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
callq 0x42d540
movl %eax, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
addq $0x8, %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x4454df
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movl $0xc, %esi
callq 0x410100
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x429db0
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x14(%rax), %esi
callq 0x42dc90
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x44(%rbp)
cmpl $0x9, -0x54(%rbp)
je 0x4454fb
cmpl $0xd, -0x54(%rbp)
je 0x4454fb
cmpl $0xa, -0x54(%rbp)
jne 0x44550e
movl $0x0, -0x60(%rbp)
movq -0x28(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x44554c
movq -0x8(%rbp), %rdi
callq 0x42f190
movl %eax, -0x60(%rbp)
cmpl $0xc, -0x54(%rbp)
je 0x445526
cmpl $0xe, -0x54(%rbp)
jne 0x44553b
movq -0x8(%rbp), %rdi
callq 0x42f190
movl %eax, -0x5c(%rbp)
movl $0x0, -0x1c(%rbp)
jmp 0x44554a
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x42cb30
movl %eax, -0x5c(%rbp)
jmp 0x44554c
movq -0x50(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x8(%rcx), %eax
movl %eax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x44563a
movq -0x8(%rbp), %rcx
movl 0x38(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x38(%rcx)
movl %eax, -0x84(%rbp)
movq -0x8(%rbp), %rcx
movl 0x34(%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x34(%rcx)
movl %eax, -0x6c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x4455b0
movq -0x30(%rbp), %rdi
movl $0x11, %esi
callq 0x429a40
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movl -0x6c(%rbp), %edx
movl -0x84(%rbp), %ecx
movl -0x68(%rbp), %r8d
addl $0x1, %r8d
addl -0x1c(%rbp), %r8d
addl -0x78(%rbp), %r8d
movl $0x72, %esi
callq 0x410130
cmpl $0x0, -0x40(%rbp)
je 0x4455e9
movq -0x30(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x429a70
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x23, %esi
callq 0x410100
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
movl -0x38(%rbp), %edx
callq 0x447e40
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x84(%rbp), %ecx
movl -0x6c(%rbp), %r8d
movl $0x7d, %esi
callq 0x410130
movl $0x0, -0x74(%rbp)
jmp 0x445674
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x24, %esi
callq 0x410100
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
movl -0x38(%rbp), %edx
callq 0x447e40
movl -0x44(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl $0x1, -0x74(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x68(%rbp), %eax
addl -0x74(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4456bd
movq -0x80(%rbp), %rax
movslq -0x70(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzwl 0x14(%rax), %eax
cmpl $0x0, %eax
jne 0x4456b0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x4456b2
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x445687
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jl 0x44573b
movq -0x80(%rbp), %rax
movslq -0x70(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpw $0x0, 0x14(%rax)
je 0x445700
movq -0x80(%rbp), %rax
movslq -0x70(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzwl 0x14(%rax), %eax
subl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x445711
movl -0x64(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x64(%rbp)
movl %eax, -0x88(%rbp)
movq -0x30(%rbp), %rdi
movl -0x6c(%rbp), %edx
movl -0x88(%rbp), %ecx
movl -0x5c(%rbp), %r8d
addl -0x70(%rbp), %r8d
movl $0x59, %esi
callq 0x410130
movl -0x70(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x4456c6
movl -0x54(%rbp), %eax
addl $-0xa, %eax
movl %eax, %ecx
movq %rcx, -0x98(%rbp)
subl $0x5, %eax
ja 0x4458b3
movq -0x98(%rbp), %rax
leaq 0x160d47(%rip), %rcx    # 0x5a64a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x30(%rbp), %rdi
movl -0x6c(%rbp), %edx
movl -0x68(%rbp), %ecx
addl -0x74(%rbp), %ecx
movl -0x5c(%rbp), %r8d
movl $0x59, %esi
callq 0x410130
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x60(%rbp), %ecx
movl $0x78, %esi
callq 0x410100
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x5c(%rbp), %ecx
movl -0x60(%rbp), %r8d
movl $0x79, %esi
callq 0x410130
movq -0x30(%rbp), %rdi
movl $0x8, %esi
callq 0x3cb9f0
jmp 0x4458ea
movq -0x30(%rbp), %rdi
movl -0x5c(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x60(%rbp), %r8d
movq -0x28(%rbp), %rax
movq 0x18(%rax), %r9
movl -0x1c(%rbp), %eax
movl $0x5b, %esi
movl %eax, (%rsp)
callq 0x429aa0
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x60(%rbp), %ecx
movl -0x5c(%rbp), %r8d
movl -0x1c(%rbp), %r9d
movl $0x82, %esi
callq 0x3cb970
jmp 0x4458ea
jmp 0x4458ea
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x8c(%rbp)
movq -0x8(%rbp), %rdi
callq 0x42f190
movl %eax, -0x90(%rbp)
movq -0x30(%rbp), %rdi
movl -0x5c(%rbp), %edx
cmpl $0x0, -0x8c(%rbp)
setl %al
andb $0x1, %al
movzbl %al, %eax
addl %eax, %edx
movl -0x1c(%rbp), %ecx
cmpl $0x0, -0x8c(%rbp)
setl %al
andb $0x1, %al
movzbl %al, %eax
subl %eax, %ecx
movl -0x90(%rbp), %r8d
movl $0x5b, %esi
callq 0x410130
cmpl $0x0, -0x8c(%rbp)
jge 0x44588f
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x90(%rbp), %ecx
movl -0x5c(%rbp), %r8d
movl $0x79, %esi
callq 0x410130
jmp 0x4458b1
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x90(%rbp), %ecx
movl -0x5c(%rbp), %r8d
movl -0x8c(%rbp), %r9d
movl $0x82, %esi
callq 0x3cb970
jmp 0x4458ea
cmpl $0x9, -0x54(%rbp)
jne 0x4458d3
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0xc(%rax), %edx
movl -0x1c(%rbp), %ecx
movl $0x50, %esi
callq 0x410100
jmp 0x4458e8
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl $0xe, %esi
callq 0x42ba70
jmp 0x4458ea
cmpl $0x0, -0x60(%rbp)
je 0x44591f
cmpl $0xb, -0x54(%rbp)
jne 0x445907
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x42d0c0
jmp 0x445913
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x42e560
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0x42e560
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x42dc90
movq -0x18(%rbp), %rax
movzbl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x445951
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl $0x3, %esi
callq 0x410100
jmp 0x445965
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl $0x5, %esi
callq 0x410100
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x445984
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
movl $0x41, %esi
callq 0x42ba70
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x42dc90
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)