// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %r8d, 0x24(%rsp)
movq %rcx, 0x18(%rsp)
movl %edx, %ebx
movl %esi, %r13d
movq %rdi, %r15
movq 0x20(%rdi), %r14
movq 0x88(%rdi), %rbp
callq 0x28e1b0
movq 0x38(%r15), %r12
movq %r12, %rcx
subq 0x50(%rbp), %rcx
movq %r14, 0x28(%rsp)
movl 0x38(%r14), %edx
movzwl 0x44(%r15), %eax
subl %eax, %edx
cmpq %rdx, %rcx
jbe 0x28e26f
leaq 0x14797e(%rip), %rsi    # 0x3d5bcb
leaq 0x147a88(%rip), %rdx    # 0x3d5cdc
leaq 0x1477d6(%rip), %r8     # 0x3d5a31
movl $0xb, %ebp
movl $0xb, %edi
movl $0x10eb2, %ecx          # imm = 0x10EB2
jmp 0x28e515
movl %eax, %r14d
subl %r13d, %r14d
jbe 0x28e2ac
leal (%rbx,%r13), %ecx
cmpl %eax, %ecx
cmovbel %ebx, %r14d
movl %r13d, %r13d
addq %r12, %r13
cmpl $0x0, 0x24(%rsp)
je 0x28e2b3
movslq %r14d, %rax
movq %rax, 0x10(%rsp)
movq 0x70(%rbp), %rdi
callq 0x2774a1
testl %eax, %eax
je 0x28e2c8
movl %eax, %ebp
movq 0x18(%rsp), %r13
jmp 0x28e2df
subl %eax, %r13d
xorl %ebp, %ebp
jmp 0x28e2ef
movslq %r14d, %rdx
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %rdi, %r13
movq %rdx, 0x10(%rsp)
jmp 0x28e2d8
movq %r13, %rdi
movq 0x18(%rsp), %r13
movq %r13, %rsi
movq 0x10(%rsp), %rdx
callq 0x17660
xorl %ebp, %ebp
addq 0x10(%rsp), %r13
movq %r13, 0x18(%rsp)
subl %r14d, %ebx
xorl %r13d, %r13d
testl %ebp, %ebp
jne 0x28e4e9
testl %ebx, %ebx
je 0x28e4e9
movq 0x28(%rsp), %rax
movl 0x38(%rax), %eax
leal -0x4(%rax), %esi
movzwl 0x44(%r15), %ecx
movl (%r12,%rcx), %ebp
bswapl %ebp
movl %ebp, 0xc(%rsp)
testb $0x4, 0x1(%r15)
movl %esi, 0x10(%rsp)
jne 0x28e397
movl 0x40(%r15), %edx
subl %ecx, %eax
addl %edx, %eax
addl $-0x5, %eax
xorl %edi, %edi
xorl %edx, %edx
divl %esi
movl %eax, %r14d
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x28e35b
leal (,%r14,4), %r12d
movq %rax, %rdi
callq *0x22d346(%rip)         # 0x4bb698
movq 0x10(%r15), %rdi
cmpl %eax, %r12d
jle 0x28e37b
leal (%r14,%r14), %eax
movslq %eax, %rsi
shlq $0x2, %rsi
callq 0x272c63
testq %rax, %rax
je 0x28e553
movq %rax, %rdi
movq %rax, 0x10(%r15)
movslq %r14d, %rdx
shlq $0x2, %rdx
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x173a0
orb $0x4, 0x1(%r15)
movl 0x10(%rsp), %esi
jmp 0x28e3b9
movq 0x10(%r15), %rcx
movl %r13d, %eax
xorl %edx, %edx
divl %esi
xorl %r14d, %r14d
cmpl $0x0, (%rcx,%rax,4)
je 0x28e3b9
movslq %eax, %r14
movl (%rcx,%r14,4), %ebp
movl %ebp, 0xc(%rsp)
movl %edx, %r13d
testl %ebp, %ebp
je 0x28e4e7
xorl %eax, %eax
cmpl $0x0, 0x24(%rsp)
sete %al
addl %eax, %eax
movl %eax, 0x3c(%rsp)
leaq 0x4(,%r14,4), %r12
movq 0x28(%rsp), %rax
cmpl 0x40(%rax), %ebp
ja 0x28e52d
movq 0x10(%r15), %rax
movl %ebp, -0x4(%rax,%r12)
movl %r13d, %r14d
subl %esi, %r14d
jae 0x28e433
leal (%rbx,%r13), %eax
movl %esi, %r14d
subl %r13d, %r14d
cmpl %esi, %eax
cmovbel %ebx, %r14d
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movl %ebp, %esi
leaq 0x40(%rsp), %rdx
movl 0x3c(%rsp), %ecx
callq *0x108(%rdi)
movslq %r14d, %rdx
testl %eax, %eax
je 0x28e443
movl %eax, %ebp
movl 0x10(%rsp), %esi
jmp 0x28e4c9
movl (%rax,%r12), %eax
testl %eax, %eax
je 0x28e481
movl %eax, 0xc(%rsp)
xorl %ebp, %ebp
jmp 0x28e49a
movq 0x40(%rsp), %rdi
movq 0x8(%rdi), %rax
addl $0x4, %r13d
addq %rax, %r13
movl (%rax), %eax
bswapl %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, 0x24(%rsp)
movq %rdx, 0x30(%rsp)
je 0x28e49f
callq 0x2774a1
movl %eax, %ebp
testl %eax, %eax
jne 0x28e4ae
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %rdx
jmp 0x28e4a7
movq 0x28(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
leaq 0xc(%rsp), %rcx
callq 0x28e55a
movl 0x10(%rsp), %esi
movl %eax, %ebp
movl %r14d, %r13d
jmp 0x28e4d3
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x17660
xorl %ebp, %ebp
movq 0x40(%rsp), %rdi
xorl %r13d, %r13d
testq %rdi, %rdi
je 0x28e4c0
callq 0x28ab87
movl 0x10(%rsp), %esi
movq 0x30(%rsp), %rdx
subl %r14d, %ebx
je 0x28e51c
addq %rdx, 0x18(%rsp)
testl %ebp, %ebp
jne 0x28e4e9
movl 0xc(%rsp), %ebp
addq $0x4, %r12
testl %ebp, %ebp
jne 0x28e3d9
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x28e51c
testl %ebx, %ebx
je 0x28e51c
leaq 0x1476d3(%rip), %rsi    # 0x3d5bcb
leaq 0x1477dd(%rip), %rdx    # 0x3d5cdc
leaq 0x14752b(%rip), %r8     # 0x3d5a31
movl $0xb, %ebp
movl $0xb, %edi
movl $0x10f45, %ecx          # imm = 0x10F45
xorl %eax, %eax
callq 0x274d08
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x147697(%rip), %rsi    # 0x3d5bcb
leaq 0x1477a1(%rip), %rdx    # 0x3d5cdc
leaq 0x1474ef(%rip), %r8     # 0x3d5a31
movl $0xb, %ebp
movl $0xb, %edi
movl $0x10ef0, %ecx          # imm = 0x10EF0
jmp 0x28e515
movl $0x7, %ebp
jmp 0x28e51c