// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: defragmentPage
static int defragmentPage(MemPage *pPage){
  int i;                     /* Loop counter */
  int pc;                    /* Address of a 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 */
  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 = sqlite3PagerTempSpace(pPage->pBt->pPager);
  data = pPage->aData;
  hdr = pPage->hdrOffset;
  cellOffset = pPage->cellOffset;
  nCell = pPage->nCell;
  assert( nCell==get2byte(&data[hdr+3]) );
  usableSize = pPage->pBt->usableSize;
  cbrk = get2byte(&data[hdr+5]);
  memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
  cbrk = usableSize;
  iCellFirst = cellOffset + 2*nCell;
  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 );
#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
    /* These conditions have already been verified in btreeInitPage()
    ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
    */
    if( pc<iCellFirst || pc>iCellLast ){
      return SQLITE_CORRUPT_BKPT;
    }
#endif
    assert( pc>=iCellFirst && pc<=iCellLast );
    size = cellSizePtr(pPage, &temp[pc]);
    cbrk -= size;
#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
    if( cbrk<iCellFirst ){
      return SQLITE_CORRUPT_BKPT;
    }
#else
    if( cbrk<iCellFirst || pc+size>usableSize ){
      return SQLITE_CORRUPT_BKPT;
    }
#endif
    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
    testcase( cbrk+size==usableSize );
    testcase( pc+size==usableSize );
    memcpy(&data[cbrk], &temp[pc], size);
    put2byte(pAddr, cbrk);
  }
  assert( cbrk>=iCellFirst );
  put2byte(&data[hdr+5], cbrk);
  data[hdr+1] = 0;
  data[hdr+2] = 0;
  data[hdr+7] = 0;
  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  if( cbrk-iCellFirst!=pPage->nFree ){
    return SQLITE_CORRUPT_BKPT;
  }
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x48(%rdi), %rax
movq 0x50(%rdi), %r14
movq (%rax), %rcx
movq 0x108(%rcx), %rdx
movzbl 0x5(%rdi), %ecx
movzwl 0xc(%rdi), %ebx
movq %rdi, -0x40(%rbp)
movzwl 0x10(%rdi), %r13d
movl 0x34(%rax), %r12d
movq %rcx, -0x50(%rbp)
movzwl 0x5(%r14,%rcx), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movq %rdx, -0x60(%rbp)
leaq (%rdx,%rax), %rdi
leaq (%r14,%rax), %rsi
movl %r12d, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
callq 0x82f0
leal (%rbx,%r13,2), %r15d
movq %r13, -0x68(%rbp)
testl %r13d, %r13d
movq %r14, -0x38(%rbp)
je 0x32aae
leal -0x4(%r12), %eax
movl %eax, -0x2c(%rbp)
leaq (%rbx,%r14), %rax
incq %rax
movq %rax, -0x48(%rbp)
xorl %r14d, %r14d
movq %r12, -0x58(%rbp)
movl %r12d, %r13d
movq -0x48(%rbp), %rax
movzwl -0x1(%rax,%r14,2), %eax
rolw $0x8, %ax
movzwl %ax, %ebx
cmpl %r15d, %ebx
jb 0x32a62
cmpl -0x2c(%rbp), %ebx
jg 0x32a62
movl %ebx, %r12d
addq -0x60(%rbp), %r12
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x3072b
movzwl %ax, %edx
subl %edx, %r13d
cmpl %r15d, %r13d
jl 0x32a78
addl %edx, %ebx
cmpl -0x58(%rbp), %ebx
jg 0x32a78
movl %r13d, %edi
addq -0x38(%rbp), %rdi
movq %r12, %rsi
callq 0x82f0
movl %r13d, %eax
rolw $0x8, %ax
movq -0x48(%rbp), %rcx
movw %ax, -0x1(%rcx,%r14,2)
movb $0x1, %r12b
jmp 0x32a9a
xorl %r12d, %r12d
movl $0xb, %edi
leaq 0x46da3(%rip), %rsi     # 0x79814
movl $0xc620, %edx           # imm = 0xC620
jmp 0x32a8c
xorl %r12d, %r12d
movl $0xb, %edi
leaq 0x46d8d(%rip), %rsi     # 0x79814
movl $0xc62c, %edx           # imm = 0xC62C
leaq 0x46aa6(%rip), %rcx     # 0x79539
xorl %eax, %eax
callq 0xb8cc
testb %r12b, %r12b
je 0x32b1e
incq %r14
cmpq %r14, -0x68(%rbp)
jne 0x329fd
jmp 0x32ab1
movl %r12d, %r13d
movl %r13d, %eax
rolw $0x8, %ax
movq -0x38(%rbp), %rcx
movq -0x50(%rbp), %rdx
movw %ax, 0x5(%rcx,%rdx)
movw $0x0, 0x1(%rcx,%rdx)
movb $0x0, 0x7(%rcx,%rdx)
movl %r15d, %edi
addq %rcx, %rdi
subl %r15d, %r13d
movslq %r13d, %r15
xorl %r14d, %r14d
xorl %esi, %esi
movq %r15, %rdx
callq 0x8200
movq -0x40(%rbp), %rax
movzwl 0xe(%rax), %eax
cmpl %eax, %r15d
je 0x32b24
leaq 0x46d16(%rip), %rsi     # 0x79814
leaq 0x46a34(%rip), %rcx     # 0x79539
movl $0xb, %r14d
movl $0xb, %edi
movl $0xc63d, %edx           # imm = 0xC63D
xorl %eax, %eax
callq 0xb8cc
jmp 0x32b24
movl $0xb, %r14d
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq