// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movl %ecx, -0x50(%rbp)
movl %edx, %r15d
movq %rsi, %r13
movq %rdi, -0x38(%rbp)
callq 0x45ece
testq %rax, %rax
je 0x4eaf2
movq %rax, %r14
movq -0x38(%rbp), %rax
incl 0x6c(%rax)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testb $0x20, 0x2(%r13)
jne 0x4e842
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x45ece
movl 0x60(%rbx), %edx
leal 0x1(%rdx), %ecx
movl %ecx, 0x60(%rbx)
movq %rax, %rdi
movl $0x26, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movl %eax, %r12d
movq -0x38(%rbp), %rax
cmpb $0x2, 0x200(%rax)
movq %r13, -0x58(%rbp)
jne 0x4e8cc
testl %r12d, %r12d
leaq 0x2fb53(%rip), %rax     # 0x7e3b0
leaq 0x2ce3d(%rip), %rdx     # 0x7b6a1
cmovnsq %rax, %rdx
movq -0x38(%rbp), %rbx
movq (%rbx), %rdi
cmpb $0x48, (%r13)
leaq 0x2ce32(%rip), %rax     # 0x7b6ad
leaq 0x2ce30(%rip), %rcx     # 0x7b6b2
cmoveq %rax, %rcx
movl 0x214(%rbx), %r8d
leaq 0x2cdf4(%rip), %rsi     # 0x7b688
xorl %eax, %eax
callq 0x11900
movq %rax, %r13
movl 0x210(%rbx), %edx
movq %r14, %rdi
movl $0x96, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq %r14, %rdi
movl %eax, %esi
movq %r13, %rdx
movq -0x58(%rbp), %r13
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x11a9f
movb (%r13), %al
cmpb $0x48, %al
jne 0x4e95f
movq 0x10(%r13), %r13
testl %r15d, %r15d
je 0x4e8f6
movq %r14, %rdi
movl $0x18, %esi
xorl %edx, %edx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq %r13, %rdi
callq 0x4b192
movb %al, -0x29(%rbp)
movq -0x38(%rbp), %rcx
movl 0x54(%rcx), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x54(%rcx)
movq -0x58(%rbp), %r13
movl %edx, 0x2c(%r13)
xorl %ecx, %ecx
cmpl $0x0, -0x50(%rbp)
sete %cl
movq %r14, %rdi
movl $0x33, %esi
xorl %r8d, %r8d
callq 0x45f15
movl %eax, -0x5c(%rbp)
testl %r15d, %r15d
jne 0x4e94d
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x4e94d
movslq 0x34(%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x8, -0x15(%rax,%rcx,8)
cmpl $0x0, -0x50(%rbp)
je 0x4ea15
xorl %r15d, %r15d
jmp 0x4ea29
movl %r12d, -0x4c(%rbp)
movq 0x20(%r13), %r12
movq -0x38(%rbp), %rbx
movl 0x58(%rbx), %ecx
incl %ecx
movl %ecx, 0x58(%rbx)
leaq -0x88(%rbp), %r13
movl %ecx, 0x4(%r13)
movb $0x0, 0x1(%r13)
movq $0x0, 0x8(%r13)
xorl %edx, %edx
cmpb $0x74, %al
sete %dl
leal (%rdx,%rdx,2), %esi
addl $0x15, %esi
movq %r14, %rdi
leal (%rdx,%rdx,2), %r14d
addl $0x3, %r14d
xorl %r15d, %r15d
movq %rdi, -0x48(%rbp)
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x45f15
movb %r14b, (%r13)
movq (%rbx), %rdi
movq 0x68(%r12), %rsi
callq 0x3479d
leaq 0x44a64(%rip), %r8      # 0x93430
movq %rbx, %rdi
movl $0x81, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3a637
movq %rax, 0x68(%r12)
movl $0x0, 0xc(%r12)
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3bffc
testl %eax, %eax
jne 0x4edd0
movl -0x84(%rbp), %r15d
movq -0x48(%rbp), %r14
movl -0x4c(%rbp), %r12d
jmp 0x4edaa
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0x366de
movq %rax, %r15
testb $0x8, 0x3(%r13)
jne 0x4eaa8
movq %r14, -0x48(%rbp)
movq 0x20(%r13), %r14
testq %r14, %r14
je 0x4eafa
cmpb $0x0, -0x29(%rbp)
jne 0x4ea4b
movb $0x62, -0x29(%rbp)
testq %r15, %r15
je 0x4ea61
movq 0x10(%r13), %rsi
movq -0x38(%rbp), %rdi
callq 0x4b225
movq %rax, 0x18(%r15)
movq -0x38(%rbp), %rax
movb 0x1f(%rax), %al
testb %al, %al
movq %r15, -0x90(%rbp)
je 0x4eb27
leal -0x1(%rax), %ecx
movq -0x38(%rbp), %rdx
movb %cl, 0x1f(%rdx)
movzbl %cl, %ecx
movl 0x28(%rdx,%rcx,4), %edx
movl %edx, -0x3c(%rbp)
testb %cl, %cl
je 0x4eb36
addb $-0x2, %al
movq -0x38(%rbp), %rcx
movb %al, 0x1f(%rcx)
movzbl %al, %eax
movl 0x28(%rcx,%rax,4), %ecx
jmp 0x4eb42
movl 0x2c(%r13), %eax
leaq -0x88(%rbp), %rdx
movb $0x7, (%rdx)
movl %eax, 0x4(%rdx)
movq $0x0, 0x8(%rdx)
movb -0x29(%rbp), %al
movb %al, 0x1(%rdx)
movq 0x20(%r13), %rax
movl $0x0, 0xc(%rax)
movq 0x20(%r13), %rsi
movq -0x38(%rbp), %rdi
callq 0x3bffc
testl %eax, %eax
je 0x4eb03
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0x9ca4
xorl %r15d, %r15d
jmp 0x4edd0
movq -0x48(%rbp), %r14
jmp 0x4ed8f
movq 0x10(%r13), %rsi
movq 0x20(%r13), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdx
movq -0x38(%rbp), %rdi
callq 0x4ef2c
movq %rax, 0x18(%r15)
jmp 0x4ed94
movq -0x38(%rbp), %rax
movl 0x58(%rax), %ecx
incl %ecx
movl %ecx, -0x3c(%rbp)
movl %ecx, 0x58(%rax)
movq -0x38(%rbp), %rax
movl 0x58(%rax), %ecx
incl %ecx
movl %ecx, 0x58(%rax)
movq -0x48(%rbp), %rdi
movl $0x18, %esi
xorl %edx, %edx
movl %ecx, -0x40(%rbp)
xorl %r8d, %r8d
callq 0x45f15
movl (%r14), %r15d
testl %r15d, %r15d
jle 0x4ece8
movq 0x8(%r14), %r14
incl %r15d
leaq -0x88(%rbp), %rbx
movq (%r14), %r13
testl %r12d, %r12d
js 0x4ebcf
leaq -0x78(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl $0x1, -0x68(%rbp)
leaq -0x2fe5(%rip), %rax     # 0x4bbb5
movq %rax, -0x88(%rbp)
leaq -0x2fb1(%rip), %rax     # 0x4bbf7
movq %rax, -0x80(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x473bb
cmpl $0x0, -0x68(%rbp)
jne 0x4ebcf
movq -0x48(%rbp), %rdi
movl %r12d, %esi
callq 0x11c3f
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x0, -0x50(%rbp)
je 0x4ec07
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4ac9f
testl %eax, %eax
je 0x4ec80
movq -0x58(%rbp), %rax
movl 0x2c(%rax), %edx
movl -0x88(%rbp), %r8d
movq -0x48(%rbp), %rdi
movl $0x43, %esi
movl -0x40(%rbp), %ecx
jmp 0x4ecd2
movq -0x38(%rbp), %rdi
movq %r13, %rsi
movl -0x3c(%rbp), %edx
callq 0x4d33b
movl %eax, %r13d
movl %r12d, -0x4c(%rbp)
movq -0x48(%rbp), %r12
movq %r12, %rdi
movl $0x2b, %esi
movl %eax, %edx
movl $0x1, %ecx
movl -0x40(%rbp), %ebx
movl %ebx, %r8d
callq 0x45f15
movq %r12, %rdi
movl %eax, %esi
leaq -0x29(%rbp), %rdx
movl $0x1, %ecx
callq 0x11a9f
movq -0x38(%rbp), %rdi
movl %r13d, %esi
movl $0x1, %edx
callq 0x4e564
movq -0x58(%rbp), %rax
movl 0x2c(%rax), %edx
movq %r12, %rdi
movl -0x4c(%rbp), %r12d
movl $0x67, %esi
movl %ebx, %ecx
leaq -0x88(%rbp), %rbx
xorl %r8d, %r8d
jmp 0x4ecd2
movq -0x38(%rbp), %rdi
movq %r13, %rsi
movl -0x3c(%rbp), %edx
callq 0x4d33b
movl %eax, %r13d
movl %r12d, %ebx
movq -0x48(%rbp), %r12
movl 0x34(%r12), %ecx
addl $0x2, %ecx
movq %r12, %rdi
movl $0x21, %esi
movl %eax, %edx
xorl %r8d, %r8d
callq 0x45f15
movq -0x58(%rbp), %rax
movl 0x2c(%rax), %edx
movq %r12, %rdi
movl %ebx, %r12d
leaq -0x88(%rbp), %rbx
movl $0x42, %esi
movl -0x40(%rbp), %ecx
movl %r13d, %r8d
callq 0x45f15
addq $0x20, %r14
decl %r15d
cmpl $0x1, %r15d
jg 0x4eb72
cmpl $0x0, -0x3c(%rbp)
movq -0x48(%rbp), %r14
movq -0x90(%rbp), %r15
je 0x4ed41
movq -0x38(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpq $0x7, %rax
ja 0x4ed41
xorl %ecx, %ecx
movq -0x38(%rbp), %rdx
movl -0x3c(%rbp), %esi
cmpl %esi, 0x84(%rdx,%rcx)
je 0x4ed38
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x4ed10
leal 0x1(%rax), %ecx
movq -0x38(%rbp), %rdx
movb %cl, 0x1f(%rdx)
movl -0x3c(%rbp), %ecx
movl %ecx, 0x28(%rdx,%rax,4)
jmp 0x4ed41
movq -0x38(%rbp), %rax
movb $0x1, 0x7c(%rax,%rcx)
cmpl $0x0, -0x40(%rbp)
je 0x4ed8f
movq -0x38(%rbp), %rax
movzbl 0x1f(%rax), %eax
cmpq $0x7, %rax
ja 0x4ed8f
xorl %ecx, %ecx
movl -0x40(%rbp), %edx
movq -0x38(%rbp), %rsi
cmpl %edx, 0x84(%rsi,%rcx)
je 0x4ed86
addq $0x18, %rcx
cmpl $0xf0, %ecx
jne 0x4ed5e
leal 0x1(%rax), %ecx
movq -0x38(%rbp), %rdx
movb %cl, 0x1f(%rdx)
movl -0x40(%rbp), %ecx
movl %ecx, 0x28(%rdx,%rax,4)
jmp 0x4ed8f
movq -0x38(%rbp), %rax
movb $0x1, 0x7c(%rax,%rcx)
testq %r15, %r15
je 0x4eda7
movq %r14, %rdi
movl -0x5c(%rbp), %esi
movq %r15, %rdx
movl $0xfffffff0, %ecx       # imm = 0xFFFFFFF0
callq 0x11a9f
xorl %r15d, %r15d
testl %r12d, %r12d
js 0x4edc7
movl 0x34(%r14), %eax
cmpl %r12d, %eax
jbe 0x4edc7
movq 0x8(%r14), %rcx
movl %r12d, %edx
leaq (%rdx,%rdx,2), %rdx
movl %eax, 0x8(%rcx,%rdx,8)
movq -0x38(%rbp), %rdi
callq 0x4cd64
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq