// File: /workspace/github_c_makefile_star2_O1/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, %r15
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, -0x48(%rbp)
movq (%rsi), %rcx
movslq 0x38(%rsi), %rsi
movq %rcx, %rax
cqto
idivq %rsi
movq %rdx, %r12
testq %rdx, %rdx
je 0x3342f
movq %r15, -0x40(%rbp)
movslq 0x38(%r14), %r13
movslq %r12d, %rax
subq %rax, %r13
movl %ebx, %r15d
subl %r13d, %r15d
jle 0x33416
movq %r12, -0x38(%rbp)
movl 0x10(%r14), %r12d
cmpl %ebx, %r12d
jge 0x3345a
addl %r12d, %r12d
cmpl %ebx, %r12d
jl 0x333ef
movq 0x20(%r14), %rsi
movq -0x48(%rbp), %rdi
movl %r12d, %edx
callq 0x33504
movq %rax, 0x20(%r14)
testq %rax, %rax
je 0x33460
movl %r12d, 0x10(%r14)
jmp 0x33467
movq 0x30(%r14), %rax
addq %r12, %rax
movq -0x40(%rbp), %rcx
movq %rax, (%rcx)
movslq %ebx, %rax
addq %rax, (%r14)
jmp 0x334eb
movq 0x8(%r14), %rdx
movq 0x18(%r14), %rdi
subq %rcx, %rdx
cmpq %rsi, %rdx
cmovgl %esi, %edx
movq 0x30(%r14), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x334f2
jmp 0x333cc
movq -0x38(%rbp), %rcx
jmp 0x33470
movl $0x7, -0x2c(%rbp)
movq -0x38(%rbp), %rcx
testq %rax, %rax
je 0x334d6
movq 0x20(%r14), %rdi
movq 0x30(%r14), %rsi
addq %rcx, %rsi
movq %r13, %rdx
callq 0x82f0
addq %r13, (%r14)
testl %r15d, %r15d
jle 0x334da
movl 0x38(%r14), %r13d
cmpl %r13d, %r15d
cmovll %r15d, %r13d
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movl %r13d, %edx
leaq -0x50(%rbp), %rcx
callq 0x33398
movl %eax, %r12d
testl %eax, %eax
je 0x334b6
movl %r12d, -0x2c(%rbp)
jmp 0x334d1
movl %ebx, %eax
subl %r15d, %eax
movslq %eax, %rdi
addq 0x20(%r14), %rdi
movq -0x50(%rbp), %rsi
movslq %r13d, %rdx
callq 0x82f0
subl %r13d, %r15d
testl %r12d, %r12d
je 0x33486
xorl %eax, %eax
jmp 0x334e7
movq 0x20(%r14), %rax
movq -0x40(%rbp), %rcx
movq %rax, (%rcx)
movb $0x1, %al
testb %al, %al
je 0x334f2
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq