// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %rdx, -0x80(%rbp)
movq (%rdi), %r14
cmpl $0x0, 0x50(%rdi)
jne 0x3d98e
cmpb $0x0, 0x50(%r14)
je 0x3d9c4
xorl %ecx, %ecx
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x3d99e
movq %rcx, 0x248(%rbx)
movq %r14, %rdi
callq 0x341f5
movq %r14, %rdi
movq -0x80(%rbp), %rsi
callq 0x34068
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r12
movq %rsi, -0x38(%rbp)
callq 0x4baec
testq %rax, %rax
je 0x3da30
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
movl $0x6a, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x5bfc5
movq %rax, %r15
movq 0x18(%r13), %rax
movq %rax, -0x60(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4a326
movl $0x0, %ebx
testl %eax, %eax
movl $0x0, %ecx
movq -0x38(%rbp), %rsi
jne 0x3d992
xorl %edx, %edx
testq %r15, %r15
setne %dl
movq %r12, %rdi
movq %r13, %rsi
callq 0x5c056
testl %eax, %eax
je 0x3da3d
xorl %ecx, %ecx
xorl %ebx, %ebx
movq -0x38(%rbp), %rsi
jmp 0x3d992
movq 0x68(%r13), %rax
testq %rax, %rax
movq %r15, -0x98(%rbp)
je 0x3da74
movl 0x28(%r14), %ecx
testl %ecx, %ecx
jle 0x3da7b
movq 0x20(%r14), %rdx
addq $0x18, %rdx
xorl %esi, %esi
cmpq %rax, (%rdx)
je 0x3da7d
incq %rsi
addq $0x20, %rdx
cmpq %rsi, %rcx
jne 0x3da5f
movl %ecx, %esi
jmp 0x3da7d
movl $0xfff0bdc0, %esi       # imm = 0xFFF0BDC0
jmp 0x3da7d
xorl %esi, %esi
movq 0x20(%r14), %rax
movq %rsi, -0x70(%rbp)
movslq %esi, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r8
movq (%r13), %rdx
movq %r12, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x45d75
xorl %ecx, %ecx
movl $0x0, %ebx
cmpl $0x1, %eax
movq -0x38(%rbp), %rsi
je 0x3d992
movl %eax, -0x3c(%rbp)
movl 0x54(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x74(%rbp)
movl %ecx, 0x54(%r12)
movq %rax, -0x68(%rbp)
movl %eax, 0x44(%rsi)
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x3daef
movl 0x54(%r12), %ecx
incl %ecx
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x3dadf
movl %ecx, 0x54(%r12)
cmpq $0x0, -0x60(%rbp)
movq %r13, -0x48(%rbp)
je 0x3db17
movq (%r13), %rax
movq 0x248(%r12), %rcx
movq %rcx, -0x58(%rbp)
movq %rax, 0x248(%r12)
movq %r12, %rbx
jmp 0x3db21
movq $0x0, -0x58(%rbp)
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x45b57
testq %rax, %rax
je 0x3dba4
cmpb $0x0, 0x1e(%r12)
jne 0x3db3d
orb $0x10, 0x9a(%rax)
movq %rax, -0x30(%rbp)
movq %r12, %rdi
movl $0x1, %esi
movq -0x70(%rbp), %rdx
callq 0x460a9
cmpq $0x0, -0x60(%rbp)
movq -0x80(%rbp), %r15
je 0x3db70
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movq %r15, %rdx
movq -0x68(%rbp), %rcx
callq 0x5c0cd
xorps %xmm0, %xmm0
leaq -0xd0(%rbp), %rdi
movaps %xmm0, (%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movq $0x0, 0x30(%rdi)
movq %r12, (%rdi)
movq -0x38(%rbp), %rax
movq %rax, 0x8(%rdi)
movq %r15, %rsi
callq 0x467fe
testl %eax, %eax
je 0x3dbad
movq -0x58(%rbp), %rcx
jmp 0x3da34
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
testb $-0x80, 0x2c(%r14)
je 0x3dbdd
movl 0x58(%r12), %ecx
incl %ecx
movl %ecx, 0x58(%r12)
movq -0x30(%rbp), %rdi
movl $0x15, %esi
xorl %edx, %edx
movl %ecx, -0x4c(%rbp)
xorl %r8d, %r8d
callq 0x45b9e
cmpq $0x0, -0x98(%rbp)
movq -0x30(%rbp), %rdx
jne 0x3dcac
cmpq $0x0, -0x80(%rbp)
jne 0x3dcac
cmpl $0x0, -0x3c(%rbp)
jne 0x3dcac
movq -0x48(%rbp), %rax
testb $0x10, 0x46(%rax)
jne 0x3dcac
movq (%r12), %rax
testb $0x8, 0x2e(%rax)
jne 0x3dc8b
movq -0x48(%rbp), %r15
movl 0x3c(%r15), %edx
movq (%r15), %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movl $0x6d, %esi
movq -0x70(%rbp), %r13
movl %r13d, %ecx
movl -0x4c(%rbp), %r8d
callq 0x45b9e
movq -0x30(%rbp), %rdi
movl %eax, %esi
movq -0x60(%rbp), %rdx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x11975
movq -0x30(%rbp), %rsi
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x3df13
movl 0x50(%r15), %edx
movq -0x30(%rbp), %rdi
movl $0x6d, %esi
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x30(%rbp), %rsi
movq 0x28(%r15), %r15
jmp 0x3dc60
movq -0x48(%rbp), %rdi
callq 0x4fce9
movq -0x30(%rbp), %rdx
testq %rax, %rax
jne 0x3dcac
movq -0x48(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x3dc1c
movl 0x58(%r12), %r13d
leal 0x1(%r13), %ecx
addl $0x2, %r13d
movl %r13d, 0x58(%r12)
movq %rdx, %rdi
movl $0x18, %esi
xorl %edx, %edx
movl %ecx, -0x3c(%rbp)
xorl %r8d, %r8d
callq 0x45b9e
movl $0x0, (%rsp)
movq %r12, %rdi
movq -0x38(%rbp), %rsi
movq -0x80(%rbp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x8, %r9d
callq 0x51e2a
movq %rax, -0x70(%rbp)
testq %rax, %rax
je 0x3dba4
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq -0x68(%rbp), %rcx
movq %r13, -0x90(%rbp)
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x4e1ad
movq -0x30(%rbp), %rdi
movl $0x76, %esi
movl -0x3c(%rbp), %r13d
movl %r13d, %edx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x45b9e
testb $-0x80, 0x2c(%r14)
je 0x3dd5e
movq -0x30(%rbp), %rdi
movl $0x20, %esi
movl -0x4c(%rbp), %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x70(%rbp), %rdi
callq 0x54d54
movq -0x30(%rbp), %rdi
callq 0x4c4bf
cmpq $0x0, -0x60(%rbp)
jne 0x3dd96
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl $0x31, %ecx
movl %eax, %r13d
callq 0x5c3bc
movl %r13d, %eax
movl -0x3c(%rbp), %r13d
movq -0x30(%rbp), %rdi
movl $0x77, %esi
movl %r13d, %edx
movl %eax, -0x3c(%rbp)
movl %eax, %ecx
movq -0x90(%rbp), %r13
movl %r13d, %r8d
callq 0x45b9e
movq -0x48(%rbp), %rsi
testb $0x10, 0x46(%rsi)
movl %eax, -0x84(%rbp)
jne 0x3ddf2
xorl %r8d, %r8d
cmpb $0x0, 0x1e(%r12)
sete %r8b
movl $0x63, (%rsp)
movq %r12, %rdi
movq -0x68(%rbp), %rdx
movl %r13d, %ecx
movq -0x98(%rbp), %r9
callq 0x5c56f
jmp 0x3de6c
movq 0x58(%rsi), %r15
testq %r15, %r15
je 0x3de06
cmpq %r14, (%r15)
je 0x3de06
movq 0x28(%r15), %r15
jmp 0x3ddf6
movq %r12, %rdi
movq -0x48(%rbp), %rsi
callq 0x5c4d9
movq -0x30(%rbp), %r13
movq %r13, %rdi
movl $0xd, %esi
xorl %edx, %edx
movl $0x1, %ecx
movq -0x90(%rbp), %r8
callq 0x45b9e
movq %r13, %rdi
movl %eax, %esi
movq %r15, %rdx
movl $0xfffffff6, %ecx       # imm = 0xFFFFFFF6
callq 0x11975
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x3de59
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)
movl -0x74(%rbp), %r13d
movq -0x30(%rbp), %rdi
movl $0xe, %esi
xorl %edx, %edx
movl -0x84(%rbp), %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x30(%rbp), %rsi
movl -0x3c(%rbp), %edx
testl %edx, %edx
jns 0x3dea7
movq 0x40(%rsi), %rax
testq %rax, %rax
je 0x3dea7
notl %edx
movl 0x34(%rsi), %ecx
movl %edx, %edx
movl %ecx, (%rax,%rdx,4)
cmpq $0x0, -0x60(%rbp)
jne 0x3df08
movq -0x48(%rbp), %rax
testb $0x10, 0x46(%rax)
jne 0x3df08
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x3deed
movq %rax, -0x60(%rbp)
movl 0x50(%rax), %ecx
movq -0x30(%rbp), %rdi
movl $0x36, %esi
movl %r13d, %edx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rax
incl %r13d
jmp 0x3dec0
movq -0x30(%rbp), %rdi
movl $0x36, %esi
movq -0x68(%rbp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq -0x30(%rbp), %rsi
cmpq $0x0, -0x70(%rbp)
je 0x3dba4
cmpb $0x0, 0x1e(%r12)
jne 0x3df32
cmpq $0x0, 0x1e0(%r12)
jne 0x3df32
movq %r12, %rdi
callq 0x5c7cc
movq -0x30(%rbp), %rsi
testb $-0x80, 0x2c(%r14)
movq -0x58(%rbp), %rcx
je 0x3da34
cmpb $0x0, 0x1e(%r12)
jne 0x3da34
cmpq $0x0, 0x1e0(%r12)
jne 0x3da34
movq %rsi, %r12
movq %rsi, %rdi
movl $0x1e, %esi
movl -0x4c(%rbp), %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x45b9e
movq %r12, %rdi
movl $0x1, %esi
callq 0x36e69
movq (%r12), %rax
cmpb $0x0, 0x50(%rax)
jne 0x3dba4
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x3dd8b(%rip), %rsi     # 0x7bd2c
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xeff4
jmp 0x3dba4