// File: /workspace/github_c_makefile_star2_O1/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, 0x28(%rsp)
movq %rdi, %rbx
movq 0x8(%rdi), %r13
movq 0x8(%r13), %rax
movq %rax, 0x18(%rsp)
cmpb $0x3, (%rdi)
jne 0x2a640d
movq %rbx, %rdi
callq 0x2a87bf
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x2a6615
movsbl 0x54(%rbx), %eax
movl %eax, 0x24(%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 0x2a64ac
movb $0x1, %al
movl %eax, 0x20(%rsp)
testb $0x2, 0x28(%rsp)
je 0x2a64cc
cmpb $0x0, 0x8(%r15)
je 0x2a6496
movl 0x14(%r15), %r12d
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2950fd
movzwl %ax, %eax
addl %r12d, %eax
addl $0x2, %eax
movq 0x18(%rsp), %rcx
movl 0x38(%rcx), %ecx
addl %ecx, %ecx
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
imulq %rcx, %rdx
shrq $0x21, %rdx
cmpl %edx, %eax
jg 0x2a6496
cmpw $0x1, 0x18(%r15)
jne 0x2a64c4
movq %rbx, %rdi
callq 0x297d6a
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x2a6615
jmp 0x2a64cc
movq %r15, %rdi
callq 0x2aa346
movl $0xb, %r12d
testl %eax, %eax
jne 0x2a6618
jmp 0x2a6447
movl $0x0, 0x20(%rsp)
cmpb $0x0, 0x8(%r15)
je 0x2a6601
testb $0x20, 0x1(%rbx)
je 0x2a64f9
movl 0x50(%rbx), %esi
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
callq 0x2979a1
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x2a6615
cmpq $0x0, 0x80(%rbx)
jne 0x2a6514
movl 0x50(%rbx), %esi
movq 0x30(%rbx), %rdx
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x2a8e26
movq 0x70(%r15), %rdi
callq 0x280e3a
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x2a6615
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x2a9035
leaq 0x14(%rsp), %r13
movl %eax, (%r13)
movzwl 0x16(%r12), %edx
movq %r15, %rdi
movl %r14d, %esi
movq %r13, %rcx
callq 0x2aa492
movl (%r13), %r12d
testl %r12d, %r12d
jne 0x2a6618
movslq 0x24(%rsp), %rbp
cmpb $0x0, 0x8(%r15)
jne 0x2a66e8
movq %rbp, 0x38(%rsp)
leaq 0x88(%rbx), %rdi
movq (%rdi), %rbp
cmpl $0x0, 0x14(%rbp)
js 0x2a66bb
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 0x2a662a
leaq 0x138d4a(%rip), %rsi    # 0x3df31f
leaq 0x138e54(%rip), %rdx    # 0x3df430
leaq 0x138ba2(%rip), %r8     # 0x3df185
xorl %ebp, %ebp
movl $0xb, %r12d
movl $0xb, %edi
movl $0x11f26, %ecx          # imm = 0x11F26
xorl %eax, %eax
callq 0x27e637
jmp 0x2a66da
movq %rbx, %rdi
callq 0x2a5a00
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x2a64d7
movl %eax, %r12d
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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 0x18(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x70(%rbp), %rdi
callq 0x280e3a
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x2a6693
addq $-0x4, %r12
movq 0x30(%rsp), %rax
leal 0x4(%rax), %ecx
movq %r13, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq %r12, %rdx
movq 0x18(%rsp), %r8
movl 0x2c(%rsp), %r9d
callq 0x2aa5ae
movzwl 0x18(%rbp), %esi
decl %esi
leaq 0x14(%rsp), %r12
movq %rbp, %rdi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
callq 0x2aa492
movl (%r12), %r12d
testl %r12d, %r12d
sete %bpl
jmp 0x2a66da
movq %rdi, %r12
movq %rbp, %rdi
callq 0x2aa346
movq %r12, %rdi
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x2a658f
movl %eax, %r12d
xorl %ebp, %ebp
testb %bpl, %bpl
movq 0x38(%rsp), %rbp
je 0x2a6618
movq %rbx, %rdi
callq 0x2aa8d7
movl %eax, 0x14(%rsp)
testl %eax, %eax
jne 0x2a6763
cmpb %bpl, 0x54(%rbx)
jle 0x2a6763
movq 0x88(%rbx), %rax
movq 0x70(%rax), %rdi
callq 0x2949b7
movb 0x54(%rbx), %al
decb %al
movb %al, 0x54(%rbx)
cmpb 0x24(%rsp), %al
jle 0x2a6744
leal -0x1(%rax), %ecx
movb %cl, 0x54(%rbx)
movsbq %al, %rax
movq 0x90(%rbx,%rax,8), %rax
testq %rax, %rax
je 0x2a673c
movq 0x70(%rax), %rdi
callq 0x2949b7
movb 0x54(%rbx), %al
cmpb %bpl, %al
jg 0x2a671c
movsbq %al, %rax
movq 0x90(%rbx,%rax,8), %rax
movq %rax, 0x88(%rbx)
movq %rbx, %rdi
callq 0x2aa8d7
movl %eax, 0x14(%rsp)
cmpl $0x0, 0x14(%rsp)
je 0x2a6774
movl 0x14(%rsp), %r12d
jmp 0x2a6618
cmpb $0x0, 0x20(%rsp)
je 0x2a67aa
movq %rbx, %rdi
callq 0x2a899d
movl %eax, %ebp
movl %eax, 0x14(%rsp)
testb $0x2, 0x28(%rsp)
je 0x2a679b
movq %rbx, %rdi
callq 0x297d1a
movb $0x3, (%rbx)
cmpl $0x10, %ebp
jne 0x2a676a
movl $0x0, 0x14(%rsp)
jmp 0x2a676a
movb $0x2, (%rbx)
movzwl 0x18(%r15), %eax
cmpw %ax, %r14w
jae 0x2a67c1
movl $0x1, 0x4(%rbx)
jmp 0x2a676a
movl $0xffffffff, 0x4(%rbx)  # imm = 0xFFFFFFFF
decl %eax
movw %ax, 0x56(%rbx)
jmp 0x2a676a