// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: pcache1Fetch
static sqlite3_pcache_page *pcache1Fetch(
  sqlite3_pcache *p, 
  unsigned int iKey, 
  int createFlag
){
  unsigned int nPinned;
  PCache1 *pCache = (PCache1 *)p;
  PGroup *pGroup;
  PgHdr1 *pPage = 0;

  assert( pCache->bPurgeable || createFlag!=1 );
  assert( pCache->bPurgeable || pCache->nMin==0 );
  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
  assert( pCache->nMin==0 || pCache->bPurgeable );
  pcache1EnterMutex(pGroup = pCache->pGroup);

  /* Step 1: Search the hash table for an existing entry. */
  if( pCache->nHash>0 ){
    unsigned int h = iKey % pCache->nHash;
    for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
  }

  /* Step 2: Abort if no existing page is found and createFlag is 0 */
  if( pPage || createFlag==0 ){
    pcache1PinPage(pPage);
    goto fetch_out;
  }

  /* The pGroup local variable will normally be initialized by the
  ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
  ** then pcache1EnterMutex() is a no-op, so we have to initialize the
  ** local variable here.  Delaying the initialization of pGroup is an
  ** optimization:  The common case is to exit the module before reaching
  ** this point.
  */
#ifdef SQLITE_MUTEX_OMIT
  pGroup = pCache->pGroup;
#endif

  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
  assert( pCache->nPage >= pCache->nRecyclable );
  nPinned = pCache->nPage - pCache->nRecyclable;
  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
  assert( pCache->n90pct == pCache->nMax*9/10 );
  if( createFlag==1 && (
        nPinned>=pGroup->mxPinned
     || nPinned>=pCache->n90pct
     || pcache1UnderMemoryPressure(pCache)
  )){
    goto fetch_out;
  }

  if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
    goto fetch_out;
  }
  assert( pCache->nHash>0 && pCache->apHash );

  /* Step 4. Try to recycle a page. */
  if( pCache->bPurgeable && pGroup->pLruTail && (
         (pCache->nPage+1>=pCache->nMax)
      || pGroup->nCurrentPage>=pGroup->nMaxPage
      || pcache1UnderMemoryPressure(pCache)
  )){
    PCache1 *pOther;
    pPage = pGroup->pLruTail;
    pcache1RemoveFromHash(pPage);
    pcache1PinPage(pPage);
    pOther = pPage->pCache;

    /* We want to verify that szPage and szExtra are the same for pOther
    ** and pCache.  Assert that we can verify this by comparing sums. */
    assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
    assert( pCache->szExtra<512 );
    assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
    assert( pOther->szExtra<512 );

    if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
      pcache1FreePage(pPage);
      pPage = 0;
    }else{
      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
    }
  }

  /* Step 5. If a usable page buffer has still not been found, 
  ** attempt to allocate a new one. 
  */
  if( !pPage ){
    if( createFlag==1 ) sqlite3BeginBenignMalloc();
    pPage = pcache1AllocPage(pCache);
    if( createFlag==1 ) sqlite3EndBenignMalloc();
  }

  if( pPage ){
    unsigned int h = iKey % pCache->nHash;
    pCache->nPage++;
    pPage->iKey = iKey;
    pPage->pNext = pCache->apHash[h];
    pPage->pCache = pCache;
    pPage->pLruPrev = 0;
    pPage->pLruNext = 0;
    *(void **)pPage->page.pExtra = 0;
    pCache->apHash[h] = pPage;
  }

fetch_out:
  if( pPage && iKey>pCache->iMaxKey ){
    pCache->iMaxKey = iKey;
  }
  pcache1LeaveMutex(pGroup);
  return &pPage->page;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r15d
