// File: /workspace/github_c_makefile_star2_O1/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, -0x3c(%rbp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
movq %rdx, %rdi
callq 0x4c4bf
movl %eax, %r14d
movq %rbx, %rdi
callq 0x4c4bf
movl %eax, -0x50(%rbp)
movq %r15, -0x78(%rbp)
movq 0x48(%r15), %rcx
movzbl (%r12), %eax
movq %rax, -0x48(%rbp)
movq %r12, -0x58(%rbp)
movl 0x4(%r12), %eax
movl %eax, -0x4c(%rbp)
movq %rcx, -0x68(%rbp)
movl 0x4(%rcx), %r12d
movb 0x1f(%r13), %al
testb %al, %al
je 0x559df
decb %al
movb %al, 0x1f(%r13)
movzbl %al, %eax
movl 0x28(%r13,%rax,4), %ecx
jmp 0x559e9
movl 0x58(%r13), %ecx
incl %ecx
movl %ecx, 0x58(%r13)
cmpl $0xa, -0x48(%rbp)
movl %ecx, -0x2c(%rbp)
je 0x559f8
cmpl $0x5, -0x48(%rbp)
jne 0x55a1c
movl 0x54(%r13), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x54(%r13)
movq %rbx, %rdi
movl $0x35, %esi
movq %rdx, -0x60(%rbp)
movl -0x3c(%rbp), %r8d
callq 0x45b9e
xorl %eax, %eax
jmp 0x55a46
movb 0x1f(%r13), %al
testb %al, %al
je 0x55a34
decb %al
movb %al, 0x1f(%r13)
movzbl %al, %eax
movl 0x28(%r13,%rax,4), %eax
jmp 0x55a3e
movl 0x58(%r13), %eax
incl %eax
movl %eax, 0x58(%r13)
movq $0x0, -0x60(%rbp)
movl %eax, -0x38(%rbp)
movq -0x78(%rbp), %r15
testb $0x40, 0xa(%r15)
jne 0x55a9c
movq %rbx, %rdi
movl $0x64, %esi
movl %r12d, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %eax, -0x34(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
movl -0x50(%rbp), %edx
callq 0x5697d
movq -0x68(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movq %rbx, %rdi
movl $0x29, %esi
movl %r12d, %edx
movl -0x2c(%rbp), %r8d
callq 0x45b9e
jmp 0x55b49
movl 0x54(%r13), %edx
movq %rdx, -0x80(%rbp)
movl %r14d, -0x30(%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 0x45b9e
movq %rbx, %rdi
movl $0x63, %esi
movl %r12d, %edx
movl -0x30(%rbp), %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %eax, -0x34(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
movl -0x50(%rbp), %edx
callq 0x5697d
movq %rbx, %rdi
movl $0x51, %esi
movl %r12d, %edx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x68(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movq %rbx, %rdi
movl $0x29, %esi
movq -0x80(%rbp), %rdx
movl -0x2c(%rbp), %r8d
callq 0x45b9e
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x55b45
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x20, -0x15(%rax,%rcx,8)
movl -0x30(%rbp), %r14d
movq -0x48(%rbp), %rax
addl $-0x8, %eax
cmpl $0x2, %eax
movl %r12d, -0x6c(%rbp)
jb 0x55bcb
cmpl $0x6, -0x48(%rbp)
je 0x55c26
cmpl $0x7, -0x48(%rbp)
jne 0x55c46
movq -0x58(%rbp), %r15
incq %r15
movq %rbx, %rdi
movl $0x2b, %esi
movl -0x2c(%rbp), %r12d
movl %r12d, %edx
movl $0x1, %ecx
movl -0x38(%rbp), %r8d
callq 0x45b9e
movq %rbx, %rdi
movl %eax, %esi
movq %r15, %rdx
movl $0x1, %ecx
callq 0x11975
movq %r13, %rdi
movl %r12d, %esi
movl $0x1, %edx
callq 0x4e50d
movq %rbx, %rdi
movl $0x67, %esi
movl -0x4c(%rbp), %edx
movl -0x38(%rbp), %ecx
xorl %r8d, %r8d
callq 0x45b9e
jmp 0x55c3d
movq %rbx, %rdi
movl $0x41, %esi
movl -0x4c(%rbp), %r12d
movl %r12d, %edx
movl -0x38(%rbp), %r15d
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq %rbx, %rdi
movl $0x42, %esi
movl %r12d, %edx
movl -0x2c(%rbp), %r12d
movl %r12d, %ecx
movl %r15d, %r8d
callq 0x45b9e
movq 0x8(%rbx), %rax
testq %rax, %rax
movl -0x34(%rbp), %r15d
je 0x55cf1
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x8, -0x15(%rax,%rcx,8)
jmp 0x55cf1
movq %r13, %rdi
movl -0x2c(%rbp), %r12d
movl %r12d, %esi
movl -0x4c(%rbp), %edx
movl $0x1, %ecx
callq 0x552b9
movl -0x34(%rbp), %r15d
jmp 0x55cf1
movl %r14d, -0x30(%rbp)
cmpl $0x0, -0x3c(%rbp)
movl -0x34(%rbp), %r15d
jle 0x55c9a
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 0x45b9e
testl %r14d, %r14d
jne 0x55c91
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x55c91
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x20, -0x15(%rax,%rcx,8)
incl %r14d
cmpl %r14d, -0x3c(%rbp)
jne 0x55c57
cmpl $0x5, -0x48(%rbp)
jne 0x55cd0
movq -0x58(%rbp), %r14
movl 0x8(%r14), %edx
movq %rbx, %rdi
movl $0x1e, %esi
movl -0x3c(%rbp), %r12d
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl 0x8(%r14), %esi
movq %r13, %rdi
movl %r12d, %edx
callq 0x4e50d
jmp 0x55ce9
movq -0x58(%rbp), %rax
movl 0x4(%rax), %edx
movq %rbx, %rdi
movl $0x11, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl -0x30(%rbp), %r14d
movl -0x2c(%rbp), %r12d
testl %r12d, %r12d
movl -0x38(%rbp), %edi
je 0x55d30
movzbl 0x1f(%r13), %eax
cmpq $0x7, %rax
ja 0x55d30
xorl %ecx, %ecx
cmpl %r12d, 0x84(%r13,%rcx)
je 0x55d2a
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x55d06
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%r13)
movl %r12d, 0x28(%r13,%rax,4)
jmp 0x55d30
movb $0x1, 0x7c(%r13,%rcx)
testl %edi, %edi
movq -0x78(%rbp), %rsi
movl -0x6c(%rbp), %edx
je 0x55d72
movzbl 0x1f(%r13), %eax
cmpq $0x7, %rax
ja 0x55d72
xorl %ecx, %ecx
cmpl %edi, 0x84(%r13,%rcx)
je 0x55d6c
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x55d48
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%r13)
movl %edi, 0x28(%r13,%rax,4)
jmp 0x55d72
movb $0x1, 0x7c(%r13,%rcx)
incl %r15d
movl -0x50(%rbp), %edi
testl %edi, %edi
jns 0x55d8f
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x55d8f
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 0x45b9e
testl %r14d, %r14d
jns 0x55dc9
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x55dc9
notl %r14d
movl 0x34(%rbx), %ecx
movl %r14d, %edx
movl %ecx, (%rax,%rdx,4)
cmpl $0xa, -0x48(%rbp)
je 0x55dd5
cmpl $0x5, -0x48(%rbp)
jne 0x55df9
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 0x45b9e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq