// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3BtreeDelete
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
  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 */
  CellInfo info;                       /* Size of the cell being deleted */
  int bSkipnext = 0;                   /* Leaf cursor in SKIPNEXT state */
  u8 bPreserve = flags & BTREE_SAVEPOSITION;  /* Keep cursor valid */

  assert( cursorOwnsBtShared(pCur) );
  assert( pBt->inTransaction==TRANS_WRITE );
  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  assert( pCur->curFlags & BTCF_WriteFlag );
  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
  assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
  if( pCur->eState==CURSOR_REQUIRESEEK ){
    rc = btreeRestoreCursorPosition(pCur);
    if( rc ) return rc;
  }
  assert( pCur->eState==CURSOR_VALID );

  iCellDepth = pCur->iPage;
  iCellIdx = pCur->ix;
  pPage = pCur->pPage;
  pCell = findCell(pPage, iCellIdx);
  if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;

  /* If the bPreserve flag is set to true, then the cursor position must
  ** be preserved following this delete operation. If the current delete
  ** will cause a b-tree rebalance, then this is done by saving the cursor
  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
  ** returning.
  **
  ** Or, if the current delete will not cause a rebalance, then the cursor
  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
  ** before or after the deleted entry. In this case set bSkipnext to true.  */
  if( bPreserve ){
    if( !pPage->leaf
     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
     || pPage->nCell==1  /* See dbfuzz001.test for a test case */
    ){
      /* A b-tree rebalance will be required after deleting this entry.
      ** Save the cursor key.  */
      rc = saveCursorKey(pCur);
      if( rc ) return rc;
    }else{
      bSkipnext = 1;
    }
  }

  /* 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 ){
    rc = sqlite3BtreePrevious(pCur, 0);
    assert( rc!=SQLITE_DONE );
    if( rc ) return rc;
  }

  /* Save the positions of any other cursors open on this table before
  ** making any modifications.  */
  if( pCur->curFlags & BTCF_Multiple ){
    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->pgnoRoot, pCur->info.nKey, 0);
  }

  /* 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 = sqlite3PagerWrite(pPage->pDbPage);
  if( rc ) return rc;
  rc = clearCell(pPage, pCell, &info);
  dropCell(pPage, iCellIdx, info.nSize, &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->pPage;
    int nCell;
    Pgno n;
    unsigned char *pTmp;

    if( pLeaf->nFree<0 ){
      rc = btreeComputeFreeSpace(pLeaf);
      if( rc ) return rc;
    }
    if( iCellDepth<pCur->iPage-1 ){
      n = pCur->apPage[iCellDepth+1]->pgno;
    }else{
      n = pCur->pPage->pgno;
    }
    pCell = findCell(pLeaf, pLeaf->nCell-1);
    if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
    nCell = pLeaf->xCellSize(pLeaf, pCell);
    assert( MX_CELL_SIZE(pBt) >= nCell );
    pTmp = pBt->pTmpSpace;
    assert( pTmp!=0 );
    rc = sqlite3PagerWrite(pLeaf->pDbPage);
    if( rc==SQLITE_OK ){
      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 ){
    releasePageNotNull(pCur->pPage);
    pCur->iPage--;
    while( pCur->iPage>iCellDepth ){
      releasePage(pCur->apPage[pCur->iPage--]);
    }
    pCur->pPage = pCur->apPage[pCur->iPage];
    rc = balance(pCur);
  }

  if( rc==SQLITE_OK ){
    if( bSkipnext ){
      assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
      assert( pPage==pCur->pPage || CORRUPT_DB );
      assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
      pCur->eState = CURSOR_SKIPNEXT;
      if( iCellIdx>=pPage->nCell ){
        pCur->skipNext = -1;
        pCur->ix = pPage->nCell-1;
      }else{
        pCur->skipNext = 1;
      }
    }else{
      rc = moveToRoot(pCur);
      if( bPreserve ){
        btreeReleaseAllCursorPages(pCur);
        pCur->eState = CURSOR_REQUIRESEEK;
      }
      if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
    }
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, 0x24(%rsp)
movq %rdi, %rbx
movq 0x8(%rdi), %r13
movq 0x8(%r13), %rax
movq %rax, 0x10(%rsp)
cmpb $0x3, (%rdi)
jne 0x29d067
movq %rbx, %rdi
callq 0x29ef8c
testl %eax, %eax
jne 0x29d23b
movsbl 0x54(%rbx), %eax
movl %eax, 0x20(%rsp)
movzwl 0x56(%rbx), %r14d
movq 0x88(%rbx), %r15
movzwl 0x1a(%r15), %eax
movq 0x60(%r15), %rcx
movzbl (%rcx,%r14,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%r14,2), %ebp
orl %edx, %ebp
andl %eax, %ebp
addq 0x50(%r15), %rbp
cmpl $0x0, 0x14(%r15)
js 0x29d102
movb $0x1, %al
movl %eax, 0x1c(%rsp)
testb $0x2, 0x24(%rsp)
je 0x29d122
cmpb $0x0, 0x8(%r15)
je 0x29d0f0
movl 0x14(%r15), %r12d
movq %r15, %rdi
movq %rbp, %rsi
callq 0x28b2bc
movzwl %ax, %eax
addl %r12d, %eax
addl $0x2, %eax
movq 0x10(%rsp), %rcx
movl 0x38(%rcx), %ecx
addl %ecx, %ecx
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
imulq %rcx, %rdx
shrq $0x21, %rdx
cmpl %edx, %eax
jg 0x29d0f0
cmpw $0x1, 0x18(%r15)
jne 0x29d11a
movq %rbx, %rdi
callq 0x28e0fb
testl %eax, %eax
jne 0x29d23b
jmp 0x29d122
movq %r15, %rdi
callq 0x2a0957
movl $0xb, %r12d
testl %eax, %eax
jne 0x29d23e
jmp 0x29d0a1
movl $0x0, 0x1c(%rsp)
cmpb $0x0, 0x8(%r15)
je 0x29d22b
testb $0x20, 0x1(%rbx)
je 0x29d14b
movl 0x50(%rbx), %esi
movq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0x28dd69
testl %eax, %eax
jne 0x29d23b
cmpq $0x0, 0x80(%rbx)
jne 0x29d166
movl 0x50(%rbx), %esi
movq 0x30(%rbx), %rdx
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x29f5d8
movq 0x70(%r15), %rdi
callq 0x2774a1
testl %eax, %eax
jne 0x29d23b
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x29f7c7
leaq 0x28(%rsp), %r13
movl %eax, (%r13)
movzwl 0x16(%r12), %edx
movq %r15, %rdi
movl %r14d, %esi
movq %r13, %rcx
callq 0x2a0ae5
movl (%r13), %r12d
testl %r12d, %r12d
jne 0x29d23e
movslq 0x20(%rsp), %rbp
cmpb $0x0, 0x8(%r15)
je 0x29d250
movq %rbx, %rdi
callq 0x2a0ea8
testl %eax, %eax
jne 0x29d23b
cmpb %bpl, 0x54(%rbx)
jle 0x29d3b4
movq 0x88(%rbx), %rax
movq 0x70(%rax), %rdi
callq 0x28ab87
movb 0x54(%rbx), %cl
decb %cl
movb %cl, 0x54(%rbx)
cmpb 0x20(%rsp), %cl
jle 0x29d38f
leal -0x1(%rcx), %eax
movb %al, 0x54(%rbx)
movsbq %cl, %rcx
movq 0x90(%rbx,%rcx,8), %rcx
testq %rcx, %rcx
je 0x29d21f
movq 0x70(%rcx), %rdi
callq 0x28ab87
movb 0x54(%rbx), %al
movl %eax, %ecx
cmpb %bpl, %al
jg 0x29d1fc
jmp 0x29d391
movq %rbx, %rdi
callq 0x29c6b1
testl %eax, %eax
je 0x29d12d
movl %eax, %r12d
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, 0x38(%rsp)
leaq 0x88(%rbx), %rdi
movq (%rdi), %rbp
cmpl $0x0, 0x14(%rbp)
js 0x29d374
movsbl 0x54(%rbx), %eax
decl %eax
movq 0x38(%rsp), %rcx
cmpl %ecx, %eax
leaq 0x98(%rbx,%rcx,8), %rax
cmovgq %rax, %rdi
movzwl 0x1a(%rbp), %eax
movq 0x60(%rbp), %rcx
movzwl 0x18(%rbp), %edx
movzbl -0x2(%rcx,%rdx,2), %esi
shll $0x8, %esi
movzbl -0x1(%rcx,%rdx,2), %r12d
orl %esi, %r12d
andl %eax, %r12d
cmpl $0x3, %r12d
ja 0x29d2d9
leaq 0x13891c(%rip), %rsi    # 0x3d5bcb
leaq 0x138a26(%rip), %rdx    # 0x3d5cdc
leaq 0x138774(%rip), %r8     # 0x3d5a31
movl $0xb, %r12d
movl $0xb, %edi
movl $0x11f26, %ecx          # imm = 0x11F26
xorl %eax, %eax
callq 0x274d08
jmp 0x29d23e
movq (%rdi), %rax
movl 0x4(%rax), %eax
movl %eax, 0x2c(%rsp)
addq 0x50(%rbp), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq *0x78(%rbp)
movzwl %ax, %eax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x70(%rbp), %rdi
callq 0x2774a1
movl %eax, 0x28(%rsp)
testl %eax, %eax
jne 0x29d342
addq $-0x4, %r12
movq 0x30(%rsp), %rax
leal 0x4(%rax), %ecx
movq %r13, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq %r12, %rdx
movq 0x10(%rsp), %r8
movl 0x2c(%rsp), %r9d
callq 0x2a0c01
movzwl 0x18(%rbp), %esi
decl %esi
leaq 0x28(%rsp), %r12
movq %rbp, %rdi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
callq 0x2a0ae5
movl (%r12), %r12d
testl %r12d, %r12d
movq 0x38(%rsp), %rbp
jne 0x29d23e
jmp 0x29d1c4
movq %rdi, %r12
movq %rbp, %rdi
callq 0x2a0957
movq %r12, %rdi
testl %eax, %eax
jne 0x29d23b
jmp 0x29d269
movl %ecx, %eax
movsbq %al, %rax
movq 0x90(%rbx,%rax,8), %rax
movq %rax, 0x88(%rbx)
movq %rbx, %rdi
callq 0x2a0ea8
testl %eax, %eax
jne 0x29d23b
cmpb $0x0, 0x1c(%rsp)
je 0x29d3e6
movq %rbx, %rdi
callq 0x29f169
movl %eax, %ebp
testb $0x2, 0x24(%rsp)
je 0x29d3d7
movq %rbx, %rdi
callq 0x28e0af
movb $0x3, (%rbx)
xorl %r12d, %r12d
cmpl $0x10, %ebp
cmovnel %ebp, %r12d
jmp 0x29d23e
movb $0x2, (%rbx)
movzwl 0x18(%r15), %eax
cmpw %ax, %r14w
jae 0x29d3fd
movl $0x1, 0x4(%rbx)
jmp 0x29d40a
movl $0xffffffff, 0x4(%rbx)  # imm = 0xFFFFFFFF
decl %eax
movw %ax, 0x56(%rbx)
xorl %r12d, %r12d
jmp 0x29d23e