// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: balance
static int balance(BtCursor *pCur){
  int rc = SQLITE_OK;
  const int nMin = pCur->pBt->usableSize * 2 / 3;
  u8 aBalanceQuickSpace[13];
  u8 *pFree = 0;

  TESTONLY( int balance_quick_called = 0 );
  TESTONLY( int balance_deeper_called = 0 );

  do {
    int iPage = pCur->iPage;
    MemPage *pPage = pCur->apPage[iPage];

    if( iPage==0 ){
      if( pPage->nOverflow ){
        /* The root page of the b-tree is overfull. In this case call the
        ** balance_deeper() function to create a new child for the root-page
        ** and copy the current contents of the root-page to it. The
        ** next iteration of the do-loop will balance the child page.
        */ 
        assert( (balance_deeper_called++)==0 );
        rc = balance_deeper(pPage, &pCur->apPage[1]);
        if( rc==SQLITE_OK ){
          pCur->iPage = 1;
          pCur->aiIdx[0] = 0;
          pCur->aiIdx[1] = 0;
          assert( pCur->apPage[1]->nOverflow );
        }
      }else{
        break;
      }
    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
      break;
    }else{
      MemPage * const pParent = pCur->apPage[iPage-1];
      int const iIdx = pCur->aiIdx[iPage-1];

      rc = sqlite3PagerWrite(pParent->pDbPage);
      if( rc==SQLITE_OK ){
#ifndef SQLITE_OMIT_QUICKBALANCE
        if( pPage->hasData
         && pPage->nOverflow==1
         && pPage->aiOvfl[0]==pPage->nCell
         && pParent->pgno!=1
         && pParent->nCell==iIdx
        ){
          /* Call balance_quick() to create a new sibling of pPage on which
          ** to store the overflow cell. balance_quick() inserts a new cell
          ** into pParent, which may cause pParent overflow. If this
          ** happens, the next interation of the do-loop will balance pParent 
          ** use either balance_nonroot() or balance_deeper(). Until this
          ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
          ** buffer. 
          **
          ** The purpose of the following assert() is to check that only a
          ** single call to balance_quick() is made for each call to this
          ** function. If this were not verified, a subtle bug involving reuse
          ** of the aBalanceQuickSpace[] might sneak in.
          */
          assert( (balance_quick_called++)==0 );
          rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
        }else
#endif
        {
          /* In this case, call balance_nonroot() to redistribute cells
          ** between pPage and up to 2 of its sibling pages. This involves
          ** modifying the contents of pParent, which may cause pParent to
          ** become overfull or underfull. The next iteration of the do-loop
          ** will balance the parent page to correct this.
          ** 
          ** If the parent page becomes overfull, the overflow cell or cells
          ** are stored in the pSpace buffer allocated immediately below. 
          ** A subsequent iteration of the do-loop will deal with this by
          ** calling balance_nonroot() (balance_deeper() may be called first,
          ** but it doesn't deal with overflow cells - just moves them to a
          ** different page). Once this subsequent call to balance_nonroot() 
          ** has completed, it is safe to release the pSpace buffer used by
          ** the previous call, as the overflow cell data will have been 
          ** copied either into the body of a database page or into the new
          ** pSpace buffer passed to the latter call to balance_nonroot().
          */
          u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
          if( pFree ){
            /* If pFree is not NULL, it points to the pSpace buffer used 
            ** by a previous call to balance_nonroot(). Its contents are
            ** now stored either on real database pages or within the 
            ** new pSpace buffer, so it may be safely freed here. */
            sqlite3PageFree(pFree);
          }

          /* The pSpace buffer will be freed after the next call to
          ** balance_nonroot(), or just before this function returns, whichever
          ** comes first. */
          pFree = pSpace;
        }
      }

      pPage->nOverflow = 0;

      /* The next iteration of the do-loop balances the parent page. */
      releasePage(pPage);
      pCur->iPage--;
    }
  }while( rc==SQLITE_OK );

  if( pFree ){
    sqlite3PageFree(pFree);
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x34(%rax), %eax
shll %eax
movl $0x3, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x10(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movswl 0x7a(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x4246e
movq -0x38(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x42464
movq -0x38(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xa8, %rsi
addq $0x8, %rsi
callq 0x43450
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x42462
movq -0x8(%rbp), %rax
movw $0x1, 0x7a(%rax)
movq -0x8(%rbp), %rax
movw $0x0, 0x7c(%rax)
movq -0x8(%rbp), %rax
movw $0x0, 0x7e(%rax)
jmp 0x42469
jmp 0x425b5
jmp 0x425a9
movq -0x38(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x4248d
movq -0x38(%rbp), %rax
movzwl 0xe(%rax), %eax
cmpl -0x10(%rbp), %eax
jg 0x4248d
jmp 0x425b5
movq -0x8(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x7c(%rax,%rcx,2), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0xef80
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x42586
movq -0x38(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
je 0x42530
movq -0x38(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x1, %eax
jne 0x42530
movq -0x38(%rbp), %rax
movzwl 0x14(%rax), %eax
movq -0x38(%rbp), %rcx
movzwl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jne 0x42530
movq -0x40(%rbp), %rax
cmpl $0x1, 0x70(%rax)
je 0x42530
movq -0x40(%rbp), %rax
movzwl 0x10(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x42530
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0x1d(%rbp), %rdx
callq 0x435c0
movl %eax, -0xc(%rbp)
jmp 0x42584
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x30(%rax), %edi
callq 0x24fa0
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
movq -0x50(%rbp), %rdx
cmpl $0x1, -0x2c(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x8(%rbp), %rax
movzbl 0x79(%rax), %r8d
callq 0x43830
movl %eax, -0xc(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x4257c
movq -0x28(%rbp), %rdi
callq 0x24ff0
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x42586
movq -0x38(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x38(%rbp), %rdi
callq 0x29fd0
movq -0x8(%rbp), %rax
movw 0x7a(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x7a(%rax)
jmp 0x425a9
jmp 0x425ab
cmpl $0x0, -0xc(%rbp)
je 0x423f4
cmpq $0x0, -0x28(%rbp)
je 0x425c5
movq -0x28(%rbp), %rdi
callq 0x24ff0
movl -0xc(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop