// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movl %r8d, -0x40(%rbp)
movq %rcx, -0x88(%rbp)
movq %rdi, %r12
movl 0x18(%rbp), %r14d
movl %r14d, -0x44(%rbp)
cmpb $0x4, 0x56(%rdi)
jne 0x34112
movl 0x50(%r12), %eax
jmp 0x34675
movq %rdx, %r8
movq (%r12), %r13
movq 0x8(%r13), %r15
testb $0x20, 0x54(%r12)
movq %rsi, -0x70(%rbp)
je 0x3415e
movl 0x48(%r12), %esi
movq %r15, %rdi
movq %r12, %rdx
movq %r9, %rbx
movq %r15, %r14
movq %r8, %r15
callq 0x26a70
movq %r15, %r8
movq %r14, %r15
movl 0x18(%rbp), %r14d
movq -0x70(%rbp), %rsi
movq %rbx, %r9
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x34675
movl 0x10(%rbp), %ecx
cmpq $0x0, 0x60(%r12)
je 0x3418f
testl %r14d, %r14d
jne 0x341fb
movq %r8, %r14
leaq -0x44(%rbp), %r8
movq %r12, %rdi
movq %r14, %rdx
movq %r9, %rbx
callq 0x370b6
movq %r14, %r8
movq %rbx, %r9
jmp 0x341f0
movq %r9, -0x38(%rbp)
movq %r13, %rdi
movq %r8, %rsi
xorl %edx, %edx
movq %r8, %rbx
callq 0x38327
movq %rbx, %r8
testq %rbx, %rbx
jle 0x341cc
movb 0x54(%r12), %al
andb $0x2, %al
je 0x341cc
leaq -0x1(%r8), %rax
cmpq %rax, 0x20(%r12)
jne 0x341cc
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %r9
jmp 0x341fb
testl %r14d, %r14d
movq -0x38(%rbp), %r9
movl 0x10(%rbp), %ecx
jne 0x341fb
leaq -0x44(%rbp), %r8
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x338ff
movq %rbx, %r8
movq -0x38(%rbp), %r9
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x34675
movsbq 0x58(%r12), %rax
movq 0x98(%r12,%rax,8), %r13
movq 0x80(%r15), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x78(%rbp)
movq 0x48(%r13), %rbx
movl $0x0, -0x30(%rbp)
movzbl 0x7(%r13), %r14d
addl -0x40(%rbp), %r9d
cmpb $0x0, 0x3(%r13)
movq %r9, -0x38(%rbp)
je 0x34272
cmpl $0x7f, %r9d
ja 0x34253
movl %r14d, %eax
movq -0x50(%rbp), %rcx
movb %r9b, (%rcx,%rax)
movl $0x1, %eax
jmp 0x3426c
movq -0x50(%rbp), %rax
leaq (%rax,%r14), %rdi
movslq %r9d, %rsi
movq %r8, %r15
callq 0x32f32
movq %r15, %r8
movzbl %al, %eax
addl %r14d, %eax
movl %eax, %r14d
movl %r14d, %edi
addq -0x50(%rbp), %rdi
movq %r8, %rsi
movq %r8, %r15
callq 0x32f32
movl %eax, %ecx
cmpb $0x0, 0x2(%r13)
je 0x342a7
movl -0x40(%rbp), %eax
movl %eax, %r8d
movq -0x88(%rbp), %r11
movl $0x0, -0x40(%rbp)
movq -0x38(%rbp), %r10
jmp 0x342b1
movl %r15d, %r10d
movq -0x70(%rbp), %r11
movq %r15, %r8
addl %r14d, %ecx
movzwl 0xa(%r13), %esi
cmpl %esi, %r10d
jle 0x34302
movzwl 0xc(%r13), %edi
movl %r10d, %eax
subl %edi, %eax
movq 0x48(%r13), %rdx
movq %r8, %r9
movl 0x34(%rdx), %r8d
addl $-0x4, %r8d
xorl %edx, %edx
divl %r8d
movq %r9, %r8
movl %edx, %r9d
addl %edi, %r9d
cmpl %esi, %r9d
cmovgl %edi, %r9d
leal (%r9,%rcx), %eax
leal (%r9,%rcx), %edx
addl $0x4, %edx
movl %edx, -0x64(%rbp)
cltq
addq -0x50(%rbp), %rax
jmp 0x3431b
leal (%r10,%rcx), %eax
cmpl $0x5, %eax
movl $0x4, %edx
cmovgel %eax, %edx
movl %edx, -0x64(%rbp)
movl %r10d, %r9d
movq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
movq %r13, -0x90(%rbp)
movq %r12, -0x98(%rbp)
testl %r10d, %r10d
jle 0x3453e
movslq %ecx, %r14
addq -0x50(%rbp), %r14
movq $0x0, -0x60(%rbp)
movl $0xcccccccd, %r12d      # imm = 0xCCCCCCCD
testl %r9d, %r9d
jne 0x344bc
movq %r14, %r15
movq %r8, -0x38(%rbp)
movl -0x30(%rbp), %r14d
cmpb $0x0, 0x21(%rbx)
je 0x343c7
movl 0x5f405(%rip), %ecx     # 0x93770
movl %r14d, %r8d
movl %r8d, %edi
leal 0x1(%rdi), %r8d
xorl %eax, %eax
cmpl $0x2, %r8d
jb 0x343b2
movl 0x34(%rbx), %r9d
imulq %r12, %r9
shrq $0x22, %r9
incl %r9d
leal -0x1(%rdi), %esi
movl %esi, %eax
xorl %edx, %edx
divl %r9d
subl %edx, %esi
movl %ecx, %eax
xorl %edx, %edx
divl 0x30(%rbx)
leal 0x1(%rsi), %edx
xorl %r9d, %r9d
cmpl %eax, %edx
sete %r9b
leal (%rsi,%r9), %eax
addl $0x2, %eax
cmpl %eax, %r8d
je 0x3436e
movl %ecx, %eax
xorl %edx, %edx
divl 0x30(%rbx)
cmpl %eax, %edi
je 0x3436e
incl %edi
movl %edi, -0x30(%rbp)
movq %r11, %r13
movq %r10, %r12
movl -0x30(%rbp), %ecx
movq %rbx, %rdi
leaq -0x78(%rbp), %rsi
leaq -0x30(%rbp), %rdx
xorl %r8d, %r8d
callq 0x274cb
movl %eax, -0x54(%rbp)
cmpb $0x0, 0x21(%rbx)
setne %cl
testl %eax, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x3442e
movl -0x30(%rbp), %esi
cmpl $0x1, %r14d
movl $0x4, %edx
sbbl $0x0, %edx
movq %rbx, %rdi
movl %r14d, %ecx
leaq -0x54(%rbp), %r8
callq 0x281b2
cmpl $0x0, -0x54(%rbp)
je 0x3442e
movq -0x78(%rbp), %rax
testq %rax, %rax
je 0x3442e
movq 0x70(%rax), %rdi
callq 0x25020
movl -0x54(%rbp), %edx
testl %edx, %edx
je 0x34459
xorl %r9d, %r9d
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x344a4
movq 0x70(%rax), %rdi
movl %edx, %r14d
callq 0x25020
movl %r14d, %edx
xorl %r9d, %r9d
movl %r14d, -0x3c(%rbp)
jmp 0x344a7
movl -0x30(%rbp), %eax
bswapl %eax
movq -0x80(%rbp), %rcx
movl %eax, (%rcx)
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x3447c
movq 0x70(%rax), %rdi
movl %edx, %r14d
callq 0x25020
movl %r14d, %edx
movq -0x78(%rbp), %rcx
movq 0x50(%rcx), %rax
movq %rax, -0x80(%rbp)
movl $0x0, (%rax)
movq %rcx, -0x60(%rbp)
movq 0x50(%rcx), %r14
addq $0x4, %r14
movl 0x34(%rbx), %r9d
addl $-0x4, %r9d
jmp 0x344aa
movl %edx, -0x3c(%rbp)
movq %r15, %r14
testl %edx, %edx
movq %r12, %r10
movq %r13, %r11
movq -0x38(%rbp), %r8
jne 0x3455f
cmpl %r9d, %r10d
movq %r9, -0x70(%rbp)
movl %r9d, %r12d
movq %r10, -0x38(%rbp)
cmovll %r10d, %r12d
testl %r8d, %r8d
jle 0x344f0
cmpl %r8d, %r12d
cmovgel %r8d, %r12d
movslq %r12d, %rdx
movq %r14, %rdi
movq %r11, %rsi
movq %r11, %r15
movq %r8, %r13
callq 0xa310
jmp 0x34503
movslq %r12d, %rdx
movq %r14, %rdi
xorl %esi, %esi
movq %r11, %r15
movq %r8, %r13
callq 0xa210
movslq %r12d, %rax
addq %rax, %r14
addq %rax, %r15
movq -0x70(%rbp), %r9
subl %eax, %r9d
subl %eax, %r13d
cmovel -0x40(%rbp), %r13d
cmoveq -0x88(%rbp), %r15
movq -0x38(%rbp), %r10
subl %eax, %r10d
movq %r15, %r11
movq %r13, %r8
movl $0xcccccccd, %r12d      # imm = 0xCCCCCCCD
jg 0x3434b
jmp 0x34546
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x34558
movq 0x70(%rax), %rdi
callq 0x25020
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x2c(%rbp)
testl %eax, %eax
movq -0x98(%rbp), %r12
movq -0x90(%rbp), %r13
jne 0x34672
movsbq 0x58(%r12), %rax
movzwl 0x70(%r12,%rax,2), %ebx
cmpl $0x0, -0x44(%rbp)
je 0x345a8
jns 0x34615
cmpw $0x0, 0x12(%r13)
je 0x34615
incl %ebx
movw %bx, 0x70(%r12,%rax,2)
movzwl %bx, %ebx
jmp 0x34615
movq 0x70(%r13), %rdi
callq 0x10764
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x34672
movzwl 0x14(%r13), %eax
movq 0x60(%r13), %rcx
movzbl (%rcx,%rbx,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%rbx,2), %esi
orl %edx, %esi
andl %eax, %esi
addq 0x50(%r13), %rsi
cmpb $0x0, 0x5(%r13)
jne 0x345e8
movl (%rsi), %eax
movq -0x50(%rbp), %rcx
movl %eax, (%rcx)
leaq -0x78(%rbp), %r14
movq %r13, %rdi
movq %r14, %rdx
callq 0x3835e
leaq -0x2c(%rbp), %r15
movl %eax, (%r15)
movzwl (%r14), %edx
movq %r13, %rdi
movl %ebx, %esi
movq %r15, %rcx
callq 0x3853a
cmpl $0x0, (%r15)
jne 0x34672
leaq -0x2c(%rbp), %r14
movq %r14, (%rsp)
movq %r13, %rdi
movl %ebx, %esi
movq -0x50(%rbp), %rdx
movl -0x64(%rbp), %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x38666
movw $0x0, 0x36(%r12)
cmpl $0x0, (%r14)
jne 0x34672
cmpb $0x0, 0x1(%r13)
je 0x34672
andb $-0x3, 0x54(%r12)
movq %r12, %rdi
callq 0x3891a
movl %eax, -0x2c(%rbp)
movsbq 0x58(%r12), %rax
movq 0x98(%r12,%rax,8), %rax
xorl %ecx, %ecx
movb %cl, 0x1(%rax)
movb %cl, 0x56(%r12)
movl -0x2c(%rbp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq