// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r15
movq %rcx, (%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, 0x50(%rsp)
movq 0x10(%rdi), %rbx
movl 0x1c(%rdx), %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rbx), %rdi
callq 0x44fbb5
movl %eax, %ebp
movq (%r12), %r14
movzbl (%r15), %esi
movq %r15, 0x40(%rsp)
movl 0x4(%r15), %eax
movl %eax, 0x2c(%rsp)
movq %r13, 0x48(%rsp)
movq (%r13), %r13
movl 0x14(%r12), %ecx
testl %ecx, %ecx
movq %rsi, 0x20(%rsp)
je 0x460c26
movl 0x10(%r12), %edx
movq %rbx, %rdi
movl $0xc, %esi
xorl %r8d, %r8d
callq 0x43a65f
movq %rbx, %rdi
movl $0xb, %esi
xorl %edx, %edx
movl 0xc(%rsp), %ecx
xorl %r8d, %r8d
callq 0x43a65f
movq 0x20(%rsp), %rsi
movq 0x18(%rbx), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0x460c26
movslq 0x14(%r12), %rcx
movl 0xb8(%rbx), %edx
notq %rcx
movl %edx, (%rax,%rcx,4)
movl 0xc(%r12), %r15d
cmpl $0xd, %esi
movl %r15d, 0x8(%rsp)
ja 0x460c56
movl $0x2600, %eax           # imm = 0x2600
btl %esi, %eax
jae 0x460c56
movq 0x40(%rsp), %rax
movl 0x8(%rax), %eax
movq %rax, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
jmp 0x460c96
movq 0x50(%rsp), %rdi
movb 0x1f(%rdi), %al
testb %al, %al
je 0x460c77
decb %al
movb %al, 0x1f(%rdi)
movzbl %al, %eax
movl 0xc4(%rdi,%rax,4), %eax
movl %eax, 0x1c(%rsp)
jmp 0x460c83
movl 0x38(%rdi), %eax
incl %eax
movl %eax, 0x1c(%rsp)
movl %eax, 0x38(%rdi)
movq (%rsp), %rsi
callq 0x44fc51
movq %rax, 0x10(%rsp)
movl 0x8(%rsp), %r15d
movl (%r14), %r14d
subl 0x8(%r12), %r14d
testb $0x1, 0x24(%r12)
movq %rbx, 0x58(%rsp)
movq %r12, 0x60(%rsp)
movl %ebp, 0x30(%rsp)
jne 0x460d07
movq %rbx, %rdi
movl $0x23, %esi
movl %r15d, %edx
movl 0xc(%rsp), %ecx
xorl %r8d, %r8d
callq 0x43a65f
movl %eax, 0x34(%rsp)
movq 0x48(%rsp), %rax
movl 0x14(%rax), %edx
movl $0x1, %r12d
testl %edx, %edx
jle 0x460cf6
movq %rbx, %rdi
movl $0x2f, %esi
movl %ebp, %ecx
movl $0x1, %r8d
callq 0x43a65f
movl %r15d, %eax
movq %rax, 0x38(%rsp)
movq (%rsp), %r15
jmp 0x460df9
movq 0x50(%rsp), %rcx
movl 0x34(%rcx), %edx
movl 0x38(%rcx), %ebp
incl %ebp
movl %ebp, 0x38(%rcx)
leal 0x1(%rdx), %eax
movl %eax, 0x34(%rcx)
cmpl $0x0, 0x14(%r12)
je 0x460d44
movq %rbx, %rdi
movl $0x11, %esi
movq %rdx, %r15
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x43a65f
movq %r15, %rdx
movl %eax, %r12d
jmp 0x460d47
xorl %r12d, %r12d
movq (%rsp), %r15
leal (%r15,%r14), %r8d
incl %r8d
movq %rbx, %rdi
movl $0x74, %esi
movq %rdx, 0x38(%rsp)
movl %ebp, %ecx
callq 0x43a65f
testl %r12d, %r12d
movl 0x8(%rsp), %edx
je 0x460da3
movl 0xb8(%rbx), %eax
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
je 0x460d87
leaq 0x144083(%rip), %rcx    # 0x5a4e08
jmp 0x460da0
leal -0x1(%rax), %ecx
testl %r12d, %r12d
cmovnsl %r12d, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x68(%rbx), %rcx
movl %eax, 0x8(%rcx)
xorl %r12d, %r12d
movq %rbx, %rdi
movl $0x22, %esi
movl 0xc(%rsp), %ecx
xorl %r8d, %r8d
callq 0x43a65f
movl %eax, 0x34(%rsp)
movq 0x48(%rsp), %rax
movl 0x14(%rax), %edx
testl %edx, %edx
jle 0x460de1
movq %rbx, %rdi
movl $0x2f, %esi
movl 0x30(%rsp), %ecx
movl $0x1, %r8d
callq 0x43a65f
movq %rbx, %rdi
movl $0x7f, %esi
movl 0x8(%rsp), %edx
movl %ebp, %ecx
movq 0x38(%rsp), %r8
callq 0x43a65f
testl %r15d, %r15d
movq 0x58(%rsp), %r15
jle 0x460e97
leal (%r14,%r12), %ebp
decl %ebp
movq (%rsp), %rdx
movl %edx, %r14d
movq %r14, %rax
shlq $0x5, %rax
xorl %ecx, %ecx
cmpw $0x1, 0x24(%r13,%rcx)
adcl $0x0, %ebp
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x460e1d
testl %edx, %edx
movq 0x58(%rsp), %r15
movq 0x38(%rsp), %r12
jle 0x460e97
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x48(%rsp)
movq %r14, %rax
shlq $0x5, %rax
addq %rax, %r13
addq $0x4, %r13
leaq -0x1(%r14), %rbx
movzwl (%r13), %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 %r15, %rdi
movl $0x5a, %esi
movl %r12d, %edx
callq 0x43a65f
addq $-0x20, %r13
cmpq $0x1, %r14
movq %rbx, %r14
ja 0x460e58
movq 0x20(%rsp), %rsi
leal -0x9(%rsi), %eax
cmpl $0x5, %eax
movq %r15, %rbx
movq (%rsp), %r15
movl 0x1c(%rsp), %r12d
ja 0x460f19
leaq 0xeb399(%rip), %rcx     # 0x54c250
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movl $0x79, %esi
movl 0x2c(%rsp), %ebp
movl %ebp, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x43a65f
movq %rbx, %rdi
movl $0x7a, %esi
movl %ebp, %edx
movq 0x10(%rsp), %rcx
movl %r12d, %r8d
callq 0x43a65f
movq 0x20(%rsp), %rsi
movslq 0xb8(%rbx), %rax
testq %rax, %rax
jle 0x460fc4
movq 0x68(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movw $0x8, -0x16(%rcx,%rax,8)
jmp 0x460fc4
movq 0x40(%rsp), %rax
movl 0x4(%rax), %edx
movq %rbx, %rdi
movl $0xe, %esi
xorl %ecx, %ecx
jmp 0x460fb7
movq 0x40(%rsp), %rax
movq 0x10(%rax), %r15
movq %rbx, %rdi
movl $0x66, %esi
movq 0x10(%rsp), %r14
movl %r14d, %edx
movq (%rsp), %rcx
movl %r12d, %r8d
callq 0x43a65f
movq %rbx, %rdi
movl %eax, %esi
movq %r15, %rdx
movq (%rsp), %r15
movl %r15d, %ecx
callq 0x40e765
movq %rbx, %rdi
movl $0x85, %esi
movl 0x2c(%rsp), %edx
movl %r12d, %ecx
movl %r14d, %r8d
callq 0x43a65f
movq 0x20(%rsp), %rsi
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x460fc4
movq 0x68(%rbx), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movl %r15d, 0x10(%rcx,%rax,8)
jmp 0x460fc4
movq 0x40(%rsp), %rax
movl 0x8(%rax), %edx
movq %rbx, %rdi
movl $0x51, %esi
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x43a65f
movq 0x20(%rsp), %rsi
testl %r12d, %r12d
movl 0xc(%rsp), %ebp
movq 0x60(%rsp), %r14
movq 0x50(%rsp), %rdx
movl 0x34(%rsp), %r8d
je 0x461037
cmpl $0xb, %esi
jne 0x460fe7
cmpl $0x1, %r15d
jne 0x46100a
cmpl $0x0, 0x10(%rsp)
je 0x46101f
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x46101f
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
leaq (%rdx,%rax,4), %rax
addq $0xc4, %rax
jmp 0x461018
cmpl %r15d, 0x28(%rdx)
jge 0x46101f
movl %r15d, 0x28(%rdx)
leaq 0x2c(%rdx), %rax
movq 0x10(%rsp), %rcx
movl %ecx, (%rax)
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x461037
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
movl %r12d, 0xc4(%rdx,%rax,4)
incl %r8d
movq 0x18(%rbx), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0x461059
movl 0x30(%rsp), %edx
notl %edx
movl 0xb8(%rbx), %ecx
movslq %edx, %rdx
movl %ecx, (%rax,%rdx,4)
xorl %eax, %eax
testb $0x1, 0x24(%r14)
sete %al
leal 0x3(,%rax,2), %esi
movq %rbx, %rdi
movl 0x8(%rsp), %edx
movl %r8d, %ecx
xorl %r8d, %r8d
callq 0x43a65f
movl 0x10(%r14), %edx
testl %edx, %edx
je 0x461096
movq %rbx, %rdi
movl $0x42, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x43a65f
movq 0x18(%rbx), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0x4610b1
notl %ebp
movl 0xb8(%rbx), %ecx
movslq %ebp, %rdx
movl %ecx, (%rax,%rdx,4)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq