// File: /workspace/github_c_makefile_star2_O2/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, %r12d
movq %rdi, %rbx
movq 0x8(%rdi), %r13
movq 0x8(%r13), %rax
movq %rax, 0x18(%rsp)
cmpb $0x3, (%rdi)
jne 0x243bc0
movq %rbx, %rdi
callq 0x2452c2
testl %eax, %eax
jne 0x243d7a
movsbl 0x54(%rbx), %eax
movl %eax, 0x28(%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 0x243c65
movb $0x1, %al
movl %eax, 0x24(%rsp)
testb $0x2, %r12b
je 0x243c81
cmpb $0x0, 0x8(%r15)
je 0x243c53
movq %r14, 0x10(%rsp)
movl %r12d, %r14d
movl 0x14(%r15), %r12d
movq %r15, %rdi
movq %rbp, %rsi
callq 0x234bc3
movzwl %ax, %eax
leal (%r12,%rax), %ecx
addl $0x2, %ecx
movl %r14d, %r12d
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
movl 0x38(%rax), %eax
addl %eax, %eax
pushq $0x3
popq %rsi
xorl %edx, %edx
divl %esi
cmpl %eax, %ecx
jg 0x243c53
cmpw $0x1, 0x18(%r15)
jne 0x243c79
movq %rbx, %rdi
callq 0x2370e0
testl %eax, %eax
jne 0x243d7a
jmp 0x243c81
movq %r15, %rdi
callq 0x2468e4
testl %eax, %eax
je 0x243bfa
pushq $0xb
popq %rax
jmp 0x243d7a
movl $0x0, 0x24(%rsp)
cmpb $0x0, 0x8(%r15)
je 0x243d6a
testb $0x20, 0x1(%rbx)
je 0x243caa
movl 0x50(%rbx), %esi
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
callq 0x236e12
testl %eax, %eax
jne 0x243d7a
cmpq $0x0, 0x80(%rbx)
jne 0x243cc5
movl 0x50(%rbx), %esi
movq 0x30(%rbx), %rdx
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x2457cd
movq 0x70(%r15), %rdi
callq 0x2236f2
testl %eax, %eax
jne 0x243d7a
leaq 0x40(%rsp), %r13
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x245990
leaq 0x2c(%rsp), %rbp
movl %eax, (%rbp)
movzwl 0x16(%r13), %edx
movq %r15, %rdi
movl %r14d, %esi
movq %rbp, %rcx
callq 0x2469c7
movl (%rbp), %eax
testl %eax, %eax
jne 0x243d7a
movslq 0x28(%rsp), %r13
cmpb $0x0, 0x8(%r15)
je 0x243d89
movq %rbx, %rdi
callq 0x246d33
testl %eax, %eax
jne 0x243d7a
cmpb %r13b, 0x54(%rbx)
jle 0x243eda
movq 0x88(%rbx), %rdi
callq 0x2376cc
movb 0x54(%rbx), %al
decb %al
movb %al, 0x54(%rbx)
movl 0x28(%rsp), %ebp
movsbq %al, %rcx
cmpb %bpl, %al
jle 0x243ebb
leal -0x1(%rcx), %eax
movb %al, 0x54(%rbx)
movq 0x90(%rbx,%rcx,8), %rdi
callq 0x2376c6
movb 0x54(%rbx), %al
jmp 0x243d45
movq %rbx, %rdi
callq 0x2432f4
testl %eax, %eax
je 0x243c8c
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x88(%rbx), %r8
movq (%r8), %rdi
cmpl $0x0, 0x14(%rdi)
js 0x243e90
movsbl 0x54(%rbx), %eax
decl %eax
cmpl %r13d, %eax
movq %r13, 0x10(%rsp)
leaq 0x98(%rbx,%r13,8), %rax
cmovgq %rax, %r8
movzwl 0x1a(%rdi), %eax
movq 0x60(%rdi), %rcx
movzwl 0x18(%rdi), %edx
movzbl -0x2(%rcx,%rdx,2), %esi
shll $0x8, %esi
movzbl -0x1(%rcx,%rdx,2), %r13d
orl %esi, %r13d
andl %eax, %r13d
cmpl $0x3, %r13d
ja 0x243de9
movl $0x11f26, %edi          # imm = 0x11F26
callq 0x232547
jmp 0x243d7a
movl %r12d, 0x3c(%rsp)
movq (%r8), %rax
movl 0x4(%rax), %eax
movl %eax, 0x38(%rsp)
addq 0x50(%rdi), %r13
movq %rdi, %r12
movq %r13, %rsi
callq *0x78(%rdi)
movzwl %ax, %eax
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x70(%r12), %rdi
callq 0x2236f2
movl %eax, 0x2c(%rsp)
testl %eax, %eax
jne 0x243e58
addq $-0x4, %r13
movq 0x30(%rsp), %rax
leal 0x4(%rax), %ecx
movq %rbp, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq %r13, %rdx
movq 0x18(%rsp), %r8
movl 0x38(%rsp), %r9d
callq 0x246aba
movzwl 0x18(%r12), %esi
decl %esi
leaq 0x2c(%rsp), %r13
movq %r12, %rdi
movq 0x30(%rsp), %rdx
movq %r13, %rcx
callq 0x2469c7
movl (%r13), %eax
testl %eax, %eax
movl 0x3c(%rsp), %r12d
movq 0x10(%rsp), %r13
jne 0x243d7a
jmp 0x243d17
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movq %r8, 0x30(%rsp)
callq 0x2468e4
movq 0x30(%rsp), %r8
movq 0x10(%rsp), %rdi
testl %eax, %eax
jne 0x243d7a
jmp 0x243d9d
movq 0x90(%rbx,%rcx,8), %rax
movq %rax, 0x88(%rbx)
movq %rbx, %rdi
callq 0x246d33
testl %eax, %eax
jne 0x243d7a
cmpb $0x0, 0x24(%rsp)
je 0x243f09
movq %rbx, %rdi
callq 0x245470
movl %eax, %ebp
testb $0x2, %r12b
je 0x243efc
movq %rbx, %rdi
callq 0x237099
movb $0x3, (%rbx)
xorl %eax, %eax
cmpl $0x10, %ebp
cmovnel %ebp, %eax
jmp 0x243d7a
movb $0x2, (%rbx)
movzwl 0x18(%r15), %eax
cmpw %ax, %r14w
jae 0x243f20
movl $0x1, 0x4(%rbx)
jmp 0x243f2a
orl $-0x1, 0x4(%rbx)
decl %eax
movw %ax, 0x56(%rbx)
xorl %eax, %eax
jmp 0x243d7a