// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x3ddff4
movq -0x10(%rbp), %rax
movswl 0x3c(%rax), %eax
cmpl $0x0, %eax
jne 0x3de00a
movq -0x10(%rbp), %rax
movzbl 0x46(%rax), %eax
cmpl $0x0, %eax
jne 0x3de00a
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3de108
movq -0x10(%rbp), %rax
movl 0x78(%rax), %edi
callq 0x3dfc40
movl %eax, -0x30(%rbp)
movl -0x28(%rbp), %edi
callq 0x3dfc40
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jl 0x3de0f8
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
leaq -0x48(%rbp), %rdx
callq 0x3dfeb0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x3de054
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3de108
movl $0x2000, -0x50(%rbp)    # imm = 0x2000
movl -0x14(%rbp), %edi
callq 0x3dff60
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x5a(%rbp)
movzwl -0x5a(%rbp), %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, %eax
je 0x3de0e0
movl -0x58(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x28(%rbp), %eax
ja 0x3de0b4
movl -0x60(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jb 0x3de0b4
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x14(%rbp), %eax
jne 0x3de0b4
movl -0x60(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x50(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x50(%rbp)
cmpl $0x0, %eax
jne 0x3de0d3
movl $0xf427, %edi           # imm = 0xF427
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x3de108
movl -0x4c(%rbp), %edi
callq 0x3dffb0
movl %eax, -0x4c(%rbp)
jmp 0x3de066
cmpl $0x0, -0x24(%rbp)
je 0x3de0e8
jmp 0x3de0f8
jmp 0x3de0ea
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3de024
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)