movl %esi, %ebx
movq %rdi, %r14
movl 0x2c(%rdi), %ecx
testl %ecx, %ecx
je 0x674a9
movl %ebx, %eax
xorl %edx, %edx
divl %ecx
movq 0x30(%r14), %rax
movq (%rax,%rdx,8), %r12
testq %r12, %r12
je 0x674ac
cmpl %ebx, 0x10(%r12)
je 0x674ac
movq 0x18(%r12), %r12
jmp 0x67496
xorl %r12d, %r12d
testl %r15d, %r15d
je 0x674ff
testq %r12, %r12
jne 0x674ff
movq (%r14), %r13
movl 0x28(%r14), %eax
cmpl $0x1, %r15d
jne 0x6751c
movl %eax, %edx
subl 0x24(%r14), %edx
cmpl 0x10(%r13), %edx
jae 0x67768
cmpl 0x1c(%r14), %edx
jae 0x67768
cmpl $0x0, 0x2eabc(%rip)     # 0x95fa0
je 0x674f6
movl 0xc(%r14), %edx
addl 0x8(%r14), %edx
cmpl 0x2eaa8(%rip), %edx     # 0x95f9c
jle 0x6750c
leaq 0x2e86f(%rip), %rdx     # 0x95d6c
jmp 0x67513
movq %r12, %rdi
callq 0x67957
jmp 0x6776b
leaq 0x2eab9(%rip), %rdx     # 0x95fcc
cmpl $0x0, (%rdx)
jne 0x67768
cmpl %ecx, %eax
jb 0x675f3
movq %r13, -0x38(%rbp)
movl %r15d, -0x2c(%rbp)
leal (%rcx,%rcx), %eax
cmpl $0x101, %eax            # imm = 0x101
movl $0x100, %r15d           # imm = 0x100
cmovael %eax, %r15d
testl %ecx, %ecx
je 0x67550
movq 0x2ea17(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0x67550
callq *%rax
leal (,%r15,8), %r13d
movl %r13d, %edi
callq 0xa2a2
movq %rax, %r12
testq %rax, %rax
je 0x67575
movslq %r13d, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x8200
cmpl $0x0, 0x2c(%r14)
je 0x6758a
movq 0x2e9e5(%rip), %rax     # 0x95f68
testq %rax, %rax
je 0x6758a
callq *%rax
testq %r12, %r12
movq -0x38(%rbp), %r13
je 0x675e4
movl 0x2c(%r14), %ecx
testq %rcx, %rcx
je 0x675d3
xorl %esi, %esi
movq 0x30(%r14), %rax
movq (%rax,%rsi,8), %rdi
testq %rdi, %rdi
je 0x675cb
movl 0x10(%rdi), %eax
xorl %edx, %edx
divl %r15d
movq 0x18(%rdi), %rax
movq (%r12,%rdx,8), %r8
movq %r8, 0x18(%rdi)
movq %rdi, (%r12,%rdx,8)
movq %rax, %rdi
testq %rax, %rax
jne 0x675ab
incq %rsi
cmpq %rcx, %rsi
jne 0x6759e
movq 0x30(%r14), %rdi
callq 0xa2e8
movq %r12, 0x30(%r14)
movl %r15d, 0x2c(%r14)
cmpq $0x0, 0x30(%r14)
movl -0x2c(%rbp), %r15d
je 0x67768
cmpl $0x0, 0x10(%r14)
je 0x676b2
movq 0x20(%r13), %r12
testq %r12, %r12
je 0x676b2
movl 0x28(%r14), %eax
incl %eax
cmpl 0x18(%r14), %eax
jae 0x6764f
movl 0x14(%r13), %eax
cmpl 0x8(%r13), %eax
jae 0x6764f
cmpl $0x0, 0x2e978(%rip)     # 0x95fa0
je 0x6763a
movl 0xc(%r14), %eax
addl 0x8(%r14), %eax
cmpl 0x2e964(%rip), %eax     # 0x95f9c
jle 0x67643
leaq 0x2e72b(%rip), %rax     # 0x95d6c
jmp 0x6764a
leaq 0x2e982(%rip), %rax     # 0x95fcc
cmpl $0x0, (%rax)
je 0x676b2
movq 0x20(%r12), %rcx
movl 0x10(%r12), %eax
xorl %edx, %edx
divl 0x2c(%rcx)
shlq $0x3, %rdx
addq 0x30(%rcx), %rdx
movq %rdx, %rax
movq (%rdx), %rsi
leaq 0x18(%rsi), %rdx
cmpq %r12, %rsi
jne 0x67666
movq (%rdx), %rdx
movq %rdx, (%rax)
decl 0x28(%rcx)
movq %r12, %rdi
callq 0x67957
movq 0x20(%r12), %rax
movl 0xc(%rax), %ecx
addl 0x8(%rax), %ecx
movl 0xc(%r14), %edx
addl 0x8(%r14), %edx
cmpl %edx, %ecx
jne 0x676aa
movl 0x10(%r14), %ecx
subl 0x10(%rax), %ecx
addl %ecx, 0x14(%r13)
jmp 0x676b5
movq %r12, %rdi
callq 0x679b8
xorl %r12d, %r12d
testq %r12, %r12
jne 0x67721
cmpl $0x1, %r15d
jne 0x676ce
movq 0x2e899(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0x676ce
callq *%rax
movl 0x8(%r14), %eax
movl 0xc(%r14), %ecx
leal (%rax,%rcx), %edi
addl $0x38, %edi
callq 0x1ce52
testq %rax, %rax
je 0x6770a
movslq 0x8(%r14), %r12
addq %rax, %r12
movq %rax, (%r12)
leaq 0x38(%r12), %rax
movq %rax, 0x8(%r12)
cmpl $0x0, 0x10(%r14)
je 0x6770d
movq (%r14), %rax
incl 0x14(%rax)
jmp 0x6770d
xorl %r12d, %r12d
cmpl $0x1, %r15d
jne 0x67721
movq 0x2e84e(%rip), %rax     # 0x95f68
testq %rax, %rax
je 0x67721
callq *%rax
testq %r12, %r12
je 0x67768
movl %ebx, %eax
xorl %edx, %edx
divl 0x2c(%r14)
incl 0x28(%r14)
movl %ebx, 0x10(%r12)
movq 0x30(%r14), %rax
movq (%rax,%rdx,8), %rax
movq %rax, 0x18(%r12)
movq %r14, 0x20(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r12)
movq 0x8(%r12), %rax
movq $0x0, (%rax)
movq 0x30(%r14), %rax
movq %r12, (%rax,%rdx,8)
jmp 0x6776b
xorl %r12d, %r12d
testq %r12, %r12
je 0x6777a
cmpl %ebx, 0x20(%r14)
jae 0x6777a
movl %ebx, 0x20(%r14)
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq