// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq 0x48(%rdi), %rax
movq 0x50(%rdi), %r12
movzbl 0x9(%rdi), %r8d
movzwl 0x12(%rdi), %r13d
movq %rdi, 0x38(%rsp)
movzwl 0x16(%rdi), %ecx
movq %rcx, 0x40(%rsp)
leal (%r13,%rcx,2), %ebx
movl 0x34(%rax), %eax
movq %rax, 0x30(%rsp)
movzbl 0x7(%r12,%r8), %eax
cmpl %esi, %eax
movq %r12, 0x10(%rsp)
movq %r8, 0x20(%rsp)
jg 0x4368bf
movl %ebx, 0x18(%rsp)
movzwl 0x1(%r12,%r8), %ecx
rolw $0x8, %cx
xorl %eax, %eax
testw %cx, %cx
je 0x4368a0
movzwl %cx, %ebx
movzwl (%r12,%rbx), %ecx
rolw $0x8, %cx
movzwl %cx, %r15d
testw %r15w, %r15w
je 0x4367fc
cmpb $0x0, (%r12,%r15)
jne 0x4368a0
cmpb $0x0, 0x1(%r12,%r15)
jne 0x4368a0
movq 0x20(%rsp), %rax
movzwl 0x5(%r12,%rax), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movl %ebx, %r14d
subl %edx, %r14d
jbe 0x436875
movq %r12, %rcx
movl 0x18(%rsp), %r12d
movzwl 0x2(%rcx,%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
testl %r15d, %r15d
je 0x436af5
movzwl 0x2(%rcx,%r15), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq %rsi, (%rsp)
leal (%rbx,%rbp), %eax
leal (%rsi,%rax), %edi
addq %rcx, %rdi
leaq (%rax,%rcx), %rsi
movl %r15d, %ecx
subl %eax, %ecx
movq %rdx, 0x8(%rsp)
movslq %ecx, %rdx
callq 0x401038
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
addl (%rsp), %ebp
jmp 0x436afd
leaq 0x11a02b(%rip), %rsi    # 0x5508a7
leaq 0x11a147(%rip), %rdx    # 0x5509ca
leaq 0x119e54(%rip), %r8     # 0x5506de
movl $0xb, %edi
movl $0xfa59, %ecx           # imm = 0xFA59
xorl %eax, %eax
callq 0x40743a
movl $0x1, %eax
cmpl $0x5, %eax
movq 0x20(%rsp), %r8
movl 0x18(%rsp), %ebx
je 0x436a74
movl $0xb, %ebp
testl %eax, %eax
jne 0x436ae4
cmpq $0x0, 0x40(%rsp)
je 0x436a5d
movq 0x30(%rsp), %rcx
leal -0x4(%rcx), %eax
movl %eax, 0x8(%rsp)
leaq (%r12,%r13), %rdx
incq %rdx
movq %rdx, 0x18(%rsp)
xorl %r15d, %r15d
movl %ecx, %ebp
movq $0x0, 0x28(%rsp)
movq %r12, (%rsp)
movq 0x18(%rsp), %rax
movzwl -0x1(%rax,%r15,2), %eax
rolw $0x8, %ax
movzwl %ax, %r14d
cmpl %ebx, %r14d
jb 0x43697c
cmpl 0x8(%rsp), %r14d
jg 0x43697c
movl %r14d, %r13d
movq (%rsp), %rax
leaq (%rax,%r13), %rsi
movq 0x38(%rsp), %rdi
callq *0x78(%rdi)
movq %rbp, %rcx
movzwl %ax, %ebp
movl %ebx, %eax
movl %ecx, %ebx
subl %ebp, %ebx
movl %eax, %r12d
cmpl %eax, %ebx
jl 0x4369ac
leal (%r14,%rbp), %eax
cmpl 0x30(%rsp), %eax
jg 0x4369ac
movq %rcx, %rdx
movl %ebx, %eax
rolw $0x8, %ax
movq 0x18(%rsp), %rcx
movw %ax, -0x1(%rcx,%r15,2)
cmpq $0x0, 0x28(%rsp)
jne 0x436a28
cmpl %r14d, %ebx
jne 0x4369d9
movl $0x8, %eax
movq $0x0, 0x28(%rsp)
jmp 0x436a23
movl $0xb, %edi
leaq 0x119f1f(%rip), %rsi    # 0x5508a7
leaq 0x11a03b(%rip), %rdx    # 0x5509ca
movl $0xfa7b, %ecx           # imm = 0xFA7B
leaq 0x119d43(%rip), %r8     # 0x5506de
xorl %eax, %eax
callq 0x40743a
movl $0x1, %eax
jmp 0x436a45
movl $0xb, %edi
leaq 0x119eef(%rip), %rsi    # 0x5508a7
leaq 0x11a00b(%rip), %rdx    # 0x5509ca
movl $0xfa81, %ecx           # imm = 0xFA81
leaq 0x119d13(%rip), %r8     # 0x5506de
xorl %eax, %eax
callq 0x40743a
movl $0x1, %eax
jmp 0x436a40
movq 0x38(%rsp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq 0x110(%rax), %rsi
movq %rsi, (%rsp)
movq 0x10(%rsp), %rcx
movq 0x20(%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 0x401048
xorl %eax, %eax
movq (%rsp), %rcx
movq %rcx, 0x28(%rsp)
cmpl %r14d, %ebx
je 0x436a40
movl %ebx, %edi
addq 0x10(%rsp), %rdi
addq (%rsp), %r13
movq %r13, %rsi
movq %rbp, %rdx
callq 0x401048
xorl %eax, %eax
movl %ebx, %ebp
movl %r12d, %ebx
testb $0x7, %al
jne 0x436adf
incq %r15
cmpq %r15, 0x40(%rsp)
jne 0x4368f5
jmp 0x436a64
movq 0x30(%rsp), %rax
movl %eax, %ebp
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %r8
movb $0x0, 0x7(%r12,%r8)
movzbl 0x7(%r12,%r8), %ecx
movl %ebp, %eax
subl %ebx, %eax
addl %eax, %ecx
movq 0x38(%rsp), %rdx
movzwl 0x14(%rdx), %edx
cmpl %edx, %ecx
jne 0x436ab2
rolw $0x8, %bp
movw %bp, 0x5(%r12,%r8)
movw $0x0, 0x1(%r12,%r8)
movl %ebx, %edi
addq %r12, %rdi
movslq %eax, %rdx
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x4010b8
jmp 0x436ae4
leaq 0x119dee(%rip), %rsi    # 0x5508a7
leaq 0x119f0a(%rip), %rdx    # 0x5509ca
leaq 0x119c17(%rip), %r8     # 0x5506de
movl $0xb, %ebp
movl $0xb, %edi
movl $0xfa95, %ecx           # imm = 0xFA95
xorl %eax, %eax
callq 0x40743a
jmp 0x436ae4
movl $0xb, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, (%rsp)
leal (%rdx,%rbp), %eax
movq %rax, 0x8(%rsp)
leaq (%rcx,%rax), %rdi
movl %edx, %esi
addq %rcx, %rsi
movslq %r14d, %rdx
movq %rcx, %r14
callq 0x401038
movl $0x5, %eax
cmpl %r12d, %r13d
jae 0x436b5e
addq %r14, %r12
leaq (%r14,%r13), %rcx
movq (%rsp), %rdi
movzwl (%rcx), %edx
rolw $0x8, %dx
movzwl %dx, %edx
movl %ebp, %esi
cmpl %ebx, %edx
jb 0x436b45
movl %edi, %esi
cmpl %r15d, %edx
jae 0x436b4e
addl %esi, %edx
rolw $0x8, %dx
movw %dx, (%rcx)
addq $0x2, %rcx
cmpq %r12, %rcx
jb 0x436b2e
movq 0x10(%rsp), %r12
jmp 0x436b61
movq %r14, %r12
movq 0x8(%rsp), %rbp
jmp 0x4368a0