// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/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->wrFlag && 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.
  */
  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
  if( rc ) return rc;

  /* If this is an insert into a table b-tree, invalidate any incrblob 
  ** cursors open on the row being replaced (assuming this is a replace
  ** operation - if it is not, the following is a no-op).  */
  if( pCur->pKeyInfo==0 ){
    invalidateIncrblobCursors(p, nKey, 0);
  }

  if( !loc ){
    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 );
  allocateTempSpace(pBt);
  newCell = pBt->pTmpSpace;
  if( newCell==0 ) return SQLITE_NOMEM;
  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
  if( rc ) goto end_insert;
  assert( szNew==cellSizePtr(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);
    }
    szOld = cellSizePtr(pPage, oldCell);
    rc = clearCell(pPage, oldCell);
    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 BtCursor.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;
  pCur->validNKey = 0;
  if( rc==SQLITE_OK && pPage->nOverflow ){
    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 $0xa8, %rsp
movq %rcx, -0x98(%rbp)
movq %rdi, %r13
movl 0x18(%rbp), %r14d
movl %r14d, -0x4c(%rbp)
cmpb $0x4, 0x77(%rdi)
jne 0x2ad08
movl 0x70(%r13), %ebx
jmp 0x2ad36
movq %rdx, -0x48(%rbp)
movq %rsi, -0x40(%rbp)
movq %r8, -0x68(%rbp)
movq %r9, -0x70(%rbp)
movq (%r13), %r12
movq 0x8(%r12), %r15
movl 0x30(%r13), %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0x1e0b8
testl %eax, %eax
je 0x2ad4a
movl %eax, %ebx
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x20(%r13)
movq %r13, -0x58(%rbp)
movq -0x48(%rbp), %rbx
jne 0x2ad6a
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2c171
movq -0x58(%rbp), %r13
testl %r14d, %r14d
je 0x2addc
movswq 0x7a(%r13), %rax
movq 0xa8(%r13,%rax,8), %r14
movq %r15, %rdi
callq 0x2c19a
movq 0x80(%r15), %rbx
testq %rbx, %rbx
je 0x2ae03
andq $0x0, -0x80(%rbp)
movq 0x48(%r14), %rax
movq %rax, -0xa0(%rbp)
andl $0x0, -0x2c(%rbp)
xorl %r12d, %r12d
cmpb $0x0, 0x3(%r14)
sete %r12b
shll $0x2, %r12d
cmpb $0x0, 0x4(%r14)
je 0x2ae07
movl %r12d, %edi
addq %rbx, %rdi
movq -0x70(%rbp), %r13
movq -0x68(%rbp), %rax
movq %rax, %r15
addl %r13d, %eax
movslq %eax, %rsi
callq 0x2bb2a
addl %eax, %r12d
jmp 0x2ae0d
movl 0x10(%rbp), %ecx
leaq -0x4c(%rbp), %r8
movq %r13, %rdi
movq -0x40(%rbp), %rsi
movq %rbx, %rdx
callq 0x2b9f7
movq -0x58(%rbp), %r13
testl %eax, %eax
jne 0x2ad34
jmp 0x2ad6f
pushq $0x7
jmp 0x2ae86
xorl %r13d, %r13d
xorl %r15d, %r15d
movslq %r12d, %rdi
addq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x2bb2a
movq %rbx, %rsi
movl %eax, %ebx
leaq -0xc0(%rbp), %rdx
movq %r14, %rdi
movq %rsi, -0x78(%rbp)
callq 0x1e436
addl %r15d, %r13d
cmpb $0x0, 0x2(%r14)
movq %r13, %rdx
je 0x2ae59
movq %r14, -0x38(%rbp)
movq -0x98(%rbp), %rsi
movl %r15d, %r13d
movq $0x0, -0x68(%rbp)
jmp 0x2ae93
movq %r15, -0x68(%rbp)
movl $0x80000000, %eax       # imm = 0x80000000
movq -0x48(%rbp), %r13
cmpq %rax, %r13
setl %al
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
setne %cl
testb %al, %cl
jne 0x2ae8c
movl $0xd757, %edi           # imm = 0xD757
callq 0x1a1fc
pushq $0xb
popq %rbx
jmp 0x2ad36
movq %r14, -0x38(%rbp)
addl %r13d, %edx
addl %r12d, %ebx
movzwl -0xa2(%rbp), %eax
movl %eax, -0x84(%rbp)
movzwl -0xa6(%rbp), %edi
movslq %ebx, %r15
movq -0x78(%rbp), %rax
addq %rax, %r15
movzwl -0xa4(%rbp), %ecx
addq %rax, %rcx
movq %rcx, -0x90(%rbp)
movq $0x0, -0x60(%rbp)
testl %edx, %edx
jle 0x2b041
testl %edi, %edi
movq %rdx, -0x70(%rbp)
jne 0x2afd8
movq %rsi, -0x40(%rbp)
movl -0x2c(%rbp), %eax
movq -0xa0(%rbp), %r15
cmpb $0x0, 0x21(%r15)
movl %eax, %r14d
movl %eax, %ebx
je 0x2af36
movq %r13, -0x48(%rbp)
movl 0x5c93a(%rip), %r12d    # 0x87840
movl %r14d, %r13d
movl %r13d, %ebx
leal 0x1(%rbx), %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x1dfda
cmpl %eax, %r13d
je 0x2af09
movl %r12d, %eax
xorl %edx, %edx
divl 0x30(%r15)
cmpl %eax, %ebx
je 0x2af09
incl %ebx
movl %ebx, -0x2c(%rbp)
movq -0x48(%rbp), %r13
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x1ec5e
movl %eax, %ebx
movl %eax, -0x50(%rbp)
cmpb $0x0, 0x21(%r15)
je 0x2af87
testl %ebx, %ebx
jne 0x2af87
movl -0x2c(%rbp), %r12d
cmpl $0x1, %r14d
pushq $0x4
popq %rdx
sbbl $0x0, %edx
movq %r15, %rdi
movl %r12d, %esi
movl %r14d, %ecx
leaq -0x50(%rbp), %r8
callq 0x1f801
movl -0x50(%rbp), %ebx
testl %ebx, %ebx
je 0x2af93
jmp 0x2b187
testl %ebx, %ebx
jne 0x2b190
movl -0x2c(%rbp), %r12d
bswapl %r12d
movq -0x90(%rbp), %rax
movl %r12d, (%rax)
movq -0x60(%rbp), %rdi
callq 0x1d90e
movq -0x80(%rbp), %rcx
movq 0x50(%rcx), %rax
movq %rax, -0x90(%rbp)
andl $0x0, (%rax)
movq %rcx, -0x60(%rbp)
movq %r15, %rax
movq 0x50(%rcx), %r15
addq $0x4, %r15
movl 0x34(%rax), %edi
addl $-0x4, %edi
movq -0x70(%rbp), %rdx
movq -0x40(%rbp), %rsi
cmpl %edi, %edx
movl %edi, -0x40(%rbp)
movl %edi, %r12d
cmovll %edx, %r12d
testl %r13d, %r13d
jle 0x2b003
cmpl %r13d, %r12d
cmovgel %r13d, %r12d
movslq %r12d, %r14
movq %r15, %rdi
movq %rsi, %rbx
movq %r14, %rdx
callq 0x82c0
jmp 0x2b016
movslq %r12d, %r14
movq %r15, %rdi
movq %rsi, %rbx
xorl %esi, %esi
movq %r14, %rdx
callq 0x81e0
movq -0x70(%rbp), %rdx
subl %r12d, %edx
addq %r14, %r15
addq %r14, %rbx
movl -0x40(%rbp), %edi
subl %r12d, %edi
subl %r12d, %r13d
cmoveq -0x98(%rbp), %rbx
cmovel -0x68(%rbp), %r13d
movq %rbx, %rsi
jmp 0x2aecd
movq -0x60(%rbp), %rdi
callq 0x1d90e
andl $0x0, -0xc0(%rbp)
movq -0x58(%rbp), %rcx
movswq 0x7a(%rcx), %rax
movzwl 0x7c(%rcx,%rax,2), %r14d
cmpl $0x0, -0x4c(%rbp)
je 0x2b0fc
jns 0x2b084
movq -0x38(%rbp), %rdx
cmpw $0x0, 0x10(%rdx)
je 0x2b084
incl %r14d
movw %r14w, 0x7c(%rcx,%rax,2)
movzwl %r14w, %r14d
movq -0x38(%rbp), %r12
leaq -0xc0(%rbp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movl %r14d, %esi
movq -0x78(%rbp), %rdx
movl -0x84(%rbp), %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2c687
movq -0x58(%rbp), %rdi
andw $0x0, 0x5e(%rdi)
movb $0x0, 0x76(%rdi)
movl -0xc0(%rbp), %ebx
testl %ebx, %ebx
jne 0x2ad36
cmpb $0x0, 0x1(%r12)
movl $0x0, %ebx
je 0x2ad36
movq %rdi, %rbx
callq 0x2c9a5
movswq 0x7a(%rbx), %rcx
movq 0xa8(%rbx,%rcx,8), %rcx
xorl %edx, %edx
movb %dl, 0x1(%rcx)
movb %dl, 0x77(%rbx)
jmp 0x2ad38
movq -0x38(%rbp), %r12
movq 0x68(%r12), %rdi
callq 0xd10b
testl %eax, %eax
jne 0x2ad34
movzwl 0x12(%r12), %eax
movq 0x60(%r12), %rcx
movzbl (%rcx,%r14,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%r14,2), %ebx
orl %edx, %ebx
andl %eax, %ebx
addq 0x50(%r12), %rbx
cmpb $0x0, 0x3(%r12)
jne 0x2b144
movl (%rbx), %eax
movq -0x78(%rbp), %rcx
movl %eax, (%rcx)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2c1c4
movl %eax, %r15d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2c290
leaq -0xc0(%rbp), %rbx
movl %eax, (%rbx)
movzwl %r15w, %edx
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rcx
callq 0x2c3f4
movl (%rbx), %ebx
testl %ebx, %ebx
jne 0x2ad36
jmp 0x2b088
movq -0x80(%rbp), %rdi
callq 0x1d90e
movq -0x60(%rbp), %rdi
callq 0x1d90e
jmp 0x2ad36