// File: /workspace/github_c_makefile_star2_O1/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, -0x3c(%rbp)
movq %rcx, -0x38(%rbp)
movl %edx, %r8d
movl %esi, %r14d
movq %rdi, %r12
movswq 0x7a(%rdi), %rax
movq 0xa8(%rdi,%rax,8), %r13
movq 0x8(%rdi), %rcx
movq %rcx, -0x58(%rbp)
leaq 0x40(%rdi), %r15
cmpw $0x0, 0x5e(%rdi)
jne 0x218eb
movzwl 0x7c(%r12,%rax,2), %eax
movzwl 0x12(%r13), %ecx
movq 0x60(%r13), %rdx
movzbl (%rdx,%rax,2), %edi
shll $0x8, %edi
movzbl 0x1(%rdx,%rax,2), %esi
orl %edi, %esi
andl %ecx, %esi
addq 0x50(%r13), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq %r8, %rbx
callq 0x21585
movq %rbx, %r8
movb $0x1, 0x76(%r12)
xorl %ecx, %ecx
cmpb $0x0, 0x2(%r13)
jne 0x218f7
movl (%r15), %ecx
leal (%r8,%r14), %eax
addl 0x50(%r12), %ecx
movl $0xd13e, %edx           # imm = 0xD13E
cmpl %ecx, %eax
ja 0x21bb2
movzwl 0x58(%r12), %ebx
addq 0x48(%r12), %rbx
movzwl 0x5a(%r12), %ecx
leaq (%rbx,%rcx), %rsi
movq -0x58(%rbp), %rdi
movl 0x34(%rdi), %edi
addq 0x50(%r13), %rdi
cmpq %rdi, %rsi
ja 0x21bb2
movl %ecx, %edx
subl %r14d, %edx
jbe 0x2197a
cmpl %ecx, %eax
movq %r8, -0x50(%rbp)
cmovbel %r8d, %edx
movl %r14d, %r14d
addq %rbx, %r14
movslq %edx, %rax
cmpl $0x0, -0x3c(%rbp)
movq %rax, -0x48(%rbp)
je 0x21986
movq 0x68(%r13), %rdi
callq 0xdebf
movl %eax, %r13d
testl %eax, %eax
movq -0x38(%rbp), %r15
jne 0x2199b
movq %r14, %rdi
movq %r15, %rsi
movq -0x48(%rbp), %rdx
jmp 0x21993
subl %ecx, %r14d
xorl %r13d, %r13d
movq -0x38(%rbp), %r15
jmp 0x219ac
movq -0x38(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x82f0
xorl %r13d, %r13d
movq -0x48(%rbp), %rax
addq %rax, %r15
movq -0x50(%rbp), %r8
subl %eax, %r8d
xorl %r14d, %r14d
testl %r13d, %r13d
jne 0x21ba3
testl %r8d, %r8d
je 0x21ba3
movq -0x58(%rbp), %rax
movl 0x34(%rax), %eax
leal -0x4(%rax), %edi
movzwl 0x5a(%r12), %ecx
movl (%rbx,%rcx), %edx
bswapl %edx
movl %edx, -0x2c(%rbp)
xorl %r13d, %r13d
cmpb $0x0, 0x78(%r12)
movl %edi, -0x48(%rbp)
je 0x21a52
cmpq $0x0, 0x28(%r12)
jne 0x21a52
movq %r15, -0x38(%rbp)
movq %r8, -0x50(%rbp)
movl 0x54(%r12), %edx
subl %ecx, %eax
addl %edx, %eax
addl $-0x5, %eax
movl %eax, %ebx
xorl %edx, %edx
divl %edi
movl %eax, %r13d
shll $0x2, %r13d
movl %r13d, %edi
callq 0xa2a2
movq %rax, %r15
testq %rax, %rax
je 0x21a2a
movslq %r13d, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x8200
movl -0x48(%rbp), %edi
cmpl %ebx, %edi
movl $0x7, %r13d
movl $0x0, %eax
cmoval %eax, %r13d
testq %r15, %r15
movq %r15, 0x28(%r12)
cmovnel %eax, %r13d
movq -0x50(%rbp), %r8
movq -0x38(%rbp), %r15
movq 0x28(%r12), %rsi
xorl %ecx, %ecx
testq %rsi, %rsi
je 0x21a77
movl %r14d, %eax
xorl %edx, %edx
divl %edi
cmpl $0x0, (%rsi,%rax,4)
je 0x21a77
movslq %eax, %rcx
movl (%rsi,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl %edx, %r14d
testl %r13d, %r13d
sete %al
movl -0x2c(%rbp), %esi
testl %esi, %esi
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x21ba3
movq %r15, -0x38(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x3c(%rbp)
sete %al
addl %eax, %eax
movl %eax, -0x5c(%rbp)
leaq 0x4(,%rcx,4), %rbx
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x21ab8
movl %esi, -0x4(%rax,%rbx)
movl %r14d, %r15d
subl %edi, %r15d
jae 0x21af6
leal (%r14,%r8), %eax
movl %edi, %r15d
subl %r14d, %r15d
cmpl %edi, %eax
movq %r8, -0x50(%rbp)
cmovbel %r8d, %r15d
movq -0x58(%rbp), %rax
movq (%rax), %rdi
leaq -0x68(%rbp), %rdx
movl -0x5c(%rbp), %ecx
callq 0xd803
movslq %r15d, %r15
testl %eax, %eax
je 0x21b27
movl %eax, %r13d
jmp 0x21b76
testq %rax, %rax
je 0x21b0a
movl (%rax,%rbx), %eax
testl %eax, %eax
je 0x21b0a
movl %eax, -0x2c(%rbp)
xorl %r13d, %r13d
jmp 0x21b87
movq -0x58(%rbp), %rdi
xorl %edx, %edx
leaq -0x2c(%rbp), %rcx
movq %r8, %r14
callq 0x21be4
movl -0x48(%rbp), %edi
movq %r14, %r8
movl %eax, %r13d
jmp 0x21b87
movq -0x68(%rbp), %rdi
movq 0x8(%rdi), %rax
addl $0x4, %r14d
addq %rax, %r14
movl (%rax), %eax
bswapl %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x21b58
callq 0xdebf
movl %eax, %r13d
testl %eax, %eax
jne 0x21b6a
movq %r14, %rdi
movq -0x38(%rbp), %rsi
jmp 0x21b5f
movq -0x38(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x82f0
xorl %r13d, %r13d
movq -0x68(%rbp), %rdi
callq 0xdcd4
xorl %r14d, %r14d
movq -0x50(%rbp), %r8
subl %r15d, %r8d
addq %r15, -0x38(%rbp)
movl %r14d, %r15d
movl -0x48(%rbp), %edi
testl %r13d, %r13d
jne 0x21ba3
testl %r8d, %r8d
je 0x21ba3
addq $0x4, %rbx
movl -0x2c(%rbp), %esi
movl %r15d, %r14d
testl %esi, %esi
jne 0x21aaa
testl %r13d, %r13d
jne 0x21bd2
movl $0xd1c6, %edx           # imm = 0xD1C6
testl %r8d, %r8d
je 0x21bd2
leaq 0x57c5b(%rip), %rsi     # 0x79814
leaq 0x57979(%rip), %rcx     # 0x79539
movl $0xb, %r13d
movl $0xb, %edi
xorl %eax, %eax
callq 0xb8cc
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq