// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: vdbePmaReadBlob
static int vdbePmaReadBlob(
  PmaReader *p,                   /* PmaReader from which to take the blob */
  int nByte,                      /* Bytes of data to read */
  u8 **ppOut                      /* OUT: Pointer to buffer containing data */
){
  int iBuf;                       /* Offset within buffer to read from */
  int nAvail;                     /* Bytes of data available in buffer */

  if( p->aMap ){
    *ppOut = &p->aMap[p->iReadOff];
    p->iReadOff += nByte;
    return SQLITE_OK;
  }

  assert( p->aBuffer );

  /* If there is no more data to be read from the buffer, read the next
  ** p->nBuffer bytes of data from the file into it. Or, if there are less
  ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
  iBuf = p->iReadOff % p->nBuffer;
  if( iBuf==0 ){
    int nRead;                    /* Bytes to read from disk */
    int rc;                       /* sqlite3OsRead() return code */

    /* Determine how many bytes of data to read. */
    if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
      nRead = p->nBuffer;
    }else{
      nRead = (int)(p->iEof - p->iReadOff);
    }
    assert( nRead>0 );

    /* Readr data from the file. Return early if an error occurs. */
    rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
    assert( rc!=SQLITE_IOERR_SHORT_READ );
    if( rc!=SQLITE_OK ) return rc;
  }
  nAvail = p->nBuffer - iBuf;

  if( nByte<=nAvail ){
    /* The requested data is available in the in-memory buffer. In this
    ** case there is no need to make a copy of the data, just return a
    ** pointer into the buffer to the caller.  */
    *ppOut = &p->aBuffer[iBuf];
    p->iReadOff += nByte;
  }else{
    /* The requested data is not all available in the in-memory buffer.
    ** In this case, allocate space at p->aAlloc[] to copy the requested
    ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
    int nRem;                     /* Bytes remaining to copy */

    /* Extend the p->aAlloc[] allocation if required. */
    if( p->nAlloc<nByte ){
      u8 *aNew;
      sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
      while( nByte>nNew ) nNew = nNew*2;
      aNew = sqlite3Realloc(p->aAlloc, nNew);
      if( !aNew ) return SQLITE_NOMEM_BKPT;
      p->nAlloc = nNew;
      p->aAlloc = aNew;
    }

    /* Copy as much data as is available in the buffer into the start of
    ** p->aAlloc[].  */
    memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
    p->iReadOff += nAvail;
    nRem = nByte - nAvail;

    /* The following loop copies up to p->nBuffer bytes per iteration into
    ** the p->aAlloc[] buffer.  */
    while( nRem>0 ){
      int rc;                     /* vdbePmaReadBlob() return code */
      int nCopy;                  /* Number of bytes to copy */
      u8 *aNext;                  /* Pointer to buffer to copy data from */

      nCopy = nRem;
      if( nRem>p->nBuffer ) nCopy = p->nBuffer;
      rc = vdbePmaReadBlob(p, nCopy, &aNext);
      if( rc!=SQLITE_OK ) return rc;
      assert( aNext!=p->aAlloc );
      memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
      nRem -= nCopy;
    }

    *ppOut = p->aAlloc;
  }

  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movl %esi, %r12d
movq %rdi, %rbx
movq (%rdi), %rcx
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x2ae83b
addq %rcx, %rax
movq %rax, (%r15)
movslq %r12d, %rax
addq %rcx, %rax
movq %rax, (%rbx)
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x38(%rbx), %rsi
movq %rcx, %rax
cqto
idivq %rsi
testq %rdx, %rdx
je 0x2ae8c9
movq %r15, 0x8(%rsp)
movslq 0x38(%rbx), %r13
movslq %edx, %rax
subq %rax, %r13
movl %r12d, %ebp
subl %r13d, %ebp
jle 0x2ae8af
movslq 0x10(%rbx), %rax
cmpl %r12d, %eax
jge 0x2ae909
movq %rdx, (%rsp)
movq %rax, %rcx
addq %rax, %rcx
cmpl $0x40, %eax
movl $0x80, %eax
cmovgeq %rcx, %rax
movslq %r12d, %rcx
movq %rax, %r15
addq %rax, %rax
cmpq %rcx, %r15
jl 0x2ae889
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x27c4f3
testq %rax, %rax
je 0x2ae8fa
movl %r15d, 0x10(%rbx)
movq %rax, 0x20(%rbx)
jmp 0x2ae900
movq 0x30(%rbx), %rax
addq %rdx, %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movslq %r12d, %rax
addq %rax, (%rbx)
jmp 0x2ae826
movq %rdx, %r14
movq 0x8(%rbx), %rdx
movq 0x18(%rbx), %rdi
subq %rcx, %rdx
cmpq %rsi, %rdx
cmovgl %esi, %edx
movq 0x30(%rbx), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
movq %r14, %rdx
movl %eax, %r14d
testl %eax, %eax
jne 0x2ae829
jmp 0x2ae84c
movl $0x7, %r14d
movq (%rsp), %rdx
testq %rax, %rax
je 0x2ae96b
movq 0x20(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq %rdx, %rsi
movq %r13, %rdx
callq 0x17660
addq %r13, (%rbx)
testl %ebp, %ebp
jle 0x2ae96f
movl 0x38(%rbx), %r13d
cmpl %r13d, %ebp
cmovll %ebp, %r13d
movq %rbx, %rdi
movl %r13d, %esi
leaq 0x10(%rsp), %rdx
callq 0x2ae7f4
movl %eax, %r15d
testl %eax, %eax
je 0x2ae94a
movl %r15d, %r14d
jmp 0x2ae966
movl %r12d, %eax
subl %ebp, %eax
movslq %eax, %rdi
addq 0x20(%rbx), %rdi
movq 0x10(%rsp), %rsi
movslq %r13d, %rdx
callq 0x17660
subl %r13d, %ebp
testl %r15d, %r15d
je 0x2ae91f
xorl %eax, %eax
jmp 0x2ae97d
movq 0x20(%rbx), %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movb $0x1, %al
testb %al, %al
jne 0x2ae826
jmp 0x2ae829