// File: /workspace/github_c_makefile_star2_O2/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, %r13d
movl %esi, %ebx
movq %rdi, %r14
movl 0x2c(%rdi), %r15d
testl %r15d, %r15d
je 0x5b29a
movl %ebx, %eax
xorl %edx, %edx
divl %r15d
movl %edx, %r12d
shlq $0x3, %r12
addq 0x30(%r14), %r12
movq (%r12), %r12
testq %r12, %r12
je 0x5b29a
cmpl %ebx, 0x10(%r12)
je 0x5b380
addq $0x18, %r12
jmp 0x5b280
testl %r13d, %r13d
je 0x5b37d
movq (%r14), %rcx
movl 0x28(%r14), %r12d
cmpl $0x1, %r13d
jne 0x5b2e2
movl %r12d, %eax
subl 0x24(%r14), %eax
cmpl 0x10(%rcx), %eax
jae 0x5b4ea
cmpl 0x1c(%r14), %eax
jae 0x5b4ea
movq %r14, %rdi
movq %rcx, -0x38(%rbp)
callq 0x5b737
movq -0x38(%rbp), %rcx
testl %eax, %eax
jne 0x5b4ea
cmpl %r15d, %r12d
jb 0x5b3c0
movq %rcx, -0x38(%rbp)
movl %r13d, -0x2c(%rbp)
leal (%r15,%r15), %eax
cmpl $0x101, %eax            # imm = 0x101
movl $0x100, %r13d           # imm = 0x100
cmovael %eax, %r13d
testl %r15d, %r15d
je 0x5b319
movq 0x2dc4e(%rip), %rax     # 0x88f60
testq %rax, %rax
je 0x5b319
callq *%rax
leal (,%r13,8), %edi
callq 0xbeb2
movq %rax, %r12
cmpl $0x0, 0x2c(%r14)
je 0x5b33e
movq 0x2dc31(%rip), %rax     # 0x88f68
testq %rax, %rax
je 0x5b33e
callq *%rax
testq %r12, %r12
je 0x5b3ad
movl 0x2c(%r14), %ecx
xorl %esi, %esi
movq 0x30(%r14), %rdi
cmpq %rcx, %rsi
je 0x5b396
movq (%rdi,%rsi,8), %rdi
testq %rdi, %rdi
je 0x5b378
movl 0x10(%rdi), %eax
xorl %edx, %edx
divl %r13d
movq 0x18(%rdi), %rax
movq (%r12,%rdx,8), %r8
movq %r8, 0x18(%rdi)
movq %rdi, (%r12,%rdx,8)
movq %rax, %rdi
jmp 0x5b356
incq %rsi
jmp 0x5b349
xorl %r12d, %r12d
movq %r12, %rdi
callq 0x5b677
testq %r12, %r12
jne 0x5b4de
jmp 0x5b4ea
callq 0x9c7b
movq %r12, 0x30(%r14)
movl %r13d, 0x2c(%r14)
movl -0x2c(%rbp), %r13d
movq -0x38(%rbp), %rcx
jmp 0x5b3c0
cmpq $0x0, 0x30(%r14)
movl -0x2c(%rbp), %r13d
movq -0x38(%rbp), %rcx
je 0x5b4ea
cmpl $0x0, 0x10(%r14)
je 0x5b435
movq 0x20(%rcx), %r12
testq %r12, %r12
je 0x5b435
movl 0x28(%r14), %eax
incl %eax
cmpl 0x18(%r14), %eax
jae 0x5b3f6
movl 0x14(%rcx), %eax
cmpl 0x8(%rcx), %eax
jae 0x5b3f6
movq %r14, %rdi
movq %rcx, %r15
callq 0x5b737
movq %r15, %rcx
testl %eax, %eax
je 0x5b435
movq %rcx, %r15
movq %r12, %rdi
callq 0x5b6d8
movq %r12, %rdi
callq 0x5b677
movq 0x20(%r12), %rax
movl 0xc(%rax), %ecx
addl 0x8(%rax), %ecx
movl 0xc(%r14), %edx
addl 0x8(%r14), %edx
cmpl %edx, %ecx
jne 0x5b42d
movl 0x10(%r14), %ecx
subl 0x10(%rax), %ecx
addl %ecx, 0x14(%r15)
jmp 0x5b4a1
movq %r12, %rdi
callq 0x5b70a
cmpl $0x1, %r13d
jne 0x5b449
movq 0x2db1e(%rip), %rax     # 0x88f60
testq %rax, %rax
je 0x5b449
callq *%rax
movl 0x8(%r14), %eax
movl 0xc(%r14), %ecx
leal (%rax,%rcx), %edi
addl $0x38, %edi
callq 0x1a6eb
testq %rax, %rax
je 0x5b485
movslq 0x8(%r14), %r12
addq %rax, %r12
movq %rax, (%r12)
leaq 0x38(%r12), %rax
movq %rax, 0x8(%r12)
cmpl $0x0, 0x10(%r14)
je 0x5b488
movq (%r14), %rax
incl 0x14(%rax)
jmp 0x5b488
xorl %r12d, %r12d
cmpl $0x1, %r13d
jne 0x5b49c
movq 0x2dad3(%rip), %rax     # 0x88f68
testq %rax, %rax
je 0x5b49c
callq *%rax
testq %r12, %r12
je 0x5b4ea
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
andq $0x0, (%rax)
movq 0x30(%r14), %rax
movq %r12, (%rax,%rdx,8)
cmpl %ebx, 0x20(%r14)
jae 0x5b4ed
movl %ebx, 0x20(%r14)
jmp 0x5b4ed
xorl %r12d, %r12d
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq