// File: /workspace/github_c_makefile_star2_O0/shifengbin[P]bishe/Output/../Persistence/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 ); /* Prevent by getAndInitPage() */
  if( gap>top ){
    if( top==0 && pPage->pBt->usableSize==65536 ){
      top = 65536;
    }else{
      return SQLITE_CORRUPT_BKPT;
    }
  }

  /* If there is enough space between gap and top for one more cell pointer
  ** array entry offset, and if the freelist is not empty, then search the
  ** freelist looking for a free 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 ){
      assert( pSpace>=data && (pSpace - data)<65536 );
      *pIdx = (int)(pSpace - data);
      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 );
    rc = defragmentPage(pPage);
    if( rc ) return rc;
    top = get2byteNotZero(&data[hdr+5]);
    assert( gap+nByte<=top );
  }


  /* Allocate memory from the gap in between the cell pointer array
  ** and the cell content area.  The btreeInitPage() 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 $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x6(%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 0xe(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x12(%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 0x4c40f
cmpl $0x0, -0x34(%rbp)
jne 0x4c3fb
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpl $0x10000, 0x34(%rax)    # imm = 0x10000
jne 0x4c3fb
movl $0x10000, -0x34(%rbp)   # imm = 0x10000
jmp 0x4c40d
movl $0xdd37, %edi           # imm = 0xDD37
callq 0x2a650
movl %eax, -0x4(%rbp)
jmp 0x4c53c
jmp 0x4c40f
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x4c43b
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x4c495
movl -0x3c(%rbp), %eax
addl $0x2, %eax
cmpl -0x34(%rbp), %eax
jg 0x4c495
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
leaq -0x38(%rbp), %rdx
callq 0x4c550
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x4c480
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4c53c
cmpl $0x0, -0x38(%rbp)
je 0x4c491
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4c53c
jmp 0x4c493
jmp 0x4c495
movl -0x3c(%rbp), %eax
addl $0x2, %eax
addl -0x14(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x4c4f3
movq -0x10(%rbp), %rdi
callq 0x4c760
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x4c4bd
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4c53c
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 $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)