// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/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;
  int iIdx = 0;
  MemPage *pPage = pCur->pPage;               /* Btree page of current entry */
  BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
#ifdef SQLITE_DIRECT_OVERFLOW_READ
  unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
#endif

  assert( pPage );
  assert( eOp==0 || eOp==1 );
  assert( pCur->eState==CURSOR_VALID );
  assert( pCur->ix<pPage->nCell );
  assert( cursorHoldsMutex(pCur) );

  getCellInfo(pCur);
  aPayload = pCur->info.pPayload;
  assert( offset+amt <= pCur->info.nPayload );

  assert( aPayload > pPage->aData );
  if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
    /* Trying to read or write past the end of the data is an error.  The
    ** conditional above is really:
    **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
    ** but is recast into its current form to avoid integer overflow problems
    */
    return SQLITE_CORRUPT_PAGE(pPage);
  }

  /* 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]);

    /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
    **
    ** The aOverflow[] 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->curFlags & BTCF_ValidOvfl)==0 ){
      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
      if( pCur->aOverflow==0
       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
      ){
        Pgno *aNew = (Pgno*)sqlite3Realloc(
            pCur->aOverflow, nOvfl*2*sizeof(Pgno)
        );
        if( aNew==0 ){
          return SQLITE_NOMEM_BKPT;
        }else{
          pCur->aOverflow = aNew;
        }
      }
      memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
      pCur->curFlags |= BTCF_ValidOvfl;
    }else{
      /* 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[offset/ovflSize] ){
        iIdx = (offset/ovflSize);
        nextPage = pCur->aOverflow[iIdx];
        offset = (offset%ovflSize);
      }
    }

    assert( rc==SQLITE_OK && amt>0 );
    while( nextPage ){
      /* If required, populate the overflow page-list cache. */
      if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
      assert( pCur->aOverflow[iIdx]==0
              || pCur->aOverflow[iIdx]==nextPage
              || CORRUPT_DB );
      pCur->aOverflow[iIdx] = nextPage;

      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.
        */
        assert( pCur->curFlags & BTCF_ValidOvfl );
        assert( pCur->pBtree->db==pBt->db );
        if( pCur->aOverflow[iIdx+1] ){
          nextPage = pCur->aOverflow[iIdx+1];
        }else{
          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).
        */
        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) there are no dirty pages in the page-cache
        **   4) the database is file-backed, and
        **   5) the page is not in the WAL file
        **   6) at least 4 bytes have already been read into the output buffer
        **
        ** 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) */
         && sqlite3PagerDirectReadOk(pBt->pPager, nextPage)    /* (3,4,5) */
         && &pBuf[-4]>=pBufStart                               /* (6) */
        ){
          sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
          u8 aSave[4];
          u8 *aWrite = &pBuf[-4];
          assert( aWrite>=pBufStart );                         /* due to (6) */
          memcpy(aSave, aWrite, 4);
          rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
          if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
          nextPage = get4byte(aWrite);
          memcpy(aWrite, aSave, 4);
        }else
