// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %r9d, %ebx
movl %r8d, %r14d
movq %rcx, %r15
movq %rdx, -0x30(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdi, -0x40(%rbp)
movq 0x10(%rdi), %r12
movl 0x34(%r12), %r13d
movq %r12, %rdi
callq 0x4c4bf
movl %eax, -0x34(%rbp)
movq %rbx, %rax
testl %ebx, %ebx
movq -0x30(%rbp), %rbx
movl %r13d, -0x5c(%rbp)
je 0x565d9
movq %r15, -0x50(%rbp)
movl %r14d, -0x44(%rbp)
movq %r12, %rdi
movl $0x28, %esi
movq %rax, %r15
movl %r15d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %eax, %r13d
leal 0x1(%r15), %r14d
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %r8d
movq %r12, %rdi
movl $0x24, %esi
movl %r14d, %ecx
callq 0x45b9e
movl %eax, %ebx
movq %r12, %rdi
movl %eax, %esi
movq 0x10(%rbp), %rdx
movl 0x18(%rbp), %ecx
callq 0x11975
addl $0x2, %ebx
movq %r12, %rdi
movl $0x25, %esi
movl %ebx, %edx
movl -0x34(%rbp), %ecx
movl %ebx, %r8d
callq 0x45b9e
testl %r13d, %r13d
js 0x5659b
movl 0x34(%r12), %eax
cmpl %r13d, %eax
jbe 0x5659b
movq 0x8(%r12), %rcx
movl %r13d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %edx
movl 0xc(%rax), %r8d
decl %r8d
movq %r12, %rdi
movl $0x1c, %esi
movl %r14d, %ecx
callq 0x45b9e
movq %r12, %rdi
movl $0x15, %esi
movl $0x1, %edx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl -0x44(%rbp), %r14d
movq -0x50(%rbp), %r15
movq -0x40(%rbp), %rax
movq (%rax), %rcx
xorl %eax, %eax
cmpb $0x0, 0x50(%rcx)
jne 0x56966
movq %r12, %rdi
movq -0x58(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x5697d
movzbl (%r15), %eax
addl $-0x6, %eax
cmpl $0x4, %eax
ja 0x566de
movq %r15, %rbx
leaq 0x1f8bf(%rip), %rcx     # 0x75ed4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl %r14d, %r13d
jmpq *%rax
movq -0x40(%rbp), %rcx
movb 0x1f(%rcx), %al
testb %al, %al
movq %r12, %r14
je 0x56712
decb %al
movb %al, 0x1f(%rcx)
movzbl %al, %eax
movl 0x28(%rcx,%rax,4), %r15d
jmp 0x5671d
cmpl $0x0, 0x8(%rbx)
movq -0x40(%rbp), %rdi
movq %r12, %r14
jne 0x568e7
movq -0x30(%rbp), %rax
movl 0xc(%rax), %eax
movl 0x48(%rdi), %edx
subl %eax, %edx
jge 0x568d5
movl 0x58(%rdi), %edx
leal 0x1(%rdx), %ecx
addl %eax, %edx
movl %edx, 0x58(%rdi)
jmp 0x568e1
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl 0x4(%rbx), %edx
movq -0x40(%rbp), %rdi
movl $0x1, %ecx
callq 0x552b9
jmp 0x5670a
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x1(%rbx), %rax
movq %rax, -0x68(%rbp)
movsbl 0x1(%rbx), %esi
callq 0x4eee9
movq %rbx, -0x50(%rbp)
movb %al, 0x1(%rbx)
movq -0x40(%rbp), %r15
movb 0x1f(%r15), %al
testb %al, %al
movq %r12, %r14
je 0x567ed
decb %al
movb %al, 0x1f(%r15)
movzbl %al, %eax
movl 0x28(%r15,%rax,4), %ebx
jmp 0x567f7
movq -0x30(%rbp), %rbx
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %ecx
movq %r12, %rdi
movl $0x1e, %esi
xorl %r8d, %r8d
callq 0x45b9e
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
movq -0x40(%rbp), %rdi
callq 0x4e50d
movl %r14d, %r13d
movq %r12, %r14
jmp 0x5690e
movl 0x58(%rcx), %r15d
incl %r15d
movl %r15d, 0x58(%rcx)
movq %rbx, -0x50(%rbp)
movb 0x1f(%rcx), %al
testb %al, %al
movl %r13d, -0x44(%rbp)
je 0x5673a
decb %al
movb %al, 0x1f(%rcx)
movzbl %al, %eax
movl 0x28(%rcx,%rax,4), %ebx
jmp 0x56742
movl 0x58(%rcx), %ebx
incl %ebx
movl %ebx, 0x58(%rcx)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %edx
movl 0xc(%rax), %ecx
movq %r14, %rdi
movl $0x2b, %esi
movl %r15d, %r8d
callq 0x45b9e
movq -0x50(%rbp), %r12
movl 0x4(%r12), %edx
movq %r14, %rdi
movl $0x41, %esi
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl 0x4(%r12), %edx
movq %r14, %rdi
movl $0x42, %esi
movl %r15d, %ecx
movl %ebx, %r8d
callq 0x45b9e
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x567a5
movslq 0x34(%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x8, -0x15(%rax,%rcx,8)
testl %ebx, %ebx
movq -0x40(%rbp), %rdx
movl -0x44(%rbp), %r13d
je 0x5689a
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x5689a
xorl %ecx, %ecx
cmpl %ebx, 0x84(%rdx,%rcx)
je 0x56895
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x567c5
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
movl %ebx, 0x28(%rdx,%rax,4)
jmp 0x5689a
movl 0x58(%r15), %ebx
incl %ebx
movl %ebx, 0x58(%r15)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movl $0x2b, %esi
movq %rax, %r12
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x45b9e
movq %r14, %rdi
movl %eax, %esi
movq -0x68(%rbp), %rdx
movl $0x1, %ecx
callq 0x11975
movl 0x8(%r12), %esi
movq %r15, %rdi
movl $0x1, %edx
callq 0x4e50d
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edx
movq %r14, %rdi
movl $0x67, %esi
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
testl %ebx, %ebx
je 0x5690e
movzbl 0x1f(%r15), %eax
cmpq $0x7, %rax
ja 0x5690e
xorl %ecx, %ecx
cmpl %ebx, 0x84(%r15,%rcx)
je 0x56975
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x5686d
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%r15)
movl %ebx, 0x28(%r15,%rax,4)
jmp 0x5690e
movb $0x1, 0x7c(%rdx,%rcx)
testl %r15d, %r15d
je 0x5690e
movzbl 0x1f(%rdx), %eax
cmpq $0x7, %rax
ja 0x5690e
xorl %ecx, %ecx
cmpl %r15d, 0x84(%rdx,%rcx)
je 0x568ce
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x568ab
leal 0x1(%rax), %ecx
movb %cl, 0x1f(%rdx)
movl %r15d, 0x28(%rdx,%rax,4)
jmp 0x5690e
movb $0x1, 0x7c(%rdx,%rcx)
jmp 0x5690e
movl 0x4c(%rdi), %ecx
leal (%rcx,%rax), %esi
movl %esi, 0x4c(%rdi)
movl %edx, 0x48(%rdi)
movl %ecx, 0x8(%rbx)
movl %eax, 0xc(%rbx)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %ecx
callq 0x552b9
movl 0x4(%rbx), %edx
movq %r14, %rdi
movl $0x11, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x58(%rbp), %rax
movl 0xc(%rax), %edx
testl %edx, %edx
je 0x5692f
movl 0x20(%rbp), %ecx
movq %r14, %rdi
movl $0x80, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x45b9e
cmpl $0x0, -0x34(%rbp)
movl -0x5c(%rbp), %ebx
jns 0x5694f
movq 0x40(%r14), %rax
testq %rax, %rax
je 0x5694f
movl -0x34(%rbp), %edx
notl %edx
movl 0x34(%r14), %ecx
movl %edx, %edx
movl %ecx, (%rax,%rdx,4)
movq %r14, %rdi
movl $0x10, %esi
movl %r13d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x7c(%r15,%rcx)
jmp 0x5690e