// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x38(%rcx), %rcx
cqto
idivq %rcx
movl %edx, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x45ea5
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
subq (%rcx), %rax
movq -0x18(%rbp), %rcx
movslq 0x38(%rcx), %rcx
cmpq %rcx, %rax
jle 0x45e5e
movq -0x18(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x34(%rbp)
jmp 0x45e70
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
subq (%rcx), %rax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
movl -0x34(%rbp), %edx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
callq 0x23e30
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x45ea3
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4600e
jmp 0x45ea5
movq -0x18(%rbp), %rax
movl 0x38(%rax), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jg 0x45ee3
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movslq -0x2c(%rbp), %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movslq -0x1c(%rbp), %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x46007
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0x1c(%rbp), %eax
jge 0x45f4d
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
shll %eax
movl %eax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x45f0d
movl -0x40(%rbp), %eax
shll %eax
movl %eax, -0x40(%rbp)
jmp 0x45efb
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x40(%rbp), %edx
callq 0x46020
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x45f43
movl $0x7, -0x4(%rbp)
jmp 0x4600e
movl -0x40(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
movslq -0x2c(%rbp), %rax
addq %rax, %rsi
movslq -0x30(%rbp), %rdx
callq 0x82f0
movslq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x1c(%rbp), %eax
subl -0x30(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jle 0x45ff8
movl -0x3c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x38(%rcx), %eax
jle 0x45fa6
movq -0x18(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x48(%rbp), %edx
leaq -0x50(%rbp), %rcx
callq 0x45e00
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x45fcb
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4600e
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x1c(%rbp), %eax
subl -0x3c(%rbp), %eax
cltq
addq %rax, %rdi
movq -0x50(%rbp), %rsi
movslq -0x48(%rbp), %rdx
callq 0x82f0
movl -0x48(%rbp), %ecx
movl -0x3c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
jmp 0x45f84
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)