// File: /workspace/github_c_makefile_star2_O3/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), %r15
movq 0x8(%r15), %rax
movq %rax, -0x38(%rbp)
movsbq 0x58(%rdi), %rax
movzwl 0x70(%rdi,%rax,2), %esi
movq %rax, -0x58(%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), %r14d
orl %edx, %r14d
andl %eax, %r14d
addq 0x50(%r12), %r14
cmpb $0x0, 0x5(%r12)
je 0x33b59
testb $0x20, 0x54(%rbx)
je 0x33aef
movl 0x48(%rbx), %esi
movq -0x38(%rbp), %rdi
movq %rbx, %rdx
callq 0x2680e
testl %eax, %eax
jne 0x33b8a
cmpq $0x0, 0x60(%rbx)
jne 0x33b04
movq 0x20(%rbx), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x37635
cmpl $0x0, -0x3c(%rbp)
je 0x33b6e
cmpb $0x0, 0x5(%r12)
je 0x33b46
movzwl 0x10(%r12), %r15d
movq %r12, %rdi
movq %r14, %rsi
callq 0x22076
movzwl %ax, %eax
addl %r15d, %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 0x33b75
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movq %rbx, %rdi
callq 0x26ba2
testl %eax, %eax
jne 0x33b8a
jmp 0x33b7c
leaq -0x6c(%rbp), %rsi
movq %rbx, %rdi
callq 0x332c8
testl %eax, %eax
jne 0x33b8a
jmp 0x33ad2
movb $0x1, %al
movl %eax, -0x2c(%rbp)
jmp 0x33b7c
movl $0x0, -0x2c(%rbp)
movq 0x70(%r12), %rdi
callq 0x107f0
testl %eax, %eax
je 0x33b9f
movl %eax, %r13d
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x4e(%rbp), %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3766c
leaq -0x40(%rbp), %r14
movl %eax, (%r14)
movzwl (%r15), %edx
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movq %r14, %rcx
callq 0x37826
movl (%r14), %r13d
testl %r13d, %r13d
jne 0x33b8d
cmpb $0x0, 0x5(%r12)
je 0x33c55
movq %rbx, %rdi
callq 0x37bc9
testl %eax, %eax
jne 0x33b8a
movb 0x58(%rbx), %al
movq -0x58(%rbp), %r14
cmpb %r14b, %al
jle 0x33c2d
leal -0x1(%rax), %ecx
movb %cl, 0x58(%rbx)
movsbq %al, %rax
movq 0x98(%rbx,%rax,8), %rax
testq %rax, %rax
je 0x33c16
movq 0x70(%rax), %rdi
callq 0x24e28
movb 0x58(%rbx), %cl
movl %ecx, %eax
cmpb %r14b, %cl
jg 0x33bf3
movq %rbx, %rdi
callq 0x37bc9
testl %eax, %eax
jne 0x33b8a
cmpb $0x0, -0x2c(%rbp)
je 0x33d51
movq %rbx, %rdi
callq 0x36760
movl %eax, %r13d
cmpl $0x0, -0x3c(%rbp)
je 0x33b8d
movb $0x3, 0x56(%rbx)
jmp 0x33b8d
movsbq 0x58(%rbx), %rax
movq 0x98(%rbx,%rax,8), %r13
movzwl 0x14(%r13), %eax
movq 0x60(%r13), %rcx
movzwl 0x12(%r13), %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 0x33cb4
leaq 0x47857(%rip), %rsi     # 0x7b4e8
leaq 0x474f5(%rip), %rcx     # 0x7b18d
movl $0xb, %r13d
movl $0xb, %edi
movl $0xf74e, %edx           # imm = 0xF74E
xorl %eax, %eax
callq 0xa4c0
jmp 0x33b8d
movq 0x50(%r13), %rax
movq %rax, -0x68(%rbp)
leaq (%rax,%r15), %rsi
movq -0x58(%rbp), %rax
movq 0xa0(%rbx,%rax,8), %rax
movl 0x88(%rax), %eax
movl %eax, -0x4c(%rbp)
movq %r13, %rdi
callq *0x78(%r13)
movzwl %ax, %eax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x70(%r13), %rdi
callq 0x107f0
movl %eax, -0x40(%rbp)
movq -0x68(%rbp), %rax
leaq (%rax,%r15), %rdx
addq $-0x4, %rdx
movq -0x60(%rbp), %r15
leal 0x4(%r15), %ecx
movq %r14, (%rsp)
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %r8
movl -0x4c(%rbp), %r9d
callq 0x37952
movzwl 0x12(%r13), %esi
decl %esi
movq %r13, %rdi
movl %r15d, %edx
movq %r14, %rcx
callq 0x37826
movl -0x40(%rbp), %r13d
testl %r13d, %r13d
jne 0x33b8d
jmp 0x33bdb
movb $0x2, 0x56(%rbx)
movzwl 0x12(%r12), %eax
cmpw %ax, -0x48(%rbp)
jae 0x33d6a
movl $0x1, 0x50(%rbx)
jmp 0x33d79
movl $0xffffffff, 0x50(%rbx) # imm = 0xFFFFFFFF
decl %eax
movw %ax, 0x70(%rbx,%r14,2)
xorl %r13d, %r13d
jmp 0x33b8d