// File: /workspace/github_c_makefile_star2_O3/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 0x672aa
movl %ebx, %eax
xorl %edx, %edx
divl %ecx
movq 0x30(%r14), %rax
movq (%rax,%rdx,8), %r12
testq %r12, %r12
je 0x672aa
cmpl %ebx, 0x10(%r12)
je 0x67307
movq 0x18(%r12), %r12
jmp 0x67297
testl %r15d, %r15d
je 0x672f8
movq (%r14), %r13
movl 0x28(%r14), %eax
cmpl $0x1, %r15d
jne 0x67324
movl %eax, %edx
subl 0x24(%r14), %edx
cmpl 0x10(%r13), %edx
jae 0x67578
cmpl 0x1c(%r14), %edx
jae 0x67578
cmpl $0x0, 0x2ecc3(%rip)     # 0x95fa0
je 0x672ef
movl 0xc(%r14), %edx
addl 0x8(%r14), %edx
cmpl 0x2ecaf(%rip), %edx     # 0x95f9c
jle 0x67314
leaq 0x2ea76(%rip), %rdx     # 0x95d6c
jmp 0x6731b
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x67758
jmp 0x6757b
movq %r12, %rdi
callq 0x67758
jmp 0x6756c
leaq 0x2ecb1(%rip), %rdx     # 0x95fcc
cmpl $0x0, (%rdx)
jne 0x67578
cmpl %ecx, %eax
jb 0x67401
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 0x67358
movq 0x2ec0f(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0x67358
callq *%rax
leal (,%r15,8), %r13d
movl %r13d, %edi
callq 0xa38c
movq %rax, %r12
testq %rax, %rax
je 0x6737d
movslq %r13d, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x8200
cmpl $0x0, 0x2c(%r14)
je 0x67392
movq 0x2ebdd(%rip), %rax     # 0x95f68
testq %rax, %rax
je 0x67392
callq *%rax
testq %r12, %r12
movq -0x38(%rbp), %r13
je 0x673f2
movl 0x2c(%r14), %ecx
testq %rcx, %rcx
je 0x673db
xorl %esi, %esi
movq 0x30(%r14), %rax
movq (%rax,%rsi,8), %rdi
testq %rdi, %rdi
je 0x673d3
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 0x673b3
incq %rsi
cmpq %rcx, %rsi
jne 0x673a6
movq 0x30(%r14), %rdi
callq 0xa3c7
movq %r12, 0x30(%r14)
movl %r15d, 0x2c(%r14)
movl -0x2c(%rbp), %r15d
jmp 0x67401
cmpq $0x0, 0x30(%r14)
movl -0x2c(%rbp), %r15d
je 0x67578
cmpl $0x0, 0x10(%r14)
je 0x674c0
movq 0x20(%r13), %r12
testq %r12, %r12
je 0x674c0
movl 0x28(%r14), %eax
incl %eax
cmpl 0x18(%r14), %eax
jae 0x6745d
movl 0x14(%r13), %eax
cmpl 0x8(%r13), %eax
jae 0x6745d
cmpl $0x0, 0x2eb6a(%rip)     # 0x95fa0
je 0x67448
movl 0xc(%r14), %eax
addl 0x8(%r14), %eax
cmpl 0x2eb56(%rip), %eax     # 0x95f9c
jle 0x67451
leaq 0x2e91d(%rip), %rax     # 0x95d6c
jmp 0x67458
leaq 0x2eb74(%rip), %rax     # 0x95fcc
cmpl $0x0, (%rax)
je 0x674c0
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 0x67474
movq (%rdx), %rdx
movq %rdx, (%rax)
decl 0x28(%rcx)
movq %r12, %rdi
callq 0x67758
movq 0x20(%r12), %rax
movl 0xc(%rax), %ecx
addl 0x8(%rax), %ecx
movl 0xc(%r14), %edx
addl 0x8(%r14), %edx
cmpl %edx, %ecx
jne 0x674b8
movl 0x10(%r14), %ecx
subl 0x10(%rax), %ecx
addl %ecx, 0x14(%r13)
jmp 0x6752c
movq %r12, %rdi
callq 0x677b9
cmpl $0x1, %r15d
jne 0x674d4
movq 0x2ea93(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0x674d4
callq *%rax
movl 0x8(%r14), %eax
movl 0xc(%r14), %ecx
leal (%rax,%rcx), %edi
addl $0x38, %edi
callq 0x1cc98
testq %rax, %rax
je 0x67510
movslq 0x8(%r14), %r12
addq %rax, %r12
movq %rax, (%r12)
leaq 0x38(%r12), %rax
movq %rax, 0x8(%r12)
cmpl $0x0, 0x10(%r14)
je 0x67513
movq (%r14), %rax
incl 0x14(%rax)
jmp 0x67513
xorl %r12d, %r12d
cmpl $0x1, %r15d
jne 0x67527
movq 0x2ea48(%rip), %rax     # 0x95f68
testq %rax, %rax
je 0x67527
callq *%rax
testq %r12, %r12
je 0x67578
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)
cmpl %ebx, 0x20(%r14)
jae 0x6757b
movl %ebx, 0x20(%r14)
jmp 0x6757b
xorl %r12d, %r12d
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq