// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3ddcf0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rsi
leaq -0x50(%rbp), %rdi
movl $0x30, %edx
callq 0x19610
movq -0x10(%rbp), %rdi
callq 0x3df0c0
movq -0x88(%rbp), %rsi
addq $0x30, %rsi
leaq -0x80(%rbp), %rdi
movl $0x30, %edx
callq 0x19610
leaq -0x50(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movl $0x30, %edx
callq 0x192c0
cmpl $0x0, %eax
je 0x3df1dd
movl $0x1, -0x4(%rbp)
jmp 0x3df28d
movzbl -0x44(%rbp), %eax
cmpl $0x0, %eax
jne 0x3df1f2
movl $0x1, -0x4(%rbp)
jmp 0x3df28d
leaq -0x20(%rbp), %r8
movl $0x1, %edi
leaq -0x50(%rbp), %rsi
movl $0x28, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3dde00
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jne 0x3df21d
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x3df226
movl $0x1, -0x4(%rbp)
jmp 0x3df28d
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
leaq -0x50(%rbp), %rsi
movl $0x30, %edx
callq 0x192c0
cmpl $0x0, %eax
je 0x3df286
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
leaq -0x50(%rbp), %rsi
movl $0x30, %edx
callq 0x19610
movq -0x10(%rbp), %rax
movzwl 0x56(%rax), %ecx
andl $0xfe00, %ecx           # imm = 0xFE00
movq -0x10(%rbp), %rax
movzwl 0x56(%rax), %eax
andl $0x1, %eax
shll $0x10, %eax
addl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)