// File: /workspace/github_c_makefile_star2_O1/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 0x2c8707
btl $0x19, %ecx
jb 0x2c8855
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 0x2b200c
incl %eax
movl %eax, 0x40(%r14)
movq %rbp, %rdi
movl $0x11, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2b200c
movl %eax, 0x4(%rsp)
testl %eax, %eax
leaq 0x1186d1(%rip), %rax    # 0x3e0de5
leaq 0x10b175(%rip), %rcx    # 0x3d3890
cmoveq %rax, %rcx
movl 0x10(%r12), %r8d
leaq 0x1186a5(%rip), %rdx    # 0x3e0dd0
movl $0x1, %ebx
movq %r15, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x2cae5e
movb (%r14), %al
cmpb $-0x78, %al
jne 0x2c874d
movq 0x20(%r12), %rcx
movl (%rcx), %ebx
movl 0x38(%r15), %r8d
leal 0x1(%r8), %ecx
addl %ebx, %r8d
xorl %edx, %edx
movb $0x0, 0x8(%rsp)
movl %ecx, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
movq %rdx, 0x14(%rsp)
movl %r8d, 0x38(%r15)
cmpb $-0x78, %al
jne 0x2c8796
movb $0xa, 0x8(%rsp)
movl %ecx, 0x14(%rsp)
movl %ebx, 0x18(%rsp)
movq %rbp, %rdi
movl $0x48, %esi
xorl %edx, %edx
jmp 0x2c87a8
movb $0x3, 0x8(%rsp)
movq %rbp, %rdi
movl $0x45, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x2b200c
cmpq $0x0, 0x60(%r12)
movq (%r15), %r13
je 0x2c8817
movq %rbp, 0x30(%rsp)
leaq 0xaf489(%rip), %rax     # 0x377c4d
leaq 0x38(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
xorl %ebx, %ebx
movq %r13, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x2bb38d
testq %rax, %rax
je 0x2c88a3
movq %rax, %rbp
movb $0x43, 0x1(%rax)
movq 0x60(%r12), %rax
movq 0x10(%rax), %rsi
testq %rsi, %rsi
je 0x2c888e
movq %r13, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2cbf97
movq %rax, %rdx
jmp 0x2c8890
leaq 0xaf44c(%rip), %rax     # 0x377c6a
leaq 0x38(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, 0x8(%rdx)
movq %r13, %rdi
movl $0x98, %esi
xorl %ecx, %ecx
callq 0x2bb38d
movq %r15, %rdi
movl $0x92, %esi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2b6fa0
movq %rax, 0x60(%r12)
jmp 0x2c88c7
movl 0x10(%r12), %ecx
leaq 0x11855d(%rip), %rdx    # 0x3e0dbe
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2cae5e
movl 0x40(%r14), %ecx
movl 0x44(%r14), %edx
movq %rbp, %rdi
movl $0xc, %esi
xorl %r8d, %r8d
callq 0x2b200c
movl 0x2c(%r14), %r12d
jmp 0x2c898d
xorl %edx, %edx
movq %r15, %rdi
movl $0x34, %esi
movq %rbp, %rcx
callq 0x2b6fa0
movq %rax, %rbx
movq 0x60(%r12), %rax
movq 0x10(%rax), %rsi
testq %rsi, %rsi
movq 0x30(%rsp), %rbp
je 0x2c88be
movq %r13, %rdi
callq 0x2afd54
movq 0x60(%r12), %rax
movq %rbx, 0x10(%rax)
movl $0x0, 0x8(%r12)
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2b9266
xorl %r12d, %r12d
testl %eax, %eax
jne 0x2c898d
movl 0xc(%rsp), %r12d
movl %r12d, 0x2c(%r14)
movl 0x4(%rsp), %edx
testl %edx, %edx
je 0x2c898d
movl 0x90(%rbp), %eax
movq (%rbp), %rcx
leaq 0x201577(%rip), %rbx    # 0x4c9e88
cmpb $0x0, 0x61(%rcx)
movq %rbx, %rcx
jne 0x2c8934
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 0x2b200c
movl 0x90(%rbp), %eax
decl %eax
movq (%rbp), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2c897d
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