// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3BtreeDelete
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
  Btree *p = pCur->pBtree;
  BtShared *pBt = p->pBt;              
  int rc;                              /* Return code */
  MemPage *pPage;                      /* Page to delete cell from */
  unsigned char *pCell;                /* Pointer to cell to delete */
  int iCellIdx;                        /* Index of cell to delete */
  int iCellDepth;                      /* Depth of node containing pCell */ 

  assert( cursorHoldsMutex(pCur) );
  assert( pBt->inTransaction==TRANS_WRITE );
  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  assert( pCur->wrFlag );
  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  assert( !hasReadConflicts(p, pCur->pgnoRoot) );

  if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
   || NEVER(pCur->eState!=CURSOR_VALID)
  ){
    return SQLITE_ERROR;  /* Something has gone awry. */
  }

  iCellDepth = pCur->iPage;
  iCellIdx = pCur->aiIdx[iCellDepth];
  pPage = pCur->apPage[iCellDepth];
  pCell = findCell(pPage, iCellIdx);

  /* If the page containing the entry to delete is not a leaf page, move
  ** the cursor to the largest entry in the tree that is smaller than
  ** the entry being deleted. This cell will replace the cell being deleted
  ** from the internal node. The 'previous' entry is used for this instead
  ** of the 'next' entry, as the previous entry is always a part of the
  ** sub-tree headed by the child page of the cell being deleted. This makes
  ** balancing the tree following the delete operation easier.  */
  if( !pPage->leaf ){
    int notUsed;
    rc = sqlite3BtreePrevious(pCur, &notUsed);
    if( rc ) return rc;
  }

  /* Save the positions of any other cursors open on this table before
  ** making any modifications. Make the page containing the entry to be 
  ** deleted writable. Then free any overflow pages associated with the 
  ** entry and finally remove the cell itself from within the page.  
  */
  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
  if( rc ) return rc;

  /* If this is a delete operation to remove a row from a table b-tree,
  ** invalidate any incrblob cursors open on the row being deleted.  */
  if( pCur->pKeyInfo==0 ){
    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
  }

  rc = sqlite3PagerWrite(pPage->pDbPage);
  if( rc ) return rc;
  rc = clearCell(pPage, pCell);
  dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
  if( rc ) return rc;

  /* If the cell deleted was not located on a leaf page, then the cursor
  ** is currently pointing to the largest entry in the sub-tree headed
  ** by the child-page of the cell that was just deleted from an internal
  ** node. The cell from the leaf node needs to be moved to the internal
  ** node to replace the deleted cell.  */
  if( !pPage->leaf ){
    MemPage *pLeaf = pCur->apPage[pCur->iPage];
    int nCell;
    Pgno n = pCur->apPage[iCellDepth+1]->pgno;
    unsigned char *pTmp;

    pCell = findCell(pLeaf, pLeaf->nCell-1);
    nCell = cellSizePtr(pLeaf, pCell);
    assert( MX_CELL_SIZE(pBt) >= nCell );

    allocateTempSpace(pBt);
    pTmp = pBt->pTmpSpace;

    rc = sqlite3PagerWrite(pLeaf->pDbPage);
    insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
    dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
    if( rc ) return rc;
  }

  /* Balance the tree. If the entry deleted was located on a leaf page,
  ** then the cursor still points to that page. In this case the first
  ** call to balance() repairs the tree, and the if(...) condition is
  ** never true.
  **
  ** Otherwise, if the entry deleted was on an internal node page, then
  ** pCur is pointing to the leaf page from which a cell was removed to
  ** replace the cell deleted from the internal node. This is slightly
  ** tricky as the leaf node may be underfull, and the internal node may
  ** be either under or overfull. In this case run the balancing algorithm
  ** on the leaf node first. If the balance proceeds far enough up the
  ** tree that we can be sure that any problem in the internal node has
  ** been corrected, so be it. Otherwise, after balancing the leaf node,
  ** walk the cursor up the tree to the internal node and balance it as 
  ** well.  */
  rc = balance(pCur);
  if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
    while( pCur->iPage>iCellDepth ){
      releasePage(pCur->apPage[pCur->iPage--]);
    }
    rc = balance(pCur);
  }

  if( rc==SQLITE_OK ){
    moveToRoot(pCur);
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movswq 0x7a(%rdi), %rcx
movzwl 0x7c(%rdi,%rcx,2), %r14d
movq 0xa8(%rdi,%rcx,8), %r15
pushq $0x1
popq %rax
cmpw 0x10(%r15), %r14w
jae 0x2b2f1
movq %rdi, %rbx
cmpb $0x1, 0x77(%rdi)
jne 0x2b2f1
movq %rcx, -0x58(%rbp)
movq (%rbx), %rax
movq %rax, -0x38(%rbp)
movq 0x8(%rax), %r12
movzwl 0x12(%r15), %eax
movq 0x60(%r15), %rcx
movzbl (%rcx,%r14,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%r14,2), %r13d
orl %edx, %r13d
andl %eax, %r13d
addq 0x50(%r15), %r13
cmpb $0x0, 0x3(%r15)
je 0x2b2dd
movl 0x30(%rbx), %esi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x1e0b8
testl %eax, %eax
jne 0x2b2f1
movq %r12, -0x40(%rbp)
cmpq $0x0, 0x20(%rbx)
jne 0x2b248
movq 0x40(%rbx), %rsi
movq -0x38(%rbp), %rdi
xorl %edx, %edx
callq 0x2c171
movq 0x68(%r15), %rdi
callq 0xd10b
testl %eax, %eax
jne 0x2b2f1
movq %r15, %rdi
movq %r13, %rsi
callq 0x2c290
leaq -0x2c(%rbp), %r12
movl %eax, (%r12)
movq %r15, %rdi
movq %r13, %rsi
callq 0x2c1c4
movzwl %ax, %edx
movq %r15, %rdi
movl %r14d, %esi
movq %r12, %rcx
callq 0x2c3f4
movl (%r12), %eax
testl %eax, %eax
movq -0x58(%rbp), %r13
jne 0x2b2f1
cmpb $0x0, 0x3(%r15)
je 0x2b300
movq %rbx, %rdi
callq 0x2c9a5
testl %eax, %eax
jne 0x2b2f1
movzwl 0x7a(%rbx), %eax
cmpw %r13w, %ax
jle 0x2b3ea
cmpw %r13w, %ax
jle 0x2b3da
leal -0x1(%rax), %ecx
movw %cx, 0x7a(%rbx)
movswq %ax, %rax
movq 0xa8(%rbx,%rax,8), %rdi
callq 0x1d90e
movzwl 0x7a(%rbx), %eax
jmp 0x2b2b5
leaq -0x64(%rbp), %rsi
movq %rbx, %rdi
callq 0x2aa3c
testl %eax, %eax
je 0x2b218
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movswq 0x7a(%rbx), %rax
movq 0xa8(%rbx,%rax,8), %rsi
movq 0xb0(%rbx,%r13,8), %rax
movl 0x70(%rax), %eax
movl %eax, -0x44(%rbp)
movzwl 0x12(%rsi), %eax
movq 0x50(%rsi), %r8
movq %r8, -0x60(%rbp)
movq 0x60(%rsi), %rcx
movzwl 0x10(%rsi), %edx
movq %rsi, %rdi
movq %rsi, -0x50(%rbp)
movzbl -0x2(%rcx,%rdx,2), %esi
shll $0x8, %esi
movzbl -0x1(%rcx,%rdx,2), %r13d
orl %esi, %r13d
andl %eax, %r13d
leaq (%r8,%r13), %rsi
callq 0x2c1c4
movzwl %ax, %eax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %r12
movq %r12, %rdi
callq 0x2c19a
movq 0x80(%r12), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0xd10b
movl %eax, -0x2c(%rbp)
movq -0x60(%rbp), %rax
leaq (%rax,%r13), %rdx
addq $-0x4, %rdx
movq -0x58(%rbp), %r13
movq -0x38(%rbp), %rax
leal 0x4(%rax), %ecx
leaq -0x2c(%rbp), %r12
movq %r12, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq -0x40(%rbp), %r8
movl -0x44(%rbp), %r9d
callq 0x2c687
movq -0x50(%rbp), %rdi
movzwl 0x10(%rdi), %esi
decl %esi
movq -0x38(%rbp), %rdx
movq %r12, %rcx
callq 0x2c3f4
movl -0x2c(%rbp), %eax
testl %eax, %eax
jne 0x2b2f1
jmp 0x2b29b
movq %rbx, %rdi
callq 0x2c9a5
testl %eax, %eax
jne 0x2b2f1
movq %rbx, %rdi
callq 0x2bba7
xorl %eax, %eax
jmp 0x2b2f1