// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: walTryBeginRead
static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
  volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
  u32 mxReadMark;                 /* Largest aReadMark[] value */
  int mxI;                        /* Index of largest aReadMark[] value */
  int i;                          /* Loop counter */
  int rc = SQLITE_OK;             /* Return code  */
  u32 mxFrame;                    /* Wal frame to lock to */

  assert( pWal->readLock<0 );     /* Not currently locked */

  /* useWal may only be set for read/write connections */
  assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );

  /* Take steps to avoid spinning forever if there is a protocol error.
  **
  ** Circumstances that cause a RETRY should only last for the briefest
  ** instances of time.  No I/O or other system calls are done while the
  ** locks are held, so the locks should not be held for very long. But
  ** if we are unlucky, another process that is holding a lock might get
  ** paged out or take a page-fault that is time-consuming to resolve,
  ** during the few nanoseconds that it is holding the lock.  In that case,
  ** it might take longer than normal for the lock to free.
  **
  ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
  ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
  ** is more of a scheduler yield than an actual delay.  But on the 10th
  ** an subsequent retries, the delays start becoming longer and longer,
  ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
  ** The total delay time before giving up is less than 10 seconds.
  */
  if( cnt>5 ){
    int nDelay = 1;                      /* Pause time in microseconds */
    if( cnt>100 ){
      VVA_ONLY( pWal->lockError = 1; )
      return SQLITE_PROTOCOL;
    }
    if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
    sqlite3OsSleep(pWal->pVfs, nDelay);
  }

  if( !useWal ){
    assert( rc==SQLITE_OK );
    if( pWal->bShmUnreliable==0 ){
      rc = walIndexReadHdr(pWal, pChanged);
    }
    if( rc==SQLITE_BUSY ){
      /* If there is not a recovery running in another thread or process
      ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
      ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
      ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
      ** would be technically correct.  But the race is benign since with
      ** WAL_RETRY this routine will be called again and will probably be
      ** right on the second iteration.
      */
      if( pWal->apWiData[0]==0 ){
        /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
        ** We assume this is a transient condition, so return WAL_RETRY. The
        ** xShmMap() implementation used by the default unix and win32 VFS
        ** modules may return SQLITE_BUSY due to a race condition in the
        ** code that determines whether or not the shared-memory region
        ** must be zeroed before the requested page is returned.
        */
        rc = WAL_RETRY;
      }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
        walUnlockShared(pWal, WAL_RECOVER_LOCK);
        rc = WAL_RETRY;
      }else if( rc==SQLITE_BUSY ){
        rc = SQLITE_BUSY_RECOVERY;
      }
    }
    if( rc!=SQLITE_OK ){
      return rc;
    }
    else if( pWal->bShmUnreliable ){
      return walBeginShmUnreliable(pWal, pChanged);
    }
  }

  assert( pWal->nWiData>0 );
  assert( pWal->apWiData[0]!=0 );
  pInfo = walCkptInfo(pWal);
  if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
#ifdef SQLITE_ENABLE_SNAPSHOT
   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
#endif
  ){
    /* The WAL has been completely backfilled (or it is empty).
    ** and can be safely ignored.
    */
    rc = walLockShared(pWal, WAL_READ_LOCK(0));
    walShmBarrier(pWal);
    if( rc==SQLITE_OK ){
      if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
        /* It is not safe to allow the reader to continue here if frames
        ** may have been appended to the log before READ_LOCK(0) was obtained.
        ** When holding READ_LOCK(0), the reader ignores the entire log file,
        ** which implies that the database file contains a trustworthy
        ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
        ** happening, this is usually correct.
        **
        ** However, if frames have been appended to the log (or if the log
        ** is wrapped and written for that matter) before the READ_LOCK(0)
        ** is obtained, that is not necessarily true. A checkpointer may
        ** have started to backfill the appended frames but crashed before
        ** it finished. Leaving a corrupt image in the database file.
        */
        walUnlockShared(pWal, WAL_READ_LOCK(0));
        return WAL_RETRY;
      }
      pWal->readLock = 0;
      return SQLITE_OK;
    }else if( rc!=SQLITE_BUSY ){
      return rc;
    }
  }

  /* If we get this far, it means that the reader will want to use
  ** the WAL to get at content from recent commits.  The job now is
  ** to select one of the aReadMark[] entries that is closest to
  ** but not exceeding pWal->hdr.mxFrame and lock that entry.
  */
  mxReadMark = 0;
  mxI = 0;
  mxFrame = pWal->hdr.mxFrame;
