// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/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 nFrag;                           /* Number of fragmented bytes on pPage */
  int top;                             /* First byte of cell content area */
  int gap;        /* First byte of gap between cell pointers and cell content */
  int rc;         /* Integer return code */
  int usableSize; /* Usable size of the page */
  
  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 );
  usableSize = pPage->pBt->usableSize;
  assert( nByte < usableSize-8 );

  nFrag = data[hdr+7];
  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
  gap = pPage->cellOffset + 2*pPage->nCell;
  top = get2byteNotZero(&data[hdr+5]);
  if( gap>top ) return SQLITE_CORRUPT_BKPT;
  testcase( gap+2==top );
  testcase( gap+1==top );
  testcase( gap==top );

  if( nFrag>=60 ){
    /* Always defragment highly fragmented pages */
    rc = defragmentPage(pPage);
    if( rc ) return rc;
    top = get2byteNotZero(&data[hdr+5]);
  }else if( gap+2<=top ){
    /* Search the freelist looking for a free slot big enough to satisfy 
    ** the request. The allocation is made from the first free slot in 
    ** the list that is large enough to accommodate it.
    */
    int pc, addr;
    for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
      int size;            /* Size of the free slot */
      if( pc>usableSize-4 || pc<addr+4 ){
        return SQLITE_CORRUPT_BKPT;
      }
      size = get2byte(&data[pc+2]);
      if( size>=nByte ){
        int x = size - nByte;
        testcase( x==4 );
        testcase( x==3 );
        if( x<4 ){
          /* Remove the slot from the free-list. Update the number of
          ** fragmented bytes within the page. */
          memcpy(&data[addr], &data[pc], 2);
          data[hdr+7] = (u8)(nFrag + x);
        }else if( size+pc > usableSize ){
          return SQLITE_CORRUPT_BKPT;
        }else{
          /* The slot remains on the free-list. Reduce its size to account
          ** for the portion used by the new allocation. */
          put2byte(&data[pc+2], x);
        }
        *pIdx = pc + x;
        return SQLITE_OK;
      }
    }
  }

  /* Check to make sure there is enough space in the gap to satisfy
  ** the allocation.  If not, defragment.
  */
  testcase( gap+2+nByte==top );
  if( gap+2+nByte>top ){
    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 $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x34(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x7, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x10(%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
subl $0x1, %eax
andl $0xffff, %eax           # imm = 0xFFFF
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jle 0x42f53
movl $0xc665, %edi           # imm = 0xC665
callq 0x24410
movl %eax, -0x4(%rbp)
jmp 0x431b4
cmpl $0x3c, -0x34(%rbp)
jl 0x42fb1
movq -0x10(%rbp), %rdi
callq 0x431c0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x42f76
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x431b4
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, -0x38(%rbp)
jmp 0x4310d
movl -0x3c(%rbp), %eax
addl $0x2, %eax
cmpl -0x38(%rbp), %eax
jg 0x4310b
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, %eax
jle 0x43109
movl -0x48(%rbp), %eax
movl -0x44(%rbp), %ecx
subl $0x4, %ecx
cmpl %ecx, %eax
jg 0x4300d
movl -0x48(%rbp), %eax
movl -0x4c(%rbp), %ecx
addl $0x4, %ecx
cmpl %ecx, %eax
jge 0x4301f
movl $0xc678, %edi           # imm = 0xC678
callq 0x24410
movl %eax, -0x4(%rbp)
jmp 0x431b4
movq -0x30(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movl -0x48(%rbp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movzbl 0x1(%rcx,%rdx), %ecx
orl %ecx, %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x14(%rbp), %eax
jl 0x430fc
movl -0x50(%rbp), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x4, -0x54(%rbp)
jge 0x43097
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq -0x30(%rbp), %rdx
movslq -0x48(%rbp), %rsi
movw (%rdx,%rsi), %dx
movw %dx, (%rax,%rcx)
movl -0x34(%rbp), %eax
addl -0x54(%rbp), %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x7, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x430e4
movl -0x50(%rbp), %eax
addl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jle 0x430b4
movl $0xc685, %edi           # imm = 0xC685
callq 0x24410
movl %eax, -0x4(%rbp)
jmp 0x431b4
movl -0x54(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x54(%rbp), %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, 0x1(%rax,%rcx)
jmp 0x430e4
movl -0x48(%rbp), %ecx
addl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x431b4
jmp 0x430fe
movl -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x42fc9
jmp 0x4310b
jmp 0x4310d
movl -0x3c(%rbp), %eax
addl $0x2, %eax
addl -0x14(%rbp), %eax
cmpl -0x38(%rbp), %eax
jle 0x4316b
movq -0x10(%rbp), %rdi
callq 0x431c0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x43135
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x431b4
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, -0x38(%rbp)
movl -0x14(%rbp), %ecx
movl -0x38(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%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 -0x38(%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 -0x38(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)