// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3CodeSubselect
SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
  int addrOnce = 0;           /* Address of OP_Once at top of subroutine */
  int rReg = 0;               /* Register storing resulting */
  Select *pSel;               /* SELECT statement to encode */
  SelectDest dest;            /* How to deal with SELECT result */
  int nReg;                   /* Registers to allocate */
  Expr *pLimit;               /* New limit expression */

  Vdbe *v = pParse->pVdbe;
  assert( v!=0 );
  testcase( pExpr->op==TK_EXISTS );
  testcase( pExpr->op==TK_SELECT );
  assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
  assert( ExprHasProperty(pExpr, EP_xIsSelect) );
  pSel = pExpr->x.pSelect;

  /* The evaluation of the EXISTS/SELECT must be repeated every time it
  ** is encountered if any of the following is true:
  **
  **    *  The right-hand side is a correlated subquery
  **    *  The right-hand side is an expression list containing variables
  **    *  We are inside a trigger
  **
  ** If all of the above are false, then we can run this code just once
  ** save the results, and reuse the same result on subsequent invocations.
  */
  if( !ExprHasProperty(pExpr, EP_VarSelect) ){
    /* If this routine has already been coded, then invoke it as a
    ** subroutine. */
    if( ExprHasProperty(pExpr, EP_Subrtn) ){
      ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
      sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
                        pExpr->y.sub.iAddr);
      return pExpr->iTable;
    }

    /* Begin coding the subroutine */
    ExprSetProperty(pExpr, EP_Subrtn);
    pExpr->y.sub.regReturn = ++pParse->nMem;
    pExpr->y.sub.iAddr =
      sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
    VdbeComment((v, "return address"));

    addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
  }

  /* For a SELECT, generate code to put the values for all columns of
  ** the first row into an array of registers and return the index of
  ** the first register.
  **
  ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
  ** into a register and return that register number.
  **
  ** In both cases, the query is augmented with "LIMIT 1".  Any
  ** preexisting limit is discarded in place of the new LIMIT 1.
  */
  ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",
        addrOnce?"":"CORRELATED ", pSel->selId));
  nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
  sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
  pParse->nMem += nReg;
  if( pExpr->op==TK_SELECT ){
    dest.eDest = SRT_Mem;
    dest.iSdst = dest.iSDParm;
    dest.nSdst = nReg;
    sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
    VdbeComment((v, "Init subquery result"));
  }else{
    dest.eDest = SRT_Exists;
    sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
    VdbeComment((v, "Init EXISTS result"));
  }
  if( pSel->pLimit ){
    /* The subquery already has a limit.  If the pre-existing limit is X
    ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
    sqlite3 *db = pParse->db;
    pLimit = sqlite3Expr(db, TK_INTEGER, "0");
    if( pLimit ){
      pLimit->affExpr = SQLITE_AFF_NUMERIC;
      pLimit = sqlite3PExpr(pParse, TK_NE,
                            sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
    }
    sqlite3ExprDelete(db, pSel->pLimit->pLeft);
    pSel->pLimit->pLeft = pLimit;
  }else{
    /* If there is no pre-existing limit add a limit of 1 */
    pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
    pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
  }
  pSel->iLimit = 0;
  if( sqlite3Select(pParse, pSel, &dest) ){
    return 0;
  }
  pExpr->iTable = rReg = dest.iSDParm;
  ExprSetVVAProperty(pExpr, EP_NoReduce);
  if( addrOnce ){
    sqlite3VdbeJumpHere(v, addrOnce);

    /* Subroutine return */
    sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
    sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
    sqlite3ClearTempRegCache(pParse);
  }

  return rReg;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x10(%rdi), %rbp
movq 0x20(%rsi), %r12
movl 0x4(%rsi), %ecx
xorl %eax, %eax
testb $0x20, %cl
jne 0x2befad
btl $0x19, %ecx
jb 0x2bf0f9
orl $0x2000000, %ecx        # imm = 0x2000000
movl %ecx, 0x4(%r14)
movl 0x38(%r15), %ecx
incl %ecx
movl %ecx, 0x38(%r15)
movl %ecx, 0x44(%r14)
movq %rbp, %rdi
movl $0x45, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2a83e1
incl %eax
movl %eax, 0x40(%r14)
movq %rbp, %rdi
movl $0x11, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movl %eax, 0x4(%rsp)
testl %eax, %eax
leaq 0x1186d6(%rip), %rax    # 0x3d7690
leaq 0x10b1af(%rip), %rcx    # 0x3ca170
cmoveq %rax, %rcx
movl 0x10(%r12), %r8d
leaq 0x1186aa(%rip), %rdx    # 0x3d767b
movl $0x1, %ebx
movq %r15, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x2c1741
movb (%r14), %al
cmpb $-0x78, %al
jne 0x2beff3
movq 0x20(%r12), %rcx
movl (%rcx), %ebx
movl 0x38(%r15), %r8d
leal 0x1(%r8), %ecx
addl %ebx, %r8d
xorl %edx, %edx
movl %ecx, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
movq %rdx, 0x1c(%rsp)
movl %r8d, 0x38(%r15)
cmpb $-0x78, %al
jne 0x2bf039
movl %ecx, 0x1c(%rsp)
movl %ebx, 0x20(%rsp)
movq %rbp, %rdi
movl $0x48, %esi
xorl %edx, %edx
callq 0x2a83e1
movb $0xa, %al
jmp 0x2bf04d
movq %rbp, %rdi
movl $0x45, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2a83e1
movb $0x3, %al
movb %al, 0x10(%rsp)
cmpq $0x0, 0x60(%r12)
movq (%r15), %r13
je 0x2bf0bb
movq %rbp, 0x8(%rsp)
leaq 0xaf975(%rip), %rax     # 0x36e9dd
leaq 0x38(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
xorl %ebp, %ebp
movq %r13, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x2b1c27
testq %rax, %rax
je 0x2bf147
movq %rax, %rbx
movb $0x43, 0x1(%rax)
movq 0x60(%r12), %rax
movq 0x10(%rax), %rsi
testq %rsi, %rsi
je 0x2bf132
movq %r13, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2c2864
movq %rax, %rdx
jmp 0x2bf134
leaq 0xaf938(%rip), %rax     # 0x36e9fa
leaq 0x38(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
movq %r13, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x2b1c27
movq %r15, %rdi
movl $0x92, %esi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2ad3b5
movq %rax, 0x60(%r12)
jmp 0x2bf16b
movl 0x10(%r12), %ecx
leaq 0x118564(%rip), %rdx    # 0x3d7669
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2c1741
movl 0x40(%r14), %ecx
movl 0x44(%r14), %edx
movq %rbp, %rdi
movl $0xc, %esi
xorl %r8d, %r8d
callq 0x2a83e1
movl 0x2c(%r14), %r12d
jmp 0x2bf231
xorl %edx, %edx
movq %r15, %rdi
movl $0x34, %esi
movq %rbx, %rcx
callq 0x2ad3b5
movq %rax, %rbp
movq 0x60(%r12), %rax
movq 0x10(%rax), %rsi
testq %rsi, %rsi
je 0x2bf162
movq %r13, %rdi
callq 0x2a6164
movq 0x60(%r12), %rax
movq %rbp, 0x10(%rax)
movq 0x8(%rsp), %rbp
movl $0x0, 0x8(%r12)
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2af648
xorl %r12d, %r12d
testl %eax, %eax
jne 0x2bf231
movl 0x14(%rsp), %r12d
movl %r12d, 0x2c(%r14)
movl 0x4(%rsp), %edx
testl %edx, %edx
je 0x2bf231
movl 0x90(%rbp), %eax
movq (%rbp), %rcx
leaq 0x1ffcd3(%rip), %rbx    # 0x4bee88
cmpb $0x0, 0x61(%rcx)
movq %rbx, %rcx
jne 0x2bf1d8
leal -0x1(%rax), %ecx
testl %edx, %edx
cmovnsl %edx, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x88(%rbp), %rcx
movl %eax, 0x8(%rcx)
movl 0x44(%r14), %edx
movq %rbp, %rdi
movl $0x41, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2a83e1
movl 0x90(%rbp), %eax
decl %eax
movq (%rbp), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2bf221
movl 0x40(%r14), %ecx
leal -0x1(%rcx), %edx
testl %ecx, %ecx
cmovlel %eax, %edx
movslq %edx, %rcx
leaq (%rcx,%rcx,2), %rbx
shlq $0x3, %rbx
addq 0x88(%rbp), %rbx
movl %eax, 0x4(%rbx)
movb $0x0, 0x1f(%r15)
movl $0x0, 0x28(%r15)
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq