// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: generateSortTail
static void generateSortTail(
  Parse *pParse,    /* Parsing context */
  Select *p,        /* The SELECT statement */
  Vdbe *v,          /* Generate code into this VDBE */
  int nColumn,      /* Number of columns of data */
  SelectDest *pDest /* Write the sorted results here */
){
  int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
  int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
  int addr;
  int iTab;
  int pseudoTab = 0;
  ExprList *pOrderBy = p->pOrderBy;

  int eDest = pDest->eDest;
  int iParm = pDest->iSDParm;

  int regRow;
  int regRowid;

  iTab = pOrderBy->iECursor;
  regRow = sqlite3GetTempReg(pParse);
  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
    pseudoTab = pParse->nTab++;
    sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
    regRowid = 0;
  }else{
    regRowid = sqlite3GetTempReg(pParse);
  }
  if( p->selFlags & SF_UseSorter ){
    int regSortOut = ++pParse->nMem;
    int ptab2 = pParse->nTab++;
    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
    codeOffset(v, p, addrContinue);
    sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
    sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
    sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
  }else{
    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
    codeOffset(v, p, addrContinue);
    sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
  }
  switch( eDest ){
    case SRT_Table:
    case SRT_EphemTab: {
      testcase( eDest==SRT_Table );
      testcase( eDest==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==1 );
      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
                        &pDest->affSdst, 1);
      sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
      break;
    }
    case SRT_Mem: {
      assert( nColumn==1 );
      sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
      /* The LIMIT clause will terminate the loop for us */
      break;
    }
#endif
    default: {
      int i;
      assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
      testcase( eDest==SRT_Output );
      testcase( eDest==SRT_Coroutine );
      for(i=0; i<nColumn; i++){
        assert( regRow!=pDest->iSdst+i );
        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
        if( i==0 ){
          sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
        }
      }
      if( eDest==SRT_Output ){
        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
      }else{
        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
      }
      break;
    }
  }
  sqlite3ReleaseTempReg(pParse, regRow);
  sqlite3ReleaseTempReg(pParse, regRowid);

  /* The bottom of the loop
  */
  sqlite3VdbeResolveLabel(v, addrContinue);
  if( p->selFlags & SF_UseSorter ){
    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
  }else{
    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
  }
  sqlite3VdbeResolveLabel(v, addrBreak);
  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
    sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r12
