// File: /workspace/github_c_makefile_star2_O0/ukontainer[P]sqlite-bench/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(v);  /* 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);
    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].zName?aOutEx[i].zName : aOutEx[i].zSpan));
    }
  }
  switch( eDest ){
    case SRT_Table:
    case SRT_EphemTab: {
      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
    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 -0x30(%rbp), %rdi
callq 0x46e490
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 (%rax), %rax
addq $0x8, %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x482f9e
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movl $0xc, %esi
callq 0x453790
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x46bed0
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x14(%rax), %esi
callq 0x46e530
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x44(%rbp)
cmpl $0x9, -0x54(%rbp)
je 0x482fba
cmpl $0xd, -0x54(%rbp)
je 0x482fba
cmpl $0xa, -0x54(%rbp)
jne 0x482fcd
movl $0x0, -0x60(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x482fe8
movq -0x8(%rbp), %rdi
callq 0x470ad0
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x46e5b0
movl %eax, -0x5c(%rbp)
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 0x4830d6
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 0x48304c
movq -0x30(%rbp), %rdi
movl $0x11, %esi
callq 0x46bb70
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 $0x74, %esi
callq 0x4537c0
cmpl $0x0, -0x40(%rbp)
je 0x483085
movq -0x30(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x46bba0
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x22, %esi
callq 0x453790
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x14(%rax), %esi
movl -0x38(%rbp), %edx
callq 0x485220
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x84(%rbp), %ecx
movl -0x6c(%rbp), %r8d
movl $0x7f, %esi
callq 0x4537c0
movl $0x0, -0x74(%rbp)
jmp 0x483110
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x23, %esi
callq 0x453790
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x14(%rax), %esi
movl -0x38(%rbp), %edx
callq 0x485220
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 0x483159
movq -0x80(%rbp), %rax
movslq -0x70(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x1c(%rax), %eax
cmpl $0x0, %eax
jne 0x48314c
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x48314e
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x483123
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jl 0x4831d7
movq -0x80(%rbp), %rax
movslq -0x70(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpw $0x0, 0x1c(%rax)
je 0x48319c
movq -0x80(%rbp), %rax
movslq -0x70(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x1c(%rax), %eax
subl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x4831ad
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 $0x5a, %esi
callq 0x4537c0
movl -0x70(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x483162
movl -0x54(%rbp), %eax
addl $-0xa, %eax
movl %eax, %ecx
movq %rcx, -0x90(%rbp)
subl $0x4, %eax
ja 0x483288
movq -0x90(%rbp), %rax
leaq 0xf683f(%rip), %rcx     # 0x579a3c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x60(%rbp), %ecx
movl $0x79, %esi
callq 0x453790
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x5c(%rbp), %ecx
movl -0x60(%rbp), %r8d
movl $0x7a, %esi
callq 0x4537c0
movq -0x30(%rbp), %rdi
movl $0x8, %esi
callq 0x411430
jmp 0x4832bf
movq -0x30(%rbp), %rdi
movl -0x5c(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x60(%rbp), %r8d
movq -0x28(%rbp), %rax
movq 0x10(%rax), %r9
movl -0x1c(%rbp), %eax
movl $0x66, %esi
movl %eax, (%rsp)
callq 0x46bbd0
movq -0x30(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x60(%rbp), %ecx
movl -0x5c(%rbp), %r8d
movl -0x1c(%rbp), %r9d
movl $0x85, %esi
callq 0x4113b0
jmp 0x4832bf
jmp 0x4832bf
cmpl $0x9, -0x54(%rbp)
jne 0x4832a8
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x1c(%rbp), %ecx
movl $0x51, %esi
callq 0x453790
jmp 0x4832bd
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl $0xe, %esi
callq 0x46e2d0
jmp 0x4832bf
cmpl $0x0, -0x60(%rbp)
je 0x4832f4
cmpl $0xb, -0x54(%rbp)
jne 0x4832dc
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x46ea10
jmp 0x4832e8
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x470060
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0x470060
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x46e530
movq -0x18(%rbp), %rax
movzbl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x483326
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl $0x3, %esi
callq 0x453790
jmp 0x48333a
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl $0x5, %esi
callq 0x453790
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x483359
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
movl $0x42, %esi
callq 0x46e2d0
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x46e530
addq $0xa0, %rsp
popq %rbp
retq
nop