// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movq %rcx, %rbx
movl %edx, %r13d
movq %rsi, %r14
movq %rdi, -0x30(%rbp)
movq (%rsi), %rcx
movl 0x38(%rsi), %r12d
movslq %r12d, %r8
movq %rcx, %rax
cqto
idivq %r8
movq %rdx, %r15
testq %rdx, %rdx
je 0x33b70
subl %r15d, %r12d
cmpl %r12d, %r13d
jle 0x33b5b
movq %rbx, -0x38(%rbp)
movl 0x10(%r14), %ebx
cmpl %r13d, %ebx
jge 0x33b99
addl %ebx, %ebx
cmpl %r13d, %ebx
jl 0x33b2f
movq 0x20(%r14), %rsi
movq -0x30(%rbp), %rdi
movl %ebx, %edx
callq 0x33c1f
movq %rax, 0x20(%r14)
testq %rax, %rax
je 0x33c18
movq %rax, %rdi
movl %ebx, 0x10(%r14)
jmp 0x33b9d
movq 0x30(%r14), %rax
addq %r15, %rax
movq %rax, (%rbx)
movslq %r13d, %rax
addq %rax, (%r14)
jmp 0x33c07
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 0x33c09
movl 0x38(%r14), %r12d
jmp 0x33b1a
movq 0x20(%r14), %rdi
movq 0x30(%r14), %rsi
addq %r15, %rsi
movslq %r12d, %rbx
movq %rbx, %rdx
callq 0x82f0
addq %rbx, (%r14)
movl %r13d, %r15d
subl %r12d, %r15d
jle 0x33bfc
leaq -0x40(%rbp), %r12
movl 0x38(%r14), %ebx
cmpl %ebx, %r15d
cmovll %r15d, %ebx
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movq %r12, %rcx
callq 0x33ae2
testl %eax, %eax
jne 0x33c09
movl %r13d, %eax
subl %r15d, %eax
movslq %eax, %rdi
addq 0x20(%r14), %rdi
movq -0x40(%rbp), %rsi
movslq %ebx, %rdx
callq 0x82f0
subl %ebx, %r15d
jg 0x33bbe
movq 0x20(%r14), %rax
movq -0x38(%rbp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x7, %eax
jmp 0x33c09