// File: /workspace/github_c_makefile_star2_O0/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
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 0x77(%rax), %eax
cmpl $0x4, %eax
jne 0x3d441
movq -0x10(%rbp), %rax
movl 0x70(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x3d6ca
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x30(%rax), %esi
movq -0x10(%rbp), %rdx
callq 0x2bad0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3d469
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3d6ca
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x3d483
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x417b0
cmpl $0x0, -0x38(%rbp)
jne 0x3d4b7
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl 0x10(%rbp), %ecx
leaq -0x38(%rbp), %r8
callq 0x40600
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3d4b5
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3d6ca
jmp 0x3d4b7
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rdi
callq 0x41820
movq -0x58(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x3d4fb
movl $0x7, -0x4(%rbp)
jmp 0x3d6ca
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 0x41860
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3d537
jmp 0x3d6c4
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movzwl 0x7c(%rax,%rcx,2), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x3d613
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0xef80
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3d571
jmp 0x3d6c4
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x48(%rbp), %rcx
movzwl 0x12(%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, 0x3(%rax)
jne 0x3d5d1
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x41c00
movw %ax, -0x6a(%rbp)
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x41db0
movl %eax, -0x34(%rbp)
movq -0x48(%rbp), %rdi
movl -0x40(%rbp), %esi
movzwl -0x6a(%rbp), %edx
leaq -0x34(%rbp), %rcx
callq 0x41f80
cmpl $0x0, -0x34(%rbp)
je 0x3d611
jmp 0x3d6c4
jmp 0x3d64d
cmpl $0x0, -0x38(%rbp)
jge 0x3d649
movq -0x48(%rbp), %rax
movzwl 0x10(%rax), %eax
cmpl $0x0, %eax
jle 0x3d649
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movswq 0x7a(%rax), %rdx
movw 0x7c(%rcx,%rdx,2), %ax
addw $0x1, %ax
movw %ax, 0x7c(%rcx,%rdx,2)
movzwl %ax, %eax
movl %eax, -0x40(%rbp)
jmp 0x3d64b
jmp 0x3d64b
jmp 0x3d64d
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 0x42110
movq -0x10(%rbp), %rax
movw $0x0, 0x5e(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x76(%rax)
cmpl $0x0, -0x34(%rbp)
jne 0x3d6c2
movq -0x48(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x3d6c2
movq -0x10(%rbp), %rdi
callq 0x423c0
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movb $0x0, 0x1(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x77(%rax)
jmp 0x3d6c4
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)