// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: selectInnerLoop
static void selectInnerLoop(
  Parse *pParse,          /* The parser context */
  Select *p,              /* The complete select statement being coded */
  ExprList *pEList,       /* List of values being extracted */
  int srcTab,             /* Pull data from this table */
  int nColumn,            /* Number of columns in the source table */
  ExprList *pOrderBy,     /* If not NULL, sort results using this key */
  DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
  SelectDest *pDest,      /* How to dispose of the results */
  int iContinue,          /* Jump here to continue with next row */
  int iBreak              /* Jump here to break out of the inner loop */
){
  Vdbe *v = pParse->pVdbe;
  int i;
  int hasDistinct;        /* True if the DISTINCT keyword is present */
  int regResult;              /* Start of memory holding result set */
  int eDest = pDest->eDest;   /* How to dispose of results */
  int iParm = pDest->iSDParm; /* First argument to disposal method */
  int nResultCol;             /* Number of result columns */

  assert( v );
  if( NEVER(v==0) ) return;
  assert( pEList!=0 );
  hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
  if( pOrderBy==0 && !hasDistinct ){
    codeOffset(v, p, iContinue);
  }

  /* Pull the requested columns.
  */
  if( nColumn>0 ){
    nResultCol = nColumn;
  }else{
    nResultCol = pEList->nExpr;
  }
  if( pDest->iSdst==0 ){
    pDest->iSdst = pParse->nMem+1;
    pDest->nSdst = nResultCol;
    pParse->nMem += nResultCol;
  }else{ 
    assert( pDest->nSdst==nResultCol );
  }
  regResult = pDest->iSdst;
  if( nColumn>0 ){
    for(i=0; i<nColumn; i++){
      sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
    }
  }else if( eDest!=SRT_Exists ){
    /* If the destination is an EXISTS(...) expression, the actual
    ** values returned by the SELECT are not required.
    */
    sqlite3ExprCacheClear(pParse);
    sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
  }
  nColumn = nResultCol;

  /* If the DISTINCT keyword was present on the SELECT statement
  ** and this row has been seen before, then do not make this row
  ** part of the result.
  */
  if( hasDistinct ){
    assert( pEList!=0 );
    assert( pEList->nExpr==nColumn );
    switch( pDistinct->eTnctType ){
      case WHERE_DISTINCT_ORDERED: {
        VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
        int iJump;              /* Jump destination */
        int regPrev;            /* Previous row content */

        /* Allocate space for the previous row */
        regPrev = pParse->nMem+1;
        pParse->nMem += nColumn;

        /* Change the OP_OpenEphemeral coded earlier to an OP_Null
        ** sets the MEM_Cleared bit on the first register of the
        ** previous value.  This will cause the OP_Ne below to always
        ** fail on the first iteration of the loop even if the first
        ** row is all NULLs.
        */
        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
        pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
        pOp->opcode = OP_Null;
        pOp->p1 = 1;
        pOp->p2 = regPrev;

        iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
        for(i=0; i<nColumn; i++){
          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
          if( i<nColumn-1 ){
            sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
          }else{
            sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
          }
          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
        }
        assert( sqlite3VdbeCurrentAddr(v)==iJump );
        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
        break;
      }

      case WHERE_DISTINCT_UNIQUE: {
        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
        break;
      }

      default: {
        assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
        break;
      }
    }
    if( pOrderBy==0 ){
      codeOffset(v, p, iContinue);
    }
  }

  switch( eDest ){
    /* In this mode, write each query result to the key of the temporary
    ** table iParm.
    */
#ifndef SQLITE_OMIT_COMPOUND_SELECT
    case SRT_Union: {
      int r1;
      r1 = sqlite3GetTempReg(pParse);
      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
      sqlite3ReleaseTempReg(pParse, r1);
      break;
    }

    /* Construct a record from the query result, but instead of
    ** saving that record, use it as a key to delete elements from
    ** the temporary table iParm.
    */
    case SRT_Except: {
      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
      break;
    }
#endif

    /* Store the result as data using a unique key.
    */
    case SRT_Table:
    case SRT_EphemTab: {
      int r1 = sqlite3GetTempReg(pParse);
      testcase( eDest==SRT_Table );
      testcase( eDest==SRT_EphemTab );
      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
      if( pOrderBy ){
        pushOntoSorter(pParse, pOrderBy, p, r1);
      }else{
        int r2 = sqlite3GetTempReg(pParse);
        sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
        sqlite3VdbeAddOp3(v, OP_Insert, iParm, 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: {
      assert( nColumn==1 );
      pDest->affSdst =
                  sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
      if( pOrderBy ){
        /* At first glance you would think we could optimize out the
        ** ORDER BY in this case since the order of entries in the set
        ** does not matter.  But there might be a LIMIT clause, in which
        ** case the order does matter */
        pushOntoSorter(pParse, pOrderBy, p, regResult);
      }else{
        int r1 = sqlite3GetTempReg(pParse);
        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
        sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
        sqlite3ReleaseTempReg(pParse, r1);
      }
      break;
    }

    /* If any row exist in the result set, record that fact and abort.
    */
    case SRT_Exists: {
      sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
      /* The LIMIT clause will terminate the loop for us */
      break;
    }

    /* 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( nColumn==1 );
      if( pOrderBy ){
        pushOntoSorter(pParse, pOrderBy, p, regResult);
      }else{
        sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
        /* The LIMIT clause will jump out of the loop for us */
      }
      break;
    }
#endif /* #ifndef SQLITE_OMIT_SUBQUERY */

    /* Send the data to the callback function or to a subroutine.  In the
    ** case of a subroutine, the subroutine itself is responsible for
    ** popping the data from the stack.
    */
    case SRT_Coroutine:
    case SRT_Output: {
      testcase( eDest==SRT_Coroutine );
      testcase( eDest==SRT_Output );
      if( pOrderBy ){
        int r1 = sqlite3GetTempReg(pParse);
        sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
        pushOntoSorter(pParse, pOrderBy, p, r1);
        sqlite3ReleaseTempReg(pParse, r1);
      }else if( eDest==SRT_Coroutine ){
        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
      }else{
        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
        sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
      }
      break;
    }

#if !defined(SQLITE_OMIT_TRIGGER)
    /* Discard the results.  This is used for SELECT statements inside
    ** the body of a TRIGGER.  The purpose of such selects is to call
    ** user-defined functions that have side effects.  We do not care
    ** about the actual results of the select.
    */
    default: {
      assert( eDest==SRT_Discard );
      break;
    }
#endif
  }

  /* Jump to the end of the loop if the LIMIT is reached.  Except, if
  ** there is a sorter, in which case the sorter has already limited
  ** the output for us.
  */
  if( pOrderBy==0 && p->iLimit ){
    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, -0x78(%rbp)
movq %rdx, -0x70(%rbp)
movq 0x10(%rdi), %rdx
testq %rdx, %rdx
je 0x4d576
movl %r8d, %ebx
movl %ecx, %r12d
movq %rsi, %r15
movq %rdi, %r13
movq 0x18(%rbp), %rcx
movq 0x10(%rbp), %rax
testq %rax, %rax
movq %rax, -0x68(%rbp)
movq %rdx, -0x88(%rbp)
je 0x4d073
cmpb $0x0, 0x1(%rax)
setne %dil
jmp 0x4d075
xorl %edi, %edi
movl 0x28(%rbp), %eax
movl %eax, -0x90(%rbp)
movl 0x20(%rbp), %eax
movl %eax, -0x44(%rbp)
movzbl (%rcx), %eax
movq %rax, -0x60(%rbp)
movl 0x4(%rcx), %eax
movl %eax, -0x34(%rbp)
cmpq $0x0, -0x78(%rbp)
setne %al
orb %dil, %al
testb $0x1, %al
movq %rcx, -0x50(%rbp)
movl %edi, %r14d
jne 0x4d0c0
movq -0x88(%rbp), %rdi
movq %r15, %rsi
movl -0x44(%rbp), %edx
callq 0x4e7af
movl %r14d, %edi
movq -0x50(%rbp), %rcx
movl %ebx, %eax
movq %rax, -0x40(%rbp)
testl %ebx, %ebx
jg 0x4d0d4
movq -0x70(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x40(%rbp)
movl 0x8(%rcx), %eax
testl %eax, %eax
jne 0x4d0fa
movl 0x58(%r13), %ecx
leal 0x1(%rcx), %eax
movq -0x50(%rbp), %rdx
movl %eax, 0x8(%rdx)
movq -0x50(%rbp), %rdx
movq -0x40(%rbp), %rsi
movl %esi, 0xc(%rdx)
addl %esi, %ecx
movl %ecx, 0x58(%r13)
movq %r13, -0x80(%rbp)
movq %r15, -0x58(%rbp)
movl %eax, %eax
movq %rax, -0x30(%rbp)
testl %ebx, %ebx
jle 0x4d13d
xorl %r13d, %r13d
pushq $0x29
popq %r15
cmpl %r13d, %ebx
je 0x4d16b
movq -0x30(%rbp), %rax
leal (%rax,%r13), %r8d
movq -0x88(%rbp), %rdi
movl %r15d, %esi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x41f4c
movl %r14d, %edi
incl %r13d
jmp 0x4d113
cmpl $0x3, -0x60(%rbp)
je 0x4d16b
movq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x4da57
xorl %ecx, %ecx
cmpl $0x5, -0x60(%rbp)
sete %cl
movq %rbx, %rdi
movq -0x70(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x49599
movl %r14d, %edi
testb %dil, %dil
movq -0x58(%rbp), %r15
movq -0x80(%rbp), %r13
movq -0x78(%rbp), %r12
movq -0x88(%rbp), %r14
je 0x4d2f4
movq -0x68(%rbp), %rbx
movzbl 0x1(%rbx), %eax
cmpl $0x1, %eax
je 0x4d2be
cmpl $0x2, %eax
jne 0x4d2cb
movl 0x58(%r13), %eax
leal 0x1(%rax), %r12d
movq -0x40(%rbp), %r15
addl %r15d, %eax
movl %eax, 0x58(%r13)
movl 0x8(%rbx), %esi
movq %r14, %rdi
callq 0x108cb
movl 0x8(%rbx), %esi
movq %r14, %rdi
callq 0x4a93d
movb $0x18, (%rax)
movl $0x1, 0x4(%rax)
movq %r12, -0x68(%rbp)
movl %r12d, 0x8(%rax)
movl 0x34(%r14), %eax
addl %r15d, %eax
movl %eax, -0x94(%rbp)
leal -0x1(%r15), %eax
movl %eax, -0x8c(%rbp)
cltq
movq %rax, -0xa0(%rbp)
xorl %r12d, %r12d
testl %r15d, %r15d
movl $0x0, %ebx
cmovgl %r15d, %ebx
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x4d29a
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r12), %rsi
movq %r13, %rdi
callq 0x466ee
movq %rax, %r13
movq -0x30(%rbp), %rax
leaq (%rax,%r15), %rdx
movq -0x68(%rbp), %rax
leaq (%rax,%r15), %r8
cmpq -0xa0(%rbp), %r15
jge 0x4d255
movq %r14, %rdi
pushq $0x4b
popq %rsi
movl -0x94(%rbp), %ecx
jmp 0x4d25e
movq %r14, %rdi
pushq $0x4c
popq %rsi
movl -0x44(%rbp), %ecx
callq 0x41f4c
movq %r14, %rdi
pushq $-0x1
popq %rsi
movq %r13, %rdx
pushq $-0x4
popq %rcx
callq 0x1074b
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x4d28a
movslq 0x34(%r14), %rcx
imulq $0x18, %rcx, %rcx
movb $-0x80, -0x15(%rax,%rcx)
incq %r15
addq $0x20, %r12
movq -0x80(%rbp), %r13
jmp 0x4d20e
pushq $0x1c
popq %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rdx
movq -0x68(%rbp), %rcx
movl -0x8c(%rbp), %r8d
callq 0x41f4c
movq -0x58(%rbp), %r15
movq -0x78(%rbp), %r12
jmp 0x4d2e1
movl 0x8(%rbx), %esi
movq %r14, %rdi
callq 0x108cb
jmp 0x4d2e1
movl 0x4(%rbx), %esi
movq %r13, %rdi
movl -0x44(%rbp), %edx
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r8
callq 0x527c8
testq %r12, %r12
jne 0x4d2f4
movq %r14, %rdi
movq %r15, %rsi
movl -0x44(%rbp), %edx
callq 0x4e7af
movq -0x60(%rbp), %rax
decl %eax
cmpl $0x9, %eax
ja 0x4d541
leaq 0x1ab92(%rip), %rcx     # 0x67e9c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r12, %r12
je 0x4d4c1
movq %r13, %rdi
callq 0x46ff9
movl %eax, %ebx
pushq $0x2b
popq %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl %eax, %r8d
callq 0x41f4c
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x52851
movq %r13, %rdi
movl %ebx, %esi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4701c
movq %r13, %rdi
callq 0x46ff9
movl %eax, %ebx
pushq $0x2b
popq %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl %eax, %r8d
callq 0x41f4c
movq %r13, %rdi
testq %r12, %r12
je 0x4d4df
movq %r12, %rsi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x52851
jmp 0x4d537
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movl -0x34(%rbp), %ecx
callq 0x41f42
jmp 0x4d541
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rbx
movsbl 0x1(%rbx), %esi
callq 0x49bf2
movb %al, 0x1(%rbx)
testq %r12, %r12
jne 0x4d49e
movq %rbx, %rcx
incq %rcx
movq %r13, %rdi
movq %rcx, %r15
callq 0x46ff9
movl %eax, %ebx
movl $0x1, (%rsp)
pushq $0x2b
popq %rsi
pushq $0x1
popq %rcx
movq %r14, %rdi
movq -0x30(%rbp), %r12
movl %r12d, %edx
movl %eax, %r8d
movq %r15, %r9
callq 0x41fd2
movq %r13, %rdi
movl %r12d, %esi
pushq $0x1
popq %rdx
movq -0x58(%rbp), %r15
callq 0x49537
pushq $0x67
popq %rsi
movq %r14, %rdi
movl -0x34(%rbp), %edx
movl %ebx, %ecx
callq 0x41f42
movq %r13, %rdi
movl %ebx, %esi
callq 0x4701c
jmp 0x4d546
movq %r13, %rdi
callq 0x46ff9
movl %eax, %ebx
pushq $0x2b
popq %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl %eax, %r8d
callq 0x41f4c
pushq $0x67
popq %rsi
movq %r14, %rdi
movl -0x34(%rbp), %edx
movl %ebx, %ecx
callq 0x41f42
jmp 0x4d537
pushq $0x68
popq %rsi
movq %r14, %rdi
movl -0x34(%rbp), %edx
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x41f4c
jmp 0x4d541
testq %r12, %r12
je 0x4d5af
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq -0x30(%rbp), %rcx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x52851
cmpl $0xa, -0x60(%rbp)
jne 0x4d588
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edx
pushq $0x11
popq %rsi
movq %r14, %rdi
callq 0x42436
jmp 0x4d546
callq 0x46ff9
movl %eax, -0x30(%rbp)
pushq $0x41
popq %rsi
movq %r14, %rdi
movl -0x34(%rbp), %r15d
movl %r15d, %edx
movl %eax, %ecx
callq 0x41f42
pushq $0x42
popq %rsi
movq %r14, %rdi
movl %r15d, %edx
movl -0x30(%rbp), %r15d
movl %ebx, %ecx
movl %r15d, %r8d
callq 0x41f4c
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x4d528
movslq 0x34(%r14), %rcx
imulq $0x18, %rcx, %rcx
movb $0x8, -0x15(%rax,%rcx)
movq %r13, %rdi
movl %r15d, %esi
callq 0x4701c
movq -0x58(%rbp), %r15
movq %r13, %rdi
movl %ebx, %esi
callq 0x4701c
testq %r12, %r12
jne 0x4d576
movl 0xc(%r15), %edx
testl %edx, %edx
je 0x4d576
pushq $-0x1
popq %r8
movq %r14, %rdi
movl $0x80, %esi
movl -0x90(%rbp), %ecx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41f4c
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1e
popq %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rbx
movl %ebx, %edx
movq -0x40(%rbp), %r12
movl %r12d, %ecx
callq 0x41f42
movq %r13, %rdi
movl %ebx, %esi
movl %r12d, %edx
callq 0x49537
jmp 0x4d546
pushq $0x1
popq %rcx
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x4da9a
jmp 0x4d546