// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3BtreeCount
SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
  i64 nEntry = 0;                      /* Value to return in *pnEntry */
  int rc;                              /* Return code */

  if( pCur->pgnoRoot==0 ){
    *pnEntry = 0;
    return SQLITE_OK;
  }
  rc = moveToRoot(pCur);

  /* Unless an error occurs, the following loop runs one iteration for each
  ** page in the B-Tree structure (not including overflow pages). 
  */
  while( rc==SQLITE_OK ){
    int iIdx;                          /* Index of child node in parent */
    MemPage *pPage;                    /* Current page of the b-tree */

    /* If this is a leaf page or the tree is not an int-key tree, then 
    ** this page contains countable entries. Increment the entry counter
    ** accordingly.
    */
    pPage = pCur->apPage[pCur->iPage];
    if( pPage->leaf || !pPage->intKey ){
      nEntry += pPage->nCell;
    }

    /* pPage is a leaf node. This loop navigates the cursor so that it 
    ** points to the first interior cell that it points to the parent of
    ** the next page in the tree that has not yet been visited. The
    ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
    ** of the page, or to the number of cells in the page if the next page
    ** to visit is the right-child of its parent.
    **
    ** If all pages in the tree have been visited, return SQLITE_OK to the
    ** caller.
    */
    if( pPage->leaf ){
      do {
        if( pCur->iPage==0 ){
          /* All pages of the b-tree have been visited. Return successfully. */
          *pnEntry = nEntry;
          return SQLITE_OK;
        }
        moveToParent(pCur);
      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );

      pCur->aiIdx[pCur->iPage]++;
      pPage = pCur->apPage[pCur->iPage];
    }

    /* Descend to the child node of the cell that the cursor currently 
    ** points at. This is the right-child if (iIdx==pPage->nCell).
    */
    iIdx = pCur->aiIdx[pCur->iPage];
    if( iIdx==pPage->nCell ){
      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
    }else{
      rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
    }
  }

  /* An error has occurred. Return an error code. */
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
cmpl $0x0, 0x30(%rdi)
je 0x2c881
movq %rdi, %r14
callq 0x3006b
xorl %r15d, %r15d
testl %eax, %eax
jne 0x2c88a
movswq 0x7a(%r14), %rax
movq 0xa8(%r14,%rax,8), %rax
movb 0x3(%rax), %cl
testb %cl, %cl
je 0x2c7d8
movzwl 0x10(%rax), %edx
addq %rdx, %r15
jmp 0x2c7de
cmpb $0x0, 0x2(%rax)
je 0x2c7cf
testb %cl, %cl
je 0x2c824
cmpw $0x0, 0x7a(%r14)
je 0x2c870
movq %r14, %rdi
callq 0x301df
movswq 0x7a(%r14), %rax
movzwl 0x7c(%r14,%rax,2), %ecx
movq 0xa8(%r14,%rax,8), %rdx
cmpw 0x10(%rdx), %cx
jae 0x2c7e2
incl %ecx
movw %cx, 0x7c(%r14,%rax,2)
movswq 0x7a(%r14), %rax
movq 0xa8(%r14,%rax,8), %rax
movswq 0x7a(%r14), %rcx
movzwl 0x7c(%r14,%rcx,2), %ecx
cmpw 0x10(%rax), %cx
jne 0x2c843
movq 0x50(%rax), %rcx
movzbl 0x5(%rax), %eax
movl 0x8(%rcx,%rax), %esi
jmp 0x2c862
movzwl 0x12(%rax), %edx
movq 0x50(%rax), %rsi
movq 0x60(%rax), %rax
movzbl (%rax,%rcx,2), %edi
shll $0x8, %edi
movzbl 0x1(%rax,%rcx,2), %eax
orl %edi, %eax
andl %edx, %eax
movl (%rsi,%rax), %esi
bswapl %esi
movq %r14, %rdi
callq 0x30218
movb $0x1, %cl
jmp 0x2c877
movq %r15, (%rbx)
xorl %eax, %eax
xorl %ecx, %ecx
testb %cl, %cl
jne 0x2c7b3
jmp 0x2c888
movq $0x0, (%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq