// File: /workspace/github_c_makefile_star2_O2/shifengbin[P]bishe/Output/../Persistence/sqlite3.c
// Function: sqlite3BtreeDelete
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, int bPreserve){
  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 */ 
  u16 szCell;                          /* Size of the cell being deleted */
  int bSkipnext = 0;                   /* Leaf cursor in SKIPNEXT state */

  assert( cursorHoldsMutex(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( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  assert( pCur->eState==CURSOR_VALID );

  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 = 0;
    rc = sqlite3BtreePrevious(pCur, &notUsed);
    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->info.nKey, 0);
  }

  /* 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)
    ){
      /* 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;
    }
  }

  /* 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, &szCell);
  dropCell(pPage, iCellIdx, szCell, &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);
    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);
    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 ){
    if( bSkipnext ){
      assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
      assert( pPage==pCur->apPage[pCur->iPage] );
      assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
      pCur->eState = CURSOR_SKIPNEXT;
      if( iCellIdx>=pPage->nCell ){
        pCur->skipNext = -1;
        pCur->aiIdx[iCellDepth] = pPage->nCell-1;
      }else{
        pCur->skipNext = 1;
      }
    }else{
      rc = moveToRoot(pCur);
      if( bPreserve ){
        pCur->eState = CURSOR_REQUIRESEEK;
      }
    }
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, -0x34(%rbp)
movq %rdi, %rbx
movq (%rdi), %r13
movq 0x8(%r13), %rax
movq %rax, -0x30(%rbp)
movsbq 0x58(%rdi), %rax
movzwl 0x70(%rdi,%rax,2), %r15d
movq %rax, -0x58(%rbp)
movq 0x98(%rdi,%rax,8), %r12
movzwl 0x14(%r12), %eax
movq 0x60(%r12), %rcx
movzbl (%rcx,%r15,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%r15,2), %r14d
orl %edx, %r14d
andl %eax, %r14d
addq 0x50(%r12), %r14
cmpb $0x0, 0x5(%r12)
je 0x3030c
testb $0x20, 0x54(%rbx)
je 0x3020b
movl 0x48(%rbx), %esi
movq -0x30(%rbp), %rdi
movq %rbx, %rdx
callq 0x23353
testl %eax, %eax
jne 0x30320
cmpq $0x0, 0x60(%rbx)
jne 0x30220
movq 0x20(%rbx), %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x31bba
movb $0x1, %r13b
cmpl $0x0, -0x34(%rbp)
je 0x30276
cmpb $0x0, 0x5(%r12)
je 0x30263
movzwl 0x10(%r12), %r13d
movq %r12, %rdi
movq %r14, %rsi
callq 0x1f35e
movzwl %ax, %eax
leal (%rax,%r13), %ecx
addl $0x2, %ecx
movq -0x30(%rbp), %rax
movl 0x34(%rax), %eax
addl %eax, %eax
xorl %r13d, %r13d
pushq $0x3
popq %rsi
xorl %edx, %edx
divl %esi
cmpl %eax, %ecx
jbe 0x30276
movq %rbx, %rdi
callq 0x2361a
movb $0x1, %r13b
testl %eax, %eax
jne 0x30320
movq 0x70(%r12), %rdi
callq 0xfd43
testl %eax, %eax
jne 0x30320
movl %r13d, -0x48(%rbp)
leaq -0x4a(%rbp), %r13
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x31bf1
leaq -0x38(%rbp), %r14
movl %eax, (%r14)
movzwl (%r13), %edx
movq %r12, %rdi
movl %r15d, %esi
movq %r14, %rcx
callq 0x31d68
movl (%r14), %eax
testl %eax, %eax
jne 0x30320
cmpb $0x0, 0x5(%r12)
je 0x3032f
movq %rbx, %rdi
callq 0x320c9
testl %eax, %eax
movl -0x48(%rbp), %r13d
jne 0x30320
movb 0x58(%rbx), %al
movq -0x58(%rbp), %r14
cmpb %r14b, %al
jle 0x30423
cmpb %r14b, %al
jle 0x30413
leal -0x1(%rax), %ecx
movb %cl, 0x58(%rbx)
movsbq %al, %rax
movq 0x98(%rbx,%rax,8), %rdi
callq 0x228f1
movb 0x58(%rbx), %al
jmp 0x302e7
leaq -0x6c(%rbp), %rsi
movq %rbx, %rdi
callq 0x2face
testl %eax, %eax
je 0x301ee
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbq 0x58(%rbx), %rax
movq 0x98(%rbx,%rax,8), %rdi
movzwl 0x14(%rdi), %eax
movq 0x60(%rdi), %rcx
movzwl 0x12(%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 0x30371
movl $0xf74e, %edi           # imm = 0xF74E
callq 0x1ec07
pushq $0xb
popq %rax
jmp 0x30320
movq 0x50(%rdi), %rax
movq %rax, -0x68(%rbp)
leaq (%rax,%r13), %rsi
movq -0x58(%rbp), %rax
movq 0xa0(%rbx,%rax,8), %rax
movl 0x88(%rax), %eax
movl %eax, -0x44(%rbp)
movq %rdi, -0x40(%rbp)
movq -0x40(%rbp), %rax
callq *0x78(%rax)
movzwl %ax, %eax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0xfd43
movl %eax, -0x38(%rbp)
movq -0x68(%rbp), %rax
leaq (%rax,%r13), %rdx
addq $-0x4, %rdx
movq -0x60(%rbp), %r13
leal 0x4(%r13), %ecx
movq %r14, (%rsp)
movq %r12, %rdi
movl %r15d, %esi
movq -0x30(%rbp), %r8
movl -0x44(%rbp), %r9d
callq 0x31e7c
movq -0x40(%rbp), %rdi
movzwl 0x12(%rdi), %esi
decl %esi
movl %r13d, %edx
movq %r14, %rcx
callq 0x31d68
movl -0x38(%rbp), %eax
testl %eax, %eax
jne 0x30320
jmp 0x302c7
movq %rbx, %rdi
callq 0x320c9
testl %eax, %eax
jne 0x30320
testb %r13b, %r13b
je 0x30443
movq %rbx, %rdi
callq 0x30ebb
cmpl $0x0, -0x34(%rbp)
je 0x30320
movb $0x3, 0x56(%rbx)
jmp 0x30320
movb $0x2, 0x56(%rbx)
movzwl 0x12(%r12), %eax
cmpw %ax, %r15w
jae 0x3045c
movl $0x1, 0x50(%rbx)
jmp 0x30468
orl $-0x1, 0x50(%rbx)
decl %eax
movw %ax, 0x70(%rbx,%r14,2)
xorl %eax, %eax
jmp 0x30320