// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/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  */

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

  /* 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 21 milliseconds.
  ** The total delay time before giving up is less than 1 second.
  */
  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)*238;  /* Max delay 21ms. Total delay 996ms */
    sqlite3OsSleep(pWal->pVfs, nDelay);
  }

  if( !useWal ){
    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;
    }
  }

  pInfo = walCkptInfo(pWal);
  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
    /* 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;
  for(i=1; i<WAL_NREADER; i++){
    u32 thisMark = pInfo->aReadMark[i];
    if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
      assert( thisMark!=READMARK_NOT_USED );
      mxReadMark = thisMark;
      mxI = i;
    }
  }
  /* There was once an "if" here. The extra "{" is to preserve indentation. */
  {
    if( (pWal->readOnly & WAL_SHM_RDONLY)==0
     && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
    ){
      for(i=1; i<WAL_NREADER; i++){
        rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
        if( rc==SQLITE_OK ){
          mxReadMark = pInfo->aReadMark[i] = pWal->hdr.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_CANTLOCK;
    }

    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.
    **
    ** This does not guarantee that the copy of the wal-index header is up to
    ** date before proceeding. That would not be possible without somehow
    ** blocking writers. It only guarantees that a dangerous checkpoint or 
    ** log-wrap (either of which would require an exclusive lock on
    ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
    */
    walShmBarrier(pWal);
    if( 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rdx
cmpl $0x6, %ecx
movq %rdi, -0x30(%rbp)
jl 0x1bae9
movl $0xf, %esi
cmpl $0x64, %ecx
ja 0x1bd97
imull $0xee, %ecx, %eax
addl $0xfffff7a2, %eax       # imm = 0xFFFFF7A2
cmpl $0xa, %ecx
movl $0x1, %esi
cmovael %eax, %esi
movq (%rdx), %rdi
callq *0x70(%rdi)
movq -0x30(%rbp), %rdx
testl %r12d, %r12d
je 0x1bb9b
movq 0x30(%rdx), %rax
movq (%rax), %r12
addq $0x60, %r12
xorl %esi, %esi
xorl %eax, %eax
xorl %ebx, %ebx
xorl %r14d, %r14d
movl 0x8(%r12,%rax,4), %ecx
cmpl %ecx, %r14d
ja 0x1bb1d
cmpl 0x58(%rdx), %ecx
cmovbel %ecx, %r14d
leal 0x1(%rax), %ecx
cmovbel %ecx, %ebx
incq %rax
cmpq $0x4, %rax
jne 0x1bb06
testb $0x2, 0x42(%rdx)
jne 0x1bb7d
cmpl 0x58(%rdx), %r14d
jb 0x1bb3a
testl %ebx, %ebx
jne 0x1bcf6
xorl %r13d, %r13d
leaq 0x4(%r13), %r15
cmpb $0x0, 0x3f(%rdx)
jne 0x1bcc6
movq 0x8(%rdx), %rdi
movq (%rdi), %rax
movl %r15d, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
cmpl $0x5, %eax
jne 0x1bcb8
incq %r13
cmpl $0x4, %r13d
movq -0x30(%rbp), %rdx
jne 0x1bb3d
movl $0x5, %esi
testl %ebx, %ebx
jne 0x1bcf6
xorl %eax, %eax
cmpl $0x5, %esi
sete %al
negl %eax
orl $0x208, %eax            # imm = 0x208
movl %eax, %esi
jmp 0x1bd97
movq %rdx, %rdi
movq %r15, %rsi
callq 0x1bdd5
testl %eax, %eax
je 0x1bc07
movl %eax, %esi
cmpl $0x5, %eax
movq -0x30(%rbp), %rcx
jne 0x1bd97
movq 0x30(%rcx), %rax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq $0x0, (%rax)
je 0x1bd97
cmpb $0x0, 0x3f(%rcx)
jne 0x1bd97
movq 0x8(%rcx), %rdi
movq (%rdi), %rax
movl $0x2, %esi
movq %rcx, %rbx
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
testl %eax, %eax
je 0x1bdb4
cmpl $0x5, %eax
movl $0x105, %esi            # imm = 0x105
jmp 0x1bd8c
movq -0x30(%rbp), %rdx
movq 0x30(%rdx), %rax
movq (%rax), %r12
movl 0x60(%r12), %eax
addq $0x60, %r12
xorl %esi, %esi
cmpl 0x58(%rdx), %eax
jne 0x1baff
movb 0x3f(%rdx), %al
xorl %esi, %esi
testb %al, %al
jne 0x1bc51
movq 0x8(%rdx), %rdi
movq (%rdi), %rax
movl $0x3, %esi
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
movq -0x30(%rbp), %rdx
movl %eax, %esi
movb 0x3f(%rdx), %al
cmpb $0x2, %al
je 0x1bc67
movq 0x8(%rdx), %rdi
movq (%rdi), %rax
movl %esi, %ebx
callq *0x78(%rax)
movl %ebx, %esi
movq -0x30(%rbp), %rdx
cmpl $0x5, %esi
je 0x1baff
testl %esi, %esi
jne 0x1bd97
leaq 0x48(%rdx), %rsi
movq 0x30(%rdx), %rax
movq (%rax), %rdi
movq %rdx, %rbx
movl $0x30, %edx
callq 0x8410
testl %eax, %eax
je 0x1bdcd
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpb $0x0, 0x3f(%rbx)
jne 0x1bd97
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x3, %esi
jmp 0x1bd52
movl %eax, %esi
testl %eax, %eax
movq -0x30(%rbp), %rdx
jne 0x1bd97
movl 0x58(%rdx), %r14d
movl %r14d, 0x8(%r12,%r13,4)
incq %r13
cmpb $0x0, 0x3f(%rdx)
jne 0x1bcf3
movq 0x8(%rdx), %rdi
movq (%rdi), %rax
movl %r15d, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movq -0x30(%rbp), %rdx
movl %r13d, %ebx
leal 0x3(%rbx), %r15d
movb 0x3f(%rdx), %al
testb %al, %al
je 0x1bd66
cmpb $0x2, %al
je 0x1bd13
movq 0x8(%rdx), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movq -0x30(%rbp), %rdx
movslq %ebx, %rax
movl 0x4(%r12,%rax,4), %eax
cmpl %r14d, %eax
jne 0x1bd3d
movq 0x30(%rdx), %rax
movq (%rax), %rdi
leaq 0x48(%rdx), %rsi
movl $0x30, %edx
callq 0x8410
movq -0x30(%rbp), %rdx
testl %eax, %eax
je 0x1bd91
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpb $0x0, 0x3f(%rdx)
jne 0x1bd97
movq 0x8(%rdx), %rdi
movq (%rdi), %rax
movl %r15d, %esi
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jmp 0x1bd97
movq 0x8(%rdx), %rdi
movq (%rdi), %rax
movl %r15d, %esi
movq %rdx, %r13
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
testl %eax, %eax
je 0x1bda8
cmpl $0x5, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmovnel %eax, %esi
jmp 0x1bd97
movw %bx, 0x3c(%rdx)
xorl %esi, %esi
movl %esi, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb 0x3f(%r13), %al
movq %r13, %rdx
jmp 0x1bd01
cmpb $0x0, 0x3f(%rbx)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jne 0x1bd97
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x2, %esi
jmp 0x1bd52
movw $0x0, 0x3c(%rbx)
jmp 0x1bd95