// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: insertCell
static void insertCell(
  MemPage *pPage,   /* Page into which we are copying */
  int i,            /* New cell becomes the i-th cell of the page */
  u8 *pCell,        /* Content of the new cell */
  int sz,           /* Bytes of content in pCell */
  u8 *pTemp,        /* Temp storage space for pCell, if needed */
  Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
  int *pRC          /* Read and write return code from here */
){
  int idx = 0;      /* Where to write new cell content in data[] */
  int j;            /* Loop counter */
  int end;          /* First byte past the last cell pointer in data[] */
  int ins;          /* Index in data[] where new cell pointer is inserted */
  int cellOffset;   /* Address of first cell pointer in data[] */
  u8 *data;         /* The content of the whole page */
  u8 *ptr;          /* Used for moving information around in data[] */
  u8 *endPtr;       /* End of the loop */

  int nSkip = (iChild ? 4 : 0);

  if( *pRC ) return;

  assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
  assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
  assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  /* The cell should normally be sized correctly.  However, when moving a
  ** malformed cell from a leaf page to an interior page, if the cell size
  ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
  ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
  ** the term after the || in the following assert(). */
  assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
  if( pPage->nOverflow || sz+2>pPage->nFree ){
    if( pTemp ){
      memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
      pCell = pTemp;
    }
    if( iChild ){
      put4byte(pCell, iChild);
    }
    j = pPage->nOverflow++;
    assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
    pPage->apOvfl[j] = pCell;
    pPage->aiOvfl[j] = (u16)i;
  }else{
    int rc = sqlite3PagerWrite(pPage->pDbPage);
    if( rc!=SQLITE_OK ){
      *pRC = rc;
      return;
    }
    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
    data = pPage->aData;
    cellOffset = pPage->cellOffset;
    end = cellOffset + 2*pPage->nCell;
    ins = cellOffset + 2*i;
    rc = allocateSpace(pPage, sz, &idx);
    if( rc ){ *pRC = rc; return; }
    /* The allocateSpace() routine guarantees the following two properties
    ** if it returns success */
    assert( idx >= end+2 );
    assert( idx+sz <= (int)pPage->pBt->usableSize );
    pPage->nCell++;
    pPage->nFree -= (u16)(2 + sz);
    memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
    if( iChild ){
      put4byte(&data[idx], iChild);
    }
    ptr = &data[end];
    endPtr = &data[ins];
    assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
    while( ptr>endPtr ){
      *(u16*)ptr = *(u16*)&ptr[-2];
      ptr -= 2;
    }
    put2byte(&data[ins], idx);
    put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
#ifndef SQLITE_OMIT_AUTOVACUUM
    if( pPage->pBt->autoVacuum ){
      /* The cell may contain a pointer to an overflow page. If so, write
      ** the entry for the overflow page into the pointer map.
      */
      ptrmapPutOvflPtr(pPage, pCell, pRC);
    }
#endif
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %r13d
xorl %r14d, %r14d
testl %r9d, %r9d
setne %al
movq 0x10(%rbp), %rcx
cmpl $0x0, (%rcx)
jne 0x2c719
movl %r9d, %r12d
movq %r8, %r15
movq %rdx, %r8
movq %rdi, %rbx
movb %al, %r14b
shll $0x2, %r14d
cmpb $0x0, 0x1(%rdi)
jne 0x2c6d2
leal 0x2(%r13), %edx
movzwl 0xe(%rbx), %eax
cmpl %eax, %edx
jle 0x2c728
testq %r15, %r15
je 0x2c6f7
movl %r14d, %eax
leaq (%r15,%rax), %rdi
addq %rax, %r8
subl %r14d, %r13d
movl %esi, %r14d
movslq %r13d, %rdx
movq %r8, %rsi
callq 0x82c0
movl %r14d, %esi
jmp 0x2c6fa
movq %r8, %r15
testl %r12d, %r12d
je 0x2c705
bswapl %r12d
movl %r12d, (%r15)
movzbl 0x1(%rbx), %eax
leal 0x1(%rax), %ecx
movb %cl, 0x1(%rbx)
movq %r15, 0x20(%rbx,%rax,8)
movw %si, 0x14(%rbx,%rax,2)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, -0x3c(%rbp)
movl %esi, -0x48(%rbp)
movq %r8, -0x60(%rbp)
movq %rcx, -0x38(%rbp)
movq 0x68(%rbx), %rdi
callq 0xd10b
testl %eax, %eax
jne 0x2c889
movq 0x50(%rbx), %r11
movzwl 0xc(%rbx), %ecx
movzwl 0x10(%rbx), %eax
movq %rcx, -0x70(%rbp)
leal (%rcx,%rax,2), %edx
movzbl 0x5(%rbx), %ecx
movzbl 0x5(%r11,%rcx), %eax
shll $0x8, %eax
movq %rcx, -0x58(%rbp)
movzbl 0x6(%r11,%rcx), %ecx
addl %ecx, %eax
decl %eax
movzwl %ax, %eax
incl %eax
movl $0xc665, %edi           # imm = 0xC665
movq %rdx, -0x50(%rbp)
cmpl %eax, %edx
jbe 0x2c794
callq 0x1a1fc
pushq $0xb
popq %rax
jmp 0x2c889
movq -0x58(%rbp), %r15
movb 0x7(%r11,%r15), %dl
cmpb $0x3c, %dl
movq %r11, -0x30(%rbp)
jb 0x2c7de
movq %rbx, %rdi
callq 0x2df83
testl %eax, %eax
jne 0x2c889
movq -0x30(%rbp), %r11
movzbl 0x5(%r11,%r15), %eax
shll $0x8, %eax
movzbl 0x6(%r11,%r15), %ecx
addl %ecx, %eax
decl %eax
movzwl %ax, %eax
incl %eax
movq -0x50(%rbp), %rcx
addl $0x2, %ecx
jmp 0x2c876
movq -0x50(%rbp), %rcx
addl $0x2, %ecx
cmpl %eax, %ecx
ja 0x2c876
movq 0x48(%rbx), %rsi
movl 0x34(%rsi), %edi
leal 0x1(%r15), %esi
movq %rdi, -0x68(%rbp)
leal -0x4(%rdi), %r10d
movl %esi, %r9d
movzwl (%r11,%r9), %esi
rolw $0x8, %si
testw %si, %si
je 0x2c872
movzwl %si, %esi
movl $0xc678, %edi           # imm = 0xC678
cmpl %r10d, %esi
jg 0x2c787
leal 0x4(%r9), %r11d
cmpl %r11d, %esi
movq -0x30(%rbp), %r11
jl 0x2c787
movzwl 0x2(%r11,%rsi), %edi
rolw $0x8, %di
movzwl %di, %r8d
movl %r8d, %r15d
subl %r13d, %r15d
jl 0x2c800
cmpl $0x3, %r15d
jg 0x2c97a
movq -0x30(%rbp), %r11
movzwl (%r11,%rsi), %eax
movw %ax, (%r11,%r9)
addb %r15b, %dl
movq -0x58(%rbp), %rax
movb %dl, 0x7(%r11,%rax)
jmp 0x2c99d
movq -0x58(%rbp), %r15
addl %r13d, %ecx
cmpl %eax, %ecx
jle 0x2c8b0
movq %rbx, %rdi
callq 0x2df83
testl %eax, %eax
je 0x2c894
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x2c719
movq -0x30(%rbp), %r11
movzbl 0x5(%r11,%r15), %eax
shll $0x8, %eax
movzbl 0x6(%r11,%r15), %ecx
addl %ecx, %eax
decl %eax
movzwl %ax, %eax
incl %eax
subl %r13d, %eax
movl %eax, %ecx
rolw $0x8, %cx
movw %cx, 0x5(%r11,%r15)
movl %eax, %r15d
movq -0x60(%rbp), %rcx
movl -0x48(%rbp), %eax
movl -0x3c(%rbp), %edx
incw 0x10(%rbx)
cltq
subw %dx, 0xe(%rbx)
movq -0x70(%rbp), %rdx
leaq (%rdx,%rax,2), %rax
movq %rax, -0x48(%rbp)
leal (%r15,%r14), %eax
movslq %eax, %rdi
addq %r11, %rdi
movl %r14d, %esi
addq %rcx, %rsi
subl %r14d, %r13d
movslq %r13d, %rdx
callq 0x82c0
movq -0x30(%rbp), %rdi
testl %r12d, %r12d
je 0x2c910
movslq %r15d, %rax
bswapl %r12d
movl %r12d, (%rdi,%rax)
movl -0x50(%rbp), %eax
addq %rdi, %rax
movq -0x48(%rbp), %r8
addq %rdi, %r8
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rsi
cmpq %r8, %rax
jbe 0x2c937
movzwl -0x2(%rax), %ecx
movw %cx, (%rax)
addq $-0x2, %rax
jmp 0x2c925
rolw $0x8, %r15w
movw %r15w, (%r8)
movb 0x11(%rbx), %al
movzbl 0x5(%rbx), %ecx
movb %al, 0x3(%rdi,%rcx)
movb 0x10(%rbx), %al
movzbl 0x5(%rbx), %ecx
movb %al, 0x4(%rdi,%rcx)
movq 0x48(%rbx), %rax
cmpb $0x0, 0x21(%rax)
je 0x2c719
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1fa21
addl %esi, %r8d
movl $0xc685, %edi           # imm = 0xC685
cmpl -0x68(%rbp), %r8d
movq -0x30(%rbp), %r11
jg 0x2c787
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r11,%rsi)
addl %esi, %r15d
jmp 0x2c8c2