// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movl 0x18(%rbp), %r12d
movl %r12d, -0x6c(%rbp)
cmpb $0x4, 0x77(%rdi)
jne 0x2dec8
movl 0x70(%rbx), %ebx
jmp 0x2def2
movl %r9d, %r13d
movq %rdx, -0x38(%rbp)
movq %rsi, -0x58(%rbp)
movq %r8, -0x68(%rbp)
movq (%rbx), %r14
movq 0x8(%r14), %r15
movl 0x30(%rbx), %esi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x20df0
testl %eax, %eax
je 0x2df06
movl %eax, %ebx
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rcx
cmpq $0x0, 0x20(%rbx)
movq -0x38(%rbp), %rdx
je 0x2dfb1
movq %rcx, -0x60(%rbp)
testl %r12d, %r12d
je 0x2dfd4
movq -0x60(%rbp), %rcx
movswq 0x7a(%rcx), %rax
movq 0xa8(%rcx,%rax,8), %r14
movq 0x80(%r15), %rbx
testq %rbx, %rbx
jne 0x2df62
movl 0x30(%r15), %edi
callq 0x1cc98
movq -0x38(%rbp), %rdx
movq %rax, %rbx
movq %rax, 0x80(%r15)
testq %rax, %rax
je 0x2e3f5
movq $0x0, -0x80(%rbp)
movq 0x48(%r14), %rax
movq %rax, -0xa0(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r12d, %r12d
cmpb $0x0, 0x3(%r14)
sete %r12b
shll $0x2, %r12d
cmpb $0x0, 0x4(%r14)
je 0x2dffc
movl %r12d, %edi
addq %rbx, %rdi
movq -0x68(%rbp), %rax
addl %r13d, %eax
movslq %eax, %rsi
callq 0x308ff
movq -0x38(%rbp), %rdx
addl %eax, %r12d
jmp 0x2e007
movq 0x8(%r14), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x2df18
cmpb $0x0, 0x78(%rax)
je 0x2dfb5
cmpq %rdx, 0x40(%rax)
jne 0x2dfb5
movb $0x0, 0x77(%rax)
jmp 0x2dfb5
movl 0x10(%rbp), %ecx
leaq -0x6c(%rbp), %r8
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x30791
movq -0x38(%rbp), %rdx
movl %eax, -0x3c(%rbp)
testl %eax, %eax
jne 0x2def0
jmp 0x2df25
xorl %r13d, %r13d
movq $0x0, -0x68(%rbp)
movslq %r12d, %rdi
addq %rbx, %rdi
movq %rdx, %rsi
movq %rdx, %r15
callq 0x308ff
movq %rbx, %rsi
movl %eax, %ebx
leaq -0xc0(%rbp), %rdx
movq %r14, %rdi
movq %rsi, -0x78(%rbp)
callq 0x2121e
movq -0x68(%rbp), %rdx
addl %edx, %r13d
cmpb $0x0, 0x2(%r14)
je 0x2e050
movq %r14, -0x48(%rbp)
movq -0x98(%rbp), %rsi
movl %edx, %r10d
xorl %edx, %edx
jmp 0x2e09c
movl $0x80000000, %eax       # imm = 0x80000000
cmpq %rax, %r15
setl %al
movq -0x58(%rbp), %rsi
testq %rsi, %rsi
setne %cl
testb %al, %cl
jne 0x2e092
leaq 0x4b7a4(%rip), %rsi     # 0x79814
leaq 0x4b4c2(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xd757, %edx           # imm = 0xD757
xorl %eax, %eax
callq 0xb9c7
jmp 0x2def2
movq %r15, %r10
movq %r14, -0x48(%rbp)
addl %r10d, %r13d
movzwl -0xa2(%rbp), %eax
movl %eax, -0x84(%rbp)
testl %r13d, %r13d
jle 0x2e2bd
movq %rdx, -0x68(%rbp)
addl %r12d, %ebx
movzwl -0xa4(%rbp), %ecx
movq -0x78(%rbp), %rax
addq %rax, %rcx
movq %rcx, -0x90(%rbp)
movslq %ebx, %rcx
addq %rax, %rcx
movzwl -0xa6(%rbp), %r14d
movq $0x0, -0x50(%rbp)
movl $0xcccccccd, %r11d      # imm = 0xCCCCCCCD
testl %r14d, %r14d
jne 0x2e225
movq %rsi, -0x58(%rbp)
movl -0x2c(%rbp), %r15d
movq -0xa0(%rbp), %r14
cmpb $0x0, 0x21(%r14)
movl %r15d, %ecx
je 0x2e170
movl 0x6672e(%rip), %esi     # 0x94840
movl %r15d, %r8d
movl %r8d, %ecx
leal 0x1(%rcx), %r8d
xorl %eax, %eax
cmpl $0x2, %r8d
jb 0x2e15a
movl 0x34(%r14), %r9d
imulq %r11, %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 0x2e115
movl %esi, %eax
xorl %edx, %edx
divl 0x30(%r14)
cmpl %eax, %ecx
je 0x2e115
incl %ecx
movl %ecx, -0x2c(%rbp)
movq %r10, -0x38(%rbp)
movq %r14, %rdi
leaq -0x80(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
xorl %r8d, %r8d
callq 0x21af9
movl %eax, %ebx
movl %eax, -0x70(%rbp)
cmpb $0x0, 0x21(%r14)
je 0x2e1c9
testl %ebx, %ebx
jne 0x2e1c9
movl -0x2c(%rbp), %r12d
cmpl $0x1, %r15d
movl $0x4, %edx
sbbl $0x0, %edx
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %ecx
leaq -0x70(%rbp), %r8
callq 0x22705
movl -0x70(%rbp), %ebx
testl %ebx, %ebx
movq -0x50(%rbp), %rcx
je 0x2e1d9
jmp 0x2e3ff
testl %ebx, %ebx
jne 0x2e411
movl -0x2c(%rbp), %r12d
movq -0x50(%rbp), %rcx
bswapl %r12d
movq -0x90(%rbp), %rax
movl %r12d, (%rax)
testq %rcx, %rcx
je 0x2e1f4
movq 0x68(%rcx), %rdi
callq 0xddd3
movq -0x80(%rbp), %rcx
movq 0x50(%rcx), %rax
movq %rax, -0x90(%rbp)
movl $0x0, (%rax)
movq %rcx, -0x50(%rbp)
movq 0x50(%rcx), %rcx
addq $0x4, %rcx
movl 0x34(%r14), %r14d
addl $-0x4, %r14d
movq -0x58(%rbp), %rsi
movq -0x38(%rbp), %r10
cmpl %r14d, %r13d
movq %r13, %rax
movl %r14d, %r13d
movq %rax, -0x38(%rbp)
cmovll %eax, %r13d
testl %r10d, %r10d
movq %rcx, -0x58(%rbp)
jle 0x2e25c
cmpl %r10d, %r13d
cmovgel %r10d, %r13d
movslq %r13d, %r12
movq %rcx, %rdi
movq %rsi, %rbx
movq %r12, %rdx
movq %r10, %r15
callq 0x82f0
jmp 0x2e272
movslq %r13d, %r12
movq %rcx, %rdi
movq %rsi, %rbx
xorl %esi, %esi
movq %r12, %rdx
movq %r10, %r15
callq 0x8200
movq -0x58(%rbp), %rcx
addq %r12, %rcx
addq %r12, %rbx
subl %r13d, %r14d
subl %r13d, %r15d
cmoveq -0x98(%rbp), %rbx
cmovel -0x68(%rbp), %r15d
movq -0x38(%rbp), %rax
subl %r13d, %eax
movq %rax, %r13
movq %rbx, %rsi
movq %r15, %r10
movl $0xcccccccd, %r11d      # imm = 0xCCCCCCCD
jg 0x2e0ea
movq -0x50(%rbp), %rax
testq %rax, %rax
je 0x2e2bd
movq 0x68(%rax), %rdi
callq 0xddd3
movl $0x0, -0x3c(%rbp)
movq -0x60(%rbp), %rcx
movswq 0x7a(%rcx), %rax
movzwl 0x7c(%rcx,%rax,2), %r14d
cmpl $0x0, -0x6c(%rbp)
je 0x2e36d
jns 0x2e2f7
movq -0x48(%rbp), %rdx
cmpw $0x0, 0x10(%rdx)
je 0x2e2f7
incl %r14d
movw %r14w, 0x7c(%rcx,%rax,2)
movzwl %r14w, %r14d
movq -0x48(%rbp), %r12
leaq -0x3c(%rbp), %rbx
movq %rbx, (%rsp)
movq %r12, %rdi
movl %r14d, %esi
movq -0x78(%rbp), %rdx
movl -0x84(%rbp), %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x31552
movq -0x60(%rbp), %rax
movw $0x0, 0x5e(%rax)
movb $0x0, 0x76(%rax)
movl (%rbx), %ebx
testl %ebx, %ebx
jne 0x2def2
cmpb $0x0, 0x1(%r12)
movl $0x0, %ebx
je 0x2def2
movq -0x60(%rbp), %rbx
movq %rbx, %rdi
callq 0x318b7
movswq 0x7a(%rbx), %rcx
movq 0xa8(%rbx,%rcx,8), %rcx
xorl %edx, %edx
movb %dl, 0x1(%rcx)
movb %dl, 0x77(%rbx)
jmp 0x2def4
movq -0x48(%rbp), %r12
movq 0x68(%r12), %rdi
callq 0xdfbf
testl %eax, %eax
jne 0x2def0
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 0x2e3b5
movl (%rbx), %eax
movq -0x78(%rbp), %rcx
movl %eax, (%rcx)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x30fe4
movl %eax, %r15d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x310b9
leaq -0x3c(%rbp), %rbx
movl %eax, (%rbx)
movzwl %r15w, %edx
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rcx
callq 0x31267
movl (%rbx), %ebx
testl %ebx, %ebx
jne 0x2def2
jmp 0x2e2fb
movl $0x7, %ebx
jmp 0x2def2
movq -0x80(%rbp), %rax
testq %rax, %rax
je 0x2e415
movq 0x68(%rax), %rdi
callq 0xddd3
movq -0x50(%rbp), %rcx
testq %rcx, %rcx
je 0x2def2
movq 0x68(%rcx), %rdi
callq 0xddd3
jmp 0x2def2