// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq (%rdi), %r14
cmpl $0x0, 0x50(%rdi)
jne 0x3b93e
cmpb $0x0, 0x50(%r14)
je 0x3b966
movq %r14, %rdi
movq %r15, %rsi
callq 0x2f60e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2f476
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r12
movq %r15, %rsi
callq 0x470f1
testq %rax, %rax
je 0x3b93e
pushq $0x6a
popq %rdx
movq %r12, %rdi
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x52d34
movq %rax, %r13
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x45ad0
testl %eax, %eax
jne 0x3b93e
xorl %edx, %edx
testq %r13, %r13
setne %dl
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x52dbf
testl %eax, %eax
jne 0x3b93e
movq -0x40(%rbp), %rax
movq 0x68(%rax), %rsi
movq %r14, %rdi
callq 0x1064e
movq 0x20(%r14), %rdx
movl %eax, -0x2c(%rbp)
movslq %eax, %rcx
shlq $0x5, %rcx
movq (%rdx,%rcx), %r8
movq -0x40(%rbp), %rax
movq (%rax), %rdx
pushq $0x9
popq %rsi
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x42151
movq -0x40(%rbp), %rdx
cmpl $0x1, %eax
je 0x3b93e
movl %eax, -0x50(%rbp)
movq %r13, -0x58(%rbp)
movl 0x54(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x6c(%rbp)
movl %ecx, 0x54(%r12)
movl %eax, 0x44(%r15)
movq %rdx, %rcx
addq $0x10, %rcx
movq %rax, -0x60(%rbp)
addl $0x2, %eax
movq %rcx, -0x68(%rbp)
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x3ba4e
movl %eax, 0x54(%r12)
addq $0x28, %rcx
incl %eax
jmp 0x3ba39
cmpq $0x0, -0x78(%rbp)
je 0x3ba75
movq (%rdx), %rax
movq 0x248(%r12), %rcx
movq %rcx, -0x80(%rbp)
movq %rax, 0x248(%r12)
movq %r12, -0x88(%rbp)
jmp 0x3ba88
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq %r12, %rdi
callq 0x41ef4
testq %rax, %rax
je 0x3bafe
cmpb $0x0, 0x1e(%r12)
jne 0x3baa4
orb $0x10, 0x9a(%rax)
movq %rax, -0x38(%rbp)
pushq $0x1
popq %rsi
movq %r12, %rdi
movl -0x2c(%rbp), %edx
callq 0x423f5
cmpq $0x0, -0x78(%rbp)
je 0x3bad0
movq %r12, %rdi
movq -0x40(%rbp), %rsi
movq %rbx, %rdx
movq -0x60(%rbp), %rcx
callq 0x52e45
xorps %xmm0, %xmm0
leaq -0xc8(%rbp), %rdi
andq $0x0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq %r12, (%rdi)
movq %r15, 0x8(%rdi)
movq %rbx, %rsi
callq 0x42880
testl %eax, %eax
movq -0x38(%rbp), %rdi
je 0x3bb1e
movq -0x88(%rbp), %rcx
testq %rcx, %rcx
je 0x3b93e
movq -0x80(%rbp), %rax
movq %rax, 0x248(%rcx)
jmp 0x3b93e
testb $-0x80, 0x2c(%r14)
jne 0x3bb2d
pushq $-0x1
popq %rax
movl %eax, -0x44(%rbp)
jmp 0x3bb46
movl 0x58(%r12), %ecx
incl %ecx
movl %ecx, 0x58(%r12)
pushq $0x15
popq %rsi
xorl %edx, %edx
movl %ecx, -0x44(%rbp)
callq 0x41f42
movq -0x58(%rbp), %rax
testq %rax, %rax
movq -0x40(%rbp), %rax
jne 0x3bb80
testq %rbx, %rbx
jne 0x3bb80
cmpl $0x0, -0x50(%rbp)
jne 0x3bb80
testb $0x10, 0x46(%rax)
jne 0x3bb80
movq %r12, %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x52f34
movq -0x40(%rbp), %rcx
testl %eax, %eax
je 0x3be0d
movl 0x58(%r12), %r13d
leal 0x1(%r13), %ecx
addl $0x2, %r13d
movl %r13d, 0x58(%r12)
pushq $0x18
popq %rsi
movq -0x38(%rbp), %rdi
xorl %edx, %edx
movl %ecx, -0x2c(%rbp)
callq 0x41f42
andl $0x0, (%rsp)
pushq $0x8
popq %r9
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4a969
testq %rax, %rax
je 0x3bafe
movq %rax, -0x50(%rbp)
pushq $-0x1
popq %rdx
movq %r12, %rdi
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rcx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x4923a
pushq $0x76
popq %rsi
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %edx
movl %eax, %ecx
callq 0x41f42
testb $-0x80, 0x2c(%r14)
je 0x3bc0e
pushq $0x20
popq %rsi
pushq $0x1
popq %rcx
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %edx
callq 0x41f42
movq -0x50(%rbp), %rdi
callq 0x4d5c3
movq -0x38(%rbp), %rdi
callq 0x47934
cmpq $0x0, -0x78(%rbp)
movl %eax, -0x50(%rbp)
jne 0x3bc40
pushq $0x31
popq %rcx
movq %r12, %rdi
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x530c4
movl -0x50(%rbp), %eax
pushq $0x77
popq %rsi
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %edx
movl %eax, %ecx
movl %r13d, %r8d
callq 0x41f4c
movl %eax, -0x2c(%rbp)
movq %r13, %rcx
movq -0x40(%rbp), %r13
testb $0x10, 0x46(%r13)
jne 0x3bc8e
xorl %r8d, %r8d
cmpb $0x0, 0x1e(%r12)
sete %r8b
movl $0x63, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %r9
callq 0x5322b
jmp 0x3bcfe
movq %r14, %rdi
movq %r13, %rsi
movq %rcx, -0x58(%rbp)
callq 0x3095c
movq %rax, -0x90(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x531a3
movl $0xfffffff6, (%rsp)     # imm = 0xFFFFFFF6
pushq $0xd
popq %rsi
pushq $0x1
popq %rcx
movq -0x38(%rbp), %r13
movq %r13, %rdi
xorl %edx, %edx
movq -0x58(%rbp), %r8
movq -0x90(%rbp), %r9
callq 0x41fd2
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x3bceb
movslq 0x34(%r13), %rcx
imulq $0x18, %rcx, %rcx
movb $0x2, -0x15(%rax,%rcx)
movq 0x1d8(%r12), %rax
testq %rax, %rax
cmoveq %r12, %rax
movb $0x1, 0x24(%rax)
movl -0x2c(%rbp), %ecx
pushq $0xe
popq %rsi
movq -0x38(%rbp), %rdi
xorl %edx, %edx
callq 0x41f42
movq -0x38(%rbp), %rdi
movl -0x50(%rbp), %edx
testl %edx, %edx
jns 0x3bd2d
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x3bd2d
notl %edx
movl 0x34(%rdi), %ecx
movl %edx, %edx
movl %ecx, (%rax,%rdx,4)
cmpq $0x0, -0x78(%rbp)
movq -0x68(%rbp), %rax
movq -0x40(%rbp), %rcx
jne 0x3bd8d
testb $0x10, 0x46(%rcx)
jne 0x3bd8d
movq (%rax), %rax
testq %rax, %rax
je 0x3bd79
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x50(%rax), %ecx
movq -0x38(%rbp), %rdi
pushq $0x36
popq %rsi
movl -0x6c(%rbp), %r13d
movl %r13d, %edx
callq 0x41f42
movq -0x68(%rbp), %rax
addq $0x28, %rax
incl %r13d
movl %r13d, -0x6c(%rbp)
jmp 0x3bd42
pushq $0x36
popq %rsi
movq -0x38(%rbp), %rdi
movq -0x60(%rbp), %rdx
callq 0x42436
movq -0x38(%rbp), %rdi
cmpb $0x0, 0x1e(%r12)
jne 0x3bdac
cmpq $0x0, 0x1e0(%r12)
jne 0x3bdac
movq %r12, %rdi
callq 0x5344f
movq -0x38(%rbp), %rdi
testb $-0x80, 0x2c(%r14)
je 0x3bafe
cmpb $0x0, 0x1e(%r12)
jne 0x3bafe
cmpq $0x0, 0x1e0(%r12)
jne 0x3bafe
pushq $0x1e
popq %rsi
pushq $0x1
popq %r12
movq %rdi, %r13
movl -0x44(%rbp), %edx
movl %r12d, %ecx
callq 0x41f42
movq %r13, %rdi
movl %r12d, %esi
callq 0x31d90
leaq 0x31f53(%rip), %rcx     # 0x6dd4c
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x31dfc
jmp 0x3bafe
movl 0x3c(%rcx), %edx
movq (%rcx), %r9
movl $0xfffffffe, (%rsp)     # imm = 0xFFFFFFFE
pushq $0x6d
popq %rsi
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %ecx
movl -0x44(%rbp), %r8d
callq 0x41fd2
movq -0x68(%rbp), %r13
movq (%r13), %r13
testq %r13, %r13
movq -0x38(%rbp), %rdi
je 0x3bd8d
movl 0x50(%r13), %edx
pushq $0x6d
popq %rsi
movl -0x2c(%rbp), %ecx
callq 0x41f42
addq $0x28, %r13
jmp 0x3be31