// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3PagerAcquire
SQLITE_PRIVATE int sqlite3PagerAcquire(
  Pager *pPager,      /* The pager open on the database file */
  Pgno pgno,          /* Page number to fetch */
  DbPage **ppPage,    /* Write a pointer to the page here */
  int flags           /* PAGER_GET_XXX flags */
){
  int rc = SQLITE_OK;
  PgHdr *pPg = 0;
  u32 iFrame = 0;                 /* Frame to read from WAL file */
  const int noContent = (flags & PAGER_GET_NOCONTENT);

  /* It is acceptable to use a read-only (mmap) page for any page except
  ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
  ** flag was specified by the caller. And so long as the db is not a 
  ** temporary or in-memory database.  */
  const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
   && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
#ifdef SQLITE_HAS_CODEC
   && pPager->xCodec==0
#endif
  );

  assert( pPager->eState>=PAGER_READER );
  assert( assert_pager_state(pPager) );
  assert( noContent==0 || bMmapOk==0 );

  if( pgno==0 ){
    return SQLITE_CORRUPT_BKPT;
  }

  /* If the pager is in the error state, return an error immediately. 
  ** Otherwise, request the page from the PCache layer. */
  if( pPager->errCode!=SQLITE_OK ){
    rc = pPager->errCode;
  }else{

    if( bMmapOk && pagerUseWal(pPager) ){
      rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
      if( rc!=SQLITE_OK ) goto pager_acquire_err;
    }

    if( iFrame==0 && bMmapOk ){
      void *pData = 0;

      rc = sqlite3OsFetch(pPager->fd, 
          (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
      );

      if( rc==SQLITE_OK && pData ){
        if( pPager->eState>PAGER_READER ){
          (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
        }
        if( pPg==0 ){
          rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
        }else{
          sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
        }
        if( pPg ){
          assert( rc==SQLITE_OK );
          *ppPage = pPg;
          return SQLITE_OK;
        }
      }
      if( rc!=SQLITE_OK ){
        goto pager_acquire_err;
      }
    }

    rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
  }

  if( rc!=SQLITE_OK ){
    /* Either the call to sqlite3PcacheFetch() returned an error or the
    ** pager was already in the error-state when this function was called.
    ** Set pPg to 0 and jump to the exception handler.  */
    pPg = 0;
    goto pager_acquire_err;
  }
  assert( (*ppPage)->pgno==pgno );
  assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );

  if( (*ppPage)->pPager && !noContent ){
    /* In this case the pcache already contains an initialized copy of
    ** the page. Return without further ado.  */
    assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
    pPager->aStat[PAGER_STAT_HIT]++;
    return SQLITE_OK;

  }else{
    /* The pager cache has created a new page. Its content needs to 
    ** be initialized.  */

    pPg = *ppPage;
    pPg->pPager = pPager;

    /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
    ** number greater than this, or the unused locking-page, is requested. */
    if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
      rc = SQLITE_CORRUPT_BKPT;
      goto pager_acquire_err;
    }

    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
      if( pgno>pPager->mxPgno ){
        rc = SQLITE_FULL;
        goto pager_acquire_err;
      }
      if( noContent ){
        /* Failure to set the bits in the InJournal bit-vectors is benign.
        ** It merely means that we might do some extra work to journal a 
        ** page that does not need to be journaled.  Nevertheless, be sure 
        ** to test the case where a malloc error occurs while trying to set 
        ** a bit in a bit vector.
        */
        sqlite3BeginBenignMalloc();
        if( pgno<=pPager->dbOrigSize ){
          TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
          testcase( rc==SQLITE_NOMEM );
        }
        TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
        testcase( rc==SQLITE_NOMEM );
        sqlite3EndBenignMalloc();
      }
      memset(pPg->pData, 0, pPager->pageSize);
      IOTRACE(("ZERO %p %d\n", pPager, pgno));
    }else{
      if( pagerUseWal(pPager) && bMmapOk==0 ){
        rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
        if( rc!=SQLITE_OK ) goto pager_acquire_err;
      }
      assert( pPg->pPager==pPager );
      pPager->aStat[PAGER_STAT_MISS]++;
      rc = readDbPage(pPg, iFrame);
      if( rc!=SQLITE_OK ){
        goto pager_acquire_err;
      }
    }
    pager_set_pagehash(pPg);
  }

  return SQLITE_OK;

