// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r8d, -0x30(%rbp)
movq %rcx, -0x88(%rbp)
movl 0x18(%rbp), %r15d
movl %r15d, -0x34(%rbp)
cmpb $0x4, 0x56(%rdi)
jne 0x2fce4
movl 0x50(%rdi), %r14d
jmp 0x3012d
movq %rdx, %r13
movq %rsi, -0x50(%rbp)
movq (%rdi), %r12
movq 0x8(%r12), %rbx
testb $0x20, 0x54(%rdi)
movq %rdi, -0x40(%rbp)
je 0x2fd1d
movl 0x48(%rdi), %esi
movq %rbx, %rdi
movq -0x40(%rbp), %rdx
movq %r9, %r14
callq 0x23353
movq %r14, %r9
movq -0x40(%rbp), %rdi
movl %eax, -0x44(%rbp)
testl %eax, %eax
jne 0x2fd98
movl 0x10(%rbp), %ecx
cmpq $0x0, 0x60(%rdi)
je 0x2fd41
testl %r15d, %r15d
jne 0x2fda0
leaq -0x34(%rbp), %r8
movq -0x50(%rbp), %rsi
movq %r13, %rdx
movq %r9, %r14
callq 0x30c82
jmp 0x2fd8d
movq %r9, %r14
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x31bba
movq -0x40(%rbp), %rdi
testq %r13, %r13
jle 0x2fd74
movb 0x54(%rdi), %al
andb $0x2, %al
je 0x2fd74
leaq -0x1(%r13), %rax
cmpq %rax, 0x20(%rdi)
jne 0x2fd74
orl $-0x1, -0x34(%rbp)
movq %r14, %r9
jmp 0x2fda0
testl %r15d, %r15d
movq %r14, %r9
movl 0x10(%rbp), %ecx
jne 0x2fda0
leaq -0x34(%rbp), %r8
xorl %esi, %esi
movq %r13, %rdx
callq 0x2f5c8
movq %r14, %r9
movq -0x40(%rbp), %rdi
testl %eax, %eax
je 0x2fda0
movl %eax, %r14d
jmp 0x3012d
movsbq 0x58(%rdi), %rax
movq 0x98(%rdi,%rax,8), %r15
movq 0x80(%rbx), %r12
andq $0x0, -0x68(%rbp)
movq 0x48(%r15), %rbx
andl $0x0, -0x2c(%rbp)
movzbl 0x7(%r15), %r14d
addl -0x30(%rbp), %r9d
cmpb $0x0, 0x3(%r15)
movq %r9, -0x60(%rbp)
je 0x2fdfc
cmpl $0x7f, %r9d
ja 0x2fde7
movl %r14d, %eax
movb %r9b, (%r12,%rax)
pushq $0x1
popq %rax
jmp 0x2fdf6
leaq (%r12,%r14), %rdi
movslq %r9d, %rsi
callq 0x2f0d2
movzbl %al, %eax
addl %r14d, %eax
movl %eax, %r14d
movl %r14d, %edi
addq %r12, %rdi
movq %r13, %rsi
callq 0x2f0d2
movl %eax, %ecx
cmpb $0x0, 0x2(%r15)
je 0x2fe2d
movl -0x30(%rbp), %eax
movl %eax, %r13d
movq -0x88(%rbp), %r10
movl $0x0, -0x30(%rbp)
movq -0x60(%rbp), %r9
jmp 0x2fe34
movl %r13d, %r9d
movq -0x50(%rbp), %r10
addl %r14d, %ecx
movzwl 0xa(%r15), %esi
pushq $0x4
popq %rdx
cmpl %esi, %r9d
movq %rbx, -0x98(%rbp)
movq %r15, -0x90(%rbp)
jle 0x2fe93
movzwl 0xc(%r15), %edi
movl %r9d, %eax
subl %edi, %eax
movq 0x48(%r15), %rdx
movl 0x34(%rdx), %r8d
addl $-0x4, %r8d
xorl %edx, %edx
divl %r8d
movl %edx, %r8d
addl %edi, %r8d
cmpl %esi, %r8d
cmovgl %edi, %r8d
leal (%r8,%rcx), %eax
leal (%r8,%rcx), %edx
addl $0x4, %edx
movl %edx, -0x54(%rbp)
cltq
addq %r12, %rax
movq %rax, -0x70(%rbp)
jmp 0x2fea7
leal (%r9,%rcx), %eax
cmpl $0x5, %eax
cmovll %edx, %eax
movl %eax, -0x54(%rbp)
movl %r9d, %r8d
movq %r12, -0x70(%rbp)
movslq %ecx, %rbx
movq %r12, -0x80(%rbp)
addq %r12, %rbx
movq $0x0, -0x78(%rbp)
movq %r10, %r14
testl %r9d, %r9d
jle 0x30036
testl %r8d, %r8d
movq %r9, -0x60(%rbp)
jne 0x2ffd0
movq %r14, -0x50(%rbp)
movl -0x2c(%rbp), %r15d
movq -0x98(%rbp), %rbx
cmpb $0x0, 0x21(%rbx)
movl %r15d, %r14d
je 0x2ff2d
movq %r13, -0xa0(%rbp)
movl 0x53978(%rip), %r13d    # 0x83870
movl %r15d, %r12d
movl %r12d, %r14d
leal 0x1(%r14), %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x23275
cmpl %eax, %r12d
je 0x2fefb
movl %r13d, %eax
xorl %edx, %edx
divl 0x30(%rbx)
cmpl %eax, %r14d
je 0x2fefb
incl %r14d
movl %r14d, -0x2c(%rbp)
movq -0xa0(%rbp), %r13
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x23c1e
movl %eax, %r14d
movl %eax, -0x58(%rbp)
cmpb $0x0, 0x21(%rbx)
je 0x2ff82
testl %r14d, %r14d
jne 0x2ff82
movl -0x2c(%rbp), %r12d
cmpl $0x1, %r15d
pushq $0x4
popq %rdx
sbbl $0x0, %edx
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %ecx
leaq -0x58(%rbp), %r8
callq 0x247e7
movl -0x58(%rbp), %r14d
testl %r14d, %r14d
je 0x2ff8f
jmp 0x30142
testl %r14d, %r14d
jne 0x30039
movl -0x2c(%rbp), %r12d
bswapl %r12d
movq -0x70(%rbp), %rax
movl %r12d, (%rax)
movq -0x78(%rbp), %rdi
callq 0x228f1
movq -0x68(%rbp), %rcx
movq 0x50(%rcx), %rax
movq %rax, -0x70(%rbp)
andl $0x0, (%rax)
movq %rcx, -0x78(%rbp)
movq %rbx, %rax
movq 0x50(%rcx), %rbx
addq $0x4, %rbx
movl 0x34(%rax), %r8d
addl $-0x4, %r8d
movq -0x60(%rbp), %r9
movq -0x50(%rbp), %r14
cmpl %r8d, %r9d
movq %r8, -0x50(%rbp)
movl %r8d, %r15d
cmovll %r9d, %r15d
testl %r13d, %r13d
jle 0x2fffd
cmpl %r13d, %r15d
cmovgel %r13d, %r15d
movslq %r15d, %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xa3b0
jmp 0x3000d
movslq %r15d, %r12
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0xa270
movq -0x60(%rbp), %r9
subl %r15d, %r9d
addq %r12, %rbx
addq %r12, %r14
movq -0x50(%rbp), %r8
subl %r15d, %r8d
subl %r15d, %r13d
cmovel -0x30(%rbp), %r13d
cmoveq -0x88(%rbp), %r14
jmp 0x2febc
xorl %r14d, %r14d
movq -0x78(%rbp), %rdi
callq 0x228f1
movl %r14d, -0x44(%rbp)
testl %r14d, %r14d
movq -0x90(%rbp), %r12
jne 0x3012d
movq -0x40(%rbp), %rcx
movsbq 0x58(%rcx), %rax
movzwl 0x70(%rcx,%rax,2), %ebx
cmpl $0x0, -0x34(%rbp)
je 0x30089
jns 0x300fd
cmpw $0x0, 0x12(%r12)
je 0x300fd
incl %ebx
movw %bx, 0x70(%rcx,%rax,2)
movzwl %bx, %ebx
jmp 0x300fd
movq 0x70(%r12), %rdi
callq 0xfd43
movl %eax, %r14d
testl %eax, %eax
jne 0x3012d
movzwl 0x14(%r12), %eax
movq 0x60(%r12), %rcx
movzbl (%rcx,%rbx,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%rbx,2), %esi
orl %edx, %esi
andl %eax, %esi
addq 0x50(%r12), %rsi
cmpb $0x0, 0x5(%r12)
jne 0x300ce
movl (%rsi), %eax
movq -0x80(%rbp), %rcx
movl %eax, (%rcx)
leaq -0x68(%rbp), %r14
movq %r12, %rdi
movq %r14, %rdx
callq 0x31bf1
leaq -0x44(%rbp), %r15
movl %eax, (%r15)
movzwl (%r14), %edx
movq %r12, %rdi
movl %ebx, %esi
movq %r15, %rcx
callq 0x31d68
movl (%r15), %r14d
testl %r14d, %r14d
jne 0x3012d
leaq -0x44(%rbp), %r14
movq %r14, (%rsp)
movq %r12, %rdi
movl %ebx, %esi
movq -0x80(%rbp), %rdx
movl -0x54(%rbp), %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x31e7c
movq -0x40(%rbp), %rax
andw $0x0, 0x36(%rax)
movl (%r14), %r14d
testl %r14d, %r14d
je 0x30150
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x228f1
jmp 0x30039
cmpb $0x0, 0x1(%r12)
movl $0x0, %r14d
je 0x3012d
movq -0x40(%rbp), %rbx
andb $-0x3, 0x54(%rbx)
movq %rbx, %rdi
callq 0x320c9
movsbq 0x58(%rbx), %rcx
movq 0x98(%rbx,%rcx,8), %rcx
xorl %edx, %edx
movb %dl, 0x1(%rcx)
movb %dl, 0x56(%rbx)
jmp 0x30130