// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: balance_quick
static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
  BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
  MemPage *pNew;                       /* Newly allocated page */
  int rc;                              /* Return Code */
  Pgno pgnoNew;                        /* Page number of pNew */

  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
  assert( pPage->nOverflow==1 );

  /* This error condition is now caught prior to reaching this function */
  if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;

  /* Allocate a new page. This page will become the right-sibling of 
  ** pPage. Make the parent page writable, so that the new divider cell
  ** may be inserted. If both these operations are successful, proceed.
  */
  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);

  if( rc==SQLITE_OK ){

    u8 *pOut = &pSpace[4];
    u8 *pCell = pPage->apOvfl[0];
    u16 szCell = cellSizePtr(pPage, pCell);
    u8 *pStop;

    assert( sqlite3PagerIswriteable(pNew->pDbPage) );
    assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
    zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
    assemblePage(pNew, 1, &pCell, &szCell);

    /* If this is an auto-vacuum database, update the pointer map
    ** with entries for the new page, and any pointer from the 
    ** cell on the page to an overflow page. If either of these
    ** operations fails, the return code is set, but the contents
    ** of the parent page are still manipulated by thh code below.
    ** That is Ok, at this point the parent page is guaranteed to
    ** be marked as dirty. Returning an error code will cause a
    ** rollback, undoing any changes made to the parent page.
    */
    if( ISAUTOVACUUM ){
      ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
      if( szCell>pNew->minLocal ){
        ptrmapPutOvflPtr(pNew, pCell, &rc);
      }
    }
  
    /* Create a divider cell to insert into pParent. The divider cell
    ** consists of a 4-byte page number (the page number of pPage) and
    ** a variable length key value (which must be the same value as the
    ** largest key on pPage).
    **
    ** To find the largest key value on pPage, first find the right-most 
    ** cell on pPage. The first two fields of this cell are the 
    ** record-length (a variable length integer at most 32-bits in size)
    ** and the key value (a variable length integer, may have any value).
    ** The first of the while(...) loops below skips over the record-length
    ** field. The second while(...) loop copies the key value from the
    ** cell on pPage into the pSpace buffer.
    */
    pCell = findCell(pPage, pPage->nCell-1);
    pStop = &pCell[9];
    while( (*(pCell++)&0x80) && pCell<pStop );
    pStop = &pCell[9];
    while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );

    /* Insert the new divider cell into pParent. */
    insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
               0, pPage->pgno, &rc);

    /* Set the right-child pointer of pParent to point to the new page. */
    put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
  
    /* Release the reference to the new page. */
    releasePage(pNew);
  }

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0x435ff
movl $0xd8af, %edi           # imm = 0xD8AF
callq 0x24410
movl %eax, -0x4(%rbp)
jmp 0x4381a
movq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x38(%rbp), %rdx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x2cef0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x43814
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x41c00
movw %ax, -0x4a(%rbp)
movq -0x30(%rbp), %rdi
movl $0xd, %esi
callq 0x2a920
movq -0x30(%rbp), %rdi
movl $0x1, %esi
leaq -0x48(%rbp), %rdx
leaq -0x4a(%rbp), %rcx
callq 0x44d70
movq -0x28(%rbp), %rax
cmpb $0x0, 0x21(%rax)
je 0x436b9
movq -0x28(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x10(%rbp), %rax
movl 0x70(%rax), %ecx
movl $0x5, %edx
leaq -0x34(%rbp), %r8
callq 0x2dd40
movzwl -0x4a(%rbp), %eax
movq -0x30(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
cmpl %ecx, %eax
jle 0x436b7
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x2e2c0
jmp 0x436b9
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x18(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movq -0x18(%rbp), %rdx
movq 0x60(%rdx), %rdx
movq -0x18(%rbp), %rsi
movzwl 0x10(%rsi), %esi
subl $0x1, %esi
shll %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
shll $0x8, %edx
movq -0x18(%rbp), %rsi
movq 0x60(%rsi), %rsi
movq -0x18(%rbp), %rdi
movzwl 0x10(%rdi), %edi
subl $0x1, %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, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x48(%rbp)
movzbl (%rax), %ecx
andl $0x80, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x59(%rbp)
je 0x4374f
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
setb %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x43758
jmp 0x4375a
jmp 0x4371f
movq -0x48(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x48(%rbp)
movb (%rax), %al
movq -0x40(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
movb %al, (%rcx)
movzbl %al, %ecx
andl $0x80, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x5a(%rbp)
je 0x437a9
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
setb %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %al
testb $0x1, %al
jne 0x437b2
jmp 0x437b4
jmp 0x43766
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movzwl 0x10(%rax), %esi
movq -0x20(%rbp), %rdx
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl 0x70(%rax), %r9d
xorl %eax, %eax
movl %eax, %r8d
leaq -0x34(%rbp), %rax
movq %rax, (%rsp)
callq 0x42110
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
addl $0x8, %eax
cltq
addq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x22060
movq -0x30(%rbp), %rdi
callq 0x29fd0
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)