// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: checkTreePage
static int checkTreePage(
  IntegrityCk *pCheck,  /* Context for the sanity check */
  int iPage,            /* Page number of the page to check */
  char *zParentContext, /* Parent context */
  i64 *pnParentMinKey, 
  i64 *pnParentMaxKey
){
  MemPage *pPage;
  int i, rc, depth, d2, pgno, cnt;
  int hdr, cellStart;
  int nCell;
  u8 *data;
  BtShared *pBt;
  int usableSize;
  char zContext[100];
  char *hit = 0;
  i64 nMinKey = 0;
  i64 nMaxKey = 0;

  sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);

  /* Check that the page exists
  */
  pBt = pCheck->pBt;
  usableSize = pBt->usableSize;
  if( iPage==0 ) return 0;
  if( checkRef(pCheck, iPage, zParentContext) ) return 0;
  if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
    checkAppendMsg(pCheck, zContext,
       "unable to get the page. error code=%d", rc);
    return 0;
  }

  /* Clear MemPage.isInit to make sure the corruption detection code in
  ** btreeInitPage() is executed.  */
  pPage->isInit = 0;
  if( (rc = btreeInitPage(pPage))!=0 ){
    assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
    checkAppendMsg(pCheck, zContext, 
                   "btreeInitPage() returns error code %d", rc);
    releasePage(pPage);
    return 0;
  }

  /* Check out all the cells.
  */
  depth = 0;
  for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
    u8 *pCell;
    u32 sz;
    CellInfo info;

    /* Check payload overflow pages
    */
    sqlite3_snprintf(sizeof(zContext), zContext,
             "On tree page %d cell %d: ", iPage, i);
    pCell = findCell(pPage,i);
    btreeParseCellPtr(pPage, pCell, &info);
    sz = info.nData;
    if( !pPage->intKey ) sz += (int)info.nKey;
    /* For intKey pages, check that the keys are in order.
    */
    else if( i==0 ) nMinKey = nMaxKey = info.nKey;
    else{
      if( info.nKey <= nMaxKey ){
        checkAppendMsg(pCheck, zContext, 
            "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
      }
      nMaxKey = info.nKey;
    }
    assert( sz==info.nPayload );
    if( (sz>info.nLocal) 
     && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
    ){
      int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
      Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
#ifndef SQLITE_OMIT_AUTOVACUUM
      if( pBt->autoVacuum ){
        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
      }
#endif
      checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
    }

    /* Check sanity of left child page.
    */
    if( !pPage->leaf ){
      pgno = get4byte(pCell);
#ifndef SQLITE_OMIT_AUTOVACUUM
      if( pBt->autoVacuum ){
        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
      }
#endif
      d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
      if( i>0 && d2!=depth ){
        checkAppendMsg(pCheck, zContext, "Child page depth differs");
      }
      depth = d2;
    }
  }

  if( !pPage->leaf ){
    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
    sqlite3_snprintf(sizeof(zContext), zContext, 
                     "On page %d at right child: ", iPage);
#ifndef SQLITE_OMIT_AUTOVACUUM
    if( pBt->autoVacuum ){
      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
    }
#endif
    checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
  }
 
  /* For intKey leaf pages, check that the min/max keys are in order
  ** with any left/parent/right pages.
  */
  if( pPage->leaf && pPage->intKey ){
    /* if we are a left child page */
    if( pnParentMinKey ){
      /* if we are the left most child page */
      if( !pnParentMaxKey ){
        if( nMaxKey > *pnParentMinKey ){
          checkAppendMsg(pCheck, zContext, 
              "Rowid %lld out of order (max larger than parent min of %lld)",
              nMaxKey, *pnParentMinKey);
        }
      }else{
        if( nMinKey <= *pnParentMinKey ){
          checkAppendMsg(pCheck, zContext, 
              "Rowid %lld out of order (min less than parent min of %lld)",
              nMinKey, *pnParentMinKey);
        }
        if( nMaxKey > *pnParentMaxKey ){
          checkAppendMsg(pCheck, zContext, 
              "Rowid %lld out of order (max larger than parent max of %lld)",
              nMaxKey, *pnParentMaxKey);
        }
        *pnParentMinKey = nMaxKey;
      }
    /* else if we're a right child page */
    } else if( pnParentMaxKey ){
      if( nMinKey <= *pnParentMaxKey ){
        checkAppendMsg(pCheck, zContext, 
            "Rowid %lld out of order (min less than parent max of %lld)",
            nMinKey, *pnParentMaxKey);
      }
    }
  }

  /* Check for complete coverage of the page
  */
  data = pPage->aData;
  hdr = pPage->hdrOffset;
  hit = sqlite3PageMalloc( pBt->pageSize );
  if( hit==0 ){
    pCheck->mallocFailed = 1;
  }else{
    int contentOffset = get2byteNotZero(&data[hdr+5]);
    assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
    memset(hit+contentOffset, 0, usableSize-contentOffset);
    memset(hit, 1, contentOffset);
    nCell = get2byte(&data[hdr+3]);
    cellStart = hdr + 12 - 4*pPage->leaf;
    for(i=0; i<nCell; i++){
      int pc = get2byte(&data[cellStart+i*2]);
      u32 size = 65536;
      int j;
      if( pc<=usableSize-4 ){
        size = cellSizePtr(pPage, &data[pc]);
      }
      if( (int)(pc+size-1)>=usableSize ){
        checkAppendMsg(pCheck, 0, 
            "Corruption detected in cell %d on page %d",i,iPage);
      }else{
        for(j=pc+size-1; j>=pc; j--) hit[j]++;
      }
    }
    i = get2byte(&data[hdr+1]);
    while( i>0 ){
      int size, j;
      assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
      size = get2byte(&data[i+2]);
      assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
      for(j=i+size-1; j>=i; j--) hit[j]++;
      j = get2byte(&data[i]);
      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
      assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
      i = j;
    }
    for(i=cnt=0; i<usableSize; i++){
      if( hit[i]==0 ){
        cnt++;
      }else if( hit[i]>1 ){
        checkAppendMsg(pCheck, 0,
          "Multiple uses for byte %d of page %d", i, iPage);
        break;
      }
    }
    if( cnt!=data[hdr+7] ){
      checkAppendMsg(pCheck, 0, 
          "Fragmentation of %d bytes reported as %d on page %d",
          cnt, data[hdr+7], iPage);
    }
  }
  sqlite3PageFree(hit);
  releasePage(pPage);
  return depth+1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r8, -0x58(%rbp)
movq %rcx, %r13
movq %rdx, %rbx
movl %esi, %r15d
andq $0x0, -0x78(%rbp)
movq %rdi, %r14
andq $0x0, -0x48(%rbp)
leaq 0x3c82b(%rip), %rdx     # 0x6c23c
xorl %r12d, %r12d
pushq $0x64
popq %rdi
leaq -0x120(%rbp), %rsi
movl %r15d, %ecx
xorl %eax, %eax
callq 0xaf26
testl %r15d, %r15d
je 0x30037
movq %r13, -0x80(%rbp)
movq (%r14), %r13
movl 0x34(%r13), %eax
movq %rax, -0x70(%rbp)
movq %r14, %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x3013e
testl %eax, %eax
jne 0x30037
movq -0x70(%rbp), %rbx
movq %r14, -0x30(%rbp)
leaq -0x90(%rbp), %rdx
xorl %r12d, %r12d
movq %r13, %rdi
movl %r15d, %esi
xorl %ecx, %ecx
callq 0x1d84e
testl %eax, %eax
je 0x2fa99
leaq 0x3c7c6(%rip), %rdx     # 0x6c246
leaq -0x120(%rbp), %rsi
movq -0x30(%rbp), %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x3004c
jmp 0x30037
movq %r13, -0x50(%rbp)
movq -0x90(%rbp), %r12
movb $0x0, (%r12)
movq %r12, %rdi
callq 0x1a4b5
testl %eax, %eax
je 0x2fae3
leaq 0x3c7b0(%rip), %rdx     # 0x6c26c
movq %r12, %rbx
xorl %r12d, %r12d
leaq -0x120(%rbp), %rsi
movq -0x30(%rbp), %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x3004c
movq %rbx, %rdi
callq 0x1d90e
jmp 0x30037
leal -0x5(%rbx), %eax
movl %eax, -0x84(%rbp)
leal -0x4(%rbx), %eax
movl %eax, -0x5c(%rbp)
leaq -0x120(%rbp), %r14
xorl %r13d, %r13d
movl $0x0, -0x38(%rbp)
movl %r15d, -0x34(%rbp)
movq %r12, -0x40(%rbp)
movzwl 0x10(%r12), %eax
cmpq %rax, %r13
jae 0x2fce9
movq -0x30(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x2fce9
pushq $0x64
popq %rdi
movq %r14, %rsi
leaq 0x3c75d(%rip), %rdx     # 0x6c292
movl %r15d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xaf26
movzwl 0x12(%r12), %eax
movq 0x60(%r12), %rcx
movzbl (%rcx,%r13,2), %edx
shll $0x8, %edx
movzbl 0x1(%rcx,%r13,2), %r15d
orl %edx, %r15d
andl %eax, %r15d
addq 0x50(%r12), %r15
movq %r12, %rdi
movq %r15, %rsi
leaq -0xb0(%rbp), %rdx
callq 0x1e436
movl -0xa0(%rbp), %ebx
cmpb $0x0, 0x2(%r12)
je 0x2fbb9
movq -0xb0(%rbp), %r12
testq %r13, %r13
je 0x2fbc1
movq -0x48(%rbp), %r8
cmpq %r8, %r12
jg 0x2fbb3
movq -0x30(%rbp), %rdi
movq %r14, %rsi
leaq 0x3c703(%rip), %rdx     # 0x6c2ac
movq %r12, %rcx
xorl %eax, %eax
callq 0x3004c
movq %r12, -0x48(%rbp)
jmp 0x2fbc9
addl -0xb0(%rbp), %ebx
jmp 0x2fbcd
movq %r12, -0x48(%rbp)
movq %r12, -0x78(%rbp)
movq -0x40(%rbp), %r12
movzwl -0x96(%rbp), %eax
cmpl %eax, %ebx
jbe 0x2fc46
movzwl -0x94(%rbp), %edi
addq %r15, %rdi
movq -0x50(%rbp), %rcx
movl 0x34(%rcx), %ecx
addq 0x50(%r12), %rcx
cmpq %rcx, %rdi
ja 0x2fc46
addl -0x84(%rbp), %ebx
subl %eax, %ebx
movl %ebx, %eax
xorl %edx, %edx
divl -0x5c(%rbp)
movl %eax, -0x68(%rbp)
callq 0x180a3
movl %eax, %r12d
movq -0x50(%rbp), %rax
cmpb $0x0, 0x21(%rax)
movq -0x30(%rbp), %rbx
je 0x2fc2f
movq %rbx, %rdi
movl %r12d, %esi
pushq $0x3
popq %rdx
movl -0x34(%rbp), %ecx
movq %r14, %r8
callq 0x2f935
movq %rbx, %rdi
xorl %esi, %esi
movl %r12d, %edx
movl -0x68(%rbp), %ecx
movq %r14, %r8
callq 0x2f738
movq -0x40(%rbp), %r12
cmpb $0x0, 0x3(%r12)
je 0x2fc5a
movl -0x38(%rbp), %ebx
movl -0x34(%rbp), %r15d
jmp 0x2fcde
movq %r15, %rdi
callq 0x180a3
movl %eax, %ebx
movq -0x50(%rbp), %rax
cmpb $0x0, 0x21(%rax)
movl -0x34(%rbp), %r15d
movq -0x30(%rbp), %r12
je 0x2fc89
movq %r12, %rdi
movl %ebx, %esi
pushq $0x5
popq %rdx
movl %r15d, %ecx
movq %r14, %r8
callq 0x2f935
testq %r13, %r13
leaq -0x48(%rbp), %r8
cmoveq %r13, %r8
movq %r12, %rdi
movl %ebx, %esi
movq %r14, %rdx
leaq -0x78(%rbp), %rcx
callq 0x2f9dc
movl %eax, %ebx
testq %r13, %r13
je 0x2fcd3
cmpl -0x38(%rbp), %ebx
movq -0x40(%rbp), %r12
je 0x2fcd7
movq -0x30(%rbp), %rdi
leaq -0x120(%rbp), %r14
movq %r14, %rsi
leaq 0x3c60e(%rip), %rdx     # 0x6c2d8
xorl %eax, %eax
callq 0x3004c
jmp 0x2fcde
movq -0x40(%rbp), %r12
leaq -0x120(%rbp), %r14
incq %r13
movl %ebx, -0x38(%rbp)
jmp 0x2fb0b
cmpb $0x0, 0x3(%r12)
movq -0x50(%rbp), %r14
jne 0x2fd80
movq 0x50(%r12), %rax
movzbl 0x5(%r12), %ecx
leaq (%rax,%rcx), %rdi
addq $0x8, %rdi
callq 0x180a3
movl %eax, %ebx
leaq 0x3c5d7(%rip), %rdx     # 0x6c2f1
pushq $0x64
popq %rdi
leaq -0x120(%rbp), %rsi
movl %r15d, %ecx
xorl %eax, %eax
callq 0xaf26
cmpb $0x0, 0x21(%r14)
movq -0x30(%rbp), %r13
je 0x2fd50
pushq $0x5
popq %rdx
leaq -0x120(%rbp), %r8
movq %r13, %rdi
movl %ebx, %esi
movl %r15d, %ecx
callq 0x2f935
xorl %eax, %eax
cmpw $0x0, 0x10(%r12)
leaq -0x48(%rbp), %r8
cmoveq %rax, %r8
leaq -0x120(%rbp), %rdx
movq %r13, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x2f9dc
cmpb $0x0, 0x3(%r12)
je 0x2fe33
cmpb $0x0, 0x2(%r12)
je 0x2fe33
movq -0x80(%rbp), %rbx
testq %rbx, %rbx
je 0x2fdf1
movq (%rbx), %r8
movq -0x58(%rbp), %r15
testq %r15, %r15
je 0x2fe11
movq -0x78(%rbp), %rcx
cmpq %r8, %rcx
jg 0x2fdc3
leaq 0x3c599(%rip), %rdx     # 0x6c34a
leaq -0x120(%rbp), %rsi
movq -0x30(%rbp), %rdi
xorl %eax, %eax
callq 0x3004c
movq -0x48(%rbp), %rcx
movq (%r15), %r8
cmpq %r8, %rcx
jle 0x2fdec
leaq 0x3c5af(%rip), %rdx     # 0x6c385
leaq -0x120(%rbp), %rsi
movq -0x30(%rbp), %rdi
xorl %eax, %eax
callq 0x3004c
movq -0x48(%rbp), %rcx
movq %rcx, (%rbx)
jmp 0x2fe33
cmpq $0x0, -0x58(%rbp)
je 0x2fe33
movq -0x78(%rbp), %rcx
movq -0x58(%rbp), %rax
movq (%rax), %r8
cmpq %r8, %rcx
jg 0x2fe33
leaq 0x3c5b3(%rip), %rdx     # 0x6c3c2
jmp 0x2fe21
movq -0x48(%rbp), %rcx
cmpq %r8, %rcx
jle 0x2fe33
leaq 0x3c4ec(%rip), %rdx     # 0x6c30d
leaq -0x120(%rbp), %rsi
movq -0x30(%rbp), %rdi
xorl %eax, %eax
callq 0x3004c
movq 0x50(%r12), %rbx
movzbl 0x5(%r12), %r15d
movl 0x30(%r14), %edi
callq 0x1a6eb
movq %rax, %r13
testq %rax, %rax
je 0x2ffd7
movl %r15d, %r14d
leaq (%rbx,%r14), %rax
movq %rax, -0x68(%rbp)
movzbl 0x5(%rbx,%r14), %eax
shll $0x8, %eax
movzbl 0x6(%rbx,%r14), %ecx
addl %ecx, %eax
decl %eax
movzwl %ax, %eax
leaq 0x1(%r13,%rax), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, -0x58(%rbp)
notl %eax
addl -0x70(%rbp), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x81e0
pushq $0x1
popq %rsi
movq %r13, %rdi
movq -0x58(%rbp), %rdx
callq 0x81e0
movzwl 0x3(%rbx,%r14), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movq %rax, -0x58(%rbp)
movzbl 0x3(%r12), %eax
shll $0x2, %eax
subq %rax, %r15
leaq (%r15,%rbx), %rax
addq $0xc, %rax
movq %rax, -0x80(%rbp)
xorl %r15d, %r15d
movq -0x30(%rbp), %r14
movq %rbx, %r8
movq %rbx, -0x50(%rbp)
cmpq -0x58(%rbp), %r15
je 0x2ff60
movq %r14, %r12
movq -0x80(%rbp), %rax
movzwl (%rax,%r15,2), %eax
rolw $0x8, %ax
movzwl %ax, %r14d
movl $0x10000, %eax          # imm = 0x10000
movl %r14d, %ebx
cmpl -0x5c(%rbp), %r14d
jg 0x2ff16
leaq (%r8,%rbx), %rsi
movq -0x40(%rbp), %rdi
callq 0x2c1c4
movq -0x50(%rbp), %r8
movzwl %ax, %eax
addl %r14d, %eax
cmpl -0x70(%rbp), %eax
jle 0x2ff48
movq %r12, %r14
movq %r12, %rdi
xorl %esi, %esi
leaq 0x3c4d0(%rip), %rdx     # 0x6c3fd
movl %r15d, %ecx
movl -0x34(%rbp), %r8d
xorl %eax, %eax
callq 0x3004c
movq -0x50(%rbp), %r8
movq -0x40(%rbp), %r12
incq %r15
jmp 0x2fed6
movl %eax, %eax
movq %r12, %r14
movq -0x40(%rbp), %r12
cmpq %rbx, %rax
jle 0x2ff43
incb -0x1(%r13,%rax)
decq %rax
jmp 0x2ff51
movq -0x68(%rbp), %rdi
leaq 0x1(%rdi), %rax
leaq 0x2(%rdi), %rcx
movl -0x34(%rbp), %r15d
movzbl (%rax), %eax
shll $0x8, %eax
movzbl (%rcx), %edx
orq %rax, %rdx
testq %rdx, %rdx
je 0x2ffaf
leaq (%r8,%rdx), %rax
movzbl 0x2(%r8,%rdx), %esi
shll $0x8, %esi
movzbl 0x3(%r8,%rdx), %ecx
addl %edx, %ecx
addl %esi, %ecx
movl %edx, %edx
cmpq %rdx, %rcx
jle 0x2ffa9
incb -0x1(%r13,%rcx)
decq %rcx
jmp 0x2ff9a
leaq 0x1(%rax), %rcx
jmp 0x2ff70
xorl %ecx, %ecx
movq -0x70(%rbp), %rdx
testl %edx, %edx
cmovlel %ecx, %edx
xorl %ebx, %ebx
cmpq %rcx, %rdx
je 0x2fffe
movb (%r13,%rcx), %al
testb %al, %al
je 0x2ffd0
cmpb $0x2, %al
jl 0x2ffd2
jmp 0x2ffe4
incl %ebx
incq %rcx
jmp 0x2ffbc
movq -0x30(%rbp), %rax
movl $0x1, 0x24(%rax)
jmp 0x30020
leaq 0x3c43c(%rip), %rdx     # 0x6c427
movq %r14, %rdi
xorl %esi, %esi
movl %r15d, %r8d
xorl %eax, %eax
callq 0x3004c
movq -0x68(%rbp), %rdi
movzbl 0x7(%rdi), %r8d
cmpl %r8d, %ebx
je 0x30020
leaq 0x3c43d(%rip), %rdx     # 0x6c44c
movq %r14, %rdi
xorl %esi, %esi
movl %ebx, %ecx
movl %r15d, %r9d
xorl %eax, %eax
callq 0x3004c
movq %r13, %rdi
callq 0x1a82f
movq %r12, %rdi
callq 0x1d90e
movl -0x38(%rbp), %r12d
incl %r12d
movl %r12d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq