// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3DeleteFrom
SQLITE_PRIVATE void sqlite3DeleteFrom(
  Parse *pParse,         /* The parser context */
  SrcList *pTabList,     /* The table from which we should delete things */
  Expr *pWhere           /* The WHERE clause.  May be null */
){
  Vdbe *v;               /* The virtual database engine */
  Table *pTab;           /* The table from which records will be deleted */
  const char *zDb;       /* Name of database holding pTab */
  int end, addr = 0;     /* A couple addresses of generated code */
  int i;                 /* Loop counter */
  WhereInfo *pWInfo;     /* Information about the WHERE clause */
  Index *pIdx;           /* For looping over indices of the table */
  int iCur;              /* VDBE Cursor number for pTab */
  sqlite3 *db;           /* Main database structure */
  AuthContext sContext;  /* Authorization context */
  NameContext sNC;       /* Name context to resolve expressions in */
  int iDb;               /* Database number */
  int memCnt = -1;       /* Memory cell used for change counting */
  int rcauth;            /* Value returned by authorization callback */

#ifndef SQLITE_OMIT_TRIGGER
  int isView;                  /* True if attempting to delete from a view */
  Trigger *pTrigger;           /* List of table triggers, if required */
#endif

  memset(&sContext, 0, sizeof(sContext));
  db = pParse->db;
  if( pParse->nErr || db->mallocFailed ){
    goto delete_from_cleanup;
  }
  assert( pTabList->nSrc==1 );

  /* Locate the table which we want to delete.  This table has to be
  ** put in an SrcList structure because some of the subroutines we
  ** will be calling are designed to work with multiple tables and expect
  ** an SrcList* parameter instead of just a Table* parameter.
  */
  pTab = sqlite3SrcListLookup(pParse, pTabList);
  if( pTab==0 )  goto delete_from_cleanup;

  /* Figure out if we have any triggers and if the table being
  ** deleted from is a view
  */
#ifndef SQLITE_OMIT_TRIGGER
  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
  isView = pTab->pSelect!=0;
#else
# define pTrigger 0
# define isView 0
#endif
#ifdef SQLITE_OMIT_VIEW
# undef isView
# define isView 0
#endif

  /* If pTab is really a view, make sure it has been initialized.
  */
  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
    goto delete_from_cleanup;
  }

  if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
    goto delete_from_cleanup;
  }
  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  assert( iDb<db->nDb );
  zDb = db->aDb[iDb].zName;
  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
  assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
  if( rcauth==SQLITE_DENY ){
    goto delete_from_cleanup;
  }
  assert(!isView || pTrigger);

  /* Assign  cursor number to the table and all its indices.
  */
  assert( pTabList->nSrc==1 );
  iCur = pTabList->a[0].iCursor = pParse->nTab++;
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
    pParse->nTab++;
  }

  /* Start the view context
  */
  if( isView ){
    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
  }

  /* Begin generating code.
  */
  v = sqlite3GetVdbe(pParse);
  if( v==0 ){
    goto delete_from_cleanup;
  }
  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
  sqlite3BeginWriteOperation(pParse, 1, iDb);

  /* If we are trying to delete from a view, realize that view into
  ** a ephemeral table.
  */
#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
  if( isView ){
    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
  }
#endif

  /* Resolve the column names in the WHERE clause.
  */
  memset(&sNC, 0, sizeof(sNC));
  sNC.pParse = pParse;
  sNC.pSrcList = pTabList;
  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
    goto delete_from_cleanup;
  }

  /* Initialize the counter of the number of rows deleted, if
  ** we are counting rows.
  */
  if( db->flags & SQLITE_CountRows ){
    memCnt = ++pParse->nMem;
    sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
  }

#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
  /* Special case: A DELETE without a WHERE clause deletes everything.
  ** It is easier just to erase the whole table. Prior to version 3.6.5,
  ** this optimization caused the row change count (the value returned by 
  ** API function sqlite3_count_changes) to be set incorrectly.  */
  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
   && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
  ){
    assert( !isView );
    sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
                      pTab->zName, P4_STATIC);
    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
      assert( pIdx->pSchema==pTab->pSchema );
      sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
    }
  }else
#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
  /* The usual case: There is a WHERE clause so we have to scan through
  ** the table and pick which records to delete.
  */
  {
    int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
    int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
    int regRowid;                   /* Actual register containing rowids */

    /* Collect rowids of every row to be deleted.
    */
    sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
    pWInfo = sqlite3WhereBegin(
        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
    );
    if( pWInfo==0 ) goto delete_from_cleanup;
    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
    sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
    if( db->flags & SQLITE_CountRows ){
      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
    }
    sqlite3WhereEnd(pWInfo);

    /* Delete every item whose key was written to the list during the
    ** database scan.  We have to delete items after the scan is complete
    ** because deleting an item can change the scan order.  */
    end = sqlite3VdbeMakeLabel(v);

    /* Unless this is a view, open cursors for the table we are 
    ** deleting from and all its indices. If this is a view, then the
    ** only effect this statement has is to fire the INSTEAD OF 
    ** triggers.  */
    if( !isView ){
      sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
    }

    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);

    /* Delete the row */
#ifndef SQLITE_OMIT_VIRTUALTABLE
    if( IsVirtual(pTab) ){
      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
      sqlite3VtabMakeWritable(pParse, pTab);
      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
      sqlite3VdbeChangeP5(v, OE_Abort);
      sqlite3MayAbort(pParse);
    }else
#endif
    {
      int count = (pParse->nested==0);    /* True to count changes */
      sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
    }

    /* End of the delete loop */
    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
    sqlite3VdbeResolveLabel(v, end);

    /* Close the cursors open on the table and its indexes. */
    if( !isView && !IsVirtual(pTab) ){
      for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
        sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
      }
      sqlite3VdbeAddOp1(v, OP_Close, iCur);
    }
  }

  /* Update the sqlite_sequence table by storing the content of the
  ** maximum rowid counter values recorded while inserting into
  ** autoincrement tables.
  */
  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
    sqlite3AutoincrementEnd(pParse);
  }

  /* Return the number of rows that were deleted. If this routine is 
  ** generating code because of a call to sqlite3NestedParse(), do not
  ** invoke the callback function.
  */
  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
    sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
    sqlite3VdbeSetNumCols(v, 1);
    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
  }

delete_from_cleanup:
  sqlite3AuthContextPop(&sContext);
  sqlite3SrcListDelete(db, pTabList);
  sqlite3ExprDelete(db, pWhere);
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq (%rdi), %r14
cmpl $0x0, 0x50(%rdi)
jne 0x3df34
cmpb $0x0, 0x50(%r14)
je 0x3df5c
movq %r14, %rdi
movq %r15, %rsi
callq 0x3493b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3479d
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r12
movq %r15, %rsi
callq 0x4bc09
testq %rax, %rax
je 0x3df34
movq %r12, %rdi
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rsi
movl $0x6a, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x5be5e
movq %rax, %r13
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x4a4d2
testl %eax, %eax
jne 0x3df34
xorl %edx, %edx
testq %r13, %r13
setne %dl
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x5bf03
movq -0x30(%rbp), %rsi
testl %eax, %eax
jne 0x3df34
movq %r13, -0x78(%rbp)
movq 0x68(%rsi), %rax
testq %rax, %rax
je 0x3dffb
movl 0x28(%r14), %ecx
testl %ecx, %ecx
jle 0x3e003
movq 0x20(%r14), %rdx
addq $0x18, %rdx
xorl %r13d, %r13d
cmpq %rax, (%rdx)
je 0x3e006
incq %r13
addq $0x20, %rdx
cmpq %r13, %rcx
jne 0x3dfe5
movl %ecx, %r13d
jmp 0x3e006
movl $0xfff0bdc0, %r13d      # imm = 0xFFF0BDC0
jmp 0x3e006
xorl %r13d, %r13d
movq 0x20(%r14), %rax
movslq %r13d, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r8
movq (%rsi), %rdx
movq %r12, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x460eb
movq -0x30(%rbp), %rdx
cmpl $0x1, %eax
je 0x3df34
movl %eax, -0x3c(%rbp)
movl 0x54(%r12), %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x54(%r12)
movq %rcx, -0x48(%rbp)
movl %ecx, 0x44(%r15)
movq 0x10(%rdx), %rcx
testq %rcx, %rcx
je 0x3e065
incl %eax
movq 0x28(%rcx), %rcx
testq %rcx, %rcx
jne 0x3e055
movl %eax, 0x54(%r12)
cmpq $0x0, -0x50(%rbp)
je 0x3e089
movq (%rdx), %rax
movq 0x248(%r12), %rcx
movq %rcx, -0x68(%rbp)
movq %rax, 0x248(%r12)
movq %r12, -0x70(%rbp)
jmp 0x3e099
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq %r12, %rdi
callq 0x45ece
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x3e114
cmpb $0x0, 0x1e(%r12)
jne 0x3e0bd
movq -0x38(%rbp), %rax
orb $0x10, 0x9a(%rax)
movq %r12, %rdi
movl $0x1, %esi
movl %r13d, %edx
callq 0x463e1
cmpq $0x0, -0x50(%rbp)
je 0x3e0e7
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq %rbx, %rdx
movq -0x48(%rbp), %rcx
callq 0x5bf73
xorps %xmm0, %xmm0
leaq -0xb0(%rbp), %rdi
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq $0x0, 0x30(%rdi)
movq %r12, (%rdi)
movq %r15, 0x8(%rdi)
movq %rbx, %rsi
callq 0x46b2f
testl %eax, %eax
je 0x3e131
movq -0x70(%rbp), %rcx
testq %rcx, %rcx
je 0x3df34
movq -0x68(%rbp), %rax
movq %rax, 0x248(%rcx)
jmp 0x3df34
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
testb $-0x80, 0x2c(%r14)
je 0x3e161
movl 0x58(%r12), %ecx
incl %ecx
movl %ecx, 0x58(%r12)
movq -0x38(%rbp), %rdi
movl $0x15, %esi
xorl %edx, %edx
movl %ecx, -0x40(%rbp)
xorl %r8d, %r8d
callq 0x45f15
cmpq $0x0, -0x78(%rbp)
movq -0x30(%rbp), %rdi
jne 0x3e215
testq %rbx, %rbx
jne 0x3e215
cmpl $0x0, -0x3c(%rbp)
jne 0x3e215
testb $0x10, 0x46(%rdi)
jne 0x3e215
movq (%r12), %rax
testb $0x8, 0x2e(%rax)
jne 0x3e200
movl 0x3c(%rdi), %edx
movq (%rdi), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movl $0x6d, %esi
movl %r13d, %ecx
movl -0x40(%rbp), %r8d
callq 0x45f15
movq -0x38(%rbp), %rdi
movl %eax, %esi
movq -0x50(%rbp), %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x11a9f
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x3e459
movq %rax, -0x30(%rbp)
movl 0x50(%rax), %edx
movq -0x38(%rbp), %rdi
movl $0x6d, %esi
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
jmp 0x3e1d2
callq 0x4fce3
movq -0x30(%rbp), %rdi
testq %rax, %rax
jne 0x3e215
cmpq $0x0, 0x20(%rdi)
je 0x3e197
movl 0x58(%r12), %r13d
leal 0x1(%r13), %ecx
addl $0x2, %r13d
movl %r13d, 0x58(%r12)
movq -0x38(%rbp), %rdi
movl $0x18, %esi
xorl %edx, %edx
movl %ecx, -0x3c(%rbp)
xorl %r8d, %r8d
callq 0x45f15
movl $0x0, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x8, %r9d
callq 0x51e94
testq %rax, %rax
je 0x3e114
movq %rax, -0x58(%rbp)
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rcx
movq %r13, -0x60(%rbp)
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x4e1f8
movq -0x38(%rbp), %r13
movq %r13, %rdi
movl $0x76, %esi
movl -0x3c(%rbp), %edx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x45f15
testb $-0x80, 0x2c(%r14)
je 0x3e2c1
movq %r13, %rdi
movl $0x20, %esi
movl -0x40(%rbp), %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq -0x58(%rbp), %rdi
callq 0x54d64
movq %r13, %rdi
callq 0x4c5eb
cmpq $0x0, -0x50(%rbp)
jne 0x3e2f8
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl $0x31, %ecx
movl %eax, %r13d
callq 0x5c2a8
movl %r13d, %eax
movq -0x38(%rbp), %r13
movq %r13, %rdi
movl $0x77, %esi
movl -0x3c(%rbp), %edx
movl %eax, -0x3c(%rbp)
movl %eax, %ecx
movq -0x60(%rbp), %r13
movl %r13d, %r8d
callq 0x45f15
movq -0x30(%rbp), %rsi
testb $0x10, 0x46(%rsi)
movl %eax, -0x58(%rbp)
jne 0x3e34e
xorl %r8d, %r8d
cmpb $0x0, 0x1e(%r12)
sete %r8b
movl $0x63, (%rsp)
movq %r12, %rdi
movq -0x48(%rbp), %rdx
movl %r13d, %ecx
movq -0x78(%rbp), %r9
callq 0x5c450
movq -0x38(%rbp), %r13
jmp 0x3e3c6
movq 0x58(%rsi), %r13
testq %r13, %r13
je 0x3e363
cmpq %r14, (%r13)
je 0x3e366
movq 0x28(%r13), %r13
jmp 0x3e352
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x5c3c5
movq -0x38(%rbp), %rdi
movl $0xd, %esi
xorl %edx, %edx
movl $0x1, %ecx
movq -0x60(%rbp), %r8
callq 0x45f15
movq -0x38(%rbp), %rdi
movl %eax, %esi
movq %r13, %rdx
movq %rdi, %r13
movl $0xfffffff6, %ecx       # imm = 0xFFFFFFF6
callq 0x11a9f
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x3e3b3
movslq 0x34(%r13), %rcx
leaq (%rcx,%rcx,2), %rcx
movb $0x2, -0x15(%rax,%rcx,8)
movq 0x1d8(%r12), %rax
testq %rax, %rax
cmoveq %r12, %rax
movb $0x1, 0x24(%rax)
movq %r13, %rdi
movl $0xe, %esi
xorl %edx, %edx
movl -0x58(%rbp), %ecx
xorl %r8d, %r8d
callq 0x45f15
movl -0x3c(%rbp), %edx
testl %edx, %edx
jns 0x3e3f6
movq 0x40(%r13), %rax
testq %rax, %rax
je 0x3e3f6
notl %edx
movl 0x34(%r13), %ecx
movl %edx, %edx
movl %ecx, (%rax,%rdx,4)
cmpq $0x0, -0x50(%rbp)
movq -0x30(%rbp), %rax
jne 0x3e459
testb $0x10, 0x46(%rax)
jne 0x3e459
movq %r13, %rdi
movq 0x10(%rax), %r13
testq %r13, %r13
je 0x3e446
movq -0x48(%rbp), %rax
incl %eax
movl %eax, -0x30(%rbp)
movl 0x50(%r13), %ecx
movq -0x38(%rbp), %rdi
movl $0x36, %esi
movl -0x30(%rbp), %edx
xorl %r8d, %r8d
callq 0x45f15
movl -0x30(%rbp), %eax
movq -0x38(%rbp), %rdi
movq 0x28(%r13), %r13
incl %eax
testq %r13, %r13
jne 0x3e419
movl $0x36, %esi
movq -0x48(%rbp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45f15
cmpb $0x0, 0x1e(%r12)
jne 0x3e474
cmpq $0x0, 0x1e0(%r12)
jne 0x3e474
movq %r12, %rdi
callq 0x5c6c0
testb $-0x80, 0x2c(%r14)
je 0x3e114
cmpb $0x0, 0x1e(%r12)
jne 0x3e114
cmpq $0x0, 0x1e0(%r12)
jne 0x3e114
movq -0x38(%rbp), %r12
movq %r12, %rdi
movl $0x1e, %esi
movl -0x40(%rbp), %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x45f15
movq %r12, %rdi
movl $0x1, %esi
callq 0x3754d
movq (%r12), %rax
cmpb $0x0, 0x50(%rax)
jne 0x3e114
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x3d84c(%rip), %rsi     # 0x7bd2c
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xf0d7
jmp 0x3e114