// File: /workspace/github_c_makefile_star2_O0/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
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 0x16(%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 0x34(%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 0x448d96
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)
cmpl $0x0, -0x5c(%rbp)
je 0x448d94
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)
xorl %eax, %eax
cmpl -0x60(%rbp), %eax
je 0x448bcc
movq -0x40(%rbp), %rax
movslq -0x60(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x448d92
movq -0x40(%rbp), %rax
movl -0x60(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x448d92
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 0x448c5b
movl $0xfa59, %edi           # imm = 0xFA59
callq 0x424f40
movl %eax, -0x4(%rbp)
jmp 0x448ff5
cmpl $0x0, -0x60(%rbp)
je 0x448cc9
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)
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 0x401030
movl -0x74(%rbp), %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
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 0x401030
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x448d8d
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 0x448d50
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 0x448d7a
movl -0x1c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x448d78
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 0x448d7a
jmp 0x448d7c
movq -0x70(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x70(%rbp)
jmp 0x448d05
jmp 0x448f51
jmp 0x448d94
jmp 0x448d96
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 0x448f40
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 0x448e00
movl -0x1c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x448e12
movl $0xfa7b, %edi           # imm = 0xFA7B
callq 0x424f40
movl %eax, -0x4(%rbp)
jmp 0x448ff5
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 0x448e4f
movl -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x448e61
movl $0xfa81, %edi           # imm = 0xFA81
callq 0x424f40
movl %eax, -0x4(%rbp)
jmp 0x448ff5
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 0x448f13
movl -0x30(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x448e99
jmp 0x448f32
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rdi
callq 0x449010
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 0x401040
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 0x401040
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x448dac
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
movzwl 0x14(%rcx), %ecx
cmpl %ecx, %eax
je 0x448f83
movl $0xfa95, %edi           # imm = 0xFA95
callq 0x424f40
movl %eax, -0x4(%rbp)
jmp 0x448ff5
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 0x4010b0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)