// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: btreeInitPage
static int btreeInitPage(MemPage *pPage){

  assert( pPage->pBt!=0 );
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );

  if( !pPage->isInit ){
    u16 pc;            /* Address of a freeblock within pPage->aData[] */
    u8 hdr;            /* Offset to beginning of page header */
    u8 *data;          /* Equal to pPage->aData */
    BtShared *pBt;        /* The main btree structure */
    int usableSize;    /* Amount of usable space on each page */
    u16 cellOffset;    /* Offset from start of page to first cell pointer */
    int nFree;         /* Number of unused bytes on the page */
    int top;           /* First byte of the cell content area */
    int iCellFirst;    /* First allowable cell or freeblock offset */
    int iCellLast;     /* Last possible cell or freeblock offset */

    pBt = pPage->pBt;

    hdr = pPage->hdrOffset;
    data = pPage->aData;
    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
    assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
    pPage->maskPage = (u16)(pBt->pageSize - 1);
    pPage->nOverflow = 0;
    usableSize = pBt->usableSize;
    pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
    pPage->aDataEnd = &data[usableSize];
    pPage->aCellIdx = &data[cellOffset];
    top = get2byteNotZero(&data[hdr+5]);
    pPage->nCell = get2byte(&data[hdr+3]);
    if( pPage->nCell>MX_CELL(pBt) ){
      /* To many cells for a single page.  The page must be corrupt */
      return SQLITE_CORRUPT_BKPT;
    }
    testcase( pPage->nCell==MX_CELL(pBt) );

    /* A malformed database page might cause us to read past the end
    ** of page when parsing a cell.  
    **
    ** The following block of code checks early to see if a cell extends
    ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
    ** returned if it does.
    */
    iCellFirst = cellOffset + 2*pPage->nCell;
    iCellLast = usableSize - 4;
#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
    {
      int i;            /* Index into the cell pointer array */
      int sz;           /* Size of a cell */

      if( !pPage->leaf ) iCellLast--;
      for(i=0; i<pPage->nCell; i++){
        pc = get2byte(&data[cellOffset+i*2]);
        testcase( pc==iCellFirst );
        testcase( pc==iCellLast );
        if( pc<iCellFirst || pc>iCellLast ){
          return SQLITE_CORRUPT_BKPT;
        }
        sz = cellSizePtr(pPage, &data[pc]);
        testcase( pc+sz==usableSize );
        if( pc+sz>usableSize ){
          return SQLITE_CORRUPT_BKPT;
        }
      }
      if( !pPage->leaf ) iCellLast++;
    }  
#endif

    /* Compute the total free space on the page */
    pc = get2byte(&data[hdr+1]);
    nFree = data[hdr+7] + top;
    while( pc>0 ){
      u16 next, size;
      if( pc<iCellFirst || pc>iCellLast ){
        /* Start of free block is off the page */
        return SQLITE_CORRUPT_BKPT; 
      }
      next = get2byte(&data[pc]);
      size = get2byte(&data[pc+2]);
      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
        /* Free blocks must be in ascending order. And the last byte of
        ** the free-block must lie on the database page.  */
        return SQLITE_CORRUPT_BKPT; 
      }
      nFree = nFree + size;
      pc = next;
    }

    /* At this point, nFree contains the sum of the offset to the start
    ** of the cell-content area plus the number of free bytes within
    ** the cell-content area. If this is greater than the usable-size
    ** of the page, then the page must be corrupted. This check also
    ** serves to verify that the offset to the start of the cell-content
    ** area, according to the page header, lies within the page.
    */
    if( nFree>usableSize ){
      return SQLITE_CORRUPT_BKPT; 
    }
    pPage->nFree = (u16)(nFree - iCellFirst);
    pPage->isInit = 1;
  }
  return SQLITE_OK;
}

// Assembly:
xorl %eax, %eax
cmpb $0x0, (%rdi)
je 0x1a4bd
retq
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq 0x48(%rdi), %r12
movq 0x50(%rdi), %r14
movzbl 0x5(%rdi), %r15d
movzbl (%r14,%r15), %esi
callq 0x1a617
testl %eax, %eax
je 0x1a4f0
movl $0xc74a, %edi           # imm = 0xC74A
jmp 0x1a5f9
movl 0x30(%r12), %eax
leal -0x1(%rax), %ecx
movw %cx, 0x12(%rbx)
movb $0x0, 0x1(%rbx)
movslq 0x34(%r12), %rcx
movzbl 0x3(%rbx), %edx
shll $0x2, %edx
movl %r15d, %esi
subl %edx, %esi
addl $0xc, %esi
movw %si, 0xc(%rbx)
leaq (%r14,%rcx), %rdx
movq %rdx, 0x58(%rbx)
movzwl %si, %esi
leaq (%rsi,%r14), %rdx
movq %rdx, 0x60(%rbx)
movzbl 0x5(%r14,%r15), %edi
movzbl 0x6(%r14,%r15), %r9d
movzwl 0x3(%r14,%r15), %edx
rolw $0x8, %dx
movzwl %dx, %r8d
movw %r8w, 0x10(%rbx)
addl $-0x8, %eax
pushq $0x6
popq %r10
xorl %edx, %edx
divl %r10d
cmpl %r8d, %eax
jae 0x1a565
movl $0xc756, %edi           # imm = 0xC756
jmp 0x1a5f9
shll $0x8, %edi
leal (%rdi,%r9), %eax
decl %eax
movzwl %ax, %edx
leal (%rsi,%r8,2), %eax
leal -0x4(%rcx), %esi
movzwl 0x1(%r14,%r15), %edi
rolw $0x8, %di
movzbl 0x7(%r14,%r15), %r8d
addl %r8d, %edx
incl %edx
testw %di, %di
je 0x1a5e9
movzwl %di, %r8d
cmpl %r8d, %eax
ja 0x1a5f4
cmpl %r8d, %esi
jl 0x1a5f4
movl %r8d, %r9d
movzwl (%r14,%r9), %edi
rolw $0x8, %di
movzwl 0x2(%r14,%r9), %r9d
rolw $0x8, %r9w
testw %di, %di
je 0x1a5d3
movzwl %di, %r10d
movzwl %r9w, %r9d
leal (%r8,%r9), %r11d
addl $0x3, %r11d
cmpl %r10d, %r11d
jb 0x1a5d7
jmp 0x1a5e2
movzwl %r9w, %r9d
addl %r9d, %r8d
addl %r9d, %edx
cmpl %ecx, %r8d
jle 0x1a58d
movl $0xc788, %edi           # imm = 0xC788
jmp 0x1a5f9
cmpl %ecx, %edx
jle 0x1a60a
movl $0xc796, %edi           # imm = 0xC796
jmp 0x1a5f9
movl $0xc781, %edi           # imm = 0xC781
callq 0x1a1fc
pushq $0xb
popq %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
subl %eax, %edx
movw %dx, 0xe(%rbx)
movb $0x1, (%rbx)
xorl %eax, %eax
jmp 0x1a601