// File: /workspace/github_c_makefile_star2_O1/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, %rbp
movq %rcx, 0x38(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r15
movq 0x10(%rdi), %rbx
movl 0x1c(%rdx), %r14d
movq %rbx, %rdi
callq 0x450901
movl %eax, 0x1c(%rsp)
movq (%r12), %rdi
movzbl (%rbp), %esi
movq %rbp, 0x28(%rsp)
movl 0x4(%rbp), %eax
movl %eax, 0x20(%rsp)
movq %r13, 0x40(%rsp)
movq (%r13), %rbp
movl %r14d, %r13d
movl 0x14(%r12), %ecx
testl %ecx, %ecx
movq %rsi, 0x48(%rsp)
je 0x4618fc
movl 0x10(%r12), %edx
movq %rdi, %r14
movq %rbx, %rdi
movl $0xc, %esi
xorl %r8d, %r8d
callq 0x43bae0
movq %rbx, %rdi
movl $0xb, %esi
xorl %edx, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x43bae0
movq %r14, %rdi
movq 0x48(%rsp), %rsi
movq 0x18(%rbx), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0x4618fc
movslq 0x14(%r12), %rcx
movl 0xb8(%rbx), %edx
notq %rcx
movl %edx, (%rax,%rcx,4)
movl 0xc(%r12), %eax
movl %eax, 0xc(%rsp)
cmpl $0xd, %esi
ja 0x46192b
movl $0x2600, %eax           # imm = 0x2600
btl %esi, %eax
jae 0x46192b
movq 0x28(%rsp), %rax
movl 0x8(%rax), %eax
movq %rax, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
jmp 0x461970
movb 0x1f(%r15), %al
movq %rdi, %r14
testb %al, %al
je 0x46194d
decb %al
movb %al, 0x1f(%r15)
movzbl %al, %eax
movl 0xc4(%r15,%rax,4), %eax
movl %eax, 0x18(%rsp)
jmp 0x46195b
movl 0x38(%r15), %eax
incl %eax
movl %eax, 0x18(%rsp)
movl %eax, 0x38(%r15)
movq %r15, %rdi
movq 0x38(%rsp), %rsi
callq 0x450994
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movl (%rdi), %r14d
subl 0x8(%r12), %r14d
testb $0x1, 0x24(%r12)
movq %r12, 0x60(%rsp)
movq %r15, 0x58(%rsp)
jne 0x4619df
movq %rbx, %rdi
movl $0x23, %esi
movl 0xc(%rsp), %r15d
movl %r15d, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x43bae0
movl %eax, 0x24(%rsp)
movq 0x40(%rsp), %rax
movl 0x14(%rax), %edx
movl $0x1, %r12d
testl %edx, %edx
jle 0x4619d2
movq %rbx, %rdi
movl $0x2f, %esi
movl 0x1c(%rsp), %ecx
movl $0x1, %r8d
callq 0x43bae0
movl %r15d, %eax
movq %rax, 0x30(%rsp)
jmp 0x461ad5
movl 0x34(%r15), %edx
movq %r15, %rcx
movl 0x38(%r15), %r15d
incl %r15d
movl %r15d, 0x38(%rcx)
leal 0x1(%rdx), %eax
movl %eax, 0x34(%rcx)
cmpl $0x0, 0x14(%r12)
je 0x461a1e
movq %rbx, %rdi
movl $0x11, %esi
movq %rdx, %r12
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x43bae0
movq %r12, %rdx
movl %eax, %r12d
jmp 0x461a21
xorl %r12d, %r12d
movq 0x38(%rsp), %rax
leal (%rax,%r14), %r8d
incl %r8d
movq %rbx, %rdi
movl $0x74, %esi
movq %rdx, 0x30(%rsp)
movl %r15d, %ecx
callq 0x43bae0
testl %r12d, %r12d
je 0x461a7b
movl 0xb8(%rbx), %eax
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
je 0x461a5f
leaq 0x1433ab(%rip), %rcx    # 0x5a4e08
jmp 0x461a78
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), %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x43bae0
movl %eax, 0x24(%rsp)
movq 0x40(%rsp), %rax
movl 0x14(%rax), %edx
testl %edx, %edx
jle 0x461abc
movq %rbx, %rdi
movl $0x2f, %esi
movl 0x1c(%rsp), %ecx
movl $0x1, %r8d
callq 0x43bae0
movq %rbx, %rdi
movl $0x7f, %esi
movl 0xc(%rsp), %edx
movl %r15d, %ecx
movq 0x30(%rsp), %r8
callq 0x43bae0
addl %r12d, %r14d
decl %r14d
movq 0x38(%rsp), %rdx
movl %edx, %r15d
testl %edx, %edx
movl %r13d, 0x54(%rsp)
movq %rbx, %r13
jle 0x461b0b
movq %r15, %rax
shlq $0x5, %rax
xorl %ecx, %ecx
cmpw $0x1, 0x24(%rbp,%rcx)
adcl $0x0, %r14d
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x461af8
testl %edx, %edx
movq 0x30(%rsp), %r12
jle 0x461b6e
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x40(%rsp)
movq %r15, %rax
shlq $0x5, %rax
addq %rax, %rbp
addq $0x4, %rbp
leaq -0x1(%r15), %rbx
movzwl (%rbp), %eax
leal -0x1(%rax), %ecx
xorl %esi, %esi
testl %eax, %eax
sete %sil
cmovel %r14d, %ecx
subl %esi, %r14d
movq 0x40(%rsp), %rax
leal (%rax,%r15), %r8d
movq %r13, %rdi
movl $0x5a, %esi
movl %r12d, %edx
callq 0x43bae0
addq $-0x20, %rbp
cmpq $0x1, %r15
movq %rbx, %r15
jg 0x461b2e
movq 0x48(%rsp), %rax
addl $-0x9, %eax
cmpl $0x5, %eax
movq %r13, %rbx
movq 0x38(%rsp), %r12
movl 0x18(%rsp), %r13d
ja 0x461bec
leaq 0xea6f9(%rip), %rcx     # 0x54c288
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movl $0x79, %esi
movl 0x20(%rsp), %ebp
movl %ebp, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x43bae0
movq %rbx, %rdi
movl $0x7a, %esi
movl %ebp, %edx
movq 0x10(%rsp), %rcx
movl %r13d, %r8d
callq 0x43bae0
movslq 0xb8(%rbx), %rax
testq %rax, %rax
jle 0x461c85
movq 0x68(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movw $0x8, -0x16(%rcx,%rax,8)
jmp 0x461c85
movq 0x28(%rsp), %rax
movl 0x4(%rax), %edx
movq %rbx, %rdi
movl $0xe, %esi
xorl %ecx, %ecx
jmp 0x461c7d
movq 0x28(%rsp), %rax
movq 0x10(%rax), %r15
movq %rbx, %rdi
movl $0x66, %esi
movq 0x10(%rsp), %r14
movl %r14d, %edx
movl %r12d, %ecx
movl %r13d, %r8d
callq 0x43bae0
movq %rbx, %rdi
movl %eax, %esi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x40e507
movq %rbx, %rdi
movl $0x85, %esi
movl 0x20(%rsp), %edx
movl %r13d, %ecx
movl %r14d, %r8d
callq 0x43bae0
movq (%rbx), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x461c85
movq 0x68(%rbx), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movl %r12d, 0x10(%rcx,%rax,8)
jmp 0x461c85
movq 0x28(%rsp), %rax
movl 0x8(%rax), %edx
movq %rbx, %rdi
movl $0x51, %esi
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x43bae0
testl %r13d, %r13d
movl 0x54(%rsp), %ebp
movq 0x60(%rsp), %r14
movq 0x58(%rsp), %rsi
movl 0xc(%rsp), %edx
movl 0x24(%rsp), %r8d
je 0x461cfe
cmpl $0xb, 0x48(%rsp)
jne 0x461cae
cmpl $0x1, %r12d
jne 0x461cd1
cmpl $0x0, 0x10(%rsp)
je 0x461ce6
movzbl 0x1f(%rsi), %eax
cmpq $0x7, %rax
ja 0x461ce6
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rsi)
leaq (%rsi,%rax,4), %rax
addq $0xc4, %rax
jmp 0x461cdf
cmpl %r12d, 0x28(%rsi)
jge 0x461ce6
movl %r12d, 0x28(%rsi)
leaq 0x2c(%rsi), %rax
movq 0x10(%rsp), %rcx
movl %ecx, (%rax)
movzbl 0x1f(%rsi), %eax
cmpq $0x7, %rax
ja 0x461cfe
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rsi)
movl %r13d, 0xc4(%rsi,%rax,4)
incl %r8d
movq 0x18(%rbx), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0x461d20
movl 0x1c(%rsp), %esi
notl %esi
movl 0xb8(%rbx), %ecx
movslq %esi, %rsi
movl %ecx, (%rax,%rsi,4)
xorl %eax, %eax
testb $0x1, 0x24(%r14)
sete %al
leal 0x3(,%rax,2), %esi
movq %rbx, %rdi
movl %r8d, %ecx
xorl %r8d, %r8d
callq 0x43bae0
movl 0x10(%r14), %edx
testl %edx, %edx
je 0x461d59
movq %rbx, %rdi
movl $0x42, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x43bae0
movq 0x18(%rbx), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0x461d74
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