// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
subq $0x60, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x30(%rdi), %rax
movq (%rax), %r15
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movaps %xmm2, 0x20(%rsp)
movaps %xmm1, 0x10(%rsp)
movaps %xmm0, (%rsp)
cmpb $0x2, 0x3f(%rdi)
je 0x28848c
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
callq *0x78(%rax)
movups 0x30(%r15), %xmm0
movups 0x40(%r15), %xmm1
movups 0x50(%r15), %xmm2
leaq 0x30(%rsp), %rsi
movaps %xmm2, 0x20(%rsi)
movaps %xmm1, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq %rsp, %rdi
movl $0x30, %edx
callq 0x17830
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
jne 0x288552
cmpb $0x0, 0xc(%rsp)
je 0x288552
xorl %ecx, %ecx
movq $-0x8, %rdx
xorl %esi, %esi
addl %ecx, %esi
addl 0x8(%rsp,%rdx), %esi
addl 0xc(%rsp,%rdx), %ecx
addl %esi, %ecx
addq $0x8, %rdx
cmpq $0x20, %rdx
jb 0x2884dd
cmpl 0x28(%rsp), %esi
jne 0x288552
cmpl 0x2c(%rsp), %ecx
jne 0x288552
leaq 0x48(%rbx), %r15
movq %rsp, %rsi
movl $0x30, %edx
movq %r15, %rdi
callq 0x17830
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x288552
movl $0x1, (%r14)
movaps (%rsp), %xmm0
movaps 0x10(%rsp), %xmm1
movaps 0x20(%rsp), %xmm2
movups %xmm2, 0x20(%r15)
movups %xmm1, 0x10(%r15)
movups %xmm0, (%r15)
movzwl 0x56(%rbx), %ecx
movl %ecx, %edx
shll $0x10, %edx
orl %ecx, %edx
andl $0x1fe00, %edx          # imm = 0x1FE00
movl %edx, 0x38(%rbx)
addq $0x60, %rsp
popq %rbx
popq %r14
popq %r15
retq