// File: /workspace/github_c_makefile_star2_O3/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), %rbx
movzbl 0x9(%rdi), %ecx
movzwl 0x12(%rdi), %r15d
movq %rdi, 0x40(%rsp)
movzwl 0x16(%rdi), %edx
movq %rdx, 0x50(%rsp)
leal (%r15,%rdx,2), %edi
movl 0x34(%rax), %eax
movq %rax, 0x30(%rsp)
movzbl 0x7(%rbx,%rcx), %eax
cmpl %esi, %eax
movq %rbx, 0x10(%rsp)
movl %edi, 0xc(%rsp)
movq %rcx, 0x20(%rsp)
jg 0x435447
movzwl 0x1(%rbx,%rcx), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x435447
movzwl %ax, %r14d
movzwl (%rbx,%r14), %eax
rolw $0x8, %ax
movzwl %ax, %edx
testw %dx, %dx
je 0x435653
cmpb $0x0, (%rbx,%rdx)
jne 0x435447
cmpb $0x0, 0x1(%rbx,%rdx)
je 0x435653
cmpq $0x0, 0x50(%rsp)
je 0x435588
movq 0x30(%rsp), %rax
leal -0x4(%rax), %ecx
movl %ecx, 0x4c(%rsp)
leaq (%r15,%rbx), %rcx
incq %rcx
movq %rcx, 0x28(%rsp)
xorl %r12d, %r12d
movl %eax, %ebp
movq $0x0, 0x38(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x28(%rsp), %rax
movzwl -0x1(%rax,%r12,2), %eax
rolw $0x8, %ax
movzwl %ax, %ebx
cmpl %edi, %ebx
jb 0x435601
cmpl 0x4c(%rsp), %ebx
jg 0x435601
movl %ebx, %r14d
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %rsi
movq 0x40(%rsp), %rdi
callq *0x78(%rdi)
movl 0xc(%rsp), %edi
movzwl %ax, %r15d
movl %ebp, %r13d
subl %r15d, %r13d
cmpl %edi, %r13d
jl 0x43562a
leal (%rbx,%r15), %eax
cmpl 0x30(%rsp), %eax
jg 0x43562a
movl %r13d, %eax
rolw $0x8, %ax
movq 0x28(%rsp), %rcx
movw %ax, -0x1(%rcx,%r12,2)
cmpq $0x0, 0x38(%rsp)
je 0x4354fc
movq 0x10(%rsp), %rbx
jmp 0x43555b
cmpl %ebx, %r13d
jne 0x435511
movq $0x0, 0x38(%rsp)
movq 0x10(%rsp), %rbx
jmp 0x435575
movq 0x40(%rsp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq 0x110(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %rax
movzwl 0x5(%rbx,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
leaq (%rcx,%rax), %rdi
leaq (%rbx,%rax), %rsi
subl %eax, %ebp
movslq %ebp, %rdx
callq 0x401048
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rsp)
movl %r13d, %edi
addq %rbx, %rdi
addq 0x18(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x401048
movl 0xc(%rsp), %edi
incq %r12
movl %r13d, %ebp
cmpq %r12, 0x50(%rsp)
jne 0x43547e
jmp 0x43558d
movq 0x30(%rsp), %r13
movq 0x20(%rsp), %rsi
movb $0x0, 0x7(%rbx,%rsi)
movzbl 0x7(%rbx,%rsi), %ecx
movl %r13d, %eax
subl %edi, %eax
addl %eax, %ecx
movq 0x40(%rsp), %rdx
movzwl 0x14(%rdx), %edx
cmpl %edx, %ecx
jne 0x4355d8
rolw $0x8, %r13w
movw %r13w, 0x5(%rbx,%rsi)
movw $0x0, 0x1(%rbx,%rsi)
movl %edi, %edi
addq %rbx, %rdi
movslq %eax, %rdx
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x4010b8
jmp 0x4356ee
leaq 0x11b350(%rip), %rsi    # 0x55092f
leaq 0x11b46c(%rip), %rdx    # 0x550a52
leaq 0x11b179(%rip), %r8     # 0x550766
movl $0xb, %ebx
movl $0xb, %edi
movl $0xfa95, %ecx           # imm = 0xFA95
jmp 0x4356e7
leaq 0x11b327(%rip), %rsi    # 0x55092f
leaq 0x11b443(%rip), %rdx    # 0x550a52
leaq 0x11b150(%rip), %r8     # 0x550766
movl $0xb, %ebx
movl $0xb, %edi
movl $0xfa7b, %ecx           # imm = 0xFA7B
jmp 0x4356e7
leaq 0x11b2fe(%rip), %rsi    # 0x55092f
leaq 0x11b41a(%rip), %rdx    # 0x550a52
leaq 0x11b127(%rip), %r8     # 0x550766
movl $0xb, %ebx
movl $0xb, %edi
movl $0xfa81, %ecx           # imm = 0xFA81
jmp 0x4356e7
movzwl 0x5(%rbx,%rcx), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movl %r14d, %r8d
subl %esi, %r8d
jbe 0x4356c3
movq %rbx, %rcx
movl %edi, %ebx
movzwl 0x2(%rcx,%r14), %eax
rolw $0x8, %ax
movzwl %ax, %r12d
testl %edx, %edx
movq %rdx, 0x18(%rsp)
je 0x4356ff
movzwl 0x2(%rcx,%rdx), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
leal (%r12,%r14), %eax
leal (%rax,%rbp), %edi
addq %rcx, %rdi
movq %rsi, 0x28(%rsp)
leaq (%rax,%rcx), %rsi
movl %edx, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
movl %r8d, %r13d
callq 0x401038
movq 0x28(%rsp), %rsi
movl %r13d, %r8d
movq 0x10(%rsp), %rcx
addl %ebp, %r12d
jmp 0x435701
leaq 0x11b265(%rip), %rsi    # 0x55092f
leaq 0x11b381(%rip), %rdx    # 0x550a52
leaq 0x11b08e(%rip), %r8     # 0x550766
movl $0xb, %ebx
movl $0xb, %edi
movl $0xfa59, %ecx           # imm = 0xFA59
xorl %eax, %eax
callq 0x40765d
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
leal (%r12,%rsi), %r13d
leaq (%rcx,%r13), %rdi
movl %esi, %esi
addq %rcx, %rsi
movslq %r8d, %rdx
callq 0x401038
cmpl %ebx, %r15d
jae 0x435768
movq 0x10(%rsp), %rax
addq %rax, %rbx
addq %r15, %rax
movl 0xc(%rsp), %edi
movq 0x18(%rsp), %rsi
movzwl (%rax), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %r12d, %edx
cmpl %r14d, %ecx
jb 0x435747
movl %ebp, %edx
cmpl %esi, %ecx
jae 0x435750
addl %edx, %ecx
rolw $0x8, %cx
movw %cx, (%rax)
addq $0x2, %rax
cmpq %rbx, %rax
jb 0x43572f
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %rsi
jmp 0x435597
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %edi
jmp 0x435597