// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: freeSpace
static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
  u16 iPtr;                             /* Address of ptr to next freeblock */
  u16 iFreeBlk;                         /* Address of the next freeblock */
  u8 hdr;                               /* Page header size.  0 or 100 */
  u8 nFrag = 0;                         /* Reduction in fragmentation */
  u16 iOrigSize = iSize;                /* Original value of iSize */
  u16 x;                                /* Offset to cell content area */
  u32 iEnd = iStart + iSize;            /* First byte past the iStart buffer */
  unsigned char *data = pPage->aData;   /* Page content */

  assert( pPage->pBt!=0 );
  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
  assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  assert( iSize>=4 );   /* Minimum cell size is 4 */
  assert( iStart<=pPage->pBt->usableSize-4 );

  /* The list of freeblocks must be in ascending order.  Find the
  ** spot on the list where iStart should be inserted.
  */
  hdr = pPage->hdrOffset;
  iPtr = hdr + 1;
  if( data[iPtr+1]==0 && data[iPtr]==0 ){
    iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
  }else{
    while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
      if( iFreeBlk<iPtr+4 ){
        if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
        return SQLITE_CORRUPT_PAGE(pPage);
      }
      iPtr = iFreeBlk;
    }
    if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
      return SQLITE_CORRUPT_PAGE(pPage);
    }
    assert( iFreeBlk>iPtr || iFreeBlk==0 );

    /* At this point:
    **    iFreeBlk:   First freeblock after iStart, or zero if none
    **    iPtr:       The address of a pointer to iFreeBlk
    **
    ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
    */
    if( iFreeBlk && iEnd+3>=iFreeBlk ){
      nFrag = iFreeBlk - iEnd;
      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
      iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
      if( iEnd > pPage->pBt->usableSize ){
        return SQLITE_CORRUPT_PAGE(pPage);
      }
      iSize = iEnd - iStart;
      iFreeBlk = get2byte(&data[iFreeBlk]);
    }

    /* If iPtr is another freeblock (that is, if iPtr is not the freelist
    ** pointer in the page header) then check to see if iStart should be
    ** coalesced onto the end of iPtr.
    */
    if( iPtr>hdr+1 ){
      int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
      if( iPtrEnd+3>=iStart ){
        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
        nFrag += iStart - iPtrEnd;
        iSize = iEnd - iPtr;
        iStart = iPtr;
      }
    }
    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
    data[hdr+7] -= nFrag;
  }
  x = get2byte(&data[hdr+5]);
  if( iStart<=x ){
    /* The new freeblock is at the beginning of the cell content area,
    ** so just extend the cell content area rather than create another
    ** freelist entry */
    if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
    if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
    put2byte(&data[hdr+1], iFreeBlk);
    put2byte(&data[hdr+5], iEnd);
  }else{
    /* Insert the new freeblock into the freelist */
    put2byte(&data[iPtr], iStart);
  }
  if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
    /* Overwrite deleted information with zeros when the secure_delete
    ** option is enabled */
    memset(&data[iStart], 0, iSize);
  }
  put2byte(&data[iStart], iFreeBlk);
  put2byte(&data[iStart+2], iSize);
  pPage->nFree += iOrigSize;
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r12d
movq %rdi, %r14
leal (%r12,%rsi), %edx
movq 0x50(%rdi), %rbp
movzbl 0x9(%rdi), %eax
leaq 0x1(%rax), %rcx
movl %ecx, %edi
cmpb $0x0, 0x2(%rbp,%rax)
jne 0x2489c3
cmpb $0x0, (%rbp,%rcx)
je 0x248a69
movl %edi, %r8d
movzwl %di, %r9d
movzwl (%rbp,%r9), %edi
rolw $0x8, %di
movzwl %di, %r11d
cmpw %si, %r11w
jae 0x2489f6
leal 0x4(%r9), %r10d
cmpl %r11d, %r10d
jbe 0x2489c3
testw %di, %di
je 0x2489f6
movl $0x102ac, %edi          # imm = 0x102AC
jmp 0x248b85
movq 0x48(%r14), %r10
movl 0x38(%r10), %r15d
leal -0x4(%r15), %r10d
cmpl %r11d, %r10d
jae 0x248a11
movl $0x102b1, %edi          # imm = 0x102B1
jmp 0x248b85
testw %di, %di
sete %r10b
leal 0x3(%rdx), %ebx
cmpl %r11d, %ebx
setb %bl
orb %r10b, %bl
je 0x248a57
xorl %r10d, %r10d
movl %r12d, %ebx
cmpl %r9d, %ecx
jae 0x248ac3
movl %ebx, %r15d
movzwl 0x2(%rbp,%r9), %r11d
rolw $0x8, %r11w
movzwl %r11w, %r11d
leal (%r11,%r9), %ebx
addl $0x3, %ebx
cmpl %esi, %ebx
jae 0x248a93
movl %r15d, %ebx
jmp 0x248ac3
movl %r11d, %r10d
subl %edx, %r10d
jae 0x248a74
movl $0x102bd, %edi          # imm = 0x102BD
jmp 0x248b85
xorl %r15d, %r15d
movl %esi, %r9d
movl %r12d, %ebx
jmp 0x248ae6
movzwl 0x2(%rbp,%r11), %edx
rolw $0x8, %dx
movzwl %dx, %edx
addl %r11d, %edx
cmpl %r15d, %edx
jbe 0x248aa5
movl $0x102c0, %edi          # imm = 0x102C0
jmp 0x248b85
addl %r9d, %r11d
subl %r11d, %esi
jae 0x248ab8
movl $0x102cd, %edi          # imm = 0x102CD
jmp 0x248b85
movl %edx, %ebx
subl %esi, %ebx
movzwl (%rbp,%r11), %edi
rolw $0x8, %di
jmp 0x248a2c
addb %sil, %r10b
movl %edx, %ebx
subl %r8d, %ebx
movl %r8d, %esi
movb 0x7(%rbp,%rax), %r9b
subb %r10b, %r9b
jae 0x248ad7
movl $0x102d3, %edi          # imm = 0x102D3
jmp 0x248b85
movb %r9b, 0x7(%rbp,%rax)
movzwl %si, %r9d
movl %edi, %r15d
movl %r8d, %edi
movzwl 0x5(%rbp,%rax), %r8d
rolw $0x8, %r8w
movzwl %r8w, %r8d
cmpl %r9d, %r8d
jae 0x248b57
movl %ebx, %r8d
movzwl %di, %eax
movl %esi, %ecx
rolw $0x8, %cx
movw %cx, (%rbp,%rax)
movq 0x48(%r14), %rax
testb $0xc, 0x28(%rax)
movzwl %si, %ebx
movl %r8d, %ecx
je 0x248b2f
leaq (%rbx,%rbp), %rdi
movzwl %cx, %edx
xorl %esi, %esi
movl %ecx, %r13d
callq 0x17380
movl %r13d, %ecx
movl %r15d, %eax
movb %ah, (%rbp,%rbx)
movb %al, 0x1(%rbp,%rbx)
movb %ch, 0x2(%rbp,%rbx)
movb %cl, 0x3(%rbp,%rbx)
addl %r12d, 0x14(%r14)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jbe 0x248b60
movl $0x102db, %edi          # imm = 0x102DB
jmp 0x248b85
movzwl %di, %edi
cmpl %edi, %ecx
jne 0x248b80
movl %ebx, %r8d
movl %r15d, %edi
rolw $0x8, %di
movw %di, (%rbp,%rcx)
movb %dh, 0x5(%rbp,%rax)
movb %dl, 0x6(%rbp,%rax)
jmp 0x248b0b
movl $0x102dc, %edi          # imm = 0x102DC
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x232547