movl %ecx, -0x38(%rbp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
movq %rdx, %rdi
callq 0x4c5eb
movl %eax, %r14d
movq %rbx, %rdi
callq 0x4c5eb
movl %eax, -0x4c(%rbp)
movq %r15, -0x78(%rbp)
movq 0x48(%r15), %rcx
movzbl (%r12), %eax
movq %rax, -0x40(%rbp)
movq %r12, -0x58(%rbp)
movl 0x4(%r12), %eax
movl %eax, -0x44(%rbp)
movq %rcx, -0x68(%rbp)
movl 0x4(%rcx), %r12d
movb 0x1f(%r13), %al
testb %al, %al
je 0x55a10
decb %al
movb %al, 0x1f(%r13)
movzbl %al, %ecx
movl 0x28(%r13,%rcx,4), %ecx
jmp 0x55a1c
movl 0x58(%r13), %ecx
incl %ecx
movl %ecx, 0x58(%r13)
xorl %eax, %eax
cmpl $0xa, -0x40(%rbp)
movl %ecx, -0x30(%rbp)
je 0x55a2b
cmpl $0x5, -0x40(%rbp)
jne 0x55a4f
movl 0x54(%r13), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x54(%r13)
movq %rbx, %rdi
movl $0x35, %esi
movq %rdx, -0x60(%rbp)
movl -0x38(%rbp), %r8d
callq 0x45f15
xorl %eax, %eax
jmp 0x55a75
testb %al, %al
je 0x55a63
decb %al
movb %al, 0x1f(%r13)
movzbl %al, %eax
movl 0x28(%r13,%rax,4), %eax
jmp 0x55a6d
movl 0x58(%r13), %eax
incl %eax
movl %eax, 0x58(%r13)
movq $0x0, -0x60(%rbp)
movl %eax, -0x48(%rbp)
movq -0x78(%rbp), %r15
testb $0x40, 0xa(%r15)
jne 0x55acb
movq %rbx, %rdi
movl $0x64, %esi
movl %r12d, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, -0x34(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
movl -0x4c(%rbp), %edx
callq 0x569a6
movq -0x68(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movq %rbx, %rdi
movl $0x29, %esi
movl %r12d, %edx
movl -0x30(%rbp), %r8d
callq 0x45f15
jmp 0x55b78
movl 0x54(%r13), %edx
movq %rdx, -0x80(%rbp)
movl %r14d, -0x2c(%rbp)
movl 0x58(%r13), %r14d
incl %r14d
movl %r14d, 0x58(%r13)
leal 0x1(%rdx), %eax
movl %eax, 0x54(%r13)
movq -0x68(%rbp), %rax
movl (%rax), %r8d
addl $0x2, %r8d
movq %rbx, %rdi
movl $0x35, %esi
movl %r14d, %ecx
callq 0x45f15
movq %rbx, %rdi
movl $0x63, %esi
movl %r12d, %edx
movl -0x2c(%rbp), %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, -0x34(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
movl -0x4c(%rbp), %edx
callq 0x569a6
movq %rbx, %rdi
movl $0x51, %esi
movl %r12d, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq -0x68(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movq %rbx, %rdi
movl $0x29, %esi
movq -0x80(%rbp), %rdx
movl -0x30(%rbp), %r8d
callq 0x45f15
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x55b74
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x20, -0x15(%rax,%rcx,8)
movl -0x2c(%rbp), %r14d
movq -0x40(%rbp), %rax
addl $-0x8, %eax
cmpl $0x2, %eax
movl %r12d, -0x6c(%rbp)
jb 0x55c05
cmpl $0x6, -0x40(%rbp)
je 0x55c60
cmpl $0x7, -0x40(%rbp)
movl %r14d, -0x2c(%rbp)
jne 0x55c80
movq -0x58(%rbp), %r14
incq %r14
movq %rbx, %rdi
movl $0x2b, %esi
movl -0x30(%rbp), %r12d
movl %r12d, %edx
movl $0x1, %ecx
movl -0x48(%rbp), %r15d
movl %r15d, %r8d
callq 0x45f15
movq %rbx, %rdi
movl %eax, %esi
movq %r14, %rdx
movl -0x2c(%rbp), %r14d
movl $0x1, %ecx
callq 0x11a9f
movq %r13, %rdi
movl %r12d, %esi
movl $0x1, %edx
callq 0x4e564
movq %rbx, %rdi
movl $0x67, %esi
movl -0x44(%rbp), %edx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x45f15
jmp 0x55c77
movq %rbx, %rdi
movl $0x41, %esi
movl -0x44(%rbp), %r12d
movl %r12d, %edx
movl -0x48(%rbp), %r15d
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq %rbx, %rdi
movl $0x42, %esi
movl %r12d, %edx
movl -0x30(%rbp), %r12d
movl %r12d, %ecx
movl %r15d, %r8d
callq 0x45f15
movq 0x8(%rbx), %rax
testq %rax, %rax
movl -0x34(%rbp), %r15d
je 0x55d27
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x8, -0x15(%rax,%rcx,8)
jmp 0x55d27
movq %r13, %rdi
movl -0x30(%rbp), %r12d
movl %r12d, %esi
movl -0x44(%rbp), %edx
movl $0x1, %ecx
callq 0x552f9
movl -0x34(%rbp), %r15d
jmp 0x55d27
cmpl $0x0, -0x38(%rbp)
movl -0x34(%rbp), %r15d
jle 0x55cd0
xorl %r14d, %r14d
movq -0x58(%rbp), %rax
movl 0x8(%rax), %r8d
addl %r14d, %r8d
movq %rbx, %rdi
movl $0x29, %esi
movq -0x60(%rbp), %rdx
movl %r14d, %ecx
callq 0x45f15
testl %r14d, %r14d
jne 0x55cc7
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x55cc7
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x20, -0x15(%rax,%rcx,8)
incl %r14d
cmpl %r14d, -0x38(%rbp)
jne 0x55c8d
cmpl $0x5, -0x40(%rbp)
jne 0x55d06
movq -0x58(%rbp), %r14
movl 0x8(%r14), %edx
movq %rbx, %rdi
movl $0x1e, %esi
movl -0x38(%rbp), %r12d
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl 0x8(%r14), %esi
movq %r13, %rdi
movl %r12d, %edx
callq 0x4e564
jmp 0x55d1f
movq -0x58(%rbp), %rax
movl 0x4(%rax), %edx
movq %rbx, %rdi
movl $0x11, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl -0x2c(%rbp), %r14d
movl -0x30(%rbp), %r12d
testl %r12d, %r12d
movl -0x48(%rbp), %edi
je 0x55d66
movzbl 0x1f(%r13), %eax
cmpq $0x7, %rax
ja 0x55d66
xorl %ecx, %ecx
cmpl %r12d, 0x84(%r13,%rcx)
je 0x55d60
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x55d3c
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%r13)
movl %r12d, 0x28(%r13,%rax,4)
jmp 0x55d66
movb $0x1, 0x7c(%r13,%rcx)
testl %edi, %edi
movq -0x78(%rbp), %rsi
movl -0x6c(%rbp), %edx
je 0x55da8
movzbl 0x1f(%r13), %eax
cmpq $0x7, %rax
ja 0x55da8
xorl %ecx, %ecx
cmpl %edi, 0x84(%r13,%rcx)
je 0x55da2
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x55d7e
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%r13)
movl %edi, 0x28(%r13,%rax,4)
jmp 0x55da8
movb $0x1, 0x7c(%r13,%rcx)
incl %r15d
movl -0x4c(%rbp), %edi
testl %edi, %edi
jns 0x55dc5
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x55dc5
notl %edi
movl 0x34(%rbx), %ecx
movl %edi, %edi
movl %ecx, (%rax,%rdi,4)
movzwl 0xa(%rsi), %eax
xorl %ecx, %ecx
testb $0x40, %al
sete %cl
leal 0x5(,%rcx,2), %esi
movq %rbx, %rdi
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x45f15
testl %r14d, %r14d
jns 0x55dff
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x55dff
notl %r14d
movl 0x34(%rbx), %ecx
movl %r14d, %edx
movl %ecx, (%rax,%rdx,4)
cmpl $0xa, -0x40(%rbp)
je 0x55e0b
cmpl $0x5, -0x40(%rbp)
jne 0x55e2f
movq %rbx, %rdi
movl $0x36, %esi
movq -0x60(%rbp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45f15
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq