// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: allocateSpace
static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
  const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
  u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
  int top;                             /* First byte of cell content area */
  int rc = SQLITE_OK;                  /* Integer return code */
  int gap;        /* First byte of gap between cell pointers and cell content */

  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  assert( pPage->pBt );
  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  assert( nByte>=0 );  /* Minimum cell size is 4 */
  assert( pPage->nFree>=nByte );
  assert( pPage->nOverflow==0 );
  assert( nByte < (int)(pPage->pBt->usableSize-8) );

  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
  gap = pPage->cellOffset + 2*pPage->nCell;
  assert( gap<=65536 );
  /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
  ** and the reserved space is zero (the usual value for reserved space)
  ** then the cell content offset of an empty page wants to be 65536.
  ** However, that integer is too large to be stored in a 2-byte unsigned
  ** integer, so a value of 0 is used in its place. */
  top = get2byte(&data[hdr+5]);
  assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
  if( gap>top ){
    if( top==0 && pPage->pBt->usableSize==65536 ){
      top = 65536;
    }else{
      return SQLITE_CORRUPT_PAGE(pPage);
    }
  }

  /* If there is enough space between gap and top for one more cell pointer,
  ** and if the freelist is not empty, then search the
  ** freelist looking for a slot big enough to satisfy the request.
  */
  testcase( gap+2==top );
  testcase( gap+1==top );
  testcase( gap==top );
  if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
    u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
    if( pSpace ){
      int g2;
      assert( pSpace+nByte<=data+pPage->pBt->usableSize );
      *pIdx = g2 = (int)(pSpace-data);
      if( NEVER(g2<=gap) ){
        return SQLITE_CORRUPT_PAGE(pPage);
      }else{
        return SQLITE_OK;
      }
    }else if( rc ){
      return rc;
    }
  }

  /* The request could not be fulfilled using a freelist slot.  Check
  ** to see if defragmentation is necessary.
  */
  testcase( gap+2+nByte==top );
  if( gap+2+nByte>top ){
    assert( pPage->nCell>0 || CORRUPT_DB );
    assert( pPage->nFree>=0 );
    rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
    if( rc ) return rc;
    top = get2byteNotZero(&data[hdr+5]);
    assert( gap+2+nByte<=top );
  }


  /* Allocate memory from the gap in between the cell pointer array
  ** and the cell content area.  The btreeComputeFreeSpace() call has already
  ** validated the freelist.  Given that the freelist is valid, there
  ** is no way that the allocation can extend off the end of the page.
  ** The assert() below verifies the previous sentence.
  */
  top -= nByte;
  put2byte(&data[hdr+5], top);
  assert( top+nByte <= (int)pPage->pBt->usableSize );
  *pIdx = top;
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x9(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x18(%rcx), %ecx
shll %ecx
addl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x5, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x404b6f
cmpl $0x0, -0x34(%rbp)
jne 0x404b5b
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpl $0x10000, 0x38(%rax)    # imm = 0x10000
jne 0x404b5b
movl $0x10000, -0x34(%rbp)   # imm = 0x10000
jmp 0x404b6d
movl $0x1024c, %edi          # imm = 0x1024C
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x404cfc
jmp 0x404b6f
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x404b9b
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x404c12
movl -0x3c(%rbp), %eax
addl $0x2, %eax
cmpl -0x34(%rbp), %eax
jg 0x404c12
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
leaq -0x38(%rbp), %rdx
callq 0x404d10
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x404bfd
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movl %ecx, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x4c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jg 0x404bf1
movl $0x1025e, %edi          # imm = 0x1025E
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x404cfc
movl $0x0, -0x4(%rbp)
jmp 0x404cfc
cmpl $0x0, -0x38(%rbp)
je 0x404c0e
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x404cfc
jmp 0x404c10
jmp 0x404c12
movl -0x3c(%rbp), %eax
addl $0x2, %eax
addl -0x14(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x404cb3
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %ecx
movl -0x14(%rbp), %eax
addl $0x2, %eax
subl %eax, %ecx
movl $0x4, %eax
cmpl %ecx, %eax
jge 0x404c4e
movl $0x4, %eax
movl %eax, -0x5c(%rbp)
jmp 0x404c60
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl -0x14(%rbp), %ecx
addl $0x2, %ecx
subl %ecx, %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x404f20
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x404c7d
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x404cfc
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x5, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
subl $0x1, %eax
andl $0xffff, %eax           # imm = 0xFFFF
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x14(%rbp), %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x34(%rbp), %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movb %dl, 0x1(%rax,%rcx)
movl -0x34(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)