// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5e520
movl %eax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x67890
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x718b4
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %edx
movl $0x28, %esi
callq 0x5ecf0
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %r8d
movq 0x10(%rbp), %r9
movl 0x18(%rbp), %eax
movl $0x24, %esi
movl %eax, (%rsp)
callq 0x5e1b0
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movl -0x48(%rbp), %edx
addl $0x2, %edx
movl -0x3c(%rbp), %ecx
movl -0x48(%rbp), %r8d
addl $0x2, %r8d
movl $0x25, %esi
callq 0x5e0f0
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x5e090
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %r8d
subl $0x1, %r8d
movl $0x1c, %esi
callq 0x5e0f0
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %ecx
movl $0x15, %esi
movl $0x1, %edx
callq 0x5e0c0
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpb $0x0, 0x50(%rax)
je 0x718cd
movl $0x0, -0x4(%rbp)
jmp 0x71b41
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x3c(%rbp), %edx
callq 0x71b50
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
addl $-0x6, %eax
movl %eax, %ecx
movq %rcx, -0x60(%rbp)
subl $0x4, %eax
ja 0x71ac3
movq -0x60(%rbp), %rax
leaq 0x38da3(%rip), %rcx     # 0xaa6a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
callq 0x66a70
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66a70
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
movl -0x4c(%rbp), %r8d
movl $0x2b, %esi
callq 0x5e0f0
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl -0x50(%rbp), %ecx
movl $0x41, %esi
callq 0x5e0c0
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl -0x4c(%rbp), %ecx
movl -0x50(%rbp), %r8d
movl $0x42, %esi
callq 0x5e0f0
movq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x5e540
movq -0x10(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x66ac0
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x66ac0
jmp 0x71af6
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movsbl 0x1(%rax), %esi
callq 0x6ab70
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x10(%rbp), %rdi
callq 0x66a70
movl %eax, -0x54(%rbp)
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x54(%rbp), %r8d
movq -0x28(%rbp), %r9
addq $0x1, %r9
movl $0x2b, %esi
movl $0x1, %ecx
movl $0x1, (%rsp)
callq 0x5e1b0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x1, %edx
callq 0x69c40
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl -0x54(%rbp), %ecx
movl $0x67, %esi
callq 0x5e0c0
movq -0x10(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x66ac0
jmp 0x71af6
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl $0x1, %ecx
callq 0x6f4f0
jmp 0x71af6
movq -0x28(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x71a8e
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x67c30
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x28(%rbp), %rax
movl 0xc(%rax), %ecx
callq 0x6f4f0
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movl $0x11, %esi
callq 0x5ecf0
jmp 0x71af6
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %ecx
movl $0x1e, %esi
callq 0x5e0c0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x69c40
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x71b1e
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
movl 0x20(%rbp), %ecx
movl $0x80, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x5e0f0
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x677f0
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %edx
movl $0x10, %esi
callq 0x5ecf0
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)