// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x64(%rbp)
movzbl -0x11(%rbp), %eax
andl $0x2, %eax
movb %al, -0x65(%rbp)
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x3, %eax
jne 0x3fc759
movq -0x10(%rbp), %rdi
callq 0x400870
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3fc757
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
jmp 0x3fc759
movq -0x10(%rbp), %rax
movsbl 0x54(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x56(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x38(%rbp), %rcx
movzwl 0x1a(%rcx), %ecx
movq -0x38(%rbp), %rdx
movq 0x60(%rdx), %rdx
movl -0x44(%rbp), %esi
shll %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
shll $0x8, %edx
movq -0x38(%rbp), %rsi
movq 0x60(%rsi), %rsi
movl -0x44(%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, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jge 0x3fc7ec
movq -0x38(%rbp), %rdi
callq 0x403790
cmpl $0x0, %eax
je 0x3fc7ec
movl $0xb, -0x4(%rbp)
jmp 0x3fcc34
cmpb $0x0, -0x65(%rbp)
je 0x3fc884
movq -0x38(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x3fc85c
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x88(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3e4550
movw %ax, %cx
movl -0x88(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
addl $0x2, %eax
movl %eax, -0x84(%rbp)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
shll %eax
movl $0x3, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movl -0x84(%rbp), %eax
cmpl %ecx, %eax
jg 0x3fc85c
movq -0x38(%rbp), %rax
movzwl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x3fc87b
movq -0x10(%rbp), %rdi
callq 0x3e9220
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3fc879
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
jmp 0x3fc882
movl $0x1, -0x64(%rbp)
jmp 0x3fc884
movq -0x38(%rbp), %rax
cmpb $0x0, 0x8(%rax)
jne 0x3fc8af
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x3fbe20
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3fc8ad
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
jmp 0x3fc8af
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x3fc8e9
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x50(%rax), %esi
movq -0x10(%rbp), %rdx
callq 0x3e8d80
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3fc8e7
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
jmp 0x3fc8e9
movq -0x10(%rbp), %rax
cmpq $0x0, 0x80(%rax)
jne 0x3fc911
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x50(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
xorl %ecx, %ecx
callq 0x4014d0
movq -0x38(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x3c6610
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3fc932
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x4017b0
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %esi
movzwl -0x4a(%rbp), %edx
leaq -0x2c(%rbp), %rcx
callq 0x403e00
cmpl $0x0, -0x2c(%rbp)
je 0x3fc96b
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
movq -0x38(%rbp), %rax
cmpb $0x0, 0x8(%rax)
jne 0x3fcb07
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jge 0x3fc9b1
movq -0x70(%rbp), %rdi
callq 0x403790
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3fc9af
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
jmp 0x3fc9b1
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
movsbl 0x54(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x3fc9e0
movq -0x10(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x90(%rax,%rcx,8), %rax
movl 0x4(%rax), %eax
movl %eax, -0x78(%rbp)
jmp 0x3fc9f1
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x70(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x70(%rbp), %rcx
movzwl 0x1a(%rcx), %ecx
movq -0x70(%rbp), %rdx
movq 0x60(%rdx), %rdx
movq -0x70(%rbp), %rsi
movzwl 0x18(%rsi), %esi
subl $0x1, %esi
shll %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
shll $0x8, %edx
movq -0x70(%rbp), %rsi
movq 0x60(%rsi), %rsi
movq -0x70(%rbp), %rdi
movzwl 0x18(%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, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
jae 0x3fca72
movl $0x11f26, %edi          # imm = 0x11F26
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
movq -0x70(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x70(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq *%rax
movzwl %ax, %eax
movl %eax, -0x74(%rbp)
movq -0x28(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x70(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x3c6610
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3fcad9
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %esi
movq -0x40(%rbp), %rdx
addq $-0x4, %rdx
movl -0x74(%rbp), %ecx
addl $0x4, %ecx
movq -0x80(%rbp), %r8
movl -0x78(%rbp), %r9d
leaq -0x2c(%rbp), %rax
movq %rax, (%rsp)
callq 0x403ff0
movq -0x70(%rbp), %rdi
movq -0x70(%rbp), %rax
movzwl 0x18(%rax), %esi
subl $0x1, %esi
movl -0x74(%rbp), %edx
leaq -0x2c(%rbp), %rcx
callq 0x403e00
cmpl $0x0, -0x2c(%rbp)
je 0x3fcb05
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3fcc34
jmp 0x3fcb07
movq -0x10(%rbp), %rdi
callq 0x404240
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3fcba9
movq -0x10(%rbp), %rax
movsbl 0x54(%rax), %eax
cmpl -0x48(%rbp), %eax
jle 0x3fcba9
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
callq 0x3e9ba0
movq -0x10(%rbp), %rax
movb 0x54(%rax), %cl
addb $-0x1, %cl
movb %cl, 0x54(%rax)
movq -0x10(%rbp), %rax
movsbl 0x54(%rax), %eax
cmpl -0x48(%rbp), %eax
jle 0x3fcb7d
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rdx
movb 0x54(%rdx), %cl
movb %cl, %sil
addb $-0x1, %sil
movb %sil, 0x54(%rdx)
movsbq %cl, %rcx
movq 0x90(%rax,%rcx,8), %rdi
callq 0x3e9b70
jmp 0x3fcb47
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbq 0x54(%rcx), %rcx
movq 0x90(%rax,%rcx,8), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rdi
callq 0x404240
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3fcc2e
cmpl $0x0, -0x64(%rbp)
je 0x3fcbfb
movq -0x10(%rbp), %rax
movb $0x2, (%rax)
movl -0x44(%rbp), %eax
movq -0x38(%rbp), %rcx
movzwl 0x18(%rcx), %ecx
cmpl %ecx, %eax
jl 0x3fcbee
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movzwl 0x18(%rax), %eax
subl $0x1, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x56(%rax)
jmp 0x3fcbf9
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x3fcc2c
movq -0x10(%rbp), %rdi
callq 0x400d30
movl %eax, -0x2c(%rbp)
cmpb $0x0, -0x65(%rbp)
je 0x3fcc1d
movq -0x10(%rbp), %rdi
callq 0x3e91b0
movq -0x10(%rbp), %rax
movb $0x3, (%rax)
cmpl $0x10, -0x2c(%rbp)
jne 0x3fcc2a
movl $0x0, -0x2c(%rbp)
jmp 0x3fcc2c
jmp 0x3fcc2e
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq