// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: walIndexTryHdr
static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){
  u32 aCksum[2];                  /* Checksum on the header content */
  WalIndexHdr h1, h2;             /* Two copies of the header content */
  WalIndexHdr volatile *aHdr;     /* Header in shared memory */

  /* The first page of the wal-index must be mapped at this point. */
  assert( pWal->nWiData>0 && pWal->apWiData[0] );

  /* Read the header. This might happen concurrently with a write to the
  ** same area of shared memory on a different CPU in a SMP,
  ** meaning it is possible that an inconsistent snapshot is read
  ** from the file. If this happens, return non-zero.
  **
  ** tag-20200519-1:
  ** There are two copies of the header at the beginning of the wal-index.
  ** When reading, read [0] first then [1].  Writes are in the reverse order.
  ** Memory barriers are used to prevent the compiler or the hardware from
  ** reordering the reads and writes.  TSAN and similar tools can sometimes
  ** give false-positive warnings about these accesses because the tools do not
  ** account for the double-read and the memory barrier. The use of mutexes
  ** here would be problematic as the memory being accessed is potentially
  ** shared among multiple processes and not all mutex implementions work
  ** reliably in that environment.
  */
  aHdr = walIndexHdr(pWal);
  memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
  walShmBarrier(pWal);
  memcpy(&h2, (void *)&aHdr[1], sizeof(h2));

  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
    return 1;   /* Dirty read */
  }
  if( h1.isInit==0 ){
    return 1;   /* Malformed header - probably all zeros */
  }
  walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
  if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
    return 1;   /* Checksum does not match */
  }

  if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
    *pChanged = 1;
    memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
    pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
    testcase( pWal->szPage<=32768 );
    testcase( pWal->szPage>=65536 );
  }

  /* The header was successfully read. Return zero. */
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x70, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x30(%rdi), %rax
movq (%rax), %r12
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
leaq 0x10(%rsp), %r15
movaps %xmm2, 0x20(%r15)
movaps %xmm1, 0x10(%r15)
movaps %xmm0, (%r15)
callq 0x23210d
movups 0x30(%r12), %xmm0
movups 0x40(%r12), %xmm1
movups 0x50(%r12), %xmm2
leaq 0x40(%rsp), %rsi
movaps %xmm2, 0x20(%rsi)
movaps %xmm1, 0x10(%rsi)
movaps %xmm0, (%rsi)
pushq $0x30
popq %rdx
movq %r15, %rdi
callq 0x177e0
pushq $0x1
popq %rbp
testl %eax, %eax
jne 0x232248
cmpb $0x0, 0x1c(%rsp)
je 0x232248
leaq 0x10(%rsp), %r15
pushq $0x28
popq %rdx
leaq 0x8(%rsp), %r12
movl %ebp, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r12, %r8
callq 0x2311b9
movl (%r12), %eax
cmpl 0x28(%r15), %eax
jne 0x232248
movl 0xc(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jne 0x232248
leaq 0x48(%rbx), %r15
leaq 0x10(%rsp), %rsi
pushq $0x30
popq %rdx
movq %r15, %rdi
callq 0x177e0
xorl %ebp, %ebp
testl %eax, %eax
je 0x232248
movl $0x1, (%r14)
movaps 0x10(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
movaps 0x30(%rsp), %xmm2
movups %xmm2, 0x20(%r15)
movups %xmm1, 0x10(%r15)
movups %xmm0, (%r15)
movzwl 0x56(%rbx), %eax
movl %eax, %ecx
shll $0x10, %ecx
orl %eax, %ecx
andl $0x1fe00, %ecx          # imm = 0x1FE00
movl %ecx, 0x38(%rbx)
movl %ebp, %eax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq