// File: /workspace/github_c_makefile_star2_O3/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, %rbp
movl %esi, %r14d
movq %rdi, %rbx
movq (%rdi), %rcx
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x2a4ca6
addq %rcx, %rax
movq %rax, (%rbp)
movslq %r14d, %rax
addq %rcx, %rax
movq %rax, (%rbx)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x38(%rbx), %r13d
movslq %r13d, %r8
movq %rcx, %rax
cqto
idivq %r8
movq %rdx, %r15
testq %rdx, %rdx
je 0x2a4d2f
subl %r15d, %r13d
cmpl %r13d, %r14d
jle 0x2a4d19
movslq 0x10(%rbx), %rax
cmpl %r14d, %eax
jge 0x2a4d5f
movq %rax, %rcx
addq %rax, %rcx
cmpl $0x40, %eax
movl $0x80, %eax
cmovgeq %rcx, %rax
movslq %r14d, %rcx
movq %rax, %r12
addq %rax, %rax
cmpq %rcx, %r12
jl 0x2a4ce7
movq 0x20(%rbx), %rdi
movq %r12, %rsi
callq 0x272c63
testq %rax, %rax
je 0x2a4dda
movq %rax, %rdi
movq %rbp, 0x8(%rsp)
movl %r12d, 0x10(%rbx)
movq %rax, 0x20(%rbx)
jmp 0x2a4d68
movq 0x30(%rbx), %rax
addq %r15, %rax
movq %rax, (%rbp)
movslq %r14d, %rax
addq %rax, (%rbx)
jmp 0x2a4c95
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rdi
subq %rcx, %rax
cmpq %r8, %rax
cmovlel %eax, %r8d
movq 0x30(%rbx), %rsi
movq (%rdi), %rax
movl %r8d, %edx
callq *0x10(%rax)
testl %eax, %eax
jne 0x2a4c97
movl 0x38(%rbx), %r13d
jmp 0x2a4cbd
movq %rbp, 0x8(%rsp)
movq 0x20(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq %r15, %rsi
movslq %r13d, %r15
movq %r15, %rdx
callq 0x17660
addq %r15, (%rbx)
movl %r14d, %ebp
subl %r13d, %ebp
jle 0x2a4dc9
leaq 0x10(%rsp), %r15
movl 0x38(%rbx), %r12d
cmpl %r12d, %ebp
cmovll %ebp, %r12d
movq %rbx, %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x2a4c62
testl %eax, %eax
jne 0x2a4c97
movl %r14d, %eax
subl %ebp, %eax
movslq %eax, %rdi
addq 0x20(%rbx), %rdi
movq 0x10(%rsp), %rsi
movslq %r12d, %rdx
callq 0x17660
subl %r12d, %ebp
jg 0x2a4d8a
movq 0x20(%rbx), %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x2a4c95
movl $0x7, %eax
jmp 0x2a4c97