// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %rbx
movswq 0x7a(%rdi), %rsi
movzwl 0x7c(%rdi,%rsi,2), %edi
movq 0xa8(%rbx,%rsi,8), %r15
movl $0x1, %r13d
cmpw 0x10(%r15), %di
jae 0x2dec6
cmpb $0x1, 0x77(%rbx)
jne 0x2dec6
movq (%rbx), %rax
movq %rax, -0x38(%rbp)
movq 0x8(%rax), %r14
movzwl 0x12(%r15), %eax
movq 0x60(%r15), %rcx
movzbl (%rcx,%rdi,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%rdi,2), %r12d
orl %edx, %r12d
andl %eax, %r12d
addq 0x50(%r15), %r12
cmpb $0x0, 0x3(%r15)
movq %rsi, -0x40(%rbp)
je 0x2dcf9
movq %rdi, -0x50(%rbp)
movl 0x30(%rbx), %esi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x21124
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x2dcf1
cmpq $0x0, 0x20(%rbx)
jne 0x2dce1
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x2dce1
movq 0x40(%rbx), %rcx
cmpb $0x0, 0x78(%rax)
je 0x2dcd8
cmpq %rcx, 0x40(%rax)
jne 0x2dcd8
movb $0x0, 0x77(%rax)
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x2dcc8
movq 0x68(%r15), %rdi
callq 0xdebf
movl %eax, -0x2c(%rbp)
testl %eax, %eax
je 0x2dd21
movl %eax, %r13d
jmp 0x2dec6
leaq -0x5c(%rbp), %rsi
movq %rdi, %r13
movq %rbx, %rdi
callq 0x2d3c4
movq %r13, %rdi
movl %eax, -0x2c(%rbp)
movl %eax, %r13d
movl %eax, -0x48(%rbp)
testl %eax, %eax
jne 0x2dec6
jmp 0x2dc93
movq %r15, %rdi
movq %r12, %rsi
callq 0x30802
leaq -0x2c(%rbp), %r13
movl %eax, (%r13)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3072b
movzwl %ax, %edx
movq %r15, %rdi
movq -0x50(%rbp), %rsi
movq %r13, %rcx
callq 0x309c5
movl (%r13), %r13d
testl %r13d, %r13d
movq -0x40(%rbp), %rcx
jne 0x2dec6
cmpb $0x0, 0x3(%r15)
je 0x2ddc6
movq %rbx, %rdi
callq 0x3110c
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x2deb5
movq -0x40(%rbp), %rax
cmpw %ax, 0x7a(%rbx)
jle 0x2deb5
movzwl 0x7a(%rbx), %eax
movq -0x40(%rbp), %rdx
cmpw %dx, %ax
jle 0x2deaa
leal -0x1(%rax), %ecx
movw %cx, 0x7a(%rbx)
movswq %ax, %rax
movq 0xa8(%rbx,%rax,8), %rax
testq %rax, %rax
je 0x2ddc0
movq 0x68(%rax), %rdi
callq 0xdcd4
movq -0x40(%rbp), %rdx
movzwl 0x7a(%rbx), %eax
jmp 0x2dd92
movswq 0x7a(%rbx), %rax
movq 0xa8(%rbx,%rax,8), %r12
movq 0xb0(%rbx,%rcx,8), %rax
movl 0x70(%rax), %eax
movl %eax, -0x44(%rbp)
movzwl 0x12(%r12), %eax
movq 0x60(%r12), %rcx
movzwl 0x10(%r12), %edx
movzbl -0x2(%rcx,%rdx,2), %esi
shll $0x8, %esi
movzbl -0x1(%rcx,%rdx,2), %r13d
orl %esi, %r13d
andl %eax, %r13d
addq 0x50(%r12), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3072b
movq %r14, %rcx
movzwl %ax, %eax
movq %rax, -0x38(%rbp)
cmpq $0x0, 0x80(%r14)
jne 0x2de3d
movl 0x30(%r14), %edi
callq 0x1ce52
movq %r14, %rcx
movq %rax, 0x80(%r14)
movq 0x80(%rcx), %rax
movq %rax, -0x58(%rbp)
movq 0x68(%r12), %rdi
callq 0xdebf
movl %eax, -0x2c(%rbp)
addq $-0x4, %r13
movq -0x38(%rbp), %rax
leal 0x4(%rax), %ecx
leaq -0x2c(%rbp), %r14
movq %r14, (%rsp)
movq %r15, %rdi
movq -0x50(%rbp), %rsi
movq %r13, %rdx
movq -0x58(%rbp), %r8
movl -0x44(%rbp), %r9d
callq 0x30cdf
movzwl 0x10(%r12), %esi
decl %esi
movq %r12, %rdi
movq -0x38(%rbp), %rdx
movq %r14, %rcx
callq 0x309c5
movl -0x2c(%rbp), %eax
testl %eax, %eax
movl -0x48(%rbp), %r13d
cmovnel %eax, %r13d
jne 0x2dec6
jmp 0x2dd69
movq %rbx, %rdi
callq 0x3110c
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %r13d
testl %r13d, %r13d
jne 0x2dec6
movq %rbx, %rdi
callq 0x3006b
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq