// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x40(%rbp)
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x9(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x18(%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %eax
movl -0x34(%rbp), %ecx
shll %ecx
addl %ecx, %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x38(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x7, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl -0x14(%rbp), %eax
jg 0x405293
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movl -0x20(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl -0x28(%rbp), %ecx
subl $0x4, %ecx
cmpl %ecx, %eax
jle 0x404fea
movl $0x10187, %edi          # imm = 0x10187
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
cmpl $0x0, -0x5c(%rbp)
je 0x405291
movq -0x40(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl -0x28(%rbp), %ecx
subl $0x4, %ecx
cmpl %ecx, %eax
jle 0x405034
movl $0x1018a, %edi          # imm = 0x1018A
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
xorl %eax, %eax
cmpl -0x60(%rbp), %eax
je 0x40506a
movq -0x40(%rbp), %rax
movslq -0x60(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x40528f
movq -0x40(%rbp), %rax
movl -0x60(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x40528f
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl -0x34(%rbp), %edx
shll %edx
addl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x40(%rbp), %rax
movl -0x5c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movl -0x5c(%rbp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x78(%rbp)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movl -0x20(%rbp), %edx
addl $0x5, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jl 0x4050f9
movl $0x10192, %edi          # imm = 0x10192
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
cmpl $0x0, -0x60(%rbp)
je 0x4051a7
movl -0x5c(%rbp), %eax
addl -0x78(%rbp), %eax
cmpl -0x60(%rbp), %eax
jle 0x405120
movl $0x10195, %edi          # imm = 0x10195
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
movq -0x40(%rbp), %rax
movl -0x60(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movl -0x60(%rbp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x74(%rbp)
movl -0x60(%rbp), %eax
addl -0x74(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x405168
movl $0x10197, %edi          # imm = 0x10197
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
movq -0x40(%rbp), %rdi
movl -0x5c(%rbp), %eax
addl -0x78(%rbp), %eax
addl -0x74(%rbp), %eax
cltq
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movl -0x5c(%rbp), %eax
addl -0x78(%rbp), %eax
cltq
addq %rax, %rsi
movl -0x60(%rbp), %eax
movl -0x5c(%rbp), %ecx
addl -0x78(%rbp), %ecx
subl %ecx, %eax
movslq %eax, %rdx
callq 0x19bf0
movl -0x74(%rbp), %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
jmp 0x4051c6
movl -0x5c(%rbp), %eax
addl -0x78(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x4051c4
movl $0x1019b, %edi          # imm = 0x1019B
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
jmp 0x4051c6
movl -0x7c(%rbp), %eax
addl -0x78(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x30(%rbp), %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movslq -0x7c(%rbp), %rax
addq %rax, %rsi
movl -0x5c(%rbp), %eax
subl -0x7c(%rbp), %eax
movslq %eax, %rdx
callq 0x19bf0
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x40528a
movq -0x70(%rbp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq -0x70(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x40524d
movl -0x1c(%rbp), %eax
addl -0x78(%rbp), %eax
sarl $0x8, %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, (%rax)
movl -0x1c(%rbp), %eax
addl -0x78(%rbp), %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, 0x1(%rax)
jmp 0x405277
movl -0x1c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x405275
movl -0x1c(%rbp), %eax
addl -0x74(%rbp), %eax
sarl $0x8, %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, (%rax)
movl -0x1c(%rbp), %eax
addl -0x74(%rbp), %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, 0x1(%rax)
jmp 0x405277
jmp 0x405279
movq -0x70(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x70(%rbp)
jmp 0x405202
jmp 0x40544e
jmp 0x405291
jmp 0x405293
movl -0x28(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x28(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x40543d
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl -0x18(%rbp), %edx
shll %edx
addl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq -0x88(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jl 0x4052fd
movl -0x1c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x40530f
movl $0x101b7, %edi          # imm = 0x101B7
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movslq -0x1c(%rbp), %rcx
addq %rcx, %rsi
callq *%rax
movzwl %ax, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movl -0x30(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x54(%rbp), %eax
jl 0x40534c
movl -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x40535e
movl $0x101bd, %edi          # imm = 0x101BD
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
movl -0x30(%rbp), %eax
sarl $0x8, %eax
movb %al, %cl
movq -0x88(%rbp), %rax
movb %cl, (%rax)
movl -0x30(%rbp), %eax
movb %al, %cl
movq -0x88(%rbp), %rax
movb %cl, 0x1(%rax)
cmpq $0x0, -0x48(%rbp)
jne 0x405410
movl -0x30(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x405396
jmp 0x40542f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rdi
callq 0x405500
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movl -0x20(%rbp), %edx
addl $0x5, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x8c(%rbp)
movq -0x48(%rbp), %rdi
movslq -0x8c(%rbp), %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movslq -0x8c(%rbp), %rax
addq %rax, %rsi
movl -0x30(%rbp), %eax
addl -0x24(%rbp), %eax
subl -0x8c(%rbp), %eax
movslq %eax, %rdx
callq 0x19610
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x30(%rbp), %rax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
movslq -0x1c(%rbp), %rax
addq %rax, %rsi
movslq -0x24(%rbp), %rdx
callq 0x19610
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4052a9
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x7, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x7, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x30(%rbp), %eax
subl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
je 0x40547d
movl $0x101d2, %edi          # imm = 0x101D2
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x4054ef
movl -0x30(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x30(%rbp), %eax
movb %al, %dl
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movb %dl, 0x1(%rax,%rcx)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rdi
movslq -0x54(%rbp), %rax
addq %rax, %rdi
movl -0x30(%rbp), %eax
subl -0x54(%rbp), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x19360
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)