// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %r13
cmpl $0x6, %ecx
jl 0x28765c
movl $0xf, %edi
cmpl $0x64, %ecx
ja 0x287bd9
leal -0x9(%rcx), %eax
imull %eax, %eax
imull $0x27, %eax, %eax
cmpl $0xa, %ecx
movl $0x1, %esi
cmovael %eax, %esi
movq (%r13), %rdi
callq *0x70(%rdi)
testl %r15d, %r15d
movq %r13, 0x8(%rsp)
je 0x28773d
leaq 0x30(%r13), %rbx
movq 0x30(%r13), %rax
movq (%rax), %r12
addq $0x60, %r12
xorl %edi, %edi
movl 0x58(%r13), %ebp
xorl %r15d, %r15d
movl $0x2, %eax
xorl %r8d, %r8d
movl (%r12,%rax,4), %ecx
cmpl %ecx, %r15d
seta %dl
cmpl %ebp, %ecx
seta %sil
orb %dl, %sil
jne 0x2876a6
leal -0x1(%rax), %r8d
movl %ecx, %r15d
incq %rax
cmpq $0x6, %rax
jne 0x28768a
testb $0x2, 0x42(%r13)
jne 0x28771e
cmpl %ebp, %r15d
jb 0x2876c4
testl %r8d, %r8d
jne 0x2878a7
movq %r8, 0x20(%rsp)
movq %rbx, (%rsp)
xorl %ebx, %ebx
movq 0x8(%rsp), %r13
leaq 0x4(%rbx), %r14
cmpb $0x0, 0x3f(%r13)
jne 0x287877
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
cmpl $0x5, %eax
jne 0x287868
incq %rbx
cmpl $0x4, %ebx
jne 0x2876d4
movl $0x5, %edi
movq 0x8(%rsp), %r13
movq (%rsp), %rbx
movq 0x20(%rsp), %r8
testl %r8d, %r8d
jne 0x2878a7
xorl %eax, %eax
cmpl $0x5, %edi
sete %al
negl %eax
orl $0x508, %eax            # imm = 0x508
movl %eax, %edi
jmp 0x287bd9
cmpb $0x0, 0x46(%r13)
je 0x2877cd
cmpb $0x0, 0x3f(%r13)
je 0x287837
movw $0x0, 0x3c(%r13)
movq 0x8(%r13), %rdi
movq (%rdi), %rax
xorl %r15d, %r15d
leaq 0x38(%rsp), %r8
xorl %esi, %esi
movl $0x8000, %edx           # imm = 0x8000
xorl %ecx, %ecx
callq *0x68(%rax)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x8, %eax
je 0x287b8f
cmpl $0x508, %eax            # imm = 0x508
jne 0x287b7f
movq 0x10(%r13), %rdi
movq 0x30(%r13), %rax
movq (%rax), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, 0x48(%r13)
movups %xmm1, 0x58(%r13)
movups %xmm2, 0x68(%r13)
movq (%rdi), %rax
leaq 0x10(%rsp), %rsi
callq *0x30(%rax)
testl %eax, %eax
je 0x287a1c
movl %eax, %ecx
jmp 0x287b8f
movq %r13, %rdi
movq %r14, %rsi
callq 0x287bea
testl %eax, %eax
je 0x287962
movl %eax, %edi
cmpl $0x5, %eax
jne 0x287bd9
movq 0x30(%r13), %rax
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpq $0x0, (%rax)
je 0x287bd9
cmpb $0x0, 0x3f(%r13)
jne 0x287bd9
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl $0x2, %esi
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
testl %eax, %eax
je 0x287ad1
cmpl $0x5, %eax
movl $0x105, %edi            # imm = 0x105
jmp 0x287947
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl $0x3, %esi
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
testl %eax, %eax
je 0x287753
cmpl $0x5, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovnel %eax, %ecx
jmp 0x287b8f
movl %eax, %edi
testl %eax, %eax
movq 0x8(%rsp), %r13
jne 0x287bd9
movl %ebp, 0x8(%r12,%rbx,4)
incq %rbx
cmpb $0x0, 0x3f(%r13)
jne 0x28789d
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movl %ebp, %r15d
movl %ebx, %r8d
movq (%rsp), %rbx
movq %r8, %rbp
leal 0x3(%r8), %r14d
movb 0x3f(%r13), %al
testb %al, %al
je 0x287924
movl (%r12), %ecx
incl %ecx
movl %ecx, 0x78(%r13)
cmpb $0x2, %al
je 0x2878ce
movq 0x8(%r13), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movslq %ebp, %rax
movl 0x4(%r12,%rax,4), %eax
cmpl %r15d, %eax
jne 0x2878f3
leaq 0x48(%r13), %rsi
movq (%rbx), %rax
movq (%rax), %rdi
movl $0x30, %edx
callq 0x17830
testl %eax, %eax
je 0x28794f
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpb $0x0, 0x3f(%r13)
jne 0x287bd9
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
jmp 0x287bd9
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
testl %eax, %eax
je 0x287959
cmpl $0x5, %eax
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmovnel %eax, %edi
jmp 0x287bd9
movw %bp, 0x3c(%r13)
jmp 0x287bd7
movb 0x3f(%r13), %al
jmp 0x2878b6
cmpb $0x0, 0x46(%r13)
jne 0x287748
leaq 0x30(%r13), %rbx
movq 0x30(%r13), %rax
movq (%rax), %r12
movl 0x60(%r12), %eax
addq $0x60, %r12
xorl %edi, %edi
cmpl 0x58(%r13), %eax
jne 0x28767b
movb 0x3f(%r13), %al
xorl %edi, %edi
testb %al, %al
jne 0x2879b6
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl $0x3, %esi
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
movl %eax, %edi
movb 0x3f(%r13), %al
cmpb $0x2, %al
je 0x2879ce
movq %rbx, %r14
movl %edi, %ebx
movq 0x8(%r13), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movl %ebx, %edi
movq %r14, %rbx
cmpl $0x5, %edi
je 0x28767b
testl %edi, %edi
jne 0x287bd9
leaq 0x48(%r13), %rsi
movq (%rbx), %rax
movq (%rax), %rdi
movl $0x30, %edx
callq 0x17830
testl %eax, %eax
je 0x287af2
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpb $0x0, 0x3f(%r13)
jne 0x287bd9
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl $0x3, %esi
jmp 0x28790d
cmpq $0x1f, 0x10(%rsp)
jg 0x287a3e
movl $0x1, (%r14)
xorl %r15d, %r15d
cmpl 0x58(%r13), %r15d
movl $0x0, %ebx
sbbl %ebx, %ebx
jmp 0x287b81
movq 0x10(%r13), %rdi
movq (%rdi), %rax
leaq 0x40(%rsp), %rsi
movl $0x20, %edx
xorl %ecx, %ecx
callq *0x10(%rax)
testl %eax, %eax
jne 0x2877c6
movq 0x68(%r13), %rax
cmpq 0x50(%rsp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jne 0x287b8f
movzwl 0x56(%r13), %ebp
callq 0x2725a7
movl $0x7, %ecx
testl %eax, %eax
jne 0x287b8f
leaq 0x18(%rbp), %r12
movq %r12, %rdi
callq 0x272ae6
movl $0x7, %ecx
testq %rax, %rax
je 0x287b8f
movq %rax, %r15
movsd 0x60(%r13), %xmm0
movl 0x58(%r13), %eax
movzwl 0x56(%r13), %r13d
addq $0x18, %r13
imulq %rax, %r13
leaq (%r12,%r13), %rax
addq $0x20, %rax
cmpq 0x10(%rsp), %rax
jle 0x287afe
xorl %eax, %eax
jmp 0x287b75
cmpb $0x0, 0x3f(%r13)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
jne 0x287bd9
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl $0x2, %esi
jmp 0x28790d
movw $0x0, 0x3c(%r13)
jmp 0x287bd7
movaps %xmm0, 0x20(%rsp)
movq %r15, %rax
addq $0x18, %rax
movq %rax, (%rsp)
addq $0x20, %r13
movq 0x8(%rsp), %rbx
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movl %r12d, %edx
movq %r13, %rcx
callq *0x10(%rax)
testl %eax, %eax
jne 0x287b70
movq %rbx, %rdi
leaq 0x34(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
movq (%rsp), %rcx
movq %r15, %r8
callq 0x2886f2
testl %eax, %eax
je 0x287b67
cmpl $0x0, 0x1c(%rsp)
jne 0x287b6b
addq %r12, %r13
leaq 0x18(,%r13), %rax
addq %rbp, %rax
cmpq 0x10(%rsp), %rax
jle 0x287b17
xorl %eax, %eax
jmp 0x287b70
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movaps 0x20(%rsp), %xmm0
movq 0x8(%rsp), %r13
movlps %xmm0, 0x60(%r13)
movl %eax, %ebx
movq %r15, %rdi
callq 0x272c00
testl %ebx, %ebx
movl %ebx, %ecx
je 0x287bd7
movl %ecx, %ebp
cmpl $0x0, 0x28(%r13)
jle 0x287bbf
xorl %ebx, %ebx
movq 0x30(%r13), %rax
movq (%rax,%rbx,8), %rdi
callq 0x272c00
movq 0x30(%r13), %rax
movq $0x0, (%rax,%rbx,8)
incq %rbx
movslq 0x28(%r13), %rax
cmpq %rax, %rbx
jl 0x287b9a
movb $0x0, 0x46(%r13)
movq %r13, %rdi
callq 0x2887af
movl $0x1, (%r14)
movl %ebp, %edi
jmp 0x287bd9
xorl %edi, %edi
movl %edi, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq