// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movsbl 0x58(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movzwl 0x70(%rax,%rcx,2), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq 0x98(%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x38(%rbp), %rcx
movzwl 0x14(%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
cmpb $0x0, 0x5(%rax)
jne 0x4537d
movl $0x0, -0x54(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x44b10
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x4537b
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x456df
jmp 0x4537d
movq -0x10(%rbp), %rax
movzbl 0x54(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x453b7
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x48(%rax), %esi
movq -0x10(%rbp), %rdx
callq 0x32ea0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x453b5
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x456df
jmp 0x453b7
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x453d5
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
xorl %edx, %edx
callq 0x4ad60
cmpl $0x0, -0x14(%rbp)
je 0x45451
movq -0x38(%rbp), %rax
cmpb $0x0, 0x5(%rax)
je 0x45429
movq -0x38(%rbp), %rax
movzwl 0x10(%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2b4b0
movw %ax, %cx
movl -0x78(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
addl $0x2, %eax
movl %eax, -0x74(%rbp)
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
shll %eax
movl $0x3, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movl -0x74(%rbp), %eax
cmpl %ecx, %eax
jle 0x45448
movq -0x10(%rbp), %rdi
callq 0x33320
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x45446
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x456df
jmp 0x4544f
movl $0x1, -0x50(%rbp)
jmp 0x45451
movq -0x38(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x12d70
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x45472
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x456df
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq -0x4a(%rbp), %rdx
callq 0x4b200
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %esi
movzwl -0x4a(%rbp), %edx
leaq -0x2c(%rbp), %rcx
callq 0x4b3f0
cmpl $0x0, -0x2c(%rbp)
je 0x454ab
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x456df
movq -0x38(%rbp), %rax
cmpb $0x0, 0x5(%rax)
jne 0x45600
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbq 0x58(%rcx), %rcx
movq 0x98(%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x98(%rax,%rcx,8), %rax
movl 0x88(%rax), %eax
movl %eax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x60(%rbp), %rcx
movzwl 0x14(%rcx), %ecx
movq -0x60(%rbp), %rdx
movq 0x60(%rdx), %rdx
movq -0x60(%rbp), %rsi
movzwl 0x12(%rsi), %esi
subl $0x1, %esi
shll %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
shll $0x8, %edx
movq -0x60(%rbp), %rsi
movq 0x60(%rsi), %rsi
movq -0x60(%rbp), %rdi
movzwl 0x12(%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 -0x60(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
jae 0x45571
movl $0xf74e, %edi           # imm = 0xF74E
callq 0x2a650
movl %eax, -0x4(%rbp)
jmp 0x456df
movq -0x60(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq *%rax
movzwl %ax, %eax
movl %eax, -0x64(%rbp)
movq -0x28(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x60(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x12d70
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %esi
movq -0x40(%rbp), %rdx
addq $-0x4, %rdx
movl -0x64(%rbp), %ecx
addl $0x4, %ecx
movq -0x70(%rbp), %r8
movl -0x68(%rbp), %r9d
leaq -0x2c(%rbp), %rax
movq %rax, (%rsp)
callq 0x4b610
movq -0x60(%rbp), %rdi
movq -0x60(%rbp), %rax
movzwl 0x12(%rax), %esi
subl $0x1, %esi
movl -0x64(%rbp), %edx
leaq -0x2c(%rbp), %rcx
callq 0x4b3f0
cmpl $0x0, -0x2c(%rbp)
je 0x455fe
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x456df
jmp 0x45600
movq -0x10(%rbp), %rdi
callq 0x4b840
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x45663
movq -0x10(%rbp), %rax
movsbl 0x58(%rax), %eax
cmpl -0x48(%rbp), %eax
jle 0x45663
jmp 0x45621
movq -0x10(%rbp), %rax
movsbl 0x58(%rax), %eax
cmpl -0x48(%rbp), %eax
jle 0x45657
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rdx
movb 0x58(%rdx), %cl
movb %cl, %sil
addb $-0x1, %sil
movb %sil, 0x58(%rdx)
movsbq %cl, %rcx
movq 0x98(%rax,%rcx,8), %rdi
callq 0x30e20
jmp 0x45621
movq -0x10(%rbp), %rdi
callq 0x4b840
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x456d9
cmpl $0x0, -0x50(%rbp)
je 0x456bb
movq -0x10(%rbp), %rax
movb $0x2, 0x56(%rax)
movl -0x44(%rbp), %eax
movq -0x38(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jl 0x456ae
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x50(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movzwl 0x12(%rax), %eax
subl $0x1, %eax
movw %ax, %dx
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movw %dx, 0x70(%rax,%rcx,2)
jmp 0x456b9
movq -0x10(%rbp), %rax
movl $0x1, 0x50(%rax)
jmp 0x456d7
movq -0x10(%rbp), %rdi
callq 0x48dd0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x456d5
movq -0x10(%rbp), %rax
movb $0x3, 0x56(%rax)
jmp 0x456d7
jmp 0x456d9
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)