#ifdef SQLITE_ENABLE_SNAPSHOT
  if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
    mxFrame = pWal->pSnapshot->mxFrame;
  }
#endif
  for(i=1; i<WAL_NREADER; i++){
    u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
    if( mxReadMark<=thisMark && thisMark<=mxFrame ){
      assert( thisMark!=READMARK_NOT_USED );
      mxReadMark = thisMark;
      mxI = i;
    }
  }
  if( (pWal->readOnly & WAL_SHM_RDONLY)==0
   && (mxReadMark<mxFrame || mxI==0)
  ){
    for(i=1; i<WAL_NREADER; i++){
      rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
      if( rc==SQLITE_OK ){
        AtomicStore(pInfo->aReadMark+i,mxFrame);
        mxReadMark = mxFrame;
        mxI = i;
        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
        break;
      }else if( rc!=SQLITE_BUSY ){
        return rc;
      }
    }
  }
  if( mxI==0 ){
    assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
    return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
  }

  rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
  if( rc ){
    return rc==SQLITE_BUSY ? WAL_RETRY : rc;
  }
  /* Now that the read-lock has been obtained, check that neither the
  ** value in the aReadMark[] array or the contents of the wal-index
  ** header have changed.
  **
  ** It is necessary to check that the wal-index header did not change
  ** between the time it was read and when the shared-lock was obtained
  ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
  ** that the log file may have been wrapped by a writer, or that frames
  ** that occur later in the log than pWal->hdr.mxFrame may have been
  ** copied into the database by a checkpointer. If either of these things
  ** happened, then reading the database with the current value of
  ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
  ** instead.
  **
  ** Before checking that the live wal-index header has not changed
  ** since it was read, set Wal.minFrame to the first frame in the wal
  ** file that has not yet been checkpointed. This client will not need
  ** to read any frames earlier than minFrame from the wal file - they
  ** can be safely read directly from the database file.
  **
  ** Because a ShmBarrier() call is made between taking the copy of
  ** nBackfill and checking that the wal-header in shared-memory still
  ** matches the one cached in pWal->hdr, it is guaranteed that the
  ** checkpointer that set nBackfill was not working with a wal-index
  ** header newer than that cached in pWal->hdr. If it were, that could
  ** cause a problem. The checkpointer could omit to checkpoint
  ** a version of page X that lies before pWal->minFrame (call that version
  ** A) on the basis that there is a newer version (version B) of the same
  ** page later in the wal file. But if version B happens to like past
  ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
  ** that it can read version A from the database file. However, since
  ** we can guarantee that the checkpointer that set nBackfill could not
  ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
  */
  pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
  walShmBarrier(pWal);
  if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
   || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
  ){
    walUnlockShared(pWal, WAL_READ_LOCK(mxI));
    return WAL_RETRY;
  }else{
    assert( mxReadMark<=pWal->hdr.mxFrame );
    pWal->readLock = (i16)mxI;
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl $0x0, -0x38(%rbp)
cmpl $0x5, -0x20(%rbp)
jle 0x3de7e6
movl $0x1, -0x40(%rbp)
cmpl $0x64, -0x20(%rbp)
jle 0x3de7bc
movl $0xf, -0x4(%rbp)
jmp 0x3deb43
cmpl $0xa, -0x20(%rbp)
jl 0x3de7d7
movl -0x20(%rbp), %eax
subl $0x9, %eax
movl -0x20(%rbp), %ecx
subl $0x9, %ecx
imull %ecx, %eax
imull $0x27, %eax, %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x40(%rbp), %esi
callq 0x3d3d30
cmpl $0x0, -0x1c(%rbp)
jne 0x3de8a1
movq -0x10(%rbp), %rax
movzbl 0x46(%rax), %eax
cmpl $0x0, %eax
jne 0x3de80d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3deb90
movl %eax, -0x38(%rbp)
cmpl $0x5, -0x38(%rbp)
jne 0x3de86d
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
cmpq $0x0, (%rax)
jne 0x3de82a
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
jmp 0x3de86b
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x3ded80
movl %eax, %ecx
movl %ecx, -0x38(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x3de85a
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x3de740
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
jmp 0x3de869
cmpl $0x5, -0x38(%rbp)
jne 0x3de867
movl $0x105, -0x38(%rbp)     # imm = 0x105
jmp 0x3de869
jmp 0x3de86b
jmp 0x3de86d
cmpl $0x0, -0x38(%rbp)
je 0x3de87e
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3deb43
movq -0x10(%rbp), %rax
cmpb $0x0, 0x46(%rax)
je 0x3de89d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3dedd0
movl %eax, -0x4(%rbp)
jmp 0x3deb43
jmp 0x3de89f
jmp 0x3de8a1
movq -0x10(%rbp), %rdi
callq 0x3de5c0
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x3de959
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jne 0x3de959
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x3ded80
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3df0c0
cmpl $0x0, -0x38(%rbp)
jne 0x3de944
movq -0x10(%rbp), %rdi
callq 0x3ddcf0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x48, %rsi
movl $0x30, %edx
callq 0x192c0
cmpl $0x0, %eax
je 0x3de92e
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x3de740
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3deb43
movq -0x10(%rbp), %rax
movw $0x0, 0x3c(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3deb43
cmpl $0x5, -0x38(%rbp)
je 0x3de955
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3deb43
jmp 0x3de957
jmp 0x3de959
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x3c(%rbp)
movl $0x1, -0x34(%rbp)
cmpl $0x5, -0x34(%rbp)
jge 0x3de9bc
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x48(%rbp), %eax
ja 0x3de9af
movl -0x48(%rbp), %eax
cmpl -0x3c(%rbp), %eax
ja 0x3de9af
movl -0x48(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x3de9b1
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3de978
movq -0x10(%rbp), %rax
movzbl 0x42(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x3dea65
movl -0x2c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jb 0x3de9e2
cmpl $0x0, -0x30(%rbp)
jne 0x3dea65
movl $0x1, -0x34(%rbp)
cmpl $0x5, -0x34(%rbp)
jge 0x3dea63
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
addl $0x3, %esi
movl $0x1, %edx
callq 0x3de5e0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x3dea43
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl -0x3c(%rbp), %edx
movl %edx, -0x50(%rbp)
movl -0x50(%rbp), %edx
movl %edx, 0x4(%rax,%rcx,4)
movl -0x3c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
addl $0x3, %esi
movl $0x1, %edx
callq 0x3de700
jmp 0x3dea63
cmpl $0x5, -0x38(%rbp)
je 0x3dea54
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3deb43
jmp 0x3dea56
jmp 0x3dea58
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3de9e9
jmp 0x3dea65
cmpl $0x0, -0x30(%rbp)
jne 0x3dea86
movl -0x38(%rbp), %edx
movl $0x508, %eax            # imm = 0x508
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x5, %edx
cmovel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x3deb43
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
addl $0x3, %esi
callq 0x3ded80
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x3deabf
cmpl $0x5, -0x38(%rbp)
jne 0x3deaae
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x5c(%rbp)
jmp 0x3deab4
movl -0x38(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3deb43
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %ecx
incl %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x78(%rax)
movq -0x10(%rbp), %rdi
callq 0x3df0c0
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x3deb17
movq -0x10(%rbp), %rdi
callq 0x3ddcf0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x48, %rsi
movl $0x30, %edx
callq 0x192c0
cmpl $0x0, %eax
je 0x3deb2f
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
addl $0x3, %esi
callq 0x3de740
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3deb43
movl -0x30(%rbp), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x3c(%rax)
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)