// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/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 */
  int iMinHash;

  /* 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 && pWal->bShmUnreliable==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.
  */
  iMinHash = walFramePage(pWal->minFrame);
  for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
    WalHashLoc sLoc;              /* Hash table location */
    int iKey;                     /* Hash slot index */
    int nCollide;                 /* Number of hash collisions remaining */
    int rc;                       /* Error code */
    u32 iH;

    rc = walHashGet(pWal, iHash, &sLoc);
    if( rc!=SQLITE_OK ){
      return rc;
    }
    nCollide = HASHTABLE_NSLOT;
    iKey = walHash(pgno);
    while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){
      u32 iFrame = iH + sLoc.iZero;
      if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){
        assert( iFrame>iRead || CORRUPT_DB );
        iRead = iFrame;
      }
      if( (nCollide--)==0 ){
        return SQLITE_CORRUPT_BKPT;
      }
      iKey = walNextHash(iKey);
    }
    if( iRead ) break;
  }

#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;
    assert( pWal->bShmUnreliable || pWal->minFrame>0 );
    for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
      if( walFramePgno(pWal, iTest)==pgno ){
        iRead2 = iTest;
        break;
      }
    }
    assert( iRead==iRead2 );
  }
#endif

  *piRead = iRead;
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl 0x58(%rdi), %r13d
testl %r13d, %r13d
je 0x28723a
movl %esi, %ebp
movq %rdi, %r14
cmpw $0x0, 0x3c(%rdi)
jne 0x287201
cmpb $0x0, 0x46(%r14)
je 0x28723a
movl $0x1000, %ebx           # imm = 0x1000
leal (%rbx,%r13), %r12d
addl 0x78(%r14), %ebx
movabsq $0xffffffff021, %rax    # imm = 0xFFFFFFFF021
addq %rax, %rbx
shrq $0xc, %rbx
addq %rax, %r12
shrq $0xc, %r12
xorl %r15d, %r15d
movl $0x0, %eax
cmpl %ebx, %r12d
jge 0x287248
movl %eax, (%rdx)
jmp 0x28732a
movl $0x0, (%rdx)
xorl %r15d, %r15d
jmp 0x28732a
movq %rdx, (%rsp)
imull $0x17f, %ebp, %eax      # imm = 0x17F
andl $0x1fff, %eax           # imm = 0x1FFF
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl %r12d, %esi
movq %rdx, %r15
callq 0x28881d
testl %eax, %eax
jne 0x28733c
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
movzwl (%rcx,%rax,2), %r9d
testw %r9w, %r9w
je 0x2872dc
movl 0x20(%rsp), %edx
xorl %eax, %eax
movl $0xffffdfff, %esi       # imm = 0xFFFFDFFF
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %r8
movzwl %r9w, %r10d
leal (%rdx,%r10), %r9d
cmpl %r13d, %r9d
ja 0x2872bf
cmpl 0x78(%r14), %r9d
jb 0x2872bf
movl (%rdi,%r10,4), %r10d
cmpl %ebp, %r10d
cmovel %r9d, %eax
incl %esi
je 0x2872fe
incl %r8d
andl $0x1fff, %r8d           # imm = 0x1FFF
movzwl (%rcx,%r8,2), %r9d
testw %r9w, %r9w
jne 0x2872a1
testl %eax, %eax
jne 0x2872f2
leal -0x1(%r12), %eax
cmpl %ebx, %r12d
movl %eax, %r12d
movq %r15, %rdx
jg 0x287261
xorl %eax, %eax
movq (%rsp), %rdx
xorl %r15d, %r15d
jmp 0x287233
leaq 0x14e8c6(%rip), %rsi    # 0x3d5bcb
leaq 0x14e9d0(%rip), %rdx    # 0x3d5cdc
leaq 0x14e71e(%rip), %r8     # 0x3d5a31
movl $0xb, %r15d
movl $0xb, %edi
movl $0xf427, %ecx           # imm = 0xF427
xorl %eax, %eax
callq 0x274d08
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r15d
jmp 0x28732a