// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: accessPayload
static int accessPayload(
  BtCursor *pCur,      /* Cursor pointing to entry to read from */
  u32 offset,          /* Begin reading this far into payload */
  u32 amt,             /* Read this many bytes */
  unsigned char *pBuf, /* Write the bytes into this buffer */ 
  int eOp              /* zero to read. non-zero to write. */
){
  unsigned char *aPayload;
  int rc = SQLITE_OK;
  u32 nKey;
  int iIdx = 0;
  MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
  BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */

  assert( pPage );
  assert( pCur->eState==CURSOR_VALID );
  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  assert( cursorHoldsMutex(pCur) );

  getCellInfo(pCur);
  aPayload = pCur->info.pCell + pCur->info.nHeader;
  nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);

  if( NEVER(offset+amt > nKey+pCur->info.nData) 
   || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
  ){
    /* Trying to read or write past the end of the data is an error */
    return SQLITE_CORRUPT_BKPT;
  }

  /* Check if data must be read/written to/from the btree page itself. */
  if( offset<pCur->info.nLocal ){
    int a = amt;
    if( a+offset>pCur->info.nLocal ){
      a = pCur->info.nLocal - offset;
    }
    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
    offset = 0;
    pBuf += a;
    amt -= a;
  }else{
    offset -= pCur->info.nLocal;
  }

  if( rc==SQLITE_OK && amt>0 ){
    const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
    Pgno nextPage;

    nextPage = get4byte(&aPayload[pCur->info.nLocal]);

#ifndef SQLITE_OMIT_INCRBLOB
    /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
    ** has not been allocated, allocate it now. The array is sized at
    ** one entry for each overflow page in the overflow chain. The
    ** page number of the first overflow page is stored in aOverflow[0],
    ** etc. A value of 0 in the aOverflow[] array means "not yet known"
    ** (the cache is lazily populated).
    */
    if( pCur->isIncrblobHandle && !pCur->aOverflow ){
      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
      /* nOvfl is always positive.  If it were zero, fetchPayload would have
      ** been used instead of this routine. */
      if( ALWAYS(nOvfl) && !pCur->aOverflow ){
        rc = SQLITE_NOMEM;
      }
    }

    /* If the overflow page-list cache has been allocated and the
    ** entry for the first required overflow page is valid, skip
    ** directly to it.
    */
    if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
      iIdx = (offset/ovflSize);
      nextPage = pCur->aOverflow[iIdx];
      offset = (offset%ovflSize);
    }
#endif

    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){

#ifndef SQLITE_OMIT_INCRBLOB
      /* If required, populate the overflow page-list cache. */
      if( pCur->aOverflow ){
        assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
        pCur->aOverflow[iIdx] = nextPage;
      }
#endif

      if( offset>=ovflSize ){
        /* The only reason to read this page is to obtain the page
        ** number for the next page in the overflow chain. The page
        ** data is not required. So first try to lookup the overflow
        ** page-list cache, if any, then fall back to the getOverflowPage()
        ** function.
        */
#ifndef SQLITE_OMIT_INCRBLOB
        if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
          nextPage = pCur->aOverflow[iIdx+1];
        } else 
#endif
          rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
        offset -= ovflSize;
      }else{
        /* Need to read this page properly. It contains some of the
        ** range of data that is being read (eOp==0) or written (eOp!=0).
        */
#ifdef SQLITE_DIRECT_OVERFLOW_READ
        sqlite3_file *fd;
#endif
        int a = amt;
        if( a + offset > ovflSize ){
          a = ovflSize - offset;
        }

#ifdef SQLITE_DIRECT_OVERFLOW_READ
        /* If all the following are true:
        **
        **   1) this is a read operation, and 
        **   2) data is required from the start of this overflow page, and
        **   3) the database is file-backed, and
        **   4) there is no open write-transaction, and
        **   5) the database is not a WAL database,
        **
        ** then data can be read directly from the database file into the
        ** output buffer, bypassing the page-cache altogether. This speeds
        ** up loading large records that span many overflow pages.
        */
        if( eOp==0                                             /* (1) */
         && offset==0                                          /* (2) */
         && pBt->inTransaction==TRANS_READ                     /* (4) */
         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
         && pBt->pPage1->aData[19]==0x01                       /* (5) */
        ){
          u8 aSave[4];
          u8 *aWrite = &pBuf[-4];
          memcpy(aSave, aWrite, 4);
          rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
          nextPage = get4byte(aWrite);
          memcpy(aWrite, aSave, 4);
        }else
#endif

        {
          DbPage *pDbPage;
          rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
              (eOp==0 ? PAGER_GET_READONLY : 0)
          );
          if( rc==SQLITE_OK ){
            aPayload = sqlite3PagerGetData(pDbPage);
            nextPage = get4byte(aPayload);
            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
            sqlite3PagerUnref(pDbPage);
            offset = 0;
          }
        }
        amt -= a;
        pBuf += a;
      }
    }
  }

  if( rc==SQLITE_OK && amt>0 ){
    return SQLITE_CORRUPT_BKPT;
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, -0x34(%rbp)
movq %rcx, %r14
movl %edx, %ebx
movl %esi, %r9d
movq %rdi, %r8
movswq 0x7a(%rdi), %rax
movq 0xa8(%rdi,%rax,8), %r15
movq 0x8(%rdi), %rcx
movq %rcx, -0x58(%rbp)
leaq 0x40(%rdi), %r12
cmpw $0x0, 0x5e(%rdi)
jne 0x1e793
movzwl 0x7c(%r8,%rax,2), %esi
movq %r15, %rdi
movq %r12, %rdx
movq %r14, -0x40(%rbp)
movq %r8, %r14
movq %rbx, %r13
movq %r9, %rbx
callq 0x1e40e
movq %rbx, %r9
movq %r13, %rbx
movq %r14, %r8
movq -0x40(%rbp), %r14
movb $0x1, 0x76(%r8)
xorl %ecx, %ecx
cmpb $0x0, 0x2(%r15)
jne 0x1e7a0
movl (%r12), %ecx
leal (%rbx,%r9), %eax
addl 0x50(%r8), %ecx
movl $0xd13e, %edi           # imm = 0xD13E
cmpl %ecx, %eax
ja 0x1ea4b
movzwl 0x58(%r8), %r13d
addq 0x48(%r8), %r13
movzwl 0x5a(%r8), %ecx
leaq (%rcx,%r13), %rdx
movq -0x58(%rbp), %rsi
movl 0x34(%rsi), %esi
addq 0x50(%r15), %rsi
cmpq %rsi, %rdx
ja 0x1ea4b
movl %ecx, %r12d
subl %r9d, %r12d
jbe 0x1e819
cmpl %ecx, %eax
cmovbel %ebx, %r12d
movl %r9d, %edi
addq %r13, %rdi
movq %r8, -0x50(%rbp)
movq 0x68(%r15), %r8
movq %r14, %rsi
movl %r12d, %edx
movl -0x34(%rbp), %ecx
callq 0x1ea66
movq -0x50(%rbp), %r8
movl %eax, %r10d
movslq %r12d, %rax
addq %rax, %r14
subl %eax, %ebx
xorl %r9d, %r9d
jmp 0x1e81f
subl %ecx, %r9d
xorl %r10d, %r10d
testl %r10d, %r10d
jne 0x1ea3d
testl %ebx, %ebx
je 0x1ea3d
movq %r9, %r12
movq %rbx, -0x48(%rbp)
movq -0x58(%rbp), %rax
movl 0x34(%rax), %ebx
leal -0x4(%rbx), %eax
movl %eax, -0x30(%rbp)
movzwl 0x5a(%r8), %r15d
addq %r15, %r13
movq %r13, %rdi
movq %r8, %r13
callq 0x180a3
movq %r13, %rdi
movl %eax, %esi
movl %eax, -0x2c(%rbp)
cmpb $0x0, 0x78(%r13)
movq 0x28(%r13), %rcx
je 0x1e883
testq %rcx, %rcx
je 0x1e88f
movq %r12, %r8
addq $0x28, %rdi
xorl %r10d, %r10d
movq -0x48(%rbp), %rbx
movl -0x30(%rbp), %r12d
jmp 0x1e8eb
xorl %r10d, %r10d
movq %r12, %r8
movl -0x30(%rbp), %r12d
jmp 0x1e8de
movl 0x54(%rdi), %eax
subl %r15d, %ebx
addl %ebx, %eax
addl $-0x5, %eax
movl %eax, -0x40(%rbp)
xorl %edx, %edx
movl -0x30(%rbp), %r15d
divl %r15d
leal (,%rax,4), %edi
movl %esi, %ebx
callq 0xbeb2
movl %ebx, %esi
movq %r12, %r8
movl %r15d, %r12d
movq %r13, %rdi
movq %rax, %rcx
movq %rax, 0x28(%r13)
testq %rax, %rax
pushq $0x7
popq %r10
movl $0x0, %eax
cmovnel %eax, %r10d
cmpl -0x40(%rbp), %r15d
cmoval %eax, %r10d
addq $0x28, %rdi
testq %rcx, %rcx
movq -0x48(%rbp), %rbx
je 0x1e90b
xorl %r9d, %r9d
movl %r8d, %eax
xorl %edx, %edx
divl %r12d
cmpl $0x0, (%rcx,%rax,4)
je 0x1e90e
movslq %eax, %r9
movl (%rcx,%r9,4), %esi
movl %esi, -0x2c(%rbp)
movl %edx, %r8d
jmp 0x1e90e
xorl %r9d, %r9d
xorl %eax, %eax
cmpl $0x0, -0x34(%rbp)
sete %al
addl %eax, %eax
movl %eax, -0x5c(%rbp)
leaq 0x4(,%r9,4), %r13
movq %rdi, -0x50(%rbp)
testl %r10d, %r10d
jne 0x1ea3d
testl %ebx, %ebx
je 0x1ea3d
testl %esi, %esi
je 0x1ea3d
movq (%rdi), %rax
testq %rax, %rax
je 0x1e963
movl %esi, -0x4(%rax,%r13)
cmpl %r12d, %r8d
jb 0x1e968
movl (%rax,%r13), %eax
testl %eax, %eax
je 0x1e9a3
movl %eax, -0x2c(%rbp)
xorl %r10d, %r10d
jmp 0x1e9c8
cmpl %r12d, %r8d
jae 0x1e9a3
leal (%r8,%rbx), %eax
movl %r12d, %ecx
subl %r8d, %r12d
cmpl %ecx, %eax
cmovbel %ebx, %r12d
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movl %esi, -0x40(%rbp)
leaq -0x70(%rbp), %rdx
movl -0x5c(%rbp), %ecx
movq %rbx, %r15
movq %r8, %rbx
callq 0xca7d
testl %eax, %eax
je 0x1e9cf
movl %eax, %r10d
movq %rbx, %r8
movq %r15, %rbx
jmp 0x1ea20
movq -0x58(%rbp), %rdi
xorl %edx, %edx
leaq -0x2c(%rbp), %rcx
movq %rbx, %r15
movq %r8, %rbx
callq 0x1eaaf
movq %rbx, %r8
movq %r15, %rbx
movq -0x50(%rbp), %rdi
movl %eax, %r10d
movl -0x2c(%rbp), %eax
subl %r12d, %r8d
movl %eax, %esi
jmp 0x1ea34
movq %r15, -0x48(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x8(%rax), %r15
movq %r15, %rdi
callq 0x180a3
movl %eax, -0x40(%rbp)
movl %eax, -0x2c(%rbp)
addl $0x4, %ebx
addq %r15, %rbx
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
movl -0x34(%rbp), %ecx
movq -0x68(%rbp), %rbx
movq %rbx, %r8
callq 0x1ea66
movl %eax, %r15d
movq %rbx, %rdi
movq -0x48(%rbp), %rbx
callq 0xcf6e
movl %r15d, %r10d
xorl %r8d, %r8d
subl %r12d, %ebx
movslq %r12d, %rax
addq %rax, %r14
movq -0x50(%rbp), %rdi
movl -0x30(%rbp), %r12d
movl -0x40(%rbp), %esi
addq $0x4, %r13
jmp 0x1e928
testl %r10d, %r10d
jne 0x1ea54
movl $0xd1c6, %edi           # imm = 0xD1C6
testl %ebx, %ebx
je 0x1ea54
callq 0x1a1fc
pushq $0xb
popq %r10
movl %r10d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq