// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rdx, %rcx
movl %esi, %r15d
movq %rdi, %rbx
movq $0x0, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x1, %esi
je 0xd853
cmpb $0x0, 0x94(%rbx)
je 0xd853
cmpb $0x1, 0x13(%rbx)
sete %al
movl %r14d, %r12d
andl $0x2, %r12d
shrl %r12d
orb %al, %r12b
jmp 0xd856
xorl %r12d, %r12d
testl %r15d, %r15d
je 0xd899
movl 0x2c(%rbx), %r13d
testl %r13d, %r13d
movq %rcx, -0x30(%rbp)
jne 0xd9be
testb %r12b, %r12b
je 0xd8c3
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
je 0xd8c3
leaq -0x34(%rbp), %rdx
movl %r15d, %esi
callq 0x1f499
movq -0x30(%rbp), %rcx
testl %eax, %eax
je 0xd8c3
movl %eax, %r13d
jmp 0xd9cb
leaq 0x6bf74(%rip), %rsi     # 0x79814
leaq 0x6bc92(%rip), %rcx     # 0x79539
movl $0xb, %r13d
movl $0xb, %edi
movl $0xaa47, %edx           # imm = 0xAA47
xorl %eax, %eax
callq 0xb8cc
jmp 0xda21
cmpl $0x0, -0x34(%rbp)
sete %al
andb %r12b, %al
cmpb $0x1, %al
jne 0xd99f
leaq -0x48(%rbp), %rcx
movq $0x0, (%rcx)
movq 0x48(%rbx), %rdi
leal -0x1(%r15), %eax
movslq 0xbc(%rbx), %rdx
movq %rdx, %rsi
movq %rax, -0x50(%rbp)
imulq %rax, %rsi
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %r13d
testl %eax, %eax
jne 0xd959
cmpq $0x0, -0x48(%rbp)
movq -0x30(%rbp), %rcx
je 0xd98c
cmpb $0x2, 0x13(%rbx)
jb 0xd930
movq 0x110(%rbx), %rdi
leaq -0x40(%rbp), %rcx
movl %r15d, %esi
xorl %edx, %edx
callq 0x1f6fc
cmpq $0x0, -0x40(%rbp)
je 0xd95f
movq 0x48(%rbx), %rdi
movslq 0xbc(%rbx), %rsi
imulq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq (%rdi), %rax
callq *0x90(%rax)
xorl %r13d, %r13d
jmp 0xd975
movq -0x30(%rbp), %rcx
jmp 0xd98c
movq -0x48(%rbp), %rdx
leaq -0x40(%rbp), %rcx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x209cb
movl %eax, %r13d
movq -0x40(%rbp), %rax
testq %rax, %rax
movq -0x30(%rbp), %rcx
je 0xd98c
movq %rax, (%rcx)
movl $0x1, %eax
jmp 0xd996
xorl %eax, %eax
testl %r13d, %r13d
setne %al
addl %eax, %eax
cmpl $0x2, %eax
je 0xd9cb
testl %eax, %eax
jne 0xda1e
movq 0x110(%rbx), %rdi
movl %r15d, %esi
movl $0x1, %edx
movq -0x30(%rbp), %rcx
callq 0x1f6fc
movq -0x30(%rbp), %rcx
movl %eax, %r13d
testl %r13d, %r13d
je 0xda08
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xd9dd
callq 0x1f452
movq -0x30(%rbp), %rcx
cmpl $0x0, 0x98(%rbx)
jne 0xd9ff
movq 0x110(%rbx), %rax
cmpl $0x0, 0x18(%rax)
jne 0xd9ff
movq %rbx, %rdi
callq 0x1d2c0
movq -0x30(%rbp), %rcx
movq $0x0, (%rcx)
jmp 0xda21
movq (%rcx), %r8
testb $0x1, %r14b
jne 0xda33
cmpq $0x0, 0x20(%r8)
je 0xda33
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 %r8, -0x40(%rbp)
movq %rbx, 0x20(%r8)
testl %r15d, %r15d
js 0xda54
movl 0x86dfa(%rip), %eax     # 0x94840
cltd
idivl 0xbc(%rbx)
incl %eax
cmpl %r15d, %eax
jne 0xda82
leaq 0x6bdb9(%rip), %rsi     # 0x79814
leaq 0x6bad7(%rip), %rcx     # 0x79539
movl $0xb, %r13d
movl $0xb, %edi
movl $0xaa8e, %edx           # imm = 0xAA8E
xorl %eax, %eax
callq 0xb8cc
movq -0x30(%rbp), %rcx
jmp 0xd9cb
cmpb $0x0, 0x12(%rbx)
je 0xdafd
movl $0xd, %r13d
cmpl %r15d, 0xc0(%rbx)
jb 0xd9cb
testb $0x1, %r14b
je 0xdae3
movq %r8, %r14
movq 0x884b5(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0xdab2
callq *%rax
cmpl %r15d, 0x20(%rbx)
jb 0xdac4
movq 0x40(%rbx), %rdi
movl %r15d, %esi
callq 0x1b642
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1b35e
movq 0x88492(%rip), %rax     # 0x95f68
testq %rax, %rax
movq %r14, %r8
je 0xdae3
callq *%rax
movq %r14, %r8
movq 0x8(%r8), %rdi
movslq 0xbc(%rbx), %rdx
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x8200
jmp 0xda21
testb $0x1, %r14b
jne 0xda88
cmpl %r15d, 0x1c(%rbx)
jb 0xda88
movq 0x48(%rbx), %rax
cmpq $0x0, (%rax)
je 0xda88
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
sete %al
orb %r12b, %al
testb $0x1, %al
jne 0xdb45
leaq -0x34(%rbp), %rdx
movl %r15d, %esi
movq %r8, %r14
callq 0x1f499
movq %r14, %r8
testl %eax, %eax
jne 0xdb5e
incl 0xf4(%rbx)
movl -0x34(%rbp), %esi
movq %r8, %rdi
callq 0x1f644
testl %eax, %eax
je 0xda1e
movl %eax, %r13d
jmp 0xda79