// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: walIndexReadHdr
static int walIndexReadHdr(Wal *pWal, int *pChanged){
  int rc;                         /* Return code */
  int badHdr;                     /* True if a header read failed */
  volatile u32 *page0;            /* Chunk of wal-index containing header */

  /* Ensure that page 0 of the wal-index (the page that contains the 
  ** wal-index header) is mapped. Return early if an error occurs here.
  */
  assert( pChanged );
  rc = walIndexPage(pWal, 0, &page0);
  if( rc!=SQLITE_OK ){
    return rc;
  };
  assert( page0 || pWal->writeLock==0 );

  /* If the first page of the wal-index has been mapped, try to read the
  ** wal-index header immediately, without holding any lock. This usually
  ** works, but may fail if the wal-index header is corrupt or currently 
  ** being modified by another thread or process.
  */
  badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);

  /* If the first attempt failed, it might have been due to a race
  ** with a writer.  So get a WRITE lock and try again.
  */
  assert( badHdr==0 || pWal->writeLock==0 );
  if( badHdr ){
    if( pWal->readOnly & WAL_SHM_RDONLY ){
      if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
        walUnlockShared(pWal, WAL_WRITE_LOCK);
        rc = SQLITE_READONLY_RECOVERY;
      }
    }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
      pWal->writeLock = 1;
      if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
        badHdr = walIndexTryHdr(pWal, pChanged);
        if( badHdr ){
          /* If the wal-index header is still malformed even while holding
          ** a WRITE lock, it can only mean that the header is corrupted and
          ** needs to be reconstructed.  So run recovery to do exactly that.
          */
          rc = walIndexRecover(pWal);
          *pChanged = 1;
        }
      }
      pWal->writeLock = 0;
      walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
    }
  }

  /* If the header is read successfully, check the version number to make
  ** sure the wal-index was not constructed with some future format that
  ** this version of SQLite cannot understand.
  */
  if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
    rc = SQLITE_CANTOPEN_BKPT;
  }

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
leaq -0x20(%rbp), %rdx
xorl %esi, %esi
callq 0x1c0bd
movl %eax, %r14d
testl %eax, %eax
jne 0x1c0af
cmpq $0x0, -0x20(%rbp)
je 0x1bf9c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1c1d0
testl %eax, %eax
je 0x1c015
testb $0x2, 0x42(%rbx)
movb 0x3f(%rbx), %al
jne 0x1bfeb
xorl %r14d, %r14d
testb %al, %al
jne 0x1bfc5
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
movl %eax, %r14d
testl %r14d, %r14d
jne 0x1c010
movb $0x1, 0x40(%rbx)
xorl %r14d, %r14d
leaq -0x20(%rbp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1c0bd
testl %eax, %eax
je 0x1c033
xorl %r15d, %r15d
movl %eax, %r14d
jmp 0x1c05c
xorl %r14d, %r14d
testb %al, %al
jne 0x1c00b
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0x6, %ecx
callq *0x70(%rax)
movl %eax, %r14d
testl %r14d, %r14d
je 0x1c01d
xorl %r15d, %r15d
jmp 0x1c07c
movb $0x1, %r15b
xorl %r14d, %r14d
jmp 0x1c07c
movl $0x108, %r14d           # imm = 0x108
xorl %r15d, %r15d
cmpb $0x0, 0x3f(%rbx)
jne 0x1c07c
movl $0x5, %ecx
jmp 0x1c06b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1c1d0
testl %eax, %eax
je 0x1c059
movq %rbx, %rdi
callq 0x1c2dd
movl %eax, %r14d
movl $0x1, (%r15)
xorl %r15d, %r15d
jmp 0x1c05c
movb $0x1, %r15b
movb $0x0, 0x40(%rbx)
cmpb $0x0, 0x3f(%rbx)
jne 0x1c07c
movl $0x9, %ecx
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
callq *0x70(%rax)
testb %r15b, %r15b
je 0x1c0af
cmpl $0x2de218, 0x48(%rbx)   # imm = 0x2DE218
je 0x1c0af
leaq 0x5d64b(%rip), %rsi     # 0x796dc
leaq 0x5d4a1(%rip), %rcx     # 0x79539
movl $0xe, %r14d
movl $0xe, %edi
movl $0xb9b2, %edx           # imm = 0xB9B2
xorl %eax, %eax
callq 0xb8cc
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq