// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdx, -0x60(%rbp)
movl %esi, -0x2c(%rbp)
movq %rdi, -0x48(%rbp)
movl 0x58(%rdi), %eax
movl $0x0, %edi
movq %rax, -0x50(%rbp)
testl %eax, %eax
je 0x1f628
movq -0x48(%rbp), %rax
cmpw $0x0, 0x3c(%rax)
je 0x1f628
movq -0x50(%rbp), %rax
addl $0x1000, %eax           # imm = 0x1000
movabsq $0xffffffff021, %r14    # imm = 0xFFFFFFFF021
addq %rax, %r14
shrq $0xc, %r14
testl %r14d, %r14d
js 0x1f628
imull $0x17f, -0x2c(%rbp), %eax # imm = 0x17F
andl $0x1fff, %eax           # imm = 0x1FFF
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rdi
movl %r14d, %esi
leaq -0x38(%rbp), %rdx
callq 0x1c0bd
testl %eax, %eax
movq %rbx, %rdx
jne 0x1f559
movq -0x38(%rbp), %rdx
testl %r14d, %r14d
je 0x1f538
movl %r14d, %r12d
shll $0xc, %r12d
addl $-0x22, %r12d
jmp 0x1f546
leaq 0x88(%rdx), %rcx
movq %rcx, -0x38(%rbp)
xorl %r12d, %r12d
addq $0x4000, %rdx           # imm = 0x4000
movq -0x38(%rbp), %rcx
addq $-0x4, %rcx
movq %rcx, -0x70(%rbp)
xorl %edi, %edi
testl %eax, %eax
movq %r14, -0x68(%rbp)
je 0x1f58e
xorl %r14d, %r14d
movl %eax, -0x30(%rbp)
testb %r14b, %r14b
je 0x1f63f
movq -0x68(%rbp), %r14
testl %r14d, %r14d
jle 0x1f628
movq %rdx, %rbx
decl %r14d
testl %edi, %edi
je 0x1f50b
jmp 0x1f628
movq -0x58(%rbp), %r15
cmpw $0x0, (%rdx,%r15,2)
je 0x1f620
leaq (%rdx,%r15,2), %rax
xorl %edi, %edi
movl $0x2001, %ebx           # imm = 0x2001
movzwl (%rax), %ecx
addl %r12d, %ecx
cmpl -0x50(%rbp), %ecx
ja 0x1f5c4
movzwl (%rax), %eax
movq -0x70(%rbp), %rsi
movl (%rsi,%rax,4), %eax
cmpl -0x2c(%rbp), %eax
cmovel %ecx, %edi
cmpl $0x1, %ebx
setne %r14b
jne 0x1f5ff
movl $0xb, -0x30(%rbp)
movl %edi, -0x3c(%rbp)
movl $0xb, %edi
leaq 0x5a231(%rip), %rsi     # 0x79814
movq %rdx, %r13
movl $0xbb13, %edx           # imm = 0xBB13
leaq 0x59f47(%rip), %rcx     # 0x79539
xorl %eax, %eax
callq 0xb8cc
movl -0x3c(%rbp), %edi
movq %r13, %rdx
decl %ebx
je 0x1f569
incl %r15d
andl $0x1fff, %r15d          # imm = 0x1FFF
leaq (%rdx,%r15,2), %rax
cmpw $0x0, (%rax)
jne 0x1f5a9
jmp 0x1f569
movb $0x1, %r14b
jmp 0x1f569
movq -0x60(%rbp), %rax
movl %edi, (%rax)
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x30(%rbp), %eax
jmp 0x1f630