// File: /workspace/github_c_makefile_star2_O1/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), %r12
movzbl 0x9(%rdi), %r8d
movzwl 0x12(%rdi), %r13d
movq %rdi, 0x40(%rsp)
movzwl 0x18(%rdi), %r14d
leal (%r13,%r14,2), %ebx
movl 0x38(%rax), %eax
movq %rax, 0x18(%rsp)
movzbl 0x7(%r12,%r8), %eax
cmpl %esi, %eax
movq %r12, 0x10(%rsp)
movq %r14, (%rsp)
movq %r8, 0x30(%rsp)
jg 0x2acb4a
movzwl 0x1(%r12,%r8), %eax
rolw $0x8, %ax
movzwl %ax, %r15d
movq 0x18(%rsp), %rax
leal -0x4(%rax), %edx
movl $0x10187, %ecx          # imm = 0x10187
cmpl %edx, %r15d
jg 0x2acb0a
xorl %eax, %eax
testl %r15d, %r15d
je 0x2acb30
movzwl (%r12,%r15), %ecx
rolw $0x8, %cx
movzwl %cx, %edi
movl $0x1018a, %ecx          # imm = 0x1018A
cmpl %edx, %edi
jle 0x2acd89
leaq 0x13280e(%rip), %rsi    # 0x3df31f
leaq 0x132918(%rip), %rdx    # 0x3df430
leaq 0x132666(%rip), %r8     # 0x3df185
movl $0xb, %edi
xorl %eax, %eax
callq 0x27e637
movl $0x1, %eax
movq (%rsp), %r14
cmpl $0x5, %eax
je 0x2acd06
movl $0xb, %ebp
testl %eax, %eax
jne 0x2acd78
testq %r14, %r14
je 0x2accef
movq 0x18(%rsp), %rcx
leal -0x4(%rcx), %eax
movl %eax, 0x28(%rsp)
leaq (%r12,%r13), %rax
incq %rax
movq %rax, 0x20(%rsp)
movq %r12, %rax
xorl %r12d, %r12d
movl %ecx, %ebp
movq $0x0, 0x38(%rsp)
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movzwl -0x1(%rax,%r12,2), %eax
rolw $0x8, %ax
movzwl %ax, %r15d
cmpl %ebx, %r15d
jb 0x2acc09
cmpl 0x28(%rsp), %r15d
jg 0x2acc09
movl %r15d, %r13d
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %rsi
movq 0x40(%rsp), %rdi
callq *0x78(%rdi)
movq %rbp, %rcx
movzwl %ax, %ebp
movl %ebx, %eax
movl %ecx, %ebx
subl %ebp, %ebx
movl %eax, %r14d
cmpl %eax, %ebx
jl 0x2acc39
leal (%r15,%rbp), %eax
cmpl 0x18(%rsp), %eax
jg 0x2acc39
movq %rcx, %rdx
movl %ebx, %eax
rolw $0x8, %ax
movq 0x20(%rsp), %rcx
movw %ax, -0x1(%rcx,%r12,2)
cmpq $0x0, 0x38(%rsp)
jne 0x2accb7
cmpl %r15d, %ebx
jne 0x2acc66
movl $0x8, %eax
movq $0x0, 0x38(%rsp)
jmp 0x2accb2
movl $0xb, %edi
leaq 0x13270a(%rip), %rsi    # 0x3df31f
leaq 0x132814(%rip), %rdx    # 0x3df430
movl $0x101b7, %ecx          # imm = 0x101B7
leaq 0x13255d(%rip), %r8     # 0x3df185
xorl %eax, %eax
callq 0x27e637
movl $0x1, %eax
jmp 0x2accd9
movl $0xb, %edi
leaq 0x1326da(%rip), %rsi    # 0x3df31f
leaq 0x1327e4(%rip), %rdx    # 0x3df430
movl $0x101bd, %ecx          # imm = 0x101BD
leaq 0x13252d(%rip), %r8     # 0x3df185
xorl %eax, %eax
callq 0x27e637
movl $0x1, %eax
jmp 0x2accd0
movq 0x40(%rsp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq 0x110(%rax), %rsi
movq %rsi, 0x8(%rsp)
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rax
movzwl 0x5(%rcx,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
leaq (%rsi,%rax), %rdi
leaq (%rcx,%rax), %rsi
subl %eax, %edx
movslq %edx, %rdx
callq 0x17660
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movq %rcx, 0x38(%rsp)
cmpl %r15d, %ebx
je 0x2accd0
movl %ebx, %edi
addq 0x10(%rsp), %rdi
addq 0x8(%rsp), %r13
movq %r13, %rsi
movq %rbp, %rdx
callq 0x17660
xorl %eax, %eax
movl %ebx, %ebp
movl %r14d, %ebx
movq (%rsp), %r14
testb $0x7, %al
jne 0x2acd73
incq %r12
cmpq %r12, %r14
jne 0x2acb81
jmp 0x2accf6
movq 0x18(%rsp), %rax
movl %eax, %ebp
movq 0x10(%rsp), %r12
movq 0x30(%rsp), %rax
movb $0x0, 0x7(%r12,%rax)
movq 0x30(%rsp), %rdx
movzbl 0x7(%r12,%rdx), %ecx
movl %ebp, %eax
subl %ebx, %eax
addl %eax, %ecx
movq 0x40(%rsp), %rsi
cmpl 0x14(%rsi), %ecx
jne 0x2acd46
rolw $0x8, %bp
movw %bp, 0x5(%r12,%rdx)
movw $0x0, 0x1(%r12,%rdx)
movl %ebx, %edi
addq %r12, %rdi
movslq %eax, %rdx
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x173a0
jmp 0x2acd78
leaq 0x1325d2(%rip), %rsi    # 0x3df31f
leaq 0x1326dc(%rip), %rdx    # 0x3df430
leaq 0x13242a(%rip), %r8     # 0x3df185
movl $0xb, %ebp
movl $0xb, %edi
movl $0x101d2, %ecx          # imm = 0x101D2
xorl %eax, %eax
callq 0x27e637
jmp 0x2acd78
movl $0xb, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %edi, %edi
movq (%rsp), %r14
je 0x2acda9
cmpb $0x0, (%r12,%rdi)
je 0x2acd9d
jmp 0x2acb34
cmpb $0x0, 0x1(%r12,%rdi)
jne 0x2acb34
movq 0x30(%rsp), %r8
movzwl 0x5(%r12,%r8), %eax
rolw $0x8, %ax
movzwl %ax, %r11d
movl $0x10192, %ecx          # imm = 0x10192
movl %r15d, %r14d
subl %r11d, %r14d
jbe 0x2acb0a
movzwl 0x2(%r12,%r15), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
leal (%r15,%rbp), %eax
testl %edi, %edi
je 0x2ace48
movl $0x10195, %ecx          # imm = 0x10195
movl %edi, %edx
subl %eax, %edx
jb 0x2acb0a
movzwl 0x2(%r12,%rdi), %ecx
rolw $0x8, %cx
movzwl %cx, %r10d
leal (%r10,%rdi), %esi
movl $0x10197, %ecx          # imm = 0x10197
cmpl 0x18(%rsp), %esi
jg 0x2acb0a
movq %rdi, 0x20(%rsp)
leal (%r10,%rax), %edi
addq %r12, %rdi
movl %eax, %esi
addq %r12, %rsi
movslq %edx, %rdx
movq %r10, 0x8(%rsp)
movq %r11, 0x28(%rsp)
callq 0x17cd0
movq 0x28(%rsp), %r11
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %rdi
addl %edx, %ebp
jmp 0x2ace59
xorl %edx, %edx
movl $0x1019b, %ecx          # imm = 0x1019B
cmpl 0x18(%rsp), %eax
jg 0x2acb0a
movq %rdx, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
movq %r12, %rcx
movl %ebx, %r12d
movq %rbp, 0x28(%rsp)
leal (%rbp,%r11), %eax
movq %rax, %rbp
leaq (%rcx,%rax), %rdi
movl %r11d, %esi
addq %rcx, %rsi
movslq %r14d, %rdx
callq 0x17cd0
movl $0x5, %eax
cmpl %r12d, %r13d
jae 0x2acee5
movq 0x10(%rsp), %rcx
addq %rcx, %r12
addq %r13, %rcx
movq (%rsp), %r14
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r8
movq 0x8(%rsp), %r9
movzwl (%rcx), %edx
rolw $0x8, %dx
movzwl %dx, %edx
movl %r8d, %esi
cmpl %r15d, %edx
jb 0x2acec9
movl %r9d, %esi
cmpl %edi, %edx
jae 0x2aced2
addl %esi, %edx
rolw $0x8, %dx
movw %dx, (%rcx)
addq $0x2, %rcx
cmpq %r12, %rcx
jb 0x2aceb0
movq 0x10(%rsp), %r12
jmp 0x2acb34
movq 0x10(%rsp), %r12
jmp 0x2acb30