// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movzwl 0x7c(%rax,%rcx,2), %eax
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movswq 0x7a(%rdx), %rdx
movq 0xa8(%rcx,%rdx,8), %rcx
movzwl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3d742
movq -0x10(%rbp), %rax
movzbl 0x77(%rax), %eax
cmpl $0x1, %eax
je 0x3d74e
movl $0x1, -0x4(%rbp)
jmp 0x3da4f
movq -0x10(%rbp), %rax
movswl 0x7a(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
movzwl 0x7c(%rax,%rcx,2), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x30(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movq -0x30(%rbp), %rdx
movq 0x60(%rdx), %rdx
movl -0x3c(%rbp), %esi
shll %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
shll $0x8, %edx
movq -0x30(%rbp), %rsi
movq 0x60(%rsi), %rsi
movl -0x3c(%rbp), %edi
shll %edi
movslq %edi, %rdi
movzbl 0x1(%rsi,%rdi), %esi
orl %esi, %edx
andl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpb $0x0, 0x3(%rax)
jne 0x3d7f4
movq -0x10(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x3ce40
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x3d7f2
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3da4f
jmp 0x3d7f4
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x30(%rax), %esi
movq -0x10(%rbp), %rdx
callq 0x2bad0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x3d81c
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3da4f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x3d83a
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rsi
xorl %edx, %edx
callq 0x417b0
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0xef80
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x3d85b
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3da4f
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x41db0
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x41c00
movq -0x70(%rbp), %rdi
movl -0x64(%rbp), %esi
movzwl %ax, %edx
leaq -0x24(%rbp), %rcx
callq 0x41f80
cmpl $0x0, -0x24(%rbp)
je 0x3d8aa
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3da4f
movq -0x30(%rbp), %rax
cmpb $0x0, 0x3(%rax)
jne 0x3d9d6
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0xa8(%rax,%rcx,8), %rax
movl 0x70(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movq -0x50(%rbp), %rdx
movq 0x60(%rdx), %rdx
movq -0x50(%rbp), %rsi
movzwl 0x10(%rsi), %esi
subl $0x1, %esi
shll %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
shll $0x8, %edx
movq -0x50(%rbp), %rsi
movq 0x60(%rsi), %rsi
movq -0x50(%rbp), %rdi
movzwl 0x10(%rdi), %edi
subl $0x1, %edi
shll %edi
movslq %edi, %rdi
movzbl 0x1(%rsi,%rdi), %esi
orl %esi, %edx
andl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x41c00
movzwl %ax, %eax
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rdi
callq 0x41820
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0xef80
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x38(%rbp), %rdx
addq $-0x4, %rdx
movl -0x54(%rbp), %ecx
addl $0x4, %ecx
movq -0x60(%rbp), %r8
movl -0x58(%rbp), %r9d
leaq -0x24(%rbp), %rax
movq %rax, (%rsp)
callq 0x42110
movq -0x50(%rbp), %rdi
movq -0x50(%rbp), %rax
movzwl 0x10(%rax), %esi
subl $0x1, %esi
movl -0x54(%rbp), %edx
leaq -0x24(%rbp), %rcx
callq 0x41f80
cmpl $0x0, -0x24(%rbp)
je 0x3d9d4
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3da4f
jmp 0x3d9d6
movq -0x10(%rbp), %rdi
callq 0x423c0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x3da3a
movq -0x10(%rbp), %rax
movswl 0x7a(%rax), %eax
cmpl -0x40(%rbp), %eax
jle 0x3da3a
jmp 0x3d9f7
movq -0x10(%rbp), %rax
movswl 0x7a(%rax), %eax
cmpl -0x40(%rbp), %eax
jle 0x3da2e
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rdx
movw 0x7a(%rdx), %cx
movw %cx, %si
addw $-0x1, %si
movw %si, 0x7a(%rdx)
movswq %cx, %rcx
movq 0xa8(%rax,%rcx,8), %rdi
callq 0x29fd0
jmp 0x3d9f7
movq -0x10(%rbp), %rdi
callq 0x423c0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x3da49
movq -0x10(%rbp), %rdi
callq 0x40900
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)