// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: vdbeSorterIterRead
static int vdbeSorterIterRead(
  sqlite3 *db,                    /* Database handle (for malloc) */
  VdbeSorterIter *p,              /* Iterator */
  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 */
  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 );

    /* Read data from the file. Return early if an error occurs. */
    rc = sqlite3OsRead(p->pFile, 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 ){
      int nNew = p->nAlloc*2;
      while( nByte>nNew ) nNew = nNew*2;
      p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
      if( !p->aAlloc ) return SQLITE_NOMEM;
      p->nAlloc = nNew;
    }

    /* 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;                     /* vdbeSorterIterRead() 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 = vdbeSorterIterRead(db, 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %rbx
movl %edx, %r12d
movq %rsi, %r14
movq %rdi, -0x30(%rbp)
movq (%rsi), %rcx
movl 0x38(%rsi), %r13d
movslq %r13d, %r8
movq %rcx, %rax
cqto
idivq %r8
testq %rdx, %rdx
je 0x2ea0e
subl %edx, %r13d
movl %r12d, %r15d
subl %r13d, %r15d
jle 0x2e9f9
movq %rbx, -0x40(%rbp)
movl 0x10(%r14), %ebx
cmpl %r12d, %ebx
jge 0x2ea40
movq %rdx, -0x38(%rbp)
addl %ebx, %ebx
cmpl %r12d, %ebx
jl 0x2e9c9
movq 0x20(%r14), %rsi
movq -0x30(%rbp), %rdi
movl %ebx, %edx
callq 0x2eac1
movq %rax, 0x20(%r14)
testq %rax, %rax
je 0x2eabc
movq %rax, %rdi
movl %ebx, 0x10(%r14)
movq -0x38(%rbp), %rdx
jmp 0x2ea44
movq 0x30(%r14), %rax
addq %rdx, %rax
movq %rax, (%rbx)
movslq %r12d, %rax
addq %rax, (%r14)
jmp 0x2eaab
movq %rdx, %r15
movq 0x8(%r14), %rax
movq 0x18(%r14), %rdi
subq %rcx, %rax
cmpq %r8, %rax
cmovlel %eax, %r8d
movq 0x30(%r14), %rsi
movq (%rdi), %rax
movl %r8d, %edx
callq *0x10(%rax)
testl %eax, %eax
jne 0x2eaad
movl 0x38(%r14), %r13d
movq %r15, %rdx
jmp 0x2e9ad
movq 0x20(%r14), %rdi
movq 0x30(%r14), %rsi
addq %rdx, %rsi
movslq %r13d, %rbx
movq %rbx, %rdx
callq 0x82c0
addq %rbx, (%r14)
leaq -0x48(%rbp), %r13
testl %r15d, %r15d
jle 0x2eaa0
movl 0x38(%r14), %ebx
cmpl %ebx, %r15d
cmovll %r15d, %ebx
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movq %r13, %rcx
callq 0x2e978
testl %eax, %eax
jne 0x2eaad
movl %r12d, %eax
subl %r15d, %eax
movslq %eax, %rdi
addq 0x20(%r14), %rdi
movq -0x48(%rbp), %rsi
movslq %ebx, %rdx
callq 0x82c0
subl %ebx, %r15d
jmp 0x2ea5d
movq 0x20(%r14), %rax
movq -0x40(%rbp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x7
popq %rax
jmp 0x2eaad