// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
xorl %ebx, %ebx
cmpb $0x0, (%rdi)
jne 0x1cb82
movq %rdi, %r14
movq 0x48(%rdi), %r13
movq 0x50(%rdi), %r15
movzbl 0x5(%rdi), %r12d
movzbl (%r15,%r12), %esi
callq 0x1cb93
testl %eax, %eax
je 0x1c9f4
leaq 0x5ce3b(%rip), %rsi     # 0x79814
leaq 0x5cb59(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc74a, %edx           # imm = 0xC74A
jmp 0x1cb7b
movl 0x30(%r13), %edx
leal -0x1(%rdx), %eax
movw %ax, 0x12(%r14)
movb $0x0, 0x1(%r14)
movslq 0x34(%r13), %rax
movzbl 0x3(%r14), %ecx
shll $0x2, %ecx
movl %r12d, %esi
subl %ecx, %esi
addl $0xc, %esi
movw %si, 0xc(%r14)
leaq (%r15,%rax), %rcx
movq %rcx, 0x58(%r14)
movzwl %si, %ecx
leaq (%rcx,%r15), %rsi
movq %rsi, 0x60(%r14)
movzbl 0x5(%r15,%r12), %esi
movzwl 0x3(%r15,%r12), %edi
rolw $0x8, %di
movzbl 0x6(%r15,%r12), %r8d
movzwl %di, %edi
movw %di, 0x10(%r14)
addl $-0x8, %edx
movl $0xaaaaaaab, %r9d       # imm = 0xAAAAAAAB
imulq %rdx, %r9
shrq $0x22, %r9
cmpl %edi, %r9d
jae 0x1ca87
leaq 0x5cda8(%rip), %rsi     # 0x79814
leaq 0x5cac6(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc756, %edx           # imm = 0xC756
jmp 0x1cb7b
shll $0x8, %esi
leal (%rsi,%r8), %edx
decl %edx
movzwl %dx, %edx
movzwl 0x1(%r15,%r12), %esi
rolw $0x8, %si
leal (%rcx,%rdi,2), %ecx
movzbl 0x7(%r15,%r12), %edi
addl %edi, %edx
incl %edx
testw %si, %si
je 0x1cb0f
leal -0x4(%rax), %edi
movzwl %si, %r8d
cmpl %r8d, %ecx
ja 0x1cb5e
cmpl %r8d, %edi
jl 0x1cb5e
movl %r8d, %r9d
movzwl (%r15,%r9), %esi
rolw $0x8, %si
movzwl 0x2(%r15,%r9), %r9d
rolw $0x8, %r9w
testw %si, %si
je 0x1cafb
movzwl %si, %r10d
movzwl %r9w, %r9d
leal (%r8,%r9), %r11d
addl $0x3, %r11d
cmpl %r10d, %r11d
jb 0x1caff
jmp 0x1cb32
movzwl %r9w, %r9d
addl %r9d, %r8d
cmpl %eax, %r8d
jg 0x1cb32
addl %r9d, %edx
testw %si, %si
jne 0x1cab2
cmpl %eax, %edx
jle 0x1cb51
leaq 0x5ccfa(%rip), %rsi     # 0x79814
leaq 0x5ca18(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc796, %edx           # imm = 0xC796
jmp 0x1cb7b
leaq 0x5ccdb(%rip), %rsi     # 0x79814
leaq 0x5c9f9(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc788, %edx           # imm = 0xC788
jmp 0x1cb7b
subl %ecx, %edx
movw %dx, 0xe(%r14)
movb $0x1, (%r14)
jmp 0x1cb82
leaq 0x5ccaf(%rip), %rsi     # 0x79814
leaq 0x5c9cd(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc781, %edx           # imm = 0xC781
xorl %eax, %eax
callq 0xb9c7
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq