// File: /workspace/github_c_makefile_star2_O3/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
movq %rcx, -0x40(%rbp)
movl %edx, %r10d
movl %esi, %r14d
movq %rdi, %r9
movswq 0x7a(%rdi), %rax
movq 0xa8(%rdi,%rax,8), %r15
movq 0x8(%rdi), %rcx
movq %rcx, -0x58(%rbp)
leaq 0x40(%rdi), %rbx
cmpw $0x0, 0x5e(%rdi)
jne 0x2159c
movzwl 0x7c(%r9,%rax,2), %eax
movzwl 0x12(%r15), %ecx
movq 0x60(%r15), %rdx
movzbl (%rdx,%rax,2), %edi
shll $0x8, %edi
movzbl 0x1(%rdx,%rax,2), %esi
orl %edi, %esi
andl %ecx, %esi
addq 0x50(%r15), %rsi
movq %r15, %rdi
movq %rbx, %rdx
movq %r10, %r12
movq %r9, %r13
movq %r14, -0x38(%rbp)
movl %r8d, %r14d
callq 0x2121e
movl %r14d, %r8d
movq -0x38(%rbp), %r14
movq %r13, %r9
movq %r12, %r10
movb $0x1, 0x76(%r13)
xorl %ecx, %ecx
cmpb $0x0, 0x2(%r15)
jne 0x215a7
movl (%rbx), %ecx
leal (%r10,%r14), %eax
addl 0x50(%r9), %ecx
movl $0xd13e, %edx           # imm = 0xD13E
cmpl %ecx, %eax
ja 0x21886
movzwl 0x58(%r9), %r12d
addq 0x48(%r9), %r12
movzwl 0x5a(%r9), %ecx
leaq (%r12,%rcx), %rsi
movq -0x58(%rbp), %rdi
movl 0x34(%rdi), %edi
addq 0x50(%r15), %rdi
cmpq %rdi, %rsi
ja 0x21886
movl %r8d, -0x44(%rbp)
movl %ecx, %r13d
subl %r14d, %r13d
jbe 0x21621
movq %r9, -0x50(%rbp)
cmpl %ecx, %eax
movq %r10, -0x38(%rbp)
cmovbel %r10d, %r13d
movl %r14d, %r14d
addq %r12, %r14
cmpl $0x0, -0x44(%rbp)
je 0x21629
movq 0x68(%r15), %rdi
callq 0xdfbf
movslq %r13d, %rbx
testl %eax, %eax
je 0x21638
movl %eax, %r15d
movq -0x40(%rbp), %r14
jmp 0x2164d
subl %ecx, %r14d
xorl %r15d, %r15d
jmp 0x21662
movslq %r13d, %rbx
movq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rdi, %r14
jmp 0x21642
movq %r14, %rdi
movq -0x40(%rbp), %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x82f0
xorl %r15d, %r15d
addq %rbx, %r14
movq %r14, -0x40(%rbp)
movq -0x38(%rbp), %r10
subl %r13d, %r10d
xorl %r14d, %r14d
movq -0x50(%rbp), %r9
testl %r15d, %r15d
jne 0x21877
testl %r10d, %r10d
je 0x21877
movq -0x58(%rbp), %rax
movl 0x34(%rax), %eax
leal -0x4(%rax), %esi
movzwl 0x5a(%r9), %ecx
movl (%r12,%rcx), %r13d
bswapl %r13d
movl %r13d, -0x2c(%rbp)
cmpb $0x0, 0x78(%r9)
movq 0x28(%r9), %rdi
movl %esi, -0x48(%rbp)
je 0x216aa
testq %rdi, %rdi
je 0x216af
addq $0x28, %r9
xorl %r15d, %r15d
jmp 0x2171f
xorl %r15d, %r15d
jmp 0x21716
movq %r10, -0x38(%rbp)
movq %r9, %r12
movl 0x54(%r9), %edx
subl %ecx, %eax
leal (%rax,%rdx), %ebx
addl $-0x5, %ebx
movl %ebx, %eax
xorl %edx, %edx
divl %esi
movl %eax, %r15d
shll $0x2, %r15d
movl %r15d, %edi
callq 0xa38c
movq %rax, %rdi
testq %rax, %rax
je 0x216ef
movslq %r15d, %rdx
movq %rdi, %r15
xorl %esi, %esi
callq 0x8200
movq %r15, %rdi
movl -0x48(%rbp), %esi
cmpl %ebx, %esi
movl $0x7, %r15d
movl $0x0, %eax
cmoval %eax, %r15d
testq %rdi, %rdi
movq %r12, %r9
movq %rdi, 0x28(%r12)
cmovnel %eax, %r15d
movq -0x38(%rbp), %r10
addq $0x28, %r9
testq %rdi, %rdi
je 0x2173e
xorl %ecx, %ecx
movl %r14d, %eax
xorl %edx, %edx
divl %esi
cmpl $0x0, (%rdi,%rax,4)
je 0x21740
movslq %eax, %rcx
movl (%rdi,%rcx,4), %r13d
movl %r13d, -0x2c(%rbp)
movl %edx, %r14d
jmp 0x21740
xorl %ecx, %ecx
testl %r15d, %r15d
jne 0x21877
testl %r13d, %r13d
je 0x21877
xorl %eax, %eax
cmpl $0x0, -0x44(%rbp)
sete %al
addl %eax, %eax
movl %eax, -0x5c(%rbp)
leaq 0x4(,%rcx,4), %r12
movq %r9, -0x50(%rbp)
movq (%r9), %rax
testq %rax, %rax
je 0x2178e
movl %r13d, -0x4(%rax,%r12)
cmpl %esi, %r14d
jb 0x21793
movl (%rax,%r12), %eax
testl %eax, %eax
je 0x217cb
movl %eax, -0x2c(%rbp)
xorl %r15d, %r15d
jmp 0x217f0
cmpl %esi, %r14d
jae 0x217cb
leal (%r14,%r10), %eax
movl %esi, %ebx
subl %r14d, %ebx
cmpl %esi, %eax
movq %r10, -0x38(%rbp)
cmovbel %r10d, %ebx
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movl %r13d, %esi
leaq -0x70(%rbp), %rdx
movl -0x5c(%rbp), %ecx
callq 0xd8e2
movslq %ebx, %rbx
testl %eax, %eax
je 0x217f8
movl %eax, %r15d
jmp 0x2184e
movq -0x58(%rbp), %rdi
movl %r13d, %esi
xorl %edx, %edx
leaq -0x2c(%rbp), %rcx
movq %r10, %rbx
callq 0x218b8
movl -0x48(%rbp), %esi
movq -0x50(%rbp), %r9
movq %rbx, %r10
movl %eax, %r15d
movl -0x2c(%rbp), %eax
subl %esi, %r14d
movl %eax, %r13d
jmp 0x21860
movq -0x70(%rbp), %rdi
movq 0x8(%rdi), %rax
addl $0x4, %r14d
addq %rax, %r14
movl (%rax), %r13d
bswapl %r13d
movl %r13d, -0x2c(%rbp)
cmpl $0x0, -0x44(%rbp)
movq %rdi, -0x68(%rbp)
je 0x21830
callq 0xdfbf
movl %eax, %r15d
testl %eax, %eax
jne 0x21842
movq %r14, %rdi
movq -0x40(%rbp), %rsi
jmp 0x21837
movq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x82f0
xorl %r15d, %r15d
movq -0x68(%rbp), %rdi
callq 0xddd3
xorl %r14d, %r14d
movq -0x38(%rbp), %r10
subl %ebx, %r10d
addq %rbx, -0x40(%rbp)
movq -0x50(%rbp), %r9
movl -0x48(%rbp), %esi
testl %r15d, %r15d
jne 0x21877
testl %r10d, %r10d
je 0x21877
addq $0x4, %r12
testl %r13d, %r13d
jne 0x2176c
testl %r15d, %r15d
jne 0x218a6
movl $0xd1c6, %edx           # imm = 0xD1C6
testl %r10d, %r10d
je 0x218a6
leaq 0x57f87(%rip), %rsi     # 0x79814
leaq 0x57ca5(%rip), %rcx     # 0x79539
movl $0xb, %r15d
movl $0xb, %edi
xorl %eax, %eax
callq 0xb9c7
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq