// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x10(%rdi), %r14
movq 0x20(%rsi), %r12
movl 0x4(%rsi), %eax
xorl %ebp, %ebp
testb $0x20, %al
jne 0x266790
btl $0x19, %eax
jb 0x2668b0
orl $0x2000000, %eax        # imm = 0x2000000
movl %eax, 0x4(%rbx)
movl 0x38(%r15), %ecx
incl %ecx
movl %ecx, 0x38(%r15)
movl %ecx, 0x44(%rbx)
pushq $0x45
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x24d51a
incl %eax
movl %eax, 0x40(%rbx)
pushq $0x11
popq %rsi
movq %r14, %rdi
callq 0x2645ec
movl %eax, %ebp
testl %ebp, %ebp
leaq 0x111317(%rip), %rax    # 0x377ab0
leaq 0x103dd0(%rip), %rcx    # 0x36a570
cmoveq %rax, %rcx
movl 0x10(%r12), %r8d
leaq 0x1112eb(%rip), %rdx    # 0x377a9b
pushq $0x1
popq %r13
movq %r15, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x268aa3
movb (%rbx), %al
cmpb $-0x78, %al
jne 0x2667cf
movq 0x20(%r12), %rcx
movl (%rcx), %r13d
movl 0x38(%r15), %r8d
andl $0x0, 0x18(%rsp)
andq $0x0, 0x28(%rsp)
leal 0x1(%r8), %ecx
andq $0x0, 0x1c(%rsp)
addl %r13d, %r8d
movl %ecx, 0x14(%rsp)
movl %r8d, 0x38(%r15)
cmpb $-0x78, %al
jne 0x266811
movl %ecx, 0x1c(%rsp)
movl %r13d, 0x20(%rsp)
pushq $0x48
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x24d51f
movb $0xa, %al
jmp 0x266820
pushq $0x45
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x24d51a
movb $0x3, %al
movb %al, 0x10(%rsp)
cmpq $0x0, 0x60(%r12)
movq (%r15), %r13
je 0x266883
movl %ebp, 0xc(%rsp)
movq %r14, %rbp
leaq 0xa84b8(%rip), %rdx     # 0x30ecf5
movq %r13, %rdi
movl $0x98, %esi
callq 0x25bd93
testq %rax, %rax
je 0x2668e2
movq %rax, %r14
movb $0x43, 0x1(%rax)
movq 0x60(%r12), %rax
movq 0x10(%rax), %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x267197
pushq $0x34
popq %rsi
movq %r15, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x254915
movq %rax, %r14
jmp 0x2668e5
leaq 0xa8488(%rip), %rdx     # 0x30ed12
movq %r13, %rdi
movl $0x98, %esi
callq 0x25bd93
movq %r15, %rdi
movl $0x92, %esi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x254915
movq %rax, 0x60(%r12)
jmp 0x266906
movl 0x10(%r12), %ecx
leaq 0x1111cd(%rip), %rdx    # 0x377a89
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x268aa3
movl 0x40(%rbx), %ecx
movl 0x44(%rbx), %edx
pushq $0xc
popq %rsi
movq %r14, %rdi
callq 0x24d51a
movl 0x2c(%rbx), %r12d
jmp 0x26696b
xorl %r14d, %r14d
movq 0x60(%r12), %rax
movq 0x10(%rax), %rsi
movq %r13, %rdi
callq 0x24b639
movq 0x60(%r12), %rax
movq %r14, 0x10(%rax)
movq %rbp, %r14
movl 0xc(%rsp), %ebp
andl $0x0, 0x8(%r12)
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2565ce
xorl %r12d, %r12d
testl %eax, %eax
jne 0x26696b
movl 0x14(%rsp), %r12d
movl %r12d, 0x2c(%rbx)
testl %ebp, %ebp
je 0x26696b
movq %r14, %rdi
movl %ebp, %esi
callq 0x2645f8
movl 0x44(%rbx), %edx
pushq $0x41
popq %rsi
movq %r14, %rdi
callq 0x265d5c
movl 0x40(%rbx), %esi
decl %esi
movl 0x90(%r14), %ebx
decl %ebx
movq %r14, %rdi
callq 0x26468f
movl %ebx, 0x4(%rax)
movb $0x0, 0x1f(%r15)
andl $0x0, 0x28(%r15)
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq