// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3WalFindFrame
SQLITE_PRIVATE int sqlite3WalFindFrame(
  Wal *pWal,                      /* WAL handle */
  Pgno pgno,                      /* Database page number to read data for */
  u32 *piRead                     /* OUT: Frame number (or zero) */
){
  u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
  u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
  int iHash;                      /* Used to loop through N hash tables */

  /* This routine is only be called from within a read transaction. */
  assert( pWal->readLock>=0 || pWal->lockError );

  /* If the "last page" field of the wal-index header snapshot is 0, then
  ** no data will be read from the wal under any circumstances. Return early
  ** in this case as an optimization.  Likewise, if pWal->readLock==0, 
  ** then the WAL is ignored by the reader so return early, as if the 
  ** WAL were empty.
  */
  if( iLast==0 || pWal->readLock==0 ){
    *piRead = 0;
    return SQLITE_OK;
  }

  /* Search the hash table or tables for an entry matching page number
  ** pgno. Each iteration of the following for() loop searches one
  ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
  **
  ** This code might run concurrently to the code in walIndexAppend()
  ** that adds entries to the wal-index (and possibly to this hash 
  ** table). This means the value just read from the hash 
  ** slot (aHash[iKey]) may have been added before or after the 
  ** current read transaction was opened. Values added after the
  ** read transaction was opened may have been written incorrectly -
  ** i.e. these slots may contain garbage data. However, we assume
  ** that any slots written before the current read transaction was
  ** opened remain unmodified.
  **
  ** For the reasons above, the if(...) condition featured in the inner
  ** loop of the following block is more stringent that would be required 
  ** if we had exclusive access to the hash-table:
  **
  **   (aPgno[iFrame]==pgno): 
  **     This condition filters out normal hash-table collisions.
  **
  **   (iFrame<=iLast): 
  **     This condition filters out entries that were added to the hash
  **     table after the current read-transaction had started.
  */
  for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
    volatile ht_slot *aHash;      /* Pointer to hash table */
    volatile u32 *aPgno;          /* Pointer to array of page numbers */
    u32 iZero;                    /* Frame number corresponding to aPgno[0] */
    int iKey;                     /* Hash slot index */
    int nCollide;                 /* Number of hash collisions remaining */
    int rc;                       /* Error code */

    rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
    if( rc!=SQLITE_OK ){
      return rc;
    }
    nCollide = HASHTABLE_NSLOT;
    for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
      u32 iFrame = aHash[iKey] + iZero;
      if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
        /* assert( iFrame>iRead ); -- not true if there is corruption */
        iRead = iFrame;
      }
      if( (nCollide--)==0 ){
        return SQLITE_CORRUPT_BKPT;
      }
    }
  }

#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
  /* If expensive assert() statements are available, do a linear search
  ** of the wal-index file content. Make sure the results agree with the
  ** result obtained using the hash indexes above.  */
  {
    u32 iRead2 = 0;
    u32 iTest;
    for(iTest=iLast; iTest>0; iTest--){
      if( walFramePgno(pWal, iTest)==pgno ){
        iRead2 = iTest;
        break;
      }
    }
    assert( iRead==iRead2 );
  }
#endif

  *piRead = iRead;
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x58(%rdi), %ebx
testl %ebx, %ebx
je 0x1f3e7
movq %rdi, %r12
cmpw $0x0, 0x3c(%rdi)
je 0x1f3e7
movq %rdx, -0x38(%rbp)
leal 0x1000(%rbx), %eax
movabsq $0xffffffff021, %r13    # imm = 0xFFFFFFFF021
addq %rax, %r13
shrq $0xc, %r13
movl $0x0, %eax
testl %r13d, %r13d
js 0x1f3df
movl %esi, %r15d
imull $0x17f, %esi, %eax      # imm = 0x17F
andl $0x1fff, %eax           # imm = 0x1FFF
movq %rax, -0x30(%rbp)
leaq -0x40(%rbp), %rdx
movq %r12, %rdi
movl %r13d, %esi
movq %rdx, %r14
callq 0x1bf63
testl %eax, %eax
jne 0x1f426
movq -0x40(%rbp), %rcx
movl %r13d, %esi
shll $0xc, %esi
addl $-0x22, %esi
leaq 0x88(%rcx), %rdi
xorl %eax, %eax
movl %r13d, %edx
subl $0x1, %edx
cmovaeq %rcx, %rdi
cmovbl %eax, %esi
movq -0x30(%rbp), %r8
cmpw $0x0, 0x4000(%rcx,%r8,2)
je 0x1f3cc
addq $-0x4, %rdi
movq -0x30(%rbp), %r9
leaq (%rcx,%r9,2), %r10
addq $0x4000, %r10           # imm = 0x4000
addq $0x4000, %rcx           # imm = 0x4000
xorl %eax, %eax
movl $0xffffdfff, %r8d       # imm = 0xFFFFDFFF
movzwl (%r10), %r11d
addl %esi, %r11d
cmpl %ebx, %r11d
ja 0x1f3b2
movzwl (%r10), %r10d
movl (%rdi,%r10,4), %r10d
cmpl %r15d, %r10d
cmovel %r11d, %eax
incl %r8d
je 0x1f3f1
incl %r9d
andl $0x1fff, %r9d           # imm = 0x1FFF
leaq (%rcx,%r9,2), %r10
cmpw $0x0, (%r10)
jne 0x1f397
testl %r13d, %r13d
jle 0x1f3df
movl %edx, %r13d
testl %eax, %eax
movq %r14, %rdx
je 0x1f32c
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x1f3ed
movl $0x0, (%rdx)
xorl %ebx, %ebx
jmp 0x1f415
leaq 0x5a41c(%rip), %rsi     # 0x79814
leaq 0x5a13a(%rip), %rcx     # 0x79539
movl $0xb, %ebx
movl $0xb, %edi
movl $0xbb13, %edx           # imm = 0xBB13
xorl %eax, %eax
callq 0xb9c7
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebx
jmp 0x1f415