// File: /workspace/github_c_makefile_star2_O2/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, 0x4(%rsp)
movq %rcx, 0x18(%rsp)
movl %edx, %ebx
movl %esi, %r13d
movq %rdi, %r12
movq 0x20(%rdi), %r15
movq 0x88(%rdi), %r14
callq 0x23718f
movq 0x38(%r12), %rbp
movq %rbp, %rcx
subq 0x50(%r14), %rcx
movq %r15, 0x20(%rsp)
movl 0x38(%r15), %edx
movq %r12, 0x10(%rsp)
movzwl 0x44(%r12), %eax
subl %eax, %edx
cmpq %rdx, %rcx
jbe 0x237244
movl $0x10eb2, %edi          # imm = 0x10EB2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x232547
movl %eax, %r12d
subl %r13d, %r12d
jbe 0x237289
leal (%rbx,%r13), %ecx
cmpl %eax, %ecx
cmovbel %ebx, %r12d
movl %r13d, %edi
addq %rbp, %rdi
movq 0x70(%r14), %r8
movq 0x18(%rsp), %r13
movq %r13, %rsi
movl %r12d, %edx
movl 0x4(%rsp), %ecx
callq 0x2374de
movl %eax, %r14d
movslq %r12d, %rax
addq %rax, %r13
movq %r13, 0x18(%rsp)
subl %eax, %ebx
xorl %r13d, %r13d
jmp 0x23728f
subl %eax, %r13d
xorl %r14d, %r14d
testl %r14d, %r14d
jne 0x2374a9
testl %ebx, %ebx
je 0x2374a9
movq 0x20(%rsp), %rax
movl 0x38(%rax), %r14d
leal -0x4(%r14), %r15d
movq 0x10(%rsp), %r12
movzwl 0x44(%r12), %eax
movq %rax, 0x28(%rsp)
addq %rax, %rbp
movq %rbp, %rdi
callq 0x22fc59
movl %eax, 0x8(%rsp)
testb $0x4, 0x1(%r12)
movl %r15d, 0xc(%rsp)
jne 0x23731d
movl 0x40(%r12), %eax
subl 0x28(%rsp), %r14d
addl %r14d, %eax
addl $-0x5, %eax
xorl %edi, %edi
xorl %edx, %edx
divl %r15d
movl %eax, %r14d
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x23734a
leal (,%r14,4), %ebp
movq %rax, %rdi
callq *0x20333a(%rip)         # 0x43a648
movq 0x10(%r12), %rdi
cmpl %eax, %ebp
movl 0x4(%rsp), %ebp
jg 0x23734e
jmp 0x237373
movq 0x10(%r12), %rcx
movl %r13d, %eax
xorl %edx, %edx
divl %r15d
xorl %r14d, %r14d
cmpl $0x0, (%rcx,%rax,4)
movl %r15d, %edi
je 0x237344
movslq %eax, %r14
movl (%rcx,%r14,4), %eax
movl %eax, 0x8(%rsp)
movl %edx, %r13d
movl 0x4(%rsp), %ebp
jmp 0x237391
movl 0x4(%rsp), %ebp
leal (%r14,%r14), %eax
movslq %eax, %rsi
shlq $0x2, %rsi
callq 0x21f85c
testq %rax, %rax
je 0x2374d8
movq %rax, %rdi
movq 0x10(%rsp), %rax
movq %rdi, 0x10(%rax)
movslq %r14d, %rdx
shlq $0x2, %rdx
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x17380
movq 0x10(%rsp), %rax
orb $0x4, 0x1(%rax)
movl 0xc(%rsp), %edi
xorl %eax, %eax
testl %ebp, %ebp
sete %al
addl %eax, %eax
movl %eax, 0x28(%rsp)
leaq 0x4(,%r14,4), %r15
movq 0x10(%rsp), %rbp
movl 0x8(%rsp), %esi
testl %esi, %esi
je 0x2374a6
movq 0x20(%rsp), %rax
cmpl 0x40(%rax), %esi
ja 0x2374b9
movq 0x10(%rbp), %rax
movl %esi, -0x4(%rax,%r15)
movl %r13d, %r12d
subl %edi, %r12d
jae 0x23740c
leal (%rbx,%r13), %eax
movl %edi, %ebp
subl %r13d, %ebp
cmpl %edi, %eax
cmovbel %ebx, %ebp
movq 0x20(%rsp), %rax
movq (%rax), %rdi
leaq 0x30(%rsp), %rdx
movl 0x28(%rsp), %ecx
callq *0x108(%rdi)
testl %eax, %eax
je 0x23741d
movl %eax, %r14d
movl %r13d, %r12d
movq 0x18(%rsp), %r13
jmp 0x237462
movl (%rax,%r15), %eax
testl %eax, %eax
je 0x23747c
movl %eax, 0x8(%rsp)
xorl %r14d, %r14d
jmp 0x237494
movq 0x30(%rsp), %r14
movq 0x8(%r14), %r12
movq %r12, %rdi
callq 0x22fc59
movl %eax, 0x8(%rsp)
addl $0x4, %r13d
addq %r12, %r13
movq %r13, %rdi
movq 0x18(%rsp), %r13
movq %r13, %rsi
movl %ebp, %edx
movl 0x4(%rsp), %ecx
movq %r14, %r8
callq 0x2374de
movl %eax, %r14d
movq 0x30(%rsp), %rdi
callq 0x2235a4
xorl %r12d, %r12d
subl %ebp, %ebx
movl 0xc(%rsp), %edi
je 0x2374c6
movslq %ebp, %rax
addq %rax, %r13
movq %r13, 0x18(%rsp)
movq 0x10(%rsp), %rbp
jmp 0x237494
movq 0x20(%rsp), %rdi
xorl %edx, %edx
leaq 0x8(%rsp), %rcx
callq 0x23751a
movl 0xc(%rsp), %edi
movl %eax, %r14d
addq $0x4, %r15
movl %r12d, %r13d
testl %r14d, %r14d
je 0x2373ab
jmp 0x2374a9
xorl %r14d, %r14d
testl %r14d, %r14d
jne 0x2374c6
testl %ebx, %ebx
je 0x2374c6
movl $0x10f45, %edi          # imm = 0x10F45
jmp 0x2374be
movl $0x10ef0, %edi          # imm = 0x10EF0
callq 0x232547
movl %eax, %r14d
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x7
popq %r14
jmp 0x2374c6