// File: /workspace/github_c_makefile_star2_O1/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, -0x3c(%rbp)
movq %rdi, %rbx
movq (%rdi), %r14
movq 0x8(%r14), %rax
movq %rax, -0x38(%rbp)
movsbq 0x58(%rdi), %rax
movzwl 0x70(%rdi,%rax,2), %esi
movq %rax, -0x60(%rbp)
movq 0x98(%rdi,%rax,8), %r12
movzwl 0x14(%r12), %eax
movq 0x60(%r12), %rcx
movzbl (%rcx,%rsi,2), %edx
shll $0x8, %edx
movq %rsi, -0x48(%rbp)
movzbl 0x1(%rcx,%rsi,2), %r13d
orl %edx, %r13d
andl %eax, %r13d
addq 0x50(%r12), %r13
cmpb $0x0, 0x5(%r12)
je 0x3477c
testb $0x20, 0x54(%rbx)
je 0x3470f
movl 0x48(%rbx), %esi
movq -0x38(%rbp), %rdi
movq %rbx, %rdx
callq 0x26a70
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x347bf
cmpq $0x0, 0x60(%rbx)
jne 0x34724
movq 0x20(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x38327
cmpl $0x0, -0x3c(%rbp)
je 0x347a0
cmpb $0x0, 0x5(%r12)
je 0x34766
movzwl 0x10(%r12), %r14d
movq %r12, %rdi
movq %r13, %rsi
callq 0x221c1
movzwl %ax, %eax
addl %r14d, %eax
addl $0x2, %eax
movq -0x38(%rbp), %rcx
movl 0x34(%rcx), %ecx
addl %ecx, %ecx
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
imulq %rcx, %rdx
shrq $0x21, %rdx
cmpl %edx, %eax
jbe 0x347a7
movb $0x1, %al
movl %eax, -0x30(%rbp)
movq %rbx, %rdi
callq 0x26e2a
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x347bf
jmp 0x347ae
leaq -0x74(%rbp), %rsi
movl $0x0, (%rsi)
movq %rbx, %rdi
callq 0x33ef2
movl %eax, -0x2c(%rbp)
movl %eax, %r15d
movl %eax, -0x50(%rbp)
testl %eax, %eax
jne 0x347c2
jmp 0x346ef
movb $0x1, %al
movl %eax, -0x30(%rbp)
jmp 0x347ae
movl $0x0, -0x30(%rbp)
movq 0x70(%r12), %rdi
callq 0x10764
movl %eax, -0x2c(%rbp)
testl %eax, %eax
je 0x347d4
movl %eax, %r15d
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x52(%rbp), %r14
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x3835e
leaq -0x2c(%rbp), %r13
movl %eax, (%r13)
movzwl (%r14), %edx
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movq %r13, %rcx
callq 0x3853a
movl (%r13), %r15d
testl %r15d, %r15d
jne 0x347c2
cmpb $0x0, 0x5(%r12)
jne 0x3491d
movsbq 0x58(%rbx), %rax
movq 0x98(%rbx,%rax,8), %r14
movzwl 0x14(%r14), %eax
movq 0x60(%r14), %rcx
movzwl 0x12(%r14), %edx
movzbl -0x2(%rcx,%rdx,2), %esi
shll $0x8, %esi
movzbl -0x1(%rcx,%rdx,2), %r15d
orl %esi, %r15d
andl %eax, %r15d
cmpl $0x3, %r15d
ja 0x34878
leaq 0x48c86(%rip), %rsi     # 0x7d4d8
leaq 0x48924(%rip), %rcx     # 0x7d17d
xorl %r14d, %r14d
movl $0xb, %r15d
movl $0xb, %edi
movl $0xf74e, %edx           # imm = 0xF74E
xorl %eax, %eax
callq 0xa4b0
jmp 0x34914
movq 0x50(%r14), %rax
movq %rax, -0x70(%rbp)
leaq (%rax,%r15), %rsi
movq -0x60(%rbp), %rax
movq 0xa0(%rbx,%rax,8), %rax
movl 0x88(%rax), %eax
movl %eax, -0x4c(%rbp)
movq %r14, %rdi
callq *0x78(%r14)
movzwl %ax, %eax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x70(%r14), %rdi
callq 0x10764
movl %eax, -0x2c(%rbp)
movq -0x70(%rbp), %rax
leaq (%rax,%r15), %rdx
addq $-0x4, %rdx
movq -0x68(%rbp), %r15
leal 0x4(%r15), %ecx
movq %r13, (%rsp)
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %r8
movl -0x4c(%rbp), %r9d
callq 0x38666
movzwl 0x12(%r14), %esi
decl %esi
movq %r14, %rdi
movl %r15d, %edx
movq %r13, %rcx
callq 0x3853a
movl -0x2c(%rbp), %eax
testl %eax, %eax
sete %r14b
movl -0x50(%rbp), %r15d
cmovnel %eax, %r15d
testb %r14b, %r14b
je 0x347c2
movq %rbx, %rdi
callq 0x3891a
movl %eax, -0x2c(%rbp)
testl %eax, %eax
movq -0x60(%rbp), %r14
movl -0x30(%rbp), %r15d
jne 0x3496f
cmpb %r14b, 0x58(%rbx)
jle 0x3496f
movb 0x58(%rbx), %al
cmpb %r14b, %al
jle 0x34964
leal -0x1(%rax), %ecx
movb %cl, 0x58(%rbx)
movsbq %al, %rax
movq 0x98(%rbx,%rax,8), %rax
testq %rax, %rax
je 0x3493a
movq 0x70(%rax), %rdi
callq 0x25020
jmp 0x3493a
movq %rbx, %rdi
callq 0x3891a
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3497e
movl -0x2c(%rbp), %r15d
jmp 0x347c2
testb %r15b, %r15b
je 0x3499a
movq %rbx, %rdi
callq 0x37334
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x34975
movb $0x3, 0x56(%rbx)
jmp 0x34975
movb $0x2, 0x56(%rbx)
movzwl 0x12(%r12), %eax
cmpw %ax, -0x48(%rbp)
jae 0x349b3
movl $0x1, 0x50(%rbx)
jmp 0x34975
movl $0xffffffff, 0x50(%rbx) # imm = 0xFFFFFFFF
decl %eax
movw %ax, 0x70(%rbx,%r14,2)
jmp 0x34975