pager_acquire_err:
  assert( rc!=SQLITE_OK );
  if( pPg ){
    sqlite3PcacheDrop(pPg);
  }
  pagerUnlockIfUnused(pPager);

  *ppPage = 0;
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r12d
movq %rdx, %r14
movl %esi, %r15d
movq %rdi, %rbx
movq $0x0, -0x40(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x1, %esi
jne 0xd927
movl 0x2c(%rbx), %r13d
testl %r13d, %r13d
jne 0xda60
xorl %ecx, %ecx
jmp 0xda3a
cmpb $0x0, 0x94(%rbx)
je 0xd943
cmpb $0x1, 0x13(%rbx)
sete %cl
movl %r12d, %eax
andl $0x2, %eax
shrl %eax
orb %cl, %al
jmp 0xd945
xorl %eax, %eax
testl %r15d, %r15d
je 0xda87
movl 0x2c(%rbx), %r13d
testl %r13d, %r13d
jne 0xda60
xorl %ecx, %ecx
testb %al, %al
je 0xda3a
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
je 0xd992
leaq -0x2c(%rbp), %rdx
movl %r15d, %esi
callq 0x1f2c3
testl %eax, %eax
jne 0xda5d
movl -0x2c(%rbp), %ecx
movb $0x1, %al
testl %ecx, %ecx
jne 0xda3c
leaq -0x48(%rbp), %rax
movq $0x0, (%rax)
movq %rax, %rcx
movq 0x48(%rbx), %rdi
leal -0x1(%r15), %eax
movslq 0xbc(%rbx), %rdx
movq %rdx, %rsi
movq %rax, -0x38(%rbp)
imulq %rax, %rsi
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %r13d
testl %eax, %eax
sete %al
leaq -0x48(%rbp), %rcx
movq (%rcx), %rdx
testq %rdx, %rdx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xdbcf
cmpb $0x2, 0x13(%rbx)
jb 0xdba9
movq 0x110(%rbx), %rdi
leaq -0x40(%rbp), %r13
movl %r15d, %esi
xorl %edx, %edx
movq %r13, %rcx
callq 0x1f4e2
movq (%r13), %rax
movq -0x48(%rbp), %rdx
testq %rax, %rax
je 0xdba9
movq 0x48(%rbx), %rdi
movslq 0xbc(%rbx), %rsi
imulq -0x38(%rbp), %rsi
movq %rax, %rbx
movq (%rdi), %rax
callq *0x90(%rax)
movq %rbx, %rax
jmp 0xdbc7
xorl %eax, %eax
movl %eax, -0x38(%rbp)
movl %ecx, -0x30(%rbp)
movq 0x110(%rbx), %rdi
movl %r15d, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x1f4e2
testl %eax, %eax
je 0xdaae
movl %eax, %r13d
cmpl $0x0, 0x98(%rbx)
jne 0xda7e
movq 0x110(%rbx), %rax
cmpl $0x0, 0x18(%rax)
jne 0xda7e
movq %rbx, %rdi
callq 0x1d0f7
movq $0x0, (%r14)
jmp 0xdac7
leaq 0x6bd86(%rip), %rsi     # 0x79814
leaq 0x6baa4(%rip), %rcx     # 0x79539
movl $0xb, %r13d
movl $0xb, %edi
movl $0xaa47, %edx           # imm = 0xAA47
xorl %eax, %eax
callq 0xb9c7
jmp 0xdac7
movq (%r14), %rcx
testb $0x1, %r12b
jne 0xdad9
cmpq $0x0, 0x20(%rcx)
je 0xdad9
incl 0xf0(%rbx)
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x20(%rcx)
testl %r15d, %r15d
js 0xdaf6
movl 0x86d58(%rip), %eax     # 0x94840
cltd
idivl 0xbc(%rbx)
incl %eax
cmpl %r15d, %eax
jne 0xdb2e
leaq 0x6bd17(%rip), %rsi     # 0x79814
movq %rcx, %r15
leaq 0x6ba32(%rip), %rcx     # 0x79539
movl $0xb, %r13d
movl $0xb, %edi
movl $0xaa8e, %edx           # imm = 0xAA8E
xorl %eax, %eax
callq 0xb9c7
movq %r15, %rcx
movq %rcx, %rdi
callq 0x1f27c
jmp 0xda60
cmpb $0x0, 0x12(%rbx)
je 0xdbe1
movl $0xd, %r13d
cmpl %r15d, 0xc0(%rbx)
jb 0xdb21
testb $0x1, %r12b
je 0xdb8f
movq %rcx, %r14
movq 0x88409(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0xdb5e
callq *%rax
cmpl %r15d, 0x20(%rbx)
jb 0xdb70
movq 0x40(%rbx), %rdi
movl %r15d, %esi
callq 0x1b472
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1b14d
movq 0x883e6(%rip), %rax     # 0x95f68
testq %rax, %rax
movq %r14, %rcx
je 0xdb8f
callq *%rax
movq %r14, %rcx
movq 0x8(%rcx), %rdi
movslq 0xbc(%rbx), %rdx
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x8200
jmp 0xdac7
leaq -0x40(%rbp), %rcx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x206cb
movl %eax, %r13d
leaq -0x40(%rbp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0xdbcf
movq %rax, (%r14)
jmp 0xdac4
testl %r13d, %r13d
jne 0xda60
movb $0x1, %al
xorl %ecx, %ecx
jmp 0xda3c
testb $0x1, %r12b
jne 0xdb38
cmpl %r15d, 0x1c(%rbx)
jb 0xdb38
movq 0x48(%rbx), %rax
cmpq $0x0, (%rax)
je 0xdb38
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
sete %al
movl -0x38(%rbp), %edx
orb %al, %dl
jne 0xdc3e
movq %rcx, %r12
leaq -0x2c(%rbp), %rdx
movl %r15d, %esi
callq 0x1f2c3
testl %eax, %eax
je 0xdc35
movl %eax, %r13d
movq %r12, %rcx
jmp 0xdb21
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq %r12, %rcx
incl 0xf4(%rbx)
movq %rcx, %r15
movq %rcx, %rdi
movl -0x30(%rbp), %esi
callq 0x1f42a
testl %eax, %eax
je 0xdac4
movl %eax, %r13d
jmp 0xdb1e