// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: generateOutputSubroutine
static int generateOutputSubroutine(
  Parse *pParse,          /* Parsing context */
  Select *p,              /* The SELECT statement */
  SelectDest *pIn,        /* Coroutine supplying data */
  SelectDest *pDest,      /* Where to send the data */
  int regReturn,          /* The return address register */
  int regPrev,            /* Previous result register.  No uniqueness if 0 */
  KeyInfo *pKeyInfo,      /* For comparing with previous entry */
  int p4type,             /* The p4 type for pKeyInfo */
  int iBreak              /* Jump here if we hit the LIMIT */
){
  Vdbe *v = pParse->pVdbe;
  int iContinue;
  int addr;

  addr = sqlite3VdbeCurrentAddr(v);
  iContinue = sqlite3VdbeMakeLabel(v);

  /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
  */
  if( regPrev ){
    int j1, j2;
    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
                              (char*)pKeyInfo, p4type);
    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
    sqlite3VdbeJumpHere(v, j1);
    sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
    sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
  }
  if( pParse->db->mallocFailed ) return 0;

  /* Suppress the first OFFSET entries if there is an OFFSET clause
  */
  codeOffset(v, p, iContinue);

  switch( pDest->eDest ){
    /* Store the result as data using a unique key.
    */
    case SRT_Table:
    case SRT_EphemTab: {
      int r1 = sqlite3GetTempReg(pParse);
      int r2 = sqlite3GetTempReg(pParse);
      testcase( pDest->eDest==SRT_Table );
      testcase( pDest->eDest==SRT_EphemTab );
      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
      sqlite3ReleaseTempReg(pParse, r2);
      sqlite3ReleaseTempReg(pParse, r1);
      break;
    }

#ifndef SQLITE_OMIT_SUBQUERY
    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
    ** then there should be a single item on the stack.  Write this
    ** item into the set table with bogus data.
    */
    case SRT_Set: {
      int r1;
      assert( pIn->nSdst==1 );
      pDest->affSdst = 
         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
      r1 = sqlite3GetTempReg(pParse);
      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
      sqlite3ReleaseTempReg(pParse, r1);
      break;
    }

#if 0  /* Never occurs on an ORDER BY query */
    /* If any row exist in the result set, record that fact and abort.
    */
    case SRT_Exists: {
      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
      /* The LIMIT clause will terminate the loop for us */
      break;
    }
#endif

    /* If this is a scalar select that is part of an expression, then
    ** store the results in the appropriate memory cell and break out
    ** of the scan loop.
    */
    case SRT_Mem: {
      assert( pIn->nSdst==1 );
      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
      /* The LIMIT clause will jump out of the loop for us */
      break;
    }
#endif /* #ifndef SQLITE_OMIT_SUBQUERY */

    /* The results are stored in a sequence of registers
    ** starting at pDest->iSdst.  Then the co-routine yields.
    */
    case SRT_Coroutine: {
      if( pDest->iSdst==0 ){
        pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
        pDest->nSdst = pIn->nSdst;
      }
      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
      break;
    }

    /* If none of the above, then the result destination must be
    ** SRT_Output.  This routine is never called with any other
    ** destination other than the ones handled above or SRT_Output.
    **
    ** For SRT_Output, results are stored in a sequence of registers.  
    ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
    ** return the next row of result.
    */
    default: {
      assert( pDest->eDest==SRT_Output );
      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
      break;
    }
  }

  /* Jump to the end of the loop if the LIMIT is reached.
  */
  if( p->iLimit ){
    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
  }

  /* Generate the subroutine return
  */
  sqlite3VdbeResolveLabel(v, iContinue);
  sqlite3VdbeAddOp1(v, OP_Return, regReturn);

  return addr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r15d
movl %r8d, -0x30(%rbp)
movq %rcx, -0x50(%rbp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movq 0x10(%rdi), %r14
movl 0x34(%r14), %eax
movl %eax, -0x2c(%rbp)
movq %r14, %rdi
callq 0x47934
movl %eax, -0x34(%rbp)
testl %r15d, %r15d
je 0x4e567
movq %rbx, -0x40(%rbp)
movq %r13, -0x48(%rbp)
pushq $0x28
popq %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x42436
movl %eax, %r13d
leal 0x1(%r15), %ebx
movl 0x8(%r12), %edx
movl 0xc(%r12), %r8d
movl 0x18(%rbp), %eax
movl %eax, (%rsp)
pushq $0x24
popq %rsi
movq %r14, %rdi
movl %ebx, %ecx
movq 0x10(%rbp), %r9
callq 0x41fd2
leal 0x2(%rax), %edx
pushq $0x25
popq %rsi
movq %r14, %rdi
movl -0x34(%rbp), %ecx
movl %edx, %r8d
callq 0x41f4c
testl %r13d, %r13d
js 0x4e534
movl 0x34(%r14), %eax
cmpl %r13d, %eax
jbe 0x4e534
movq 0x8(%r14), %rcx
movl %r13d, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rcx,%rdx)
movl 0x8(%r12), %edx
movl 0xc(%r12), %r8d
decl %r8d
pushq $0x1c
popq %rsi
movq %r14, %rdi
movl %ebx, %ecx
callq 0x41f4c
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %r15d, %ecx
callq 0x41f42
movq -0x48(%rbp), %r13
movq -0x40(%rbp), %rbx
movq (%rbx), %rcx
xorl %eax, %eax
cmpb $0x0, 0x50(%rcx)
jne 0x4e759
movq %r14, %rdi
movq %r13, %rsi
movl -0x34(%rbp), %edx
callq 0x4e7af
movq -0x50(%rbp), %r15
movzbl (%r15), %eax
addl $-0x6, %eax
cmpl $0x4, %eax
ja 0x4e6e2
leaq 0x19925(%rip), %rcx     # 0x67ec4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x46ff9
movl %eax, -0x48(%rbp)
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0x46ff9
movl %eax, %ebx
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
pushq $0x2b
popq %rsi
movq %r14, %rdi
movl -0x48(%rbp), %r12d
movl %r12d, %r8d
callq 0x41f4c
movl 0x4(%r15), %edx
pushq $0x41
popq %rsi
movq %r14, %rdi
movl %ebx, %ecx
callq 0x41f42
movl 0x4(%r15), %edx
movl %r12d, %r15d
pushq $0x42
popq %rsi
movq %r14, %rdi
movl %r12d, %ecx
movl %ebx, %r8d
callq 0x41f4c
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x4e61c
movslq 0x34(%r14), %rcx
imulq $0x18, %rcx, %rcx
movb $0x8, -0x15(%rax,%rcx)
movq -0x40(%rbp), %r12
movq %r12, %rdi
movl %ebx, %esi
callq 0x4701c
movq %r12, %rdi
movl %r15d, %esi
jmp 0x4e6db
movl 0x8(%r15), %edx
testl %edx, %edx
je 0x4e768
movl 0xc(%r15), %ecx
jmp 0x4e784
movl 0x8(%r12), %esi
movl 0x4(%r15), %edx
pushq $0x1
popq %rcx
movq %rbx, %rdi
callq 0x4da9a
jmp 0x4e709
movq (%r13), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x1(%r15), %rax
movq %rax, -0x40(%rbp)
movsbl 0x1(%r15), %esi
callq 0x49bf2
movb %al, 0x1(%r15)
movq %rbx, %rdi
callq 0x46ff9
movq %rbx, %r15
movl %eax, %ebx
movl 0x8(%r12), %edx
movl $0x1, (%rsp)
pushq $0x2b
popq %rsi
pushq $0x1
popq %rcx
movq %r14, %rdi
movl %eax, %r8d
movq -0x40(%rbp), %r9
callq 0x41fd2
movl 0x8(%r12), %esi
movq %r15, %rdi
pushq $0x1
popq %rdx
callq 0x49537
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edx
pushq $0x67
popq %rsi
movq %r14, %rdi
movl %ebx, %ecx
callq 0x41f42
movq %r15, %rdi
movl %ebx, %esi
callq 0x4701c
jmp 0x4e709
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
pushq $0x1e
popq %rsi
movq %r14, %rdi
callq 0x41f42
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
movq %rbx, %rdi
callq 0x49537
movl -0x30(%rbp), %ebx
movl -0x2c(%rbp), %r15d
movl 0xc(%r13), %edx
testl %edx, %edx
je 0x4e72c
movl 0x20(%rbp), %ecx
pushq $-0x1
popq %r8
movq %r14, %rdi
movl $0x80, %esi
callq 0x41f4c
cmpl $0x0, -0x34(%rbp)
jns 0x4e749
movq 0x40(%r14), %rax
testq %rax, %rax
je 0x4e749
movl -0x34(%rbp), %edx
notl %edx
movl 0x34(%r14), %ecx
movl %edx, %edx
movl %ecx, (%rax,%rdx,4)
pushq $0x10
popq %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x42436
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%r12), %esi
movq %rbx, %rdi
callq 0x47c2c
movl %eax, %edx
movl %eax, 0x8(%r15)
movl 0xc(%r12), %ecx
movl %ecx, 0xc(%r15)
movq %rbx, %rdi
movl -0x30(%rbp), %ebx
movl -0x2c(%rbp), %r15d
movl 0x8(%r12), %esi
callq 0x4da9a
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edx
pushq $0x11
popq %rsi
movq %r14, %rdi
callq 0x42436
jmp 0x4e710