#endif

        {
          DbPage *pDbPage;
          rc = sqlite3PagerGet(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;
        if( amt==0 ) return rc;
        pBuf += a;
      }
      if( rc ) break;
      iIdx++;
    }
  }

  if( rc==SQLITE_OK && amt>0 ){
    /* Overflow chain ends prematurely */
    return SQLITE_CORRUPT_PAGE(pPage);
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, 0x14(%rsp)
movq %rcx, %rbp
movq %rdx, 0x8(%rsp)
movl %esi, %r13d
movq %rdi, %r15
movq 0x20(%rdi), %r14
movq 0x88(%rdi), %rbx
callq 0x297df9
movq 0x38(%r15), %r12
movq %r12, %rdx
subq 0x50(%rbx), %rdx
movq %r14, 0x20(%rsp)
movl 0x38(%r14), %esi
movzwl 0x44(%r15), %eax
subl %eax, %esi
movl $0x10eb2, %ecx          # imm = 0x10EB2
cmpq %rsi, %rdx
ja 0x298175
movl %eax, %ecx
subl %r13d, %ecx
jbe 0x297ee0
movq 0x8(%rsp), %rsi
leal (%rsi,%r13), %edx
cmpl %eax, %edx
cmovbel %esi, %ecx
movl %r13d, %r13d
addq %r12, %r13
movslq %ecx, %r14
cmpl $0x0, 0x14(%rsp)
je 0x297eec
movq 0x70(%rbx), %rdi
callq 0x280e3a
movq %rbp, %rbx
movl %eax, %ebp
testl %eax, %eax
jne 0x297eff
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x17660
jmp 0x297efd
movq %rbp, 0x18(%rsp)
subl %eax, %r13d
xorl %ebp, %ebp
jmp 0x297f17
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x17660
movq %rbp, %rbx
xorl %ebp, %ebp
addq %r14, %rbx
movq %rbx, 0x18(%rsp)
movq 0x8(%rsp), %rax
subl %r14d, %eax
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
testl %ebp, %ebp
jne 0x298162
cmpl $0x0, 0x8(%rsp)
je 0x298162
movq 0x20(%rsp), %rax
movl 0x38(%rax), %eax
leal -0x4(%rax), %edi
movzwl 0x44(%r15), %ecx
movl (%r12,%rcx), %edx
bswapl %edx
movl %edx, (%rsp)
testb $0x4, 0x1(%r15)
movl %edi, 0x4(%rsp)
jne 0x297fbd
movl 0x40(%r15), %edx
subl %ecx, %eax
addl %edx, %eax
addl $-0x5, %eax
xorl %ebp, %ebp
xorl %edx, %edx
divl %edi
movl %eax, %r14d
movq 0x10(%r15), %rdi
testq %rdi, %rdi
je 0x297f7d
leal (,%r14,4), %ebx
callq *0x22e71f(%rip)         # 0x4c6698
cmpl %eax, %ebx
jle 0x297f9e
movq 0x10(%r15), %rdi
leal (%r14,%r14), %eax
movslq %eax, %rsi
shlq $0x2, %rsi
callq 0x27c4f3
testq %rax, %rax
je 0x298156
movq %rax, 0x10(%r15)
movq 0x10(%r15), %rdi
movslq %r14d, %rdx
shlq $0x2, %rdx
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x173a0
orb $0x4, 0x1(%r15)
movl 0x4(%rsp), %edi
jmp 0x297fdc
movq 0x10(%r15), %rcx
movl %r13d, %eax
xorl %edx, %edx
divl %edi
xorl %ebx, %ebx
cmpl $0x0, (%rcx,%rax,4)
je 0x297fdc
movslq %eax, %rbx
movl (%rcx,%rbx,4), %eax
movl %eax, (%rsp)
movl %edx, %r13d
movl (%rsp), %esi
movb $0x1, %al
testl %esi, %esi
je 0x29811c
xorl %eax, %eax
cmpl $0x0, 0x14(%rsp)
sete %al
addl %eax, %eax
movl %eax, 0x2c(%rsp)
leaq 0x4(,%rbx,4), %r12
movl $0x7, %r14d
movq 0x20(%rsp), %rax
cmpl 0x40(%rax), %esi
ja 0x298126
movq 0x10(%r15), %rax
movl %esi, -0x4(%rax,%r12)
movl %r13d, %ebx
subl %edi, %ebx
jae 0x29805a
movq 0x8(%rsp), %rcx
leal (%rcx,%r13), %eax
movl %edi, %ebx
subl %r13d, %ebx
cmpl %edi, %eax
cmovbel %ecx, %ebx
movq 0x20(%rsp), %rax
movq (%rax), %rdi
leaq 0x30(%rsp), %rdx
movl 0x2c(%rsp), %ecx
callq *0x108(%rdi)
testl %eax, %eax
je 0x298069
movl %eax, %ebp
jmp 0x2980dd
movl (%rax,%r12), %eax
testl %eax, %eax
je 0x29809f
movl %eax, (%rsp)
xorl %ebp, %ebp
jmp 0x2980b4
movq 0x30(%rsp), %rdi
movq 0x8(%rdi), %rax
addl $0x4, %r13d
addq %rax, %r13
movl (%rax), %eax
bswapl %eax
movl %eax, (%rsp)
cmpl $0x0, 0x14(%rsp)
je 0x2980b9
callq 0x280e3a
movl %eax, %ebp
testl %eax, %eax
jne 0x2980cb
movslq %ebx, %rdx
movq %r13, %rdi
movq 0x18(%rsp), %rsi
jmp 0x2980c4
movq 0x20(%rsp), %rdi
xorl %edx, %edx
movq %rsp, %rcx
callq 0x2981b3
movl 0x4(%rsp), %edi
movl %eax, %ebp
movl %ebx, %r13d
jmp 0x298103
movslq %ebx, %rdx
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x17660
xorl %ebp, %ebp
movq 0x30(%rsp), %rdi
xorl %r13d, %r13d
testq %rdi, %rdi
je 0x2980dd
callq 0x2949b7
movl 0x4(%rsp), %edi
xorl %ecx, %ecx
movq 0x8(%rsp), %rdx
cmpl %ebx, %edx
movl %ebx, %eax
cmovel %ecx, %eax
subl %ebx, %edx
movq %rdx, 0x8(%rsp)
cmovel %ebp, %r14d
je 0x29815c
cltq
addq %rax, 0x18(%rsp)
testl %ebp, %ebp
jne 0x298118
movl (%rsp), %esi
addq $0x4, %r12
testl %esi, %esi
jne 0x298007
xorl %ebp, %ebp
movb $0x1, %al
jmp 0x29815e
movl $0x7, %r14d
xorl %ebp, %ebp
jmp 0x29815e
leaq 0x1471f2(%rip), %rsi    # 0x3df31f
leaq 0x1472fc(%rip), %rdx    # 0x3df430
leaq 0x14704a(%rip), %r8     # 0x3df185
xorl %ebp, %ebp
movl $0xb, %r14d
movl $0xb, %edi
movl $0x10ef0, %ecx          # imm = 0x10EF0
xorl %eax, %eax
callq 0x27e637
jmp 0x29815c
movl $0x7, %r14d
xorl %eax, %eax
testb %al, %al
je 0x29819c
testl %ebp, %ebp
jne 0x2981ae
movl $0x10f45, %ecx          # imm = 0x10F45
movl %ebp, %r14d
cmpl $0x0, 0x8(%rsp)
je 0x29819c
leaq 0x1471a3(%rip), %rsi    # 0x3df31f
leaq 0x1472ad(%rip), %rdx    # 0x3df430
leaq 0x146ffb(%rip), %r8     # 0x3df185
movl $0xb, %r14d
movl $0xb, %edi
xorl %eax, %eax
callq 0x27e637
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %r14d
jmp 0x29819c