// File: /workspace/github_c_makefile_star2_O2/ukontainer[P]sqlite-bench/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]) );
  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 ){
      int iFree2 = get2byte(&data[iFree]);

      /* pageFindSlot() has already verified that free blocks are sorted
      ** in order of offset within the page, and that no block extends
      ** past the end of the page. Provided the two free slots do not 
      ** overlap, this guarantees that the memmove() calls below will not
      ** overwrite the usableSize byte buffer, even if the database page
      ** is corrupt.  */
      assert( iFree2==0 || iFree2>iFree );
      assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
      assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );

      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 ){
          assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
          sz2 = get2byte(&data[iFree2+2]);
          assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
          memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
          sz += sz2;
        }
        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:
  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 $0x58, %rsp
movq 0x48(%rdi), %rax
movq 0x50(%rdi), %r14
movzbl 0x9(%rdi), %ecx
movzwl 0x12(%rdi), %ebx
movq %rdi, 0x30(%rsp)
movzwl 0x16(%rdi), %edx
movq %rdx, 0x50(%rsp)
leal (%rbx,%rdx,2), %edx
movl 0x34(%rax), %eax
movq %rax, 0x48(%rsp)
movq %rcx, 0x10(%rsp)
movzbl 0x7(%r14,%rcx), %eax
movl %edx, 0xc(%rsp)
movl %edx, %ecx
movq %rcx, 0x40(%rsp)
cmpl %esi, %eax
jg 0x42e4ab
movq 0x10(%rsp), %rax
movzwl 0x1(%r14,%rax), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x42e4ab
movzwl %ax, %r15d
movzwl (%r14,%r15), %eax
rolw $0x8, %ax
movzwl %ax, %r12d
testw %r12w, %r12w
je 0x42e637
cmpb $0x0, (%r14,%r12)
jne 0x42e4ab
cmpb $0x0, 0x1(%r14,%r12)
je 0x42e637
movq 0x48(%rsp), %rax
leal -0x4(%rax), %ecx
movl %ecx, 0x3c(%rsp)
leaq (%rbx,%r14), %rcx
incq %rcx
movq %rcx, 0x20(%rsp)
xorl %ebp, %ebp
movq %r14, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movl %eax, %ebx
cmpq %rbp, 0x50(%rsp)
je 0x42e5c4
movq 0x20(%rsp), %rax
movzwl -0x1(%rax,%rbp,2), %eax
rolw $0x8, %ax
movzwl %ax, %r13d
cmpl 0xc(%rsp), %r13d
jb 0x42e61f
cmpl 0x3c(%rsp), %r13d
jg 0x42e61f
movl %r13d, %r15d
movq 0x18(%rsp), %rax
leaq (%rax,%r15), %rsi
movq 0x30(%rsp), %rdi
callq *0x78(%rdi)
movzwl %ax, %r12d
movq %rbx, %rcx
subl %r12d, %ebx
cmpl 0xc(%rsp), %ebx
jl 0x42e629
leal (%r12,%r13), %eax
cmpl 0x48(%rsp), %eax
jg 0x42e629
movl %ebx, %eax
rolw $0x8, %ax
movq 0x20(%rsp), %rdx
movw %ax, -0x1(%rdx,%rbp,2)
cmpq $0x0, 0x28(%rsp)
jne 0x42e5a7
cmpl %r13d, %ebx
jne 0x42e566
movq $0x0, 0x28(%rsp)
jmp 0x42e5bc
movq 0x30(%rsp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq 0x110(%rax), %r13
movq 0x10(%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 0x401048
movq %r13, 0x18(%rsp)
movq %r13, 0x28(%rsp)
movl %ebx, %edi
addq %r14, %rdi
addq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r12, %rdx
callq 0x401048
incq %rbp
jmp 0x42e4d5
movq 0x10(%rsp), %rax
movb $0x0, 0x7(%r14,%rax)
xorl %ecx, %ecx
movl %ebx, %eax
subl 0xc(%rsp), %eax
addl %eax, %ecx
movq 0x30(%rsp), %rdx
movzwl 0x14(%rdx), %edx
cmpl %edx, %ecx
jne 0x42e630
rolw $0x8, %bx
movq 0x10(%rsp), %rcx
movw %bx, 0x5(%r14,%rcx)
andw $0x0, 0x1(%r14,%rcx)
movq 0x40(%rsp), %rdi
addq %r14, %rdi
movslq %eax, %rdx
xorl %esi, %esi
callq 0x4010b8
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xfa7b, %edi           # imm = 0xFA7B
jmp 0x42e6ac
movl $0xfa81, %edi           # imm = 0xFA81
jmp 0x42e6ac
movl $0xfa95, %edi           # imm = 0xFA95
jmp 0x42e6ac
movq 0x10(%rsp), %rax
movzwl 0x5(%r14,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movl %r15d, %ecx
subl %edx, %ecx
jbe 0x42e6a7
movzwl 0x2(%r14,%r15), %eax
rolw $0x8, %ax
movzwl %ax, %r13d
testl %r12d, %r12d
je 0x42e6bf
movzwl 0x2(%r14,%r12), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
leal (%r15,%r13), %eax
leal (%rax,%rbp), %edi
addq %r14, %rdi
leaq (%rax,%r14), %rsi
movl %ecx, 0x20(%rsp)
movl %r12d, %ecx
subl %eax, %ecx
movq %rdx, 0x28(%rsp)
movslq %ecx, %rdx
callq 0x401038
movq 0x28(%rsp), %rdx
movl 0x20(%rsp), %ecx
movq %rbp, 0x18(%rsp)
addl %ebp, %r13d
jmp 0x42e6c8
movl $0xfa59, %edi           # imm = 0xFA59
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x418527
movq $0x0, 0x18(%rsp)
movq 0x40(%rsp), %rax
leaq (%r14,%rax), %rbp
leal (%rdx,%r13), %eax
movq %rax, 0x20(%rsp)
leaq (%rax,%r14), %rdi
movl %edx, %esi
addq %r14, %rsi
movslq %ecx, %rdx
callq 0x401038
leaq (%rbx,%r14), %rax
movq 0x18(%rsp), %rsi
cmpq %rbp, %rax
jae 0x42e721
movzwl (%rax), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %r13d, %edx
cmpl %r15d, %ecx
jb 0x42e712
movl %esi, %edx
cmpl %r12d, %ecx
jae 0x42e71b
addl %edx, %ecx
rolw $0x8, %cx
movw %cx, (%rax)
addq $0x2, %rax
jmp 0x42e6f4
movq 0x10(%rsp), %rax
movzbl 0x7(%r14,%rax), %ecx
movq 0x20(%rsp), %rbx
jmp 0x42e5d1