// File: /workspace/github_c_makefile_star2_O3/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, -0x34(%rbp)
movq %rcx, -0x90(%rbp)
movq %rdx, %r10
movq %rdi, %rdx
movl 0x18(%rbp), %r13d
movl %r13d, -0x38(%rbp)
cmpb $0x4, 0x56(%rdi)
jne 0x334f1
movl 0x50(%rdx), %r14d
jmp 0x33a24
movq %rsi, -0x58(%rbp)
movq (%rdx), %r12
movq 0x8(%r12), %rbx
testb $0x20, 0x54(%rdx)
movq %rdx, -0x48(%rbp)
je 0x3352a
movl 0x48(%rdx), %esi
movq %rbx, %rdi
movq %r10, %r14
movq %r9, %r15
callq 0x2680e
movq %r15, %r9
movq %r14, %r10
movq -0x48(%rbp), %rdx
testl %eax, %eax
jne 0x335cb
movl 0x10(%rbp), %ecx
cmpq $0x0, 0x60(%rdx)
je 0x33565
testl %r13d, %r13d
jne 0x335d3
leaq -0x38(%rbp), %r8
movq %rdx, %rdi
movq -0x58(%rbp), %rsi
movq %r10, %rdx
movq %r10, %r14
movq %r9, %r15
callq 0x364cd
movq %r15, %r9
movq %r14, %r10
movq -0x48(%rbp), %rdx
movl %eax, -0x3c(%rbp)
jmp 0x335c7
movq %r9, %r14
movq %r12, %rdi
movq %r10, %rsi
xorl %edx, %edx
movq %r10, %r15
callq 0x37635
movq %r15, %r10
movq -0x48(%rbp), %rdx
testq %r15, %r15
jle 0x335a1
movb 0x54(%rdx), %al
andb $0x2, %al
je 0x335a1
leaq -0x1(%r10), %rax
cmpq %rax, 0x20(%rdx)
jne 0x335a1
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movq %r14, %r9
jmp 0x335d3
testl %r13d, %r13d
movq %r14, %r9
movl 0x10(%rbp), %ecx
jne 0x335d3
leaq -0x38(%rbp), %r8
movq %rdx, %rdi
xorl %esi, %esi
movq %r10, %rdx
callq 0x32d6f
movq %r14, %r9
movq %r15, %r10
movq -0x48(%rbp), %rdx
testl %eax, %eax
je 0x335d3
movl %eax, %r14d
jmp 0x33a24
movsbq 0x58(%rdx), %rax
movq 0x98(%rdx,%rax,8), %r12
movq 0x80(%rbx), %r13
movq $0x0, -0x78(%rbp)
movq 0x48(%r12), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x2c(%rbp)
movzbl 0x7(%r12), %r14d
addl -0x34(%rbp), %r9d
cmpb $0x0, 0x3(%r12)
je 0x33629
cmpl $0x7f, %r9d
ja 0x3362e
movl %r14d, %eax
movq %r9, %r15
movb %r9b, (%r13,%rax)
movl $0x1, %eax
jmp 0x33646
movq %r9, %r15
jmp 0x3364c
leaq (%r14,%r13), %rdi
movq %r9, %r15
movslq %r9d, %rsi
movq %r10, %rbx
callq 0x323de
movq %rbx, %r10
movzbl %al, %eax
addl %r14d, %eax
movl %eax, %r14d
movl %r14d, %edi
addq %r13, %rdi
movq %r10, %rsi
movq %r10, %rbx
callq 0x323de
movl %eax, %ecx
cmpb $0x0, 0x2(%r12)
je 0x33680
movl -0x34(%rbp), %eax
movl %eax, %r10d
movq -0x90(%rbp), %r9
movl $0x0, -0x34(%rbp)
movq %r15, %r11
jmp 0x3368a
movl %ebx, %r11d
movq %rbx, %r10
movq -0x58(%rbp), %r9
addl %r14d, %ecx
movzwl 0xa(%r12), %esi
cmpl %esi, %r11d
jle 0x336d7
movzwl 0xc(%r12), %edi
movl %r11d, %eax
subl %edi, %eax
movq 0x48(%r12), %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, -0x30(%rbp)
cltq
addq %r13, %rax
jmp 0x336f8
leal (%r11,%rcx), %eax
cmpl $0x5, %eax
movl $0x4, %edx
cmovgel %eax, %edx
movl %edx, -0x30(%rbp)
movq %r13, %rax
movl %r11d, %r8d
testl %r11d, %r11d
jle 0x338f7
movq %rax, -0x88(%rbp)
movq %r12, -0x70(%rbp)
movslq %ecx, %r14
movq %r13, -0x68(%rbp)
addq %r13, %r14
movq $0x0, -0x50(%rbp)
movl $0xcccccccd, %ebx       # imm = 0xCCCCCCCD
movq %r9, %r13
testl %r8d, %r8d
jne 0x33865
movq %r13, -0x58(%rbp)
movl -0x2c(%rbp), %r13d
movq -0x80(%rbp), %r14
cmpb $0x0, 0x21(%r14)
movl %r13d, %ecx
je 0x337a0
movl 0x5e03e(%rip), %esi     # 0x91780
movl %r13d, %r8d
movl %r8d, %ecx
leal 0x1(%rcx), %r8d
xorl %eax, %eax
cmpl $0x2, %r8d
jb 0x3378a
movl 0x34(%r14), %r9d
imulq %rbx, %r9
shrq $0x22, %r9
incl %r9d
leal -0x1(%rcx), %edi
movl %edi, %eax
xorl %edx, %edx
divl %r9d
subl %edx, %edi
movl %esi, %eax
xorl %edx, %edx
divl 0x30(%r14)
leal 0x1(%rdi), %edx
xorl %r9d, %r9d
cmpl %eax, %edx
sete %r9b
leal (%rdi,%r9), %eax
addl $0x2, %eax
cmpl %eax, %r8d
je 0x33745
movl %esi, %eax
xorl %edx, %edx
divl 0x30(%r14)
cmpl %eax, %ecx
je 0x33745
incl %ecx
movl %ecx, -0x2c(%rbp)
movq %r11, %rbx
movq %r10, %r15
movq %r14, %rdi
leaq -0x78(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
xorl %r8d, %r8d
callq 0x2720e
movq %r14, %rcx
movl %eax, %r14d
movl %eax, -0x5c(%rbp)
cmpb $0x0, 0x21(%rcx)
je 0x33802
testl %r14d, %r14d
jne 0x33802
movl -0x2c(%rbp), %r12d
cmpl $0x1, %r13d
movl $0x4, %edx
sbbl $0x0, %edx
movq -0x80(%rbp), %rdi
movl %r12d, %esi
movl %r13d, %ecx
leaq -0x5c(%rbp), %r8
callq 0x27df6
movl -0x5c(%rbp), %r14d
testl %r14d, %r14d
movq -0x50(%rbp), %rcx
je 0x33813
jmp 0x33904
testl %r14d, %r14d
jne 0x33924
movl -0x2c(%rbp), %r12d
movq -0x50(%rbp), %rcx
bswapl %r12d
movq -0x88(%rbp), %rax
movl %r12d, (%rax)
testq %rcx, %rcx
je 0x3382e
movq 0x70(%rcx), %rdi
callq 0x24e28
movq -0x78(%rbp), %rcx
movq 0x50(%rcx), %rax
movq %rax, -0x88(%rbp)
movl $0x0, (%rax)
movq %rcx, -0x50(%rbp)
movq 0x50(%rcx), %r14
addq $0x4, %r14
movq -0x80(%rbp), %rax
movl 0x34(%rax), %r8d
addl $-0x4, %r8d
movq %r15, %r10
movq %rbx, %r11
movq -0x58(%rbp), %r13
cmpl %r8d, %r11d
movq %r8, -0x98(%rbp)
movl %r8d, %r15d
movq %r11, -0x58(%rbp)
cmovll %r11d, %r15d
testl %r10d, %r10d
jle 0x3389c
cmpl %r10d, %r15d
cmovgel %r10d, %r15d
movslq %r15d, %r12
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r10, %rbx
callq 0xa320
jmp 0x338af
movslq %r15d, %r12
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r10, %rbx
callq 0xa220
addq %r12, %r14
addq %r12, %r13
movq -0x98(%rbp), %r8
subl %r15d, %r8d
subl %r15d, %ebx
cmovel -0x34(%rbp), %ebx
cmoveq -0x90(%rbp), %r13
movq -0x58(%rbp), %r11
subl %r15d, %r11d
movq %rbx, %r10
movl $0xcccccccd, %ebx       # imm = 0xCCCCCCCD
jg 0x3371d
xorl %r14d, %r14d
movq -0x50(%rbp), %rcx
testq %rcx, %rcx
movq -0x70(%rbp), %r12
movq -0x68(%rbp), %r13
jne 0x33939
movl $0x0, -0x3c(%rbp)
movq -0x48(%rbp), %rdx
jmp 0x33953
movq -0x78(%rbp), %rax
testq %rax, %rax
je 0x3391a
movq 0x70(%rax), %rdi
callq 0x24e28
movq -0x50(%rbp), %rcx
movq -0x70(%rbp), %r12
movq -0x68(%rbp), %r13
jmp 0x33930
movq -0x70(%rbp), %r12
movq -0x68(%rbp), %r13
movq -0x50(%rbp), %rcx
testq %rcx, %rcx
je 0x33a24
movq 0x70(%rcx), %rdi
callq 0x24e28
movl %r14d, -0x3c(%rbp)
testl %r14d, %r14d
movq -0x48(%rbp), %rdx
jne 0x33a24
movsbq 0x58(%rdx), %rax
movzwl 0x70(%rdx,%rax,2), %ebx
cmpl $0x0, -0x38(%rbp)
je 0x3396d
js 0x339e1
movl -0x30(%rbp), %ecx
jmp 0x339f7
movq 0x70(%r12), %rdi
callq 0x107f0
movl %eax, %r14d
testl %eax, %eax
jne 0x33a24
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 0x339b0
movl (%rsi), %eax
movl %eax, (%r13)
leaq -0x78(%rbp), %r14
movq %r12, %rdi
movq %r14, %rdx
callq 0x3766c
leaq -0x3c(%rbp), %r15
movl %eax, (%r15)
movzwl (%r14), %edx
movq %r12, %rdi
movl %ebx, %esi
movq %r15, %rcx
callq 0x37826
movl (%r15), %r14d
testl %r14d, %r14d
jne 0x33a24
jmp 0x33965
cmpw $0x0, 0x12(%r12)
movl -0x30(%rbp), %ecx
je 0x339f7
incl %ebx
movw %bx, 0x70(%rdx,%rax,2)
movzwl %bx, %ebx
leaq -0x3c(%rbp), %r14
movq %r14, (%rsp)
movq %r12, %rdi
movl %ebx, %esi
movq %r13, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x37952
movq -0x48(%rbp), %rdi
movw $0x0, 0x36(%rdi)
movl (%r14), %r14d
testl %r14d, %r14d
je 0x33a36
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x1(%r12)
movl $0x0, %r14d
je 0x33a24
andb $-0x3, 0x54(%rdi)
movq %rdi, %rbx
callq 0x37bc9
movsbq 0x58(%rbx), %rcx
movq 0x98(%rbx,%rcx,8), %rcx
xorl %edx, %edx
movb %dl, 0x1(%rcx)
movb %dl, 0x56(%rbx)
jmp 0x33a27