// File: /workspace/github_c_makefile_star2_O1/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)
movl 0x18(%rbp), %r14d
movl %r14d, -0x64(%rbp)
cmpb $0x4, 0x77(%rdi)
jne 0x2d682
movl 0x70(%rdi), %eax
jmp 0x2dc00
movl %r8d, %r13d
movq %rdx, -0x38(%rbp)
movq %rsi, -0x78(%rbp)
movq %r9, -0x80(%rbp)
movq (%rdi), %r12
movq %rdi, %rbx
movq 0x8(%r12), %r15
movl 0x30(%rdi), %esi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x21124
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x2dc00
movq %rbx, -0x50(%rbp)
cmpq $0x0, 0x20(%rbx)
movq -0x38(%rbp), %rdx
je 0x2d773
testl %r14d, %r14d
je 0x2d797
movq -0x50(%rbp), %rcx
movswq 0x7a(%rcx), %rax
movq 0xa8(%rcx,%rax,8), %rbx
cmpq $0x0, 0x80(%r15)
jne 0x2d700
movl 0x30(%r15), %edi
callq 0x1ce52
movq -0x38(%rbp), %rdx
movq %rax, 0x80(%r15)
movq 0x80(%r15), %rax
testq %rax, %rax
je 0x2d7bf
movq %rax, -0x58(%rbp)
movq $0x0, -0x88(%rbp)
movq 0x48(%rbx), %rax
movq %rax, -0xa0(%rbp)
movl $0x0, -0x30(%rbp)
xorl %r12d, %r12d
cmpb $0x0, 0x3(%rbx)
sete %r12b
shll $0x2, %r12d
cmpb $0x0, 0x4(%rbx)
movq %rbx, -0x48(%rbp)
je 0x2d7c9
movl %r12d, %edi
movq -0x58(%rbp), %rbx
addq %rbx, %rdi
movq %r13, %rax
movq -0x80(%rbp), %r13
movq %rax, -0x70(%rbp)
addl %r13d, %eax
movslq %eax, %rsi
callq 0x2fffb
movq -0x38(%rbp), %rdx
addl %eax, %r12d
jmp 0x2d7d8
movq 0x8(%r12), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x2d6c8
cmpb $0x0, 0x78(%rax)
je 0x2d778
cmpq %rdx, 0x40(%rax)
jne 0x2d778
movb $0x0, 0x77(%rax)
jmp 0x2d778
movl 0x10(%rbp), %ecx
leaq -0x64(%rbp), %r8
movq -0x50(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x2feac
movq -0x38(%rbp), %rdx
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x2dc00
jmp 0x2d6d1
movl $0x7, %eax
jmp 0x2dc00
xorl %r13d, %r13d
movq $0x0, -0x70(%rbp)
movq -0x58(%rbp), %rbx
movslq %r12d, %rdi
addq %rbx, %rdi
movq %rdx, %rsi
movq %rdx, %r14
callq 0x2fffb
movl %eax, %r15d
leaq -0xc0(%rbp), %rdx
movq %rbx, %rsi
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x21585
movq -0x70(%rbp), %r10
addl %r10d, %r13d
cmpb $0x0, 0x2(%rbx)
je 0x2d820
movq -0x98(%rbp), %r8
movq $0x0, -0x70(%rbp)
jmp 0x2d86f
movl $0x80000000, %eax       # imm = 0x80000000
cmpq %rax, %r14
setl %al
movq -0x78(%rbp), %r8
testq %r8, %r8
setne %cl
testb %al, %cl
jne 0x2d869
leaq 0x4bfd4(%rip), %rsi     # 0x79814
leaq 0x4bcf2(%rip), %rcx     # 0x79539
movl $0x0, -0x5c(%rbp)
movl $0xb, %ebx
movl $0xb, %edi
movl $0xd757, %edx           # imm = 0xD757
xorl %eax, %eax
callq 0xb8cc
jmp 0x2dacc
movq %r14, %r10
addl %r10d, %r13d
movzwl -0xa2(%rbp), %eax
movl %eax, -0x5c(%rbp)
testl %r13d, %r13d
jle 0x2dab0
addl %r12d, %r15d
movzwl -0xa4(%rbp), %ecx
movq -0x58(%rbp), %rax
addq %rax, %rcx
movq %rcx, -0x90(%rbp)
movslq %r15d, %r14
addq %rax, %r14
movq %r13, %rax
movzwl -0xa6(%rbp), %edx
movq $0x0, -0x38(%rbp)
movl $0xcccccccd, %r11d      # imm = 0xCCCCCCCD
testl %edx, %edx
jne 0x2da26
movq %r8, -0x78(%rbp)
movq %rax, %r12
movl -0x30(%rbp), %r15d
movq -0xa0(%rbp), %rbx
cmpb $0x0, 0x21(%rbx)
je 0x2d93a
movl 0x66f62(%rip), %ecx     # 0x94840
movl %r15d, %r8d
movl %r8d, %edi
leal 0x1(%rdi), %r8d
xorl %eax, %eax
cmpl $0x2, %r8d
jb 0x2d925
movl 0x34(%rbx), %r9d
imulq %r11, %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 0x2d8e1
movl %ecx, %eax
xorl %edx, %edx
divl 0x30(%rbx)
cmpl %eax, %edi
je 0x2d8e1
incl %edi
movl %edi, -0x30(%rbp)
movq %r10, %r13
movl -0x30(%rbp), %ecx
movq %rbx, %rdi
leaq -0x88(%rbp), %rsi
leaq -0x30(%rbp), %rdx
xorl %r8d, %r8d
callq 0x21e3f
movl %eax, -0x3c(%rbp)
cmpb $0x0, 0x21(%rbx)
setne %cl
testl %eax, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x2d9a4
movl -0x30(%rbp), %esi
cmpl $0x1, %r15d
movl $0x4, %edx
sbbl $0x0, %edx
movq %rbx, %rdi
movl %r15d, %ecx
leaq -0x3c(%rbp), %r8
callq 0x22b09
cmpl $0x0, -0x3c(%rbp)
je 0x2d9a4
movq -0x88(%rbp), %rax
testq %rax, %rax
je 0x2d9a4
movq 0x68(%rax), %rdi
callq 0xdcd4
movl -0x3c(%rbp), %r15d
testl %r15d, %r15d
je 0x2d9c9
xorl %edx, %edx
movq -0x38(%rbp), %rax
testq %rax, %rax
je 0x2d9c3
movq 0x68(%rax), %rdi
callq 0xdcd4
xorl %edx, %edx
movl %r15d, -0x60(%rbp)
jmp 0x2da13
movl -0x30(%rbp), %eax
bswapl %eax
movq -0x90(%rbp), %rcx
movl %eax, (%rcx)
movq -0x38(%rbp), %rax
testq %rax, %rax
je 0x2d9e9
movq 0x68(%rax), %rdi
callq 0xdcd4
movq -0x88(%rbp), %rcx
movq 0x50(%rcx), %rax
movq %rax, -0x90(%rbp)
movl $0x0, (%rax)
movq %rcx, -0x38(%rbp)
movq 0x50(%rcx), %r14
addq $0x4, %r14
movl 0x34(%rbx), %edx
addl $-0x4, %edx
testl %r15d, %r15d
movq %r12, %rax
movq -0x78(%rbp), %r8
movq %r13, %r10
jne 0x2dc12
cmpl %edx, %eax
movl %edx, %r13d
movl %edx, %r12d
movq %rax, -0x80(%rbp)
cmovll %eax, %r12d
testl %r10d, %r10d
jle 0x2da5b
cmpl %r10d, %r12d
cmovgel %r10d, %r12d
movslq %r12d, %rdx
movq %r14, %rbx
movq %r14, %rdi
movq %r8, %rsi
movq %r8, %r15
movq %r10, %r14
callq 0x82f0
jmp 0x2da71
movslq %r12d, %rdx
movq %r14, %rbx
movq %r14, %rdi
xorl %esi, %esi
movq %r8, %r15
movq %r10, %r14
callq 0x8200
movslq %r12d, %rax
addq %rax, %rbx
addq %rax, %r15
movl %r13d, %edx
subl %eax, %edx
subl %eax, %r14d
cmoveq -0x98(%rbp), %r15
cmovel -0x70(%rbp), %r14d
movq -0x80(%rbp), %r13
subl %eax, %r13d
movq %r13, %rax
movq %r15, %r8
movq %r14, %r10
movl $0xcccccccd, %r11d      # imm = 0xCCCCCCCD
movq %rbx, %r14
jg 0x2d8b8
jmp 0x2dab8
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
testq %rax, %rax
je 0x2daca
movq 0x68(%rax), %rdi
callq 0xdcd4
xorl %ebx, %ebx
movl %ebx, -0x2c(%rbp)
testl %ebx, %ebx
jne 0x2dbfd
movq -0x50(%rbp), %rcx
movswq 0x7a(%rcx), %rax
movzwl 0x7c(%rcx,%rax,2), %ebx
cmpl $0x0, -0x64(%rbp)
je 0x2db13
jns 0x2db9a
movq -0x48(%rbp), %rcx
cmpw $0x0, 0x10(%rcx)
je 0x2db9a
incl %ebx
movq -0x50(%rbp), %rcx
movw %bx, 0x7c(%rcx,%rax,2)
movzwl %bx, %ebx
jmp 0x2db9a
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0xdebf
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x2dbfd
movq -0x48(%rbp), %r12
movzwl 0x12(%r12), %eax
movq 0x60(%r12), %rcx
movzbl (%rcx,%rbx,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%rbx,2), %r14d
orl %edx, %r14d
andl %eax, %r14d
addq 0x50(%r12), %r14
cmpb $0x0, 0x3(%r12)
jne 0x2db63
movl (%r14), %eax
movq -0x58(%rbp), %rcx
movl %eax, (%rcx)
movq %r12, %rdi
movq %r14, %rsi
callq 0x3072b
movl %eax, %r15d
movq %r12, %rdi
movq %r14, %rsi
callq 0x30802
leaq -0x2c(%rbp), %r14
movl %eax, (%r14)
movzwl %r15w, %edx
movq %r12, %rdi
movl %ebx, %esi
movq %r14, %rcx
callq 0x309c5
cmpl $0x0, (%r14)
jne 0x2dbfd
movzwl -0x5c(%rbp), %ecx
leaq -0x2c(%rbp), %r14
movq %r14, (%rsp)
movq -0x48(%rbp), %rdi
movl %ebx, %esi
movq -0x58(%rbp), %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x30cdf
movq -0x50(%rbp), %rax
movw $0x0, 0x5e(%rax)
movb $0x0, 0x76(%rax)
cmpl $0x0, (%r14)
jne 0x2dbfd
movq -0x48(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x2dbfd
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x3110c
movl %eax, -0x2c(%rbp)
movswq 0x7a(%rbx), %rax
movq 0xa8(%rbx,%rax,8), %rax
xorl %ecx, %ecx
movb %cl, 0x1(%rax)
movb %cl, 0x77(%rbx)
movl -0x2c(%rbp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x60(%rbp), %eax
movl %eax, %ebx
jmp 0x2dacc