// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movl %ecx, %r13d
xorl %r14d, %r14d
testl %r9d, %r9d
setne %al
movq 0x10(%rbp), %rcx
cmpl $0x0, (%rcx)
jne 0x315e4
movl %r9d, %r12d
movq %r8, %r15
movq %rdx, %r8
movq %rdi, %rbx
movb %al, %r14b
shll $0x2, %r14d
cmpb $0x0, 0x1(%rdi)
jne 0x3159d
leal 0x2(%r13), %edx
movzwl 0xe(%rbx), %eax
cmpl %eax, %edx
jle 0x315f3
testq %r15, %r15
je 0x315c2
movl %r14d, %eax
leaq (%r15,%rax), %rdi
addq %rax, %r8
subl %r14d, %r13d
movslq %r13d, %rdx
movl %esi, %r14d
movq %r8, %rsi
callq 0x82f0
movl %r14d, %esi
jmp 0x315c5
movq %r8, %r15
testl %r12d, %r12d
je 0x315d0
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 $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, -0x60(%rbp)
movl %esi, -0x48(%rbp)
movq %r8, -0x68(%rbp)
movq %rcx, -0x40(%rbp)
movq 0x68(%rbx), %rdi
callq 0xdfbf
testl %eax, %eax
je 0x31616
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x315e4
movq 0x50(%rbx), %r11
movzwl 0xc(%rbx), %ecx
movzwl 0x10(%rbx), %eax
movq %rax, -0x58(%rbp)
movq %rcx, -0x78(%rbp)
leal (%rcx,%rax,2), %esi
movzbl 0x5(%rbx), %ecx
movzbl 0x5(%r11,%rcx), %eax
shll $0x8, %eax
movq %rcx, -0x30(%rbp)
movzbl 0x6(%r11,%rcx), %ecx
addl %ecx, %eax
decl %eax
movzwl %ax, %eax
incl %eax
movl $0xc665, %edx           # imm = 0xC665
movq %rsi, -0x50(%rbp)
cmpl %eax, %esi
jbe 0x3167c
leaq 0x481b3(%rip), %rsi     # 0x79814
leaq 0x47ed1(%rip), %rcx     # 0x79539
movl $0xb, %r15d
movl $0xb, %edi
xorl %eax, %eax
callq 0xb9c7
jmp 0x316ed
movq -0x30(%rbp), %rcx
movb 0x7(%r11,%rcx), %sil
cmpb $0x3c, %sil
movq %r11, -0x38(%rbp)
jb 0x316c4
movq %rbx, %rdi
callq 0x33068
testl %eax, %eax
jne 0x316ea
movq -0x38(%rbp), %r11
movq -0x30(%rbp), %rcx
movzbl 0x5(%r11,%rcx), %eax
shll $0x8, %eax
movzbl 0x6(%r11,%rcx), %ecx
addl %ecx, %eax
decl %eax
movzwl %ax, %eax
incl %eax
movq -0x50(%rbp), %rcx
addl $0x2, %ecx
jmp 0x316cf
movq -0x50(%rbp), %rcx
addl $0x2, %ecx
cmpl %eax, %ecx
jbe 0x316f9
addl %r13d, %ecx
cmpl %eax, %ecx
jle 0x317a2
movq %rbx, %rdi
callq 0x33068
testl %eax, %eax
je 0x31782
movl %eax, %r15d
movq -0x40(%rbp), %rax
movl %r15d, (%rax)
jmp 0x315e4
movq 0x48(%rbx), %rdx
movl 0x34(%rdx), %r8d
movq -0x30(%rbp), %rdx
leal 0x1(%rdx), %edi
movq %r8, -0x70(%rbp)
leal -0x4(%r8), %r10d
movl %edi, %r9d
movzwl (%r11,%r9), %edx
rolw $0x8, %dx
testw %dx, %dx
je 0x316cf
movzwl %dx, %edi
movl $0xc678, %edx           # imm = 0xC678
cmpl %r10d, %edi
jg 0x3165a
leal 0x4(%r9), %r11d
cmpl %r11d, %edi
movq -0x38(%rbp), %r11
jl 0x3165a
movzwl 0x2(%r11,%rdi), %edx
rolw $0x8, %dx
movzwl %dx, %r8d
movl %r8d, %r15d
subl %r13d, %r15d
jl 0x31710
cmpl $0x3, %r15d
jg 0x3188c
movq -0x38(%rbp), %r11
movzwl (%r11,%rdi), %eax
movw %ax, (%r11,%r9)
addb %r15b, %sil
movq -0x30(%rbp), %rax
movb %sil, 0x7(%r11,%rax)
jmp 0x318af
movq -0x38(%rbp), %r11
movq -0x30(%rbp), %rcx
movzbl 0x5(%r11,%rcx), %eax
shll $0x8, %eax
movzbl 0x6(%r11,%rcx), %ecx
addl %ecx, %eax
decl %eax
movzwl %ax, %eax
incl %eax
subl %r13d, %eax
movl %eax, %ecx
rolw $0x8, %cx
movq -0x30(%rbp), %rdx
movw %cx, 0x5(%r11,%rdx)
movl %eax, %r15d
movq -0x68(%rbp), %rcx
movl -0x48(%rbp), %eax
movq -0x58(%rbp), %rdx
addl %edx, %edx
movq %rdx, -0x58(%rbp)
movslq %eax, %rdx
incw 0x10(%rbx)
movq -0x78(%rbp), %rax
leaq (%rax,%rdx,2), %rax
movq %rax, -0x48(%rbp)
movl -0x60(%rbp), %eax
subw %ax, 0xe(%rbx)
addq %rdx, %rdx
movq %rdx, -0x60(%rbp)
leal (%r15,%r14), %eax
movslq %eax, %rdi
addq %r11, %rdi
movl %r14d, %esi
addq %rcx, %rsi
subl %r14d, %r13d
movslq %r13d, %rdx
callq 0x82f0
movq -0x38(%rbp), %rsi
testl %r12d, %r12d
je 0x31818
movslq %r15d, %rax
bswapl %r12d
movl %r12d, (%rsi,%rax)
movq -0x48(%rbp), %rdi
addq %rsi, %rdi
movq -0x60(%rbp), %rax
cmpl %eax, -0x58(%rbp)
movq -0x40(%rbp), %rdx
jle 0x31845
movl -0x50(%rbp), %eax
addq %rsi, %rax
movzwl -0x2(%rax), %ecx
movw %cx, (%rax)
leaq -0x2(%rax), %rcx
movq %rcx, %rax
cmpq %rdi, %rcx
ja 0x31832
rolw $0x8, %r15w
movw %r15w, (%rdi)
movb 0x11(%rbx), %al
movzbl 0x5(%rbx), %ecx
movb %al, 0x3(%rsi,%rcx)
movb 0x10(%rbx), %al
movzbl 0x5(%rbx), %ecx
movb %al, 0x4(%rsi,%rcx)
movq 0x48(%rbx), %rax
cmpb $0x0, 0x21(%rax)
movq -0x68(%rbp), %rsi
je 0x315e4
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b30
addl %edi, %r8d
movl $0xc685, %edx           # imm = 0xC685
cmpl -0x70(%rbp), %r8d
movq -0x38(%rbp), %r11
jg 0x3165a
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r11,%rdi)
addl %edi, %r15d
jmp 0x317b8