// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3CodeSubselect
SQLITE_PRIVATE int sqlite3CodeSubselect(
  Parse *pParse,          /* Parsing context */
  Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
  int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
  int isRowid             /* If true, LHS of IN operator is a rowid */
){
  int testAddr = -1;                      /* One-time test address */
  int rReg = 0;                           /* Register storing resulting */
  Vdbe *v = sqlite3GetVdbe(pParse);
  if( NEVER(v==0) ) return 0;
  sqlite3ExprCachePush(pParse);

  /* This code must be run in its entirety 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( !ExprHasAnyProperty(pExpr, EP_VarSelect) ){
    testAddr = sqlite3CodeOnce(pParse);
  }

#ifndef SQLITE_OMIT_EXPLAIN
  if( pParse->explain==2 ){
    char *zMsg = sqlite3MPrintf(
        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
        pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
    );
    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
  }
#endif

  switch( pExpr->op ){
    case TK_IN: {
      char affinity;              /* Affinity of the LHS of the IN */
      int addr;                   /* Address of OP_OpenEphemeral instruction */
      Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
      KeyInfo *pKeyInfo = 0;      /* Key information */

      if( rMayHaveNull ){
        sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
      }

      affinity = sqlite3ExprAffinity(pLeft);

      /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
      ** expression it is handled the same way.  An ephemeral table is 
      ** filled with single-field index keys representing the results
      ** from the SELECT or the <exprlist>.
      **
      ** If the 'x' expression is a column value, or the SELECT...
      ** statement returns a column value, then the affinity of that
      ** column is used to build the index keys. If both 'x' and the
      ** SELECT... statement are columns, then numeric affinity is used
      ** if either column has NUMERIC or INTEGER affinity. If neither
      ** 'x' nor the SELECT... statement are columns, then numeric affinity
      ** is used.
      */
      pExpr->iTable = pParse->nTab++;
      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
      if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1);

      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
        /* Case 1:     expr IN (SELECT ...)
        **
        ** Generate code to write the results of the select into the temporary
        ** table allocated and opened above.
        */
        SelectDest dest;
        ExprList *pEList;

        assert( !isRowid );
        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
        dest.affSdst = (u8)affinity;
        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
        pExpr->x.pSelect->iLimit = 0;
        testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
        if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
          sqlite3DbFree(pParse->db, pKeyInfo);
          return 0;
        }
        pEList = pExpr->x.pSelect->pEList;
        assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
        assert( pEList!=0 );
        assert( pEList->nExpr>0 );
        pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
                                                         pEList->a[0].pExpr);
      }else if( ALWAYS(pExpr->x.pList!=0) ){
        /* Case 2:     expr IN (exprlist)
        **
        ** For each expression, build an index key from the evaluation and
        ** store it in the temporary table. If <expr> is a column, then use
        ** that columns affinity when building index keys. If <expr> is not
        ** a column, use numeric affinity.
        */
        int i;
        ExprList *pList = pExpr->x.pList;
        struct ExprList_item *pItem;
        int r1, r2, r3;

        if( !affinity ){
          affinity = SQLITE_AFF_NONE;
        }
        if( pKeyInfo ){
          pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
        }

        /* Loop through each expression in <exprlist>. */
        r1 = sqlite3GetTempReg(pParse);
        r2 = sqlite3GetTempReg(pParse);
        sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
        for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
          Expr *pE2 = pItem->pExpr;
          int iValToIns;

          /* If the expression is not constant then we will need to
          ** disable the test that was generated above that makes sure
          ** this code only executes once.  Because for a non-constant
          ** expression we need to rerun this code each time.
          */
          if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
            sqlite3VdbeChangeToNoop(v, testAddr);
            testAddr = -1;
          }

          /* Evaluate the expression and insert it into the temp table */
          if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
            sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
          }else{
            r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
            if( isRowid ){
              sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
                                sqlite3VdbeCurrentAddr(v)+2);
              sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
            }else{
              sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
              sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
            }
          }
        }
        sqlite3ReleaseTempReg(pParse, r1);
        sqlite3ReleaseTempReg(pParse, r2);
      }
      if( pKeyInfo ){
        sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO_HANDOFF);
      }
      break;
    }

    case TK_EXISTS:
    case TK_SELECT:
    default: {
      /* If this has to be a scalar SELECT.  Generate code to put the
      ** value of this select in a memory cell and record the number
      ** of the memory cell in iColumn.  If this is an EXISTS, write
      ** an integer 0 (not exists) or 1 (exists) into a memory cell
      ** and record that memory cell in iColumn.
      */
      Select *pSel;                         /* SELECT statement to encode */
      SelectDest dest;                      /* How to deal with SELECt result */

      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;
      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
      if( pExpr->op==TK_SELECT ){
        dest.eDest = SRT_Mem;
        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
        VdbeComment((v, "Init subquery result"));
      }else{
        dest.eDest = SRT_Exists;
        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
        VdbeComment((v, "Init EXISTS result"));
      }
      sqlite3ExprDelete(pParse->db, pSel->pLimit);
      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
                                  &sqlite3IntTokens[1]);
      pSel->iLimit = 0;
      if( sqlite3Select(pParse, pSel, &dest) ){
        return 0;
      }
      rReg = dest.iSDParm;
      ExprSetIrreducible(pExpr);
      break;
    }
  }

  if( testAddr>=0 ){
    sqlite3VdbeJumpHere(v, testAddr);
  }
  sqlite3ExprCachePop(pParse, 1);

  return rReg;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, -0x38(%rbp)
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r12
callq 0x41ef4
testq %rax, %rax
je 0x499b2
movq %rax, %r14
incl 0x6c(%r12)
testb $0x20, 0x2(%rbx)
jne 0x49659
movq %r12, %rdi
callq 0x49b86
jmp 0x4965c
pushq $-0x1
popq %rax
movl %eax, -0x30(%rbp)
cmpb $0x2, 0x200(%r12)
jne 0x496d0
cmpl $0x0, -0x30(%rbp)
leaq 0x26cd0(%rip), %rax     # 0x70345
leaq 0x24045(%rip), %rdx     # 0x6d6c1
cmovnsq %rax, %rdx
movq (%r12), %rdi
cmpb $0x48, (%rbx)
leaq 0x2403f(%rip), %rax     # 0x6d6cd
leaq 0x2403d(%rip), %rcx     # 0x6d6d2
cmoveq %rax, %rcx
movl 0x214(%r12), %r8d
leaq 0x24000(%rip), %rsi     # 0x6d6a8
xorl %eax, %eax
callq 0x10594
movl 0x210(%r12), %edx
orl $-0x1, (%rsp)
movq %r14, %rdi
movl $0x96, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rax, %r9
callq 0x41fd2
movb (%rbx), %al
cmpb $0x48, %al
jne 0x49754
movq 0x10(%rbx), %r13
testl %r15d, %r15d
je 0x496ef
pushq $0x18
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x41f42
movq %r13, %rdi
callq 0x4667c
movl %eax, %r13d
movb %al, -0x29(%rbp)
movl 0x54(%r12), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x54(%r12)
movl %edx, 0x2c(%rbx)
xorl %ecx, %ecx
cmpl $0x0, -0x38(%rbp)
sete %cl
pushq $0x33
popq %rsi
movq %r14, %rdi
callq 0x41f42
movl %eax, -0x4c(%rbp)
testl %r15d, %r15d
jne 0x4973f
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x4973f
movslq 0x34(%r14), %rcx
imulq $0x18, %rcx, %rcx
movb $0x8, -0x15(%rax,%rcx)
cmpl $0x0, -0x38(%rbp)
movl -0x30(%rbp), %r15d
je 0x497e8
xorl %edx, %edx
jmp 0x497f7
movq 0x20(%rbx), %r13
movl 0x58(%r12), %ecx
incl %ecx
movl %ecx, 0x58(%r12)
leaq -0x5c(%rbp), %rdx
movl %ecx, 0x4(%rdx)
movb $0x0, 0x1(%rdx)
andq $0x0, 0x8(%rdx)
xorl %edx, %edx
cmpb $0x74, %al
sete %dl
leal (%rdx,%rdx,2), %esi
addl $0x15, %esi
leal (%rdx,%rdx,2), %ebx
addl $0x3, %ebx
xorl %r15d, %r15d
movq %r14, %rdi
xorl %edx, %edx
callq 0x41f42
leaq -0x5c(%rbp), %rax
movb %bl, (%rax)
movq (%r12), %rdi
movq 0x68(%r13), %rsi
callq 0x2f476
leaq 0x3ccd2(%rip), %r8      # 0x86480
movq %r12, %rdi
movl $0x81, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x36a09
movq %rax, 0x68(%r13)
andl $0x0, 0xc(%r13)
movq %r12, %rdi
movq %r13, %rsi
leaq -0x5c(%rbp), %rdx
callq 0x380ad
testl %eax, %eax
jne 0x49a3a
movl -0x58(%rbp), %r15d
jmp 0x49a15
movq (%r12), %rdi
pushq $0x1
popq %rsi
callq 0x3107c
movq %rax, %rdx
testb $0x8, 0x3(%rbx)
jne 0x49975
movq %rbx, -0x48(%rbp)
movq 0x20(%rbx), %rbx
testq %rbx, %rbx
je 0x499d8
testb %r13b, %r13b
jne 0x4981b
movb $0x62, -0x29(%rbp)
testq %rdx, %rdx
movq %rdx, %r13
je 0x49837
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
movq %r12, %rdi
callq 0x466ee
movq %rax, 0x18(%r13)
movq %r13, -0x70(%rbp)
movq %r12, %rdi
callq 0x46ff9
movl %eax, -0x3c(%rbp)
movq %r12, -0x68(%rbp)
movq %r12, %rdi
callq 0x46ff9
pushq $0x18
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
movl %eax, -0x34(%rbp)
movl %eax, %ecx
callq 0x41f42
movl (%rbx), %r12d
movq 0x8(%rbx), %rbx
testl %r12d, %r12d
jle 0x499ba
movq (%rbx), %r13
testl %r15d, %r15d
js 0x49897
movq %r13, %rdi
callq 0x49607
testl %eax, %eax
jne 0x49897
movq %r14, %rdi
movl %r15d, %esi
callq 0x108cb
pushq $-0x1
popq %r15
cmpl $0x0, -0x38(%rbp)
je 0x498cb
movq %r13, %rdi
leaq -0x5c(%rbp), %rsi
callq 0x46204
testl %eax, %eax
je 0x498cb
movq -0x48(%rbp), %rax
movl 0x2c(%rax), %edx
movl -0x5c(%rbp), %r8d
movq %r14, %rdi
pushq $0x43
popq %rsi
movl -0x34(%rbp), %ecx
callq 0x41f4c
jmp 0x49969
movl %r15d, -0x30(%rbp)
movq %r14, %r15
movq -0x68(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
movl -0x3c(%rbp), %edx
callq 0x484da
movl %eax, %r13d
cmpl $0x0, -0x38(%rbp)
je 0x4991f
movq %r15, %r14
movl 0x34(%r15), %ecx
addl $0x2, %ecx
movq %r15, %rdi
pushq $0x21
popq %rsi
movl %r13d, %edx
callq 0x41f42
movq -0x48(%rbp), %rax
movl 0x2c(%rax), %edx
movq %r15, %rdi
pushq $0x42
popq %rsi
movl -0x34(%rbp), %ecx
movl %r13d, %r8d
callq 0x41f4c
jmp 0x49965
movl $0x1, (%rsp)
movq %r15, %rdi
pushq $0x2b
popq %rsi
movl %r13d, %edx
pushq $0x1
popq %rcx
movl -0x34(%rbp), %r8d
leaq -0x29(%rbp), %r9
callq 0x41fd2
movq %r14, %rdi
movq %r15, %r14
movl %r13d, %esi
pushq $0x1
popq %rdx
callq 0x49537
movq -0x48(%rbp), %rax
movl 0x2c(%rax), %edx
movq %r15, %rdi
pushq $0x67
popq %rsi
movl -0x34(%rbp), %ecx
callq 0x41f42
movl -0x30(%rbp), %r15d
decl %r12d
addq $0x20, %rbx
jmp 0x4986b
movq %rdx, %r15
movl 0x2c(%rbx), %eax
leaq -0x5c(%rbp), %rdx
movb $0x7, (%rdx)
movl %eax, 0x4(%rdx)
andq $0x0, 0x8(%rdx)
movb %r13b, 0x1(%rdx)
movq 0x20(%rbx), %rax
andl $0x0, 0xc(%rax)
movq 0x20(%rbx), %rsi
movq %r12, %rdi
callq 0x380ad
testl %eax, %eax
je 0x499e3
movq (%r12), %rdi
movq %r15, %rsi
callq 0x968b
xorl %r15d, %r15d
jmp 0x49a3a
movq -0x68(%rbp), %r12
movq %r12, %rdi
movl -0x3c(%rbp), %esi
callq 0x4701c
movq %r12, %rdi
movl -0x34(%rbp), %esi
callq 0x4701c
movq -0x70(%rbp), %rdx
movl %r15d, -0x30(%rbp)
testq %rdx, %rdx
jne 0x49a04
jmp 0x49a12
movq 0x10(%rbx), %rsi
movq 0x20(%rbx), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdx
movq %r12, %rdi
callq 0x49bae
movq %r15, %rdx
movq %rax, 0x18(%r15)
pushq $-0x10
popq %rcx
movq %r14, %rdi
movl -0x4c(%rbp), %esi
callq 0x1074b
xorl %r15d, %r15d
movl -0x30(%rbp), %edx
testl %edx, %edx
js 0x49a32
movl 0x34(%r14), %eax
cmpl %edx, %eax
jbe 0x49a32
movq 0x8(%r14), %rcx
movl %edx, %edx
imulq $0x18, %rdx, %rdx
movl %eax, 0x8(%rcx,%rdx)
movq %r12, %rdi
callq 0x4803b
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq