// File: /workspace/github_c_makefile_star2_O0/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
subq $0x90, %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, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x5e(%rax), %eax
cmpl $0x0, %eax
jne 0x2c83c
movq -0x10(%rbp), %rax
movswl 0x7a(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq 0xa8(%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movzwl 0x7c(%rax,%rcx,2), %esi
movq -0x10(%rbp), %rdx
addq $0x40, %rdx
callq 0x2c000
movq -0x10(%rbp), %rax
movb $0x1, 0x76(%rax)
jmp 0x2c83e
jmp 0x2c83e
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x58(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rax
movzbl 0x2(%rax), %eax
cmpl $0x0, %eax
je 0x2c86c
xorl %eax, %eax
movl %eax, -0x74(%rbp)
jmp 0x2c877
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x14(%rbp), %eax
addl -0x18(%rbp), %eax
movl -0x38(%rbp), %ecx
movq -0x10(%rbp), %rdx
addl 0x50(%rdx), %ecx
cmpl %ecx, %eax
ja 0x2c8b7
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x5a(%rcx), %ecx
addq %rcx, %rax
movq -0x48(%rbp), %rcx
movq 0x50(%rcx), %rcx
movq -0x50(%rbp), %rdx
movl 0x34(%rdx), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jbe 0x2c8c9
movl $0xd13e, %edi           # imm = 0xD13E
callq 0x24410
movl %eax, -0x4(%rbp)
jmp 0x2cbf3
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x5a(%rcx), %ecx
cmpl %ecx, %eax
jae 0x2c947
movl -0x18(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
addl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x5a(%rcx), %ecx
cmpl %ecx, %eax
jbe 0x2c8fe
movq -0x10(%rbp), %rax
movzwl 0x5a(%rax), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %eax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movl -0x58(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x48(%rbp), %rax
movq 0x68(%rax), %r8
callq 0x2cc00
movl %eax, -0x34(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x58(%rbp), %ecx
movq -0x20(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x58(%rbp), %ecx
movl -0x18(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x18(%rbp)
jmp 0x2c957
movq -0x10(%rbp), %rax
movzwl 0x5a(%rax), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x2cbd2
cmpl $0x0, -0x18(%rbp)
jbe 0x2cbd2
movq -0x50(%rbp), %rax
movl 0x34(%rax), %eax
subl $0x4, %eax
movl %eax, -0x5c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movzwl 0x5a(%rax), %eax
addq %rax, %rdi
callq 0x20a50
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x78(%rax), %eax
cmpl $0x0, %eax
je 0x2c9fa
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x2c9fa
movq -0x10(%rbp), %rax
movl 0x54(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x5a(%rcx), %ecx
subl %ecx, %eax
addl -0x5c(%rbp), %eax
subl $0x1, %eax
xorl %edx, %edx
divl -0x5c(%rbp)
movl %eax, -0x64(%rbp)
movslq -0x64(%rbp), %rax
shlq $0x2, %rax
movl %eax, %edi
callq 0xd860
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
cmpl $0x0, -0x64(%rbp)
je 0x2c9f8
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x2c9f8
movl $0x7, -0x34(%rbp)
jmp 0x2c9fa
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x2ca4f
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x80(%rbp)
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x5c(%rbp)
movl %eax, %ecx
movq -0x80(%rbp), %rax
movl %ecx, %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0x2ca4f
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x5c(%rbp)
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x5c(%rbp)
movl %edx, -0x14(%rbp)
jmp 0x2ca51
xorl %eax, %eax
cmpl $0x0, -0x34(%rbp)
movb %al, -0x81(%rbp)
jne 0x2ca7a
xorl %eax, %eax
cmpl $0x0, -0x18(%rbp)
movb %al, -0x81(%rbp)
jbe 0x2ca7a
cmpl $0x0, -0x60(%rbp)
setne %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0x2ca89
jmp 0x2cbd0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x2caa6
movl -0x60(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x3c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x14(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jb 0x2cb10
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x2cae9
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x2cae9
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
jmp 0x2cb00
movq -0x50(%rbp), %rdi
movl -0x60(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
leaq -0x60(%rbp), %rcx
callq 0x2cc70
movl %eax, -0x34(%rbp)
movl -0x5c(%rbp), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
jmp 0x2cbc0
movl -0x18(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
addl -0x14(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jbe 0x2cb2a
movl -0x5c(%rbp), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rdi
movl -0x60(%rbp), %esi
movl -0x24(%rbp), %edx
xorl %ecx, %ecx
movl $0x2, %eax
cmpl $0x0, %edx
cmovel %eax, %ecx
leaq -0x70(%rbp), %rdx
callq 0xe5f0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x2cba4
movq -0x70(%rbp), %rdi
callq 0xec10
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x20a50
movl %eax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %eax
addl $0x4, %eax
movl %eax, %eax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movl -0x68(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x70(%rbp), %r8
callq 0x2cc00
movl %eax, -0x34(%rbp)
movq -0x70(%rbp), %rdi
callq 0xec30
movl $0x0, -0x14(%rbp)
movl -0x68(%rbp), %ecx
movl -0x18(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x18(%rbp)
movl -0x68(%rbp), %ecx
movq -0x20(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
jmp 0x2cbc2
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2ca51
jmp 0x2cbd2
cmpl $0x0, -0x34(%rbp)
jne 0x2cbed
cmpl $0x0, -0x18(%rbp)
jbe 0x2cbed
movl $0xd1c6, %edi           # imm = 0xD1C6
callq 0x24410
movl %eax, -0x4(%rbp)
jmp 0x2cbf3
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop