// File: /workspace/github_c_makefile_star2_O0/shifengbin[P]bishe/Output/../Persistence/sqlite3.c
// Function: sqlite3BtreeInsert
SQLITE_PRIVATE int sqlite3BtreeInsert(
  BtCursor *pCur,                /* Insert data into the table of this cursor */
  const void *pKey, i64 nKey,    /* The key of the new record */
  const void *pData, int nData,  /* The data of the new record */
  int nZero,                     /* Number of extra 0 bytes to append to data */
  int appendBias,                /* True if this is likely an append */
  int seekResult                 /* Result of prior MovetoUnpacked() call */
){
  int rc;
  int loc = seekResult;          /* -1: before desired location  +1: after */
  int szNew = 0;
  int idx;
  MemPage *pPage;
  Btree *p = pCur->pBtree;
  BtShared *pBt = p->pBt;
  unsigned char *oldCell;
  unsigned char *newCell = 0;

  if( pCur->eState==CURSOR_FAULT ){
    assert( pCur->skipNext!=SQLITE_OK );
    return pCur->skipNext;
  }

  assert( cursorHoldsMutex(pCur) );
  assert( (pCur->curFlags & BTCF_WriteFlag)!=0
              && pBt->inTransaction==TRANS_WRITE
              && (pBt->btsFlags & BTS_READ_ONLY)==0 );
  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );

  /* Assert that the caller has been consistent. If this cursor was opened
  ** expecting an index b-tree, then the caller should be inserting blob
  ** keys with no associated data. If the cursor was opened expecting an
  ** intkey table, the caller should be inserting integer keys with a
  ** blob of associated data.  */
  assert( (pKey==0)==(pCur->pKeyInfo==0) );

  /* Save the positions of any other cursors open on this table.
  **
  ** In some cases, the call to btreeMoveto() below is a no-op. For
  ** example, when inserting data into a table with auto-generated integer
  ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
  ** integer key to use. It then calls this function to actually insert the 
  ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
  ** that the cursor is already where it needs to be and returns without
  ** doing any work. To avoid thwarting these optimizations, it is important
  ** not to clear the cursor here.
  */
  if( pCur->curFlags & BTCF_Multiple ){
    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
    if( rc ) return rc;
  }

  if( pCur->pKeyInfo==0 ){
    assert( pKey==0 );
    /* If this is an insert into a table b-tree, invalidate any incrblob 
    ** cursors open on the row being replaced */
    invalidateIncrblobCursors(p, nKey, 0);

    /* If the cursor is currently on the last row and we are appending a
    ** new row onto the end, set the "loc" to avoid an unnecessary
    ** btreeMoveto() call */
    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
      && pCur->info.nKey==nKey-1 ){
       loc = -1;
    }else if( loc==0 ){
      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
      if( rc ) return rc;
    }
  }else if( loc==0 ){
    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
    if( rc ) return rc;
  }
  assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );

  pPage = pCur->apPage[pCur->iPage];
  assert( pPage->intKey || nKey>=0 );
  assert( pPage->leaf || !pPage->intKey );

  TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
          pCur->pgnoRoot, nKey, nData, pPage->pgno,
          loc==0 ? "overwrite" : "new entry"));
  assert( pPage->isInit );
  newCell = pBt->pTmpSpace;
  assert( newCell!=0 );
  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
  if( rc ) goto end_insert;
  assert( szNew==pPage->xCellSize(pPage, newCell) );
  assert( szNew <= MX_CELL_SIZE(pBt) );
  idx = pCur->aiIdx[pCur->iPage];
  if( loc==0 ){
    u16 szOld;
    assert( idx<pPage->nCell );
    rc = sqlite3PagerWrite(pPage->pDbPage);
    if( rc ){
      goto end_insert;
    }
    oldCell = findCell(pPage, idx);
    if( !pPage->leaf ){
      memcpy(newCell, oldCell, 4);
    }
    rc = clearCell(pPage, oldCell, &szOld);
    dropCell(pPage, idx, szOld, &rc);
    if( rc ) goto end_insert;
  }else if( loc<0 && pPage->nCell>0 ){
    assert( pPage->leaf );
    idx = ++pCur->aiIdx[pCur->iPage];
  }else{
    assert( pPage->leaf );
  }
  insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
  assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );

  /* If no error has occurred and pPage has an overflow cell, call balance() 
  ** to redistribute the cells within the tree. Since balance() may move
  ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
  ** variables.
  **
  ** Previous versions of SQLite called moveToRoot() to move the cursor
  ** back to the root page as balance() used to invalidate the contents
  ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
  ** set the cursor state to "invalid". This makes common insert operations
  ** slightly faster.
  **
  ** There is a subtle but important optimization here too. When inserting
  ** multiple records into an intkey b-tree using a single cursor (as can
  ** happen while processing an "INSERT INTO ... SELECT" statement), it
  ** is advantageous to leave the cursor pointing to the last entry in
  ** the b-tree if possible. If the cursor is left pointing to the last
  ** entry in the table, and the next row inserted has an integer key
  ** larger than the largest existing key, it is possible to insert the
  ** row without seeking the cursor. This can be a big performance boost.
  */
  pCur->info.nSize = 0;
  if( rc==SQLITE_OK && pPage->nOverflow ){
    pCur->curFlags &= ~(BTCF_ValidNKey);
    rc = balance(pCur);

    /* Must make sure nOverflow is reset to zero even if the balance()
    ** fails. Internal data structure corruption will result otherwise. 
    ** Also, set the cursor state to invalid. This stops saveCursorPosition()
    ** from trying to save the current position of the cursor.  */
    pCur->apPage[pCur->iPage]->nOverflow = 0;
    pCur->eState = CURSOR_INVALID;
  }
  assert( pCur->apPage[pCur->iPage]->nOverflow==0 );

end_insert:
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x56(%rax), %eax
cmpl $0x4, %eax
jne 0x44fa1
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x45288
movq -0x10(%rbp), %rax
movzbl 0x54(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x44fdb
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x48(%rax), %esi
movq -0x10(%rbp), %rdx
callq 0x32ea0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x44fd9
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x45288
jmp 0x44fdb
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x45062
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x4ad60
movq -0x10(%rbp), %rax
movzbl 0x54(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x4502a
cmpq $0x0, -0x20(%rbp)
jle 0x4502a
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x4502a
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
jmp 0x45060
cmpl $0x0, -0x38(%rbp)
jne 0x4505e
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl 0x10(%rbp), %ecx
xorl %eax, %eax
movl %eax, %esi
leaq -0x38(%rbp), %r8
callq 0x443c0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x4505c
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x45288
jmp 0x4505e
jmp 0x45060
jmp 0x45098
cmpl $0x0, -0x38(%rbp)
jne 0x45096
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl 0x10(%rbp), %ecx
leaq -0x38(%rbp), %r8
callq 0x48970
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x45094
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x45288
jmp 0x45096
jmp 0x45098
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbq 0x58(%rcx), %rcx
movq 0x98(%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %r9d
movl -0x30(%rbp), %r10d
leaq -0x3c(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x4adf0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x450fc
jmp 0x45282
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbq 0x58(%rcx), %rcx
movzwl 0x70(%rax,%rcx,2), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x451cb
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x12d70
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x45136
jmp 0x45282
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x48(%rbp), %rcx
movzwl 0x14(%rcx), %ecx
movq -0x48(%rbp), %rdx
movq 0x60(%rdx), %rdx
movl -0x40(%rbp), %esi
shll %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
shll $0x8, %edx
movq -0x48(%rbp), %rsi
movq 0x60(%rsi), %rsi
movl -0x40(%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, -0x60(%rbp)
movq -0x48(%rbp), %rax
cmpb $0x0, 0x5(%rax)
jne 0x45196
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq -0x6a(%rbp), %rdx
callq 0x4b200
movl %eax, -0x34(%rbp)
movq -0x48(%rbp), %rdi
movl -0x40(%rbp), %esi
movzwl -0x6a(%rbp), %edx
leaq -0x34(%rbp), %rcx
callq 0x4b3f0
cmpl $0x0, -0x34(%rbp)
je 0x451c9
jmp 0x45282
jmp 0x45205
cmpl $0x0, -0x38(%rbp)
jge 0x45201
movq -0x48(%rbp), %rax
movzwl 0x12(%rax), %eax
cmpl $0x0, %eax
jle 0x45201
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movsbq 0x58(%rax), %rdx
movw 0x70(%rcx,%rdx,2), %ax
addw $0x1, %ax
movw %ax, 0x70(%rcx,%rdx,2)
movzwl %ax, %eax
movl %eax, -0x40(%rbp)
jmp 0x45203
jmp 0x45203
jmp 0x45205
movq -0x48(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x68(%rbp), %rdx
movl -0x3c(%rbp), %ecx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
leaq -0x34(%rbp), %rax
movq %rax, (%rsp)
callq 0x4b610
movq -0x10(%rbp), %rax
movw $0x0, 0x36(%rax)
cmpl $0x0, -0x34(%rbp)
jne 0x45280
movq -0x48(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x45280
movq -0x10(%rbp), %rax
movzbl 0x54(%rax), %ecx
andl $-0x3, %ecx
movb %cl, 0x54(%rax)
movq -0x10(%rbp), %rdi
callq 0x4b840
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbq 0x58(%rcx), %rcx
movq 0x98(%rax,%rcx,8), %rax
movb $0x0, 0x1(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x56(%rax)
jmp 0x45282
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)