// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3e9390
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x50(%rcx), %rcx
subq %rcx, %rax
movq -0x48(%rbp), %rcx
movl 0x38(%rcx), %ecx
movq -0x10(%rbp), %rdx
movzwl 0x44(%rdx), %edx
subl %edx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x3e9504
movl $0x10eb2, %edi          # imm = 0x10EB2
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x3e986a
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x44(%rcx), %ecx
cmpl %ecx, %eax
jae 0x3e9582
movl -0x18(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
addl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x44(%rcx), %ecx
cmpl %ecx, %eax
jbe 0x3e9539
movq -0x10(%rbp), %rax
movzwl 0x44(%rax), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %eax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movl -0x4c(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x40(%rbp), %rax
movq 0x70(%rax), %r8
callq 0x3e9880
movl %eax, -0x34(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x4c(%rbp), %ecx
movq -0x20(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x4c(%rbp), %ecx
movl -0x18(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x18(%rbp)
jmp 0x3e9592
movq -0x10(%rbp), %rax
movzwl 0x44(%rax), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x3e9849
cmpl $0x0, -0x18(%rbp)
jbe 0x3e9849
movq -0x48(%rbp), %rax
movl 0x38(%rax), %eax
subl $0x4, %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movzwl 0x44(%rax), %eax
addq %rax, %rdi
callq 0x3db720
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x3e968c
movq -0x10(%rbp), %rax
movl 0x40(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x44(%rcx), %ecx
subl %ecx, %eax
addl -0x50(%rbp), %eax
subl $0x1, %eax
xorl %edx, %edx
divl -0x50(%rbp)
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x3e9627
movl -0x58(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x3c08a0
movl %eax, %ecx
movl -0x74(%rbp), %eax
cmpl %ecx, %eax
jle 0x3e9665
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x58(%rbp), %eax
shll %eax
movslq %eax, %rsi
shlq $0x2, %rsi
callq 0x3c0910
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x3e9657
movl $0x7, -0x4(%rbp)
jmp 0x3e986a
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x3e9665
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x58(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x19360
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
orl $0x4, %ecx
movb %cl, 0x1(%rax)
jmp 0x3e96d8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x80(%rbp)
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x50(%rbp)
movl %eax, %ecx
movq -0x80(%rbp), %rax
movl %ecx, %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x3e96d6
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x50(%rbp)
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x50(%rbp)
movl %edx, -0x14(%rbp)
jmp 0x3e96d8
jmp 0x3e96da
cmpl $0x0, -0x54(%rbp)
je 0x3e9847
movl -0x54(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jbe 0x3e9702
movl $0x10ef0, %edi          # imm = 0x10EF0
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x3e986a
movl -0x54(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x14(%rbp), %eax
cmpl -0x50(%rbp), %eax
jb 0x3e9773
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x3e974c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
jmp 0x3e9763
movq -0x48(%rbp), %rdi
movl -0x54(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
leaq -0x54(%rbp), %rcx
callq 0x3e98f0
movl %eax, -0x34(%rbp)
movl -0x50(%rbp), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
jmp 0x3e9831
movl -0x18(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
addl -0x14(%rbp), %eax
cmpl -0x50(%rbp), %eax
jbe 0x3e978d
movl -0x50(%rbp), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movl -0x54(%rbp), %esi
movl -0x24(%rbp), %edx
xorl %ecx, %ecx
movl $0x2, %eax
cmpl $0x0, %edx
cmovel %eax, %ecx
leaq -0x70(%rbp), %rdx
callq 0x3c6020
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x3e9807
movq -0x70(%rbp), %rdi
callq 0x3c6290
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3db720
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %eax
addl $0x4, %eax
movl %eax, %eax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movl -0x64(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x70(%rbp), %r8
callq 0x3e9880
movl %eax, -0x34(%rbp)
movq -0x70(%rbp), %rdi
callq 0x3c62b0
movl $0x0, -0x14(%rbp)
movl -0x64(%rbp), %ecx
movl -0x18(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x3e9820
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3e986a
movl -0x64(%rbp), %ecx
movq -0x20(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3e9839
jmp 0x3e9847
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3e96da
jmp 0x3e9849
cmpl $0x0, -0x34(%rbp)
jne 0x3e9864
cmpl $0x0, -0x18(%rbp)
jbe 0x3e9864
movl $0x10f45, %edi          # imm = 0x10F45
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x3e986a
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)