// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, 0x30(%rsp)
movl %esi, 0x8(%rsp)
movl 0x58(%rdi), %eax
movl $0x0, %ebp
movq %rax, 0x28(%rsp)
testl %eax, %eax
je 0x29105d
movq %rdi, %rbx
cmpw $0x0, 0x3c(%rdi)
jne 0x290f03
cmpb $0x0, 0x46(%rbx)
je 0x29105d
movl $0x1000, %r14d          # imm = 0x1000
movq 0x28(%rsp), %rax
leal (%rax,%r14), %r12d
addl 0x78(%rbx), %r14d
movabsq $0xffffffff021, %rax    # imm = 0xFFFFFFFF021
addq %rax, %r14
shrq $0xc, %r14
addq %rax, %r12
shrq $0xc, %r12
cmpl %r14d, %r12d
jl 0x29105d
imull $0x17f, 0x8(%rsp), %eax # imm = 0x17F
andl $0x1fff, %eax           # imm = 0x1FFF
movl %eax, 0x1c(%rsp)
leaq 0x40(%rsp), %r15
xorl %ebp, %ebp
movq %rbx, 0x10(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x292625
movl $0x1, %ecx
testl %eax, %eax
je 0x290f79
movl %eax, 0xc(%rsp)
jmp 0x291041
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x50(%rsp), %r13d
movl $0x2001, %ebx           # imm = 0x2001
movl 0x1c(%rsp), %r15d
movl %r15d, %eax
movzwl (%r14,%rax,2), %eax
testw %ax, %ax
je 0x291029
movzwl %ax, %ecx
leal (%rcx,%r13), %eax
cmpl 0x28(%rsp), %eax
ja 0x290fd0
movq 0x10(%rsp), %rdx
cmpl 0x78(%rdx), %eax
jb 0x290fd0
movl %ecx, %ecx
movq 0x38(%rsp), %rdx
movl (%rdx,%rcx,4), %ecx
cmpl 0x8(%rsp), %ecx
cmovel %eax, %ebp
cmpl $0x1, %ebx
jne 0x291005
movl $0xb, 0xc(%rsp)
movl $0xb, %edi
leaq 0x14e336(%rip), %rsi    # 0x3df31f
leaq 0x14e440(%rip), %rdx    # 0x3df430
movl $0xf427, %ecx           # imm = 0xF427
leaq 0x14e189(%rip), %r8     # 0x3df185
xorl %eax, %eax
callq 0x27e637
jmp 0x29100f
incl %r15d
andl $0x1fff, %r15d          # imm = 0x1FFF
decl %ebx
jne 0x290f97
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r14
leaq 0x40(%rsp), %r15
movl $0x1, %ecx
jmp 0x291041
xorl %ecx, %ecx
testl %ebp, %ebp
setne %cl
addl %ecx, %ecx
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r14
leaq 0x40(%rsp), %r15
testl %ecx, %ecx
jne 0x291058
leal -0x1(%r12), %eax
cmpl %r14d, %r12d
movl %eax, %r12d
jg 0x290f59
jmp 0x29105d
cmpl $0x2, %ecx
jne 0x291075
movq 0x30(%rsp), %rax
movl %ebp, (%rax)
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %eax
jmp 0x291066