// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
xorl %ebx, %ebx
cmpb $0x0, (%rdi)
jne 0x1cd27
movq %rdi, %r14
movq 0x48(%rdi), %r15
movq 0x50(%rdi), %r13
movzbl 0x5(%rdi), %r12d
movzbl (%r13,%r12), %esi
callq 0x1cd4d
testl %eax, %eax
je 0x1cb9e
leaq 0x5cc91(%rip), %rsi     # 0x79814
leaq 0x5c9af(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc74a, %edx           # imm = 0xC74A
jmp 0x1cd20
movl 0x30(%r15), %eax
leal -0x1(%rax), %ecx
movw %cx, 0x12(%r14)
movb $0x0, 0x1(%r14)
movslq 0x34(%r15), %r9
movzbl 0x3(%r14), %ecx
shll $0x2, %ecx
movl %r12d, %edx
subl %ecx, %edx
addl $0xc, %edx
movw %dx, 0xc(%r14)
leaq (%r9,%r13), %rcx
movq %rcx, 0x58(%r14)
movzwl %dx, %ecx
leaq (%rcx,%r13), %rdx
movq %rdx, 0x60(%r14)
movzbl 0x5(%r13,%r12), %edx
movzwl 0x3(%r13,%r12), %esi
rolw $0x8, %si
movzbl 0x6(%r13,%r12), %edi
movzwl %si, %esi
movw %si, 0x10(%r14)
addl $-0x8, %eax
movl $0xaaaaaaab, %r8d       # imm = 0xAAAAAAAB
imulq %rax, %r8
shrq $0x22, %r8
cmpl %esi, %r8d
jae 0x1cc31
leaq 0x5cbfe(%rip), %rsi     # 0x79814
leaq 0x5c91c(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc756, %edx           # imm = 0xC756
jmp 0x1cd20
shll $0x8, %edx
leal (%rdx,%rdi), %eax
decl %eax
movzwl %ax, %eax
leal (%rcx,%rsi,2), %ecx
movl %ecx, -0x30(%rbp)
movq %r9, -0x40(%rbp)
leal -0x4(%r9), %ecx
movl %ecx, -0x34(%rbp)
movzwl 0x1(%r13,%r12), %r15d
rolw $0x8, %r15w
movzbl 0x7(%r13,%r12), %ecx
addl %ecx, %eax
incl %eax
movl %eax, -0x2c(%rbp)
testw %r15w, %r15w
je 0x1ccfb
movzwl %r15w, %eax
cmpl %eax, -0x30(%rbp)
ja 0x1ccc3
cmpl %eax, -0x34(%rbp)
jl 0x1ccc3
movzwl (%r13,%rax), %ecx
movzwl 0x2(%r13,%rax), %edx
rolw $0x8, %cx
rolw $0x8, %dx
movzwl %dx, %edx
testw %cx, %cx
je 0x1cca6
movzwl %cx, %esi
leal (%rax,%rdx), %edi
addl $0x3, %edi
cmpl %esi, %edi
jae 0x1ccad
addl %edx, %eax
cmpl -0x40(%rbp), %eax
jle 0x1ccf0
xorl %r12d, %r12d
movl $0xb, %edi
leaq 0x5cb58(%rip), %rsi     # 0x79814
movl $0xc788, %edx           # imm = 0xC788
jmp 0x1ccd7
xorl %r12d, %r12d
movl $0xb, %edi
leaq 0x5cb42(%rip), %rsi     # 0x79814
movl $0xc781, %edx           # imm = 0xC781
leaq 0x5c85b(%rip), %rcx     # 0x79539
xorl %eax, %eax
callq 0xb8cc
testb %r12b, %r12b
jne 0x1cc65
jmp 0x1cd38
addl %edx, -0x2c(%rbp)
movb $0x1, %r12b
movl %ecx, %r15d
jmp 0x1cce5
movl -0x2c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x1cd3f
leaq 0x5cb0a(%rip), %rsi     # 0x79814
leaq 0x5c828(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xc796, %edx           # imm = 0xC796
xorl %eax, %eax
callq 0xb8cc
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xb, %ebx
jmp 0x1cd27
subl -0x30(%rbp), %eax
movw %ax, 0xe(%r14)
movb $0x1, (%r14)
jmp 0x1cd27