// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: defragmentPage
static int defragmentPage(MemPage *pPage, int nMaxFrag){
  int i;                     /* Loop counter */
  int pc;                    /* Address of the i-th cell */
  int hdr;                   /* Offset to the page header */
  int size;                  /* Size of a cell */
  int usableSize;            /* Number of usable bytes on a page */
  int cellOffset;            /* Offset to the cell pointer array */
  int cbrk;                  /* Offset to the cell content area */
  int nCell;                 /* Number of cells on the page */
  unsigned char *data;       /* The page data */
  unsigned char *temp;       /* Temp area for cell content */
  unsigned char *src;        /* Source of content */
  int iCellFirst;            /* First allowable cell index */
  int iCellLast;             /* Last possible cell index */

  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  assert( pPage->pBt!=0 );
  assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
  assert( pPage->nOverflow==0 );
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  temp = 0;
  src = data = pPage->aData;
  hdr = pPage->hdrOffset;
  cellOffset = pPage->cellOffset;
  nCell = pPage->nCell;
  assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
  iCellFirst = cellOffset + 2*nCell;
  usableSize = pPage->pBt->usableSize;

  /* This block handles pages with two or fewer free blocks and nMaxFrag
  ** or fewer fragmented bytes. In this case it is faster to move the
  ** two (or one) blocks of cells using memmove() and add the required
  ** offsets to each pointer in the cell-pointer array than it is to
  ** reconstruct the entire page.  */
  if( (int)data[hdr+7]<=nMaxFrag ){
    int iFree = get2byte(&data[hdr+1]);
    if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
    if( iFree ){
      int iFree2 = get2byte(&data[iFree]);
      if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
      if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
        u8 *pEnd = &data[cellOffset + nCell*2];
        u8 *pAddr;
        int sz2 = 0;
        int sz = get2byte(&data[iFree+2]);
        int top = get2byte(&data[hdr+5]);
        if( top>=iFree ){
          return SQLITE_CORRUPT_PAGE(pPage);
        }
        if( iFree2 ){
          if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
          sz2 = get2byte(&data[iFree2+2]);
          if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
          memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
          sz += sz2;
        }else if( NEVER(iFree+sz>usableSize) ){
          return SQLITE_CORRUPT_PAGE(pPage);
        }

        cbrk = top+sz;
        assert( cbrk+(iFree-top) <= usableSize );
        memmove(&data[cbrk], &data[top], iFree-top);
        for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
          pc = get2byte(pAddr);
          if( pc<iFree ){ put2byte(pAddr, pc+sz); }
          else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
        }
        goto defragment_out;
      }
    }
  }

  cbrk = usableSize;
  iCellLast = usableSize - 4;
  for(i=0; i<nCell; i++){
    u8 *pAddr;     /* The i-th cell pointer */
    pAddr = &data[cellOffset + i*2];
    pc = get2byte(pAddr);
    testcase( pc==iCellFirst );
    testcase( pc==iCellLast );
    /* These conditions have already been verified in btreeInitPage()
    ** if PRAGMA cell_size_check=ON.
    */
    if( pc<iCellFirst || pc>iCellLast ){
      return SQLITE_CORRUPT_PAGE(pPage);
    }
    assert( pc>=iCellFirst && pc<=iCellLast );
    size = pPage->xCellSize(pPage, &src[pc]);
    cbrk -= size;
    if( cbrk<iCellFirst || pc+size>usableSize ){
      return SQLITE_CORRUPT_PAGE(pPage);
    }
    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
    testcase( cbrk+size==usableSize );
    testcase( pc+size==usableSize );
    put2byte(pAddr, cbrk);
    if( temp==0 ){
      int x;
      if( cbrk==pc ) continue;
      temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
      x = get2byte(&data[hdr+5]);
      memcpy(&temp[x], &data[x], (cbrk+size) - x);
      src = temp;
    }
    memcpy(&data[cbrk], &src[pc], size);
  }
  data[hdr+7] = 0;

 defragment_out:
  assert( pPage->nFree>=0 );
  if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
    return SQLITE_CORRUPT_PAGE(pPage);
  }
  assert( cbrk>=iCellFirst );
  put2byte(&data[hdr+5], cbrk);
  data[hdr+1] = 0;
  data[hdr+2] = 0;
  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x48(%rdi), %rax
movq 0x50(%rdi), %r14
movzbl 0x9(%rdi), %ecx
movzwl 0x12(%rdi), %r12d
movq %rdi, 0x28(%rsp)
movzwl 0x18(%rdi), %edx
movq %rdx, 0x40(%rsp)
leal (%r12,%rdx,2), %edx
movl 0x38(%rax), %edi
movzbl 0x7(%r14,%rcx), %eax
movl %edx, 0xc(%rsp)
movl %edx, %edx
cmpl %esi, %eax
jle 0x248dd2
movq %rdx, 0x18(%rsp)
leal -0x4(%rdi), %eax
movl %eax, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
leaq (%r12,%r14), %rax
incq %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
movq %r14, (%rsp)
movq $0x0, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
movl %edi, %ebp
cmpq %rbx, 0x40(%rsp)
je 0x248e1c
movq 0x10(%rsp), %rax
movzwl -0x1(%rax,%rbx,2), %eax
rolw $0x8, %ax
movzwl %ax, %r13d
cmpl 0xc(%rsp), %r13d
jb 0x248e6f
cmpl 0x8(%rsp), %r13d
jg 0x248e6f
movl %r13d, %r15d
movq (%rsp), %rax
leaq (%rax,%r15), %rsi
movq 0x28(%rsp), %rdi
callq *0x78(%rdi)
movzwl %ax, %r12d
movq %rbp, %rcx
subl %r12d, %ebp
cmpl 0xc(%rsp), %ebp
jl 0x248e76
leal (%r12,%r13), %eax
cmpl 0x38(%rsp), %eax
jg 0x248e76
movl %ebp, %eax
rolw $0x8, %ax
movq 0x10(%rsp), %rdx
movw %ax, -0x1(%rdx,%rbx,2)
cmpq $0x0, 0x30(%rsp)
jne 0x248db6
cmpl %r13d, %ebp
jne 0x248d76
movq $0x0, 0x30(%rsp)
jmp 0x248dca
movq 0x28(%rsp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq 0x110(%rax), %r13
movq 0x20(%rsp), %rax
movzwl 0x5(%r14,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
leaq (%rax,%r13), %rdi
leaq (%r14,%rax), %rsi
subl %eax, %ecx
movslq %ecx, %rdx
callq 0x17630
movq %r13, (%rsp)
movq %r13, 0x30(%rsp)
movl %ebp, %edi
addq %r14, %rdi
addq (%rsp), %r15
movq %r15, %rsi
movq %r12, %rdx
callq 0x17630
incq %rbx
jmp 0x248ce6
movzwl 0x1(%r14,%rcx), %eax
rolw $0x8, %ax
movzwl %ax, %r15d
leal -0x4(%rdi), %eax
movl %eax, 0x8(%rsp)
cmpl %eax, %r15d
jle 0x248df6
movl $0x10187, %edi          # imm = 0x10187
jmp 0x248e82
testl %r15d, %r15d
je 0x248e95
movzwl (%r14,%r15), %eax
rolw $0x8, %ax
movzwl %ax, %esi
cmpl 0x8(%rsp), %esi
jbe 0x248e9f
movl $0x1018a, %edi          # imm = 0x1018A
jmp 0x248e82
movq 0x20(%rsp), %rdx
movb $0x0, 0x7(%r14,%rdx)
xorl %ecx, %ecx
movl %ebp, %eax
subl 0xc(%rsp), %eax
addl %eax, %ecx
movq 0x28(%rsp), %rsi
cmpl 0x14(%rsi), %ecx
movq 0x18(%rsp), %rdi
jne 0x248e7d
rolw $0x8, %bp
movw %bp, 0x5(%r14,%rdx)
andw $0x0, 0x1(%r14,%rdx)
addq %r14, %rdi
movslq %eax, %rdx
xorl %esi, %esi
callq 0x17380
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x101b7, %edi          # imm = 0x101B7
jmp 0x248e82
movl $0x101bd, %edi          # imm = 0x101BD
jmp 0x248e82
movl $0x101d2, %edi          # imm = 0x101D2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x232547
movq %rdx, 0x18(%rsp)
jmp 0x248cbf
movq %rdx, 0x18(%rsp)
testl %esi, %esi
je 0x248ebf
cmpb $0x0, (%r14,%rsi)
jne 0x248cbf
cmpb $0x0, 0x1(%r14,%rsi)
jne 0x248cbf
movzwl 0x5(%r14,%rcx), %eax
rolw $0x8, %ax
movzwl %ax, %r9d
movl %r15d, %ebp
subl %r9d, %ebp
jbe 0x248efc
movq %rcx, 0x20(%rsp)
movzwl 0x2(%r14,%r15), %eax
rolw $0x8, %ax
movzwl %ax, %ebx
leal (%rbx,%r15), %eax
testl %esi, %esi
je 0x248f06
movl %esi, %ecx
subl %eax, %ecx
jae 0x248f1b
movl $0x10195, %edi          # imm = 0x10195
jmp 0x248e82
movl $0x10192, %edi          # imm = 0x10192
jmp 0x248e82
movq %rsi, 0x10(%rsp)
xorl %ecx, %ecx
cmpl %edi, %eax
jle 0x248f64
movl $0x1019b, %edi          # imm = 0x1019B
jmp 0x248e82
movzwl 0x2(%r14,%rsi), %edx
rolw $0x8, %dx
movzwl %dx, %r8d
leal (%r8,%rsi), %edx
cmpl %edi, %edx
jle 0x248f3b
movl $0x10197, %edi          # imm = 0x10197
jmp 0x248e82
movq %rsi, 0x10(%rsp)
leal (%r8,%rax), %edi
addq %r14, %rdi
movl %eax, %esi
addq %r14, %rsi
movslq %ecx, %rdx
movq %r8, (%rsp)
movq %r9, %r13
callq 0x17c50
movq %r13, %r9
movq (%rsp), %rcx
addl %ecx, %ebx
movq 0x18(%rsp), %rax
leaq (%r14,%rax), %r13
leal (%rbx,%r9), %eax
movq %rax, (%rsp)
leaq (%rax,%r14), %rdi
movl %r9d, %esi
addq %r14, %rsi
movslq %ebp, %rdx
movq %rcx, %rbp
callq 0x17c50
movq %rbp, %rdi
leaq (%r12,%r14), %rax
movq 0x10(%rsp), %rsi
cmpq %r13, %rax
jae 0x248fc1
movzwl (%rax), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %ebx, %edx
cmpl %r15d, %ecx
jb 0x248fb2
movl %edi, %edx
cmpl %esi, %ecx
jae 0x248fbb
addl %edx, %ecx
rolw $0x8, %cx
movw %cx, (%rax)
addq $0x2, %rax
jmp 0x248f96
movq 0x20(%rsp), %rdx
movzbl 0x7(%r14,%rdx), %ecx
movq (%rsp), %rbp
jmp 0x248e29