// File: /workspace/github_c_makefile_star2_O0/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
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5e000
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x6a107
movl $0x0, -0x4(%rbp)
jmp 0x6a6d8
movq -0x10(%rbp), %rdi
callq 0x67ee0
movq -0x18(%rbp), %rax
movzwl 0x2(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x6a12c
movq -0x10(%rbp), %rdi
callq 0x6aa40
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x200(%rax), %eax
cmpl $0x2, %eax
jne 0x6a1c1
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x24(%rbp), %ecx
leaq 0x4604c(%rip), %rdx     # 0xb019d
leaq 0x48c9d(%rip), %rax     # 0xb2df5
cmpl $0x0, %ecx
cmovgeq %rax, %rdx
movq -0x18(%rbp), %rax
movzbl (%rax), %esi
leaq 0x46041(%rip), %rcx     # 0xb01ae
leaq 0x46035(%rip), %rax     # 0xb01a9
cmpl $0x48, %esi
cmoveq %rax, %rcx
movq -0x10(%rbp), %rax
movl 0x214(%rax), %r8d
leaq 0x45ff7(%rip), %rsi     # 0xb0184
movb $0x0, %al
callq 0x137f0
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x210(%rax), %edx
movq -0x38(%rbp), %r9
movl $0x96, %esi
xorl %r8d, %r8d
movl %r8d, %ecx
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
callq 0x5e1b0
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0xbc(%rbp)
subl $0x14, %eax
je 0x6a5bd
jmp 0x6a1d9
movl -0xbc(%rbp), %eax
subl $0x48, %eax
je 0x6a1fa
jmp 0x6a1e6
movl -0xbc(%rbp), %eax
subl $0x74, %eax
je 0x6a5bd
jmp 0x6a5bf
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x6a227
movq -0x30(%rbp), %rdi
movl -0x1c(%rbp), %ecx
movl $0x18, %esi
xorl %edx, %edx
callq 0x5e0c0
movq -0x48(%rbp), %rdi
callq 0x658e0
movb %al, -0x39(%rbp)
movq -0x10(%rbp), %rax
movl 0x54(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x54(%rax)
movq -0x18(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %edx
cmpl $0x0, -0x20(%rbp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movl $0x33, %esi
callq 0x5e0c0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x6a283
movq -0x30(%rbp), %rdi
movl $0x8, %esi
callq 0x5e540
cmpl $0x0, -0x20(%rbp)
je 0x6a294
xorl %eax, %eax
movq %rax, -0xc8(%rbp)
jmp 0x6a2ac
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0x4a710
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x2(%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x800, %eax            # imm = 0x800
jne 0x6a36a
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %edx
leaq -0x60(%rbp), %rdi
movl $0x7, %esi
callq 0x5ef20
movb -0x39(%rbp), %al
movb %al, -0x5f(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
leaq -0x60(%rbp), %rdx
callq 0x529a0
cmpl $0x0, %eax
je 0x6a32f
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0x9cd0
movl $0x0, -0x4(%rbp)
jmp 0x6a6d8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdx
callq 0x6aa80
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x6a59c
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x6a59a
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x78(%rbp)
cmpb $0x0, -0x39(%rbp)
jne 0x6a38f
movb $0x62, -0x39(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x6a3b2
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x659b0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rdi
callq 0x66a70
movl %eax, -0x84(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66a70
movl %eax, -0x88(%rbp)
movq -0x30(%rbp), %rdi
movl -0x88(%rbp), %ecx
movl $0x18, %esi
xorl %edx, %edx
callq 0x5e0c0
movq -0x78(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
cmpl $0x0, -0x6c(%rbp)
jle 0x6a57c
movq -0x80(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
cmpl $0x0, -0x24(%rbp)
jl 0x6a43d
movq -0x98(%rbp), %rdi
callq 0x6a0a0
cmpl $0x0, %eax
jne 0x6a43d
movq -0x30(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x13e60
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0x20(%rbp)
je 0x6a482
movq -0x98(%rbp), %rdi
leaq -0x9c(%rbp), %rsi
callq 0x65040
cmpl $0x0, %eax
je 0x6a482
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %edx
movl -0x88(%rbp), %ecx
movl -0x9c(%rbp), %r8d
movl $0x43, %esi
callq 0x5e0f0
jmp 0x6a560
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
movl -0x84(%rbp), %edx
callq 0x687c0
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x6a504
movq -0x30(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0xcc(%rbp)
movq -0x30(%rbp), %rdi
callq 0x5e520
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %edx
movl %eax, %ecx
addl $0x2, %ecx
movl $0x21, %esi
callq 0x5e0c0
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %edx
movl -0x88(%rbp), %ecx
movl -0x8c(%rbp), %r8d
movl $0x42, %esi
callq 0x5e0f0
jmp 0x6a55e
movq -0x30(%rbp), %rdi
movl -0x8c(%rbp), %edx
movl -0x88(%rbp), %r8d
movl $0x2b, %esi
movl $0x1, %ecx
leaq -0x39(%rbp), %r9
movl $0x1, (%rsp)
callq 0x5e1b0
movq -0x10(%rbp), %rdi
movl -0x8c(%rbp), %esi
movl $0x1, %edx
callq 0x69c40
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %edx
movl -0x88(%rbp), %ecx
movl $0x67, %esi
callq 0x5e0c0
jmp 0x6a560
jmp 0x6a562
movl -0x6c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x80(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x80(%rbp)
jmp 0x6a3fb
movq -0x10(%rbp), %rdi
movl -0x84(%rbp), %esi
callq 0x66ac0
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %esi
callq 0x66ac0
jmp 0x6a59c
cmpq $0x0, -0x50(%rbp)
je 0x6a5b8
movq -0x30(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x50(%rbp), %rdx
movl $0xfffffff0, %ecx       # imm = 0xFFFFFFF0
callq 0x13c20
jmp 0x6a6b2
jmp 0x6a5bf
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %edx
addl $0x1, %edx
movl %edx, 0x58(%rax)
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
callq 0x5ef20
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x74, %eax
jne 0x6a614
movb $0x6, -0xb8(%rbp)
movq -0x30(%rbp), %rdi
movl -0xb4(%rbp), %ecx
movl $0x18, %esi
xorl %edx, %edx
callq 0x5e0c0
jmp 0x6a631
movb $0x3, -0xb8(%rbp)
movq -0x30(%rbp), %rdi
movl -0xb4(%rbp), %ecx
movl $0x15, %esi
xorl %edx, %edx
callq 0x5e0c0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0xa8(%rbp), %rax
movq 0x68(%rax), %rsi
callq 0x47330
movq -0x10(%rbp), %rdi
movl $0x81, %esi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x64e84(%rip), %r8      # 0xcf4e0
addq $0x10, %r8
movq %rcx, %rdx
callq 0x508b0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0xa8(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rdi
movq -0xa8(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
callq 0x529a0
cmpl $0x0, %eax
je 0x6a6a9
movl $0x0, -0x4(%rbp)
jmp 0x6a6d8
movl -0xb4(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x24(%rbp)
jl 0x6a6c4
movq -0x30(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x5e090
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x68220
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)