// File: /workspace/github_c_makefile_star2_O3/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
movl $0x1, %eax
cmpw 0x10(%r15), %r14w
jae 0x2e5b9
movq %rdi, %rbx
cmpb $0x1, 0x77(%rdi)
jne 0x2e5b9
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 0x2e5a5
movl 0x30(%rbx), %esi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x20df0
testl %eax, %eax
jne 0x2e5b9
movq %r12, -0x40(%rbp)
cmpq $0x0, 0x20(%rbx)
jne 0x2e4f7
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x2e4f7
movq 0x40(%rbx), %rcx
cmpb $0x0, 0x78(%rax)
je 0x2e4ee
cmpq %rcx, 0x40(%rax)
jne 0x2e4ee
movb $0x0, 0x77(%rax)
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x2e4de
movq 0x68(%r15), %rdi
callq 0xdfbf
testl %eax, %eax
jne 0x2e5b9
movq %r15, %rdi
movq %r13, %rsi
callq 0x310b9
leaq -0x2c(%rbp), %r12
movl %eax, (%r12)
movq %r15, %rdi
movq %r13, %rsi
callq 0x30fe4
movzwl %ax, %edx
movq %r15, %rdi
movl %r14d, %esi
movq %r12, %rcx
callq 0x31267
movl (%r12), %eax
testl %eax, %eax
movq -0x58(%rbp), %r13
jne 0x2e5b9
cmpb $0x0, 0x3(%r15)
je 0x2e5c8
movq %rbx, %rdi
callq 0x318b7
testl %eax, %eax
jne 0x2e5b9
movzwl 0x7a(%rbx), %eax
cmpw %r13w, %ax
jle 0x2e599
leal -0x1(%rax), %ecx
movw %cx, 0x7a(%rbx)
movswq %ax, %rax
movq 0xa8(%rbx,%rax,8), %rax
testq %rax, %rax
je 0x2e585
movq 0x68(%rax), %rdi
callq 0xddd3
movzwl 0x7a(%rbx), %ecx
movl %ecx, %eax
cmpw %r13w, %cx
jg 0x2e560
movq %rbx, %rdi
callq 0x318b7
testl %eax, %eax
jne 0x2e5b9
movq %rbx, %rdi
callq 0x3096f
xorl %eax, %eax
jmp 0x2e5b9
leaq -0x5c(%rbp), %rsi
movq %rbx, %rdi
callq 0x2dc0b
testl %eax, %eax
je 0x2e4a8
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), %rdi
movq 0xb0(%rbx,%r13,8), %rax
movl 0x70(%rax), %eax
movl %eax, -0x44(%rbp)
movzwl 0x12(%rdi), %eax
movq 0x60(%rdi), %rcx
movzwl 0x10(%rdi), %edx
movzbl -0x2(%rcx,%rdx,2), %esi
shll $0x8, %esi
movzbl -0x1(%rcx,%rdx,2), %r13d
orl %esi, %r13d
andl %eax, %r13d
addq 0x50(%rdi), %r13
movq %rdi, -0x38(%rbp)
movq %r13, %rsi
callq 0x30fe4
movzwl %ax, %eax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %r12
movq 0x80(%r12), %rax
testq %rax, %rax
jne 0x2e63d
movl 0x30(%r12), %edi
callq 0x1cc98
movq %rax, 0x80(%r12)
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rcx
movq 0x68(%rcx), %rdi
callq 0xdfbf
movl %eax, -0x2c(%rbp)
addq $-0x4, %r13
movq -0x50(%rbp), %rax
leal 0x4(%rax), %ecx
leaq -0x2c(%rbp), %r12
movq %r12, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq %r13, %rdx
movq -0x40(%rbp), %r8
movl -0x44(%rbp), %r9d
callq 0x31552
movq -0x38(%rbp), %rdi
movzwl 0x10(%rdi), %esi
decl %esi
movq -0x50(%rbp), %rdx
movq %r12, %rcx
callq 0x31267
movl -0x2c(%rbp), %eax
testl %eax, %eax
movq -0x58(%rbp), %r13
jne 0x2e5b9
jmp 0x2e54a