// File: /workspace/github_c_makefile_star2_O2/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, %r9
movl %esi, %ebp
movq %rdi, %rbx
movq (%rdi), %rcx
movq 0x40(%rdi), %rax
testq %rax, %rax
je 0x24a418
addq %rcx, %rax
movq %rax, (%r9)
movslq %ebp, %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), %r14d
movslq %r14d, %r8
movq %rcx, %rax
cqto
idivq %r8
movq %rdx, %r15
testq %rdx, %rdx
je 0x24a4a2
subl %r15d, %r14d
movl %ebp, %r13d
subl %r14d, %r13d
jle 0x24a48d
movslq 0x10(%rbx), %rax
cmpl %ebp, %eax
movq %r9, 0x8(%rsp)
jge 0x24a4d8
movq %rax, %rcx
addq %rax, %rcx
cmpl $0x40, %eax
movl $0x80, %eax
cmovgeq %rcx, %rax
movslq %ebp, %rcx
movq %rax, %r12
addq %rax, %rax
cmpq %rcx, %r12
jl 0x24a460
movq 0x20(%rbx), %rdi
movq %r12, %rsi
callq 0x21f85c
testq %rax, %rax
je 0x24a54b
movq %rax, %rdi
movl %r12d, 0x10(%rbx)
movq %rax, 0x20(%rbx)
jmp 0x24a4dc
movq 0x30(%rbx), %rax
addq %r15, %rax
movq %rax, (%r9)
movslq %ebp, %rax
addq %rax, (%rbx)
jmp 0x24a407
movq %r9, %r12
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 0x24a409
movl 0x38(%rbx), %r14d
movq %r12, %r9
jmp 0x24a42f
movq 0x20(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq %r15, %rsi
movslq %r14d, %r15
movq %r15, %rdx
callq 0x17630
addq %r15, (%rbx)
leaq 0x10(%rsp), %r15
testl %r13d, %r13d
jle 0x24a53a
movl 0x38(%rbx), %r12d
cmpl %r12d, %r13d
cmovll %r13d, %r12d
movq %rbx, %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x24a3d6
testl %eax, %eax
jne 0x24a409
movl %ebp, %eax
subl %r13d, %eax
movslq %eax, %rdi
addq 0x20(%rbx), %rdi
movq 0x10(%rsp), %rsi
movslq %r12d, %rdx
callq 0x17630
subl %r12d, %r13d
jmp 0x24a4f6
movq 0x20(%rbx), %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x24a407
pushq $0x7
popq %rax
jmp 0x24a409