// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: syncJournal
static int syncJournal(Pager *pPager, int newHdr){
  int rc;                         /* Return code */

  assert( pPager->eState==PAGER_WRITER_CACHEMOD
       || pPager->eState==PAGER_WRITER_DBMOD
  );
  assert( assert_pager_state(pPager) );
  assert( !pagerUseWal(pPager) );

  rc = sqlite3PagerExclusiveLock(pPager);
  if( rc!=SQLITE_OK ) return rc;

  if( !pPager->noSync ){
    assert( !pPager->tempFile );
    if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
      const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
      assert( isOpen(pPager->jfd) );

      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
        /* This block deals with an obscure problem. If the last connection
        ** that wrote to this database was operating in persistent-journal
        ** mode, then the journal file may at this point actually be larger
        ** than Pager.journalOff bytes. If the next thing in the journal
        ** file happens to be a journal-header (written as part of the
        ** previous connection's transaction), and a crash or power-failure 
        ** occurs after nRec is updated but before this connection writes 
        ** anything else to the journal file (or commits/rolls back its 
        ** transaction), then SQLite may become confused when doing the 
        ** hot-journal rollback following recovery. It may roll back all
        ** of this connections data, then proceed to rolling back the old,
        ** out-of-date data that follows it. Database corruption.
        **
        ** To work around this, if the journal file does appear to contain
        ** a valid header following Pager.journalOff, then write a 0x00
        ** byte to the start of it to prevent it from being recognized.
        **
        ** Variable iNextHdrOffset is set to the offset at which this
        ** problematic header will occur, if it exists. aMagic is used 
        ** as a temporary buffer to inspect the first couple of bytes of
        ** the potential journal header.
        */
        i64 iNextHdrOffset;
        u8 aMagic[8];
        u8 zHeader[sizeof(aJournalMagic)+4];

        memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
        put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);

        iNextHdrOffset = journalHdrOffset(pPager);
        rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
        if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
          static const u8 zerobyte = 0;
          rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
        }
        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
          return rc;
        }

        /* Write the nRec value into the journal file header. If in
        ** full-synchronous mode, sync the journal first. This ensures that
        ** all data has really hit the disk before nRec is updated to mark
        ** it as a candidate for rollback.
        **
        ** This is not required if the persistent media supports the
        ** SAFE_APPEND property. Because in this case it is not possible 
        ** for garbage data to be appended to the file, the nRec field
        ** is populated with 0xFFFFFFFF when the journal header is written
        ** and never needs to be updated.
        */
        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
          PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
          IOTRACE(("JSYNC %p\n", pPager))
          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
          if( rc!=SQLITE_OK ) return rc;
        }
        IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
        rc = sqlite3OsWrite(
            pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
        );
        if( rc!=SQLITE_OK ) return rc;
      }
      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
        PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
        IOTRACE(("JSYNC %p\n", pPager))
        rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| 
          (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
        );
        if( rc!=SQLITE_OK ) return rc;
      }

      pPager->journalHdr = pPager->journalOff;
      if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
        pPager->nRec = 0;
        rc = writeJournalHdr(pPager);
        if( rc!=SQLITE_OK ) return rc;
      }
    }else{
      pPager->journalHdr = pPager->journalOff;
    }
  }

  /* Unless the pager is in noSync mode, the journal file was just 
  ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on 
  ** all pages.
  */
  sqlite3PcacheClearSyncFlags(pPager->pPCache);
  pPager->eState = PAGER_WRITER_DBMOD;
  assert( assert_pager_state(pPager) );
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
callq 0x24470
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x214bc
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x21701
movq -0x10(%rbp), %rax
cmpb $0x0, 0xb(%rax)
jne 0x216e2
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, (%rax)
je 0x216d0
movq -0x10(%rbp), %rax
movzbl 0x9(%rax), %eax
cmpl $0x4, %eax
je 0x216d0
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x210d0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
andl $0x200, %ecx            # imm = 0x200
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x21631
movq 0x899ad(%rip), %rax     # 0xaaec4
movq %rax, -0x3c(%rbp)
leaq -0x3c(%rbp), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rax
movl 0x30(%rax), %esi
callq 0x22060
movq -0x10(%rbp), %rdi
callq 0x244b0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
leaq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rcx
movl $0x8, %edx
callq 0x23e30
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x2159c
leaq -0x30(%rbp), %rdi
leaq 0x8995a(%rip), %rsi     # 0xaaec4
movl $0x8, %edx
callq 0x81c0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x2159c
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x28(%rbp), %rcx
leaq 0x8993d(%rip), %rsi     # 0xaaecc
movl $0x1, %edx
callq 0xf430
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x215b6
cmpl $0x20a, -0x18(%rbp)     # imm = 0x20A
je 0x215b6
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x21701
movq -0x10(%rbp), %rax
movzbl 0xc(%rax), %eax
cmpl $0x0, %eax
je 0x215fd
movl -0x1c(%rbp), %ecx
andl $0x400, %ecx            # imm = 0x400
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x215fd
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x10(%rbp), %rax
movzbl 0xf(%rax), %esi
callq 0x22ce0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x215fb
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x21701
jmp 0x215fd
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
leaq -0x3c(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rcx
movl $0xc, %edx
callq 0xf430
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2162f
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x21701
jmp 0x21631
movl -0x1c(%rbp), %ecx
andl $0x400, %ecx            # imm = 0x400
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x21682
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x10(%rbp), %rax
movzbl 0xf(%rax), %esi
movq -0x10(%rbp), %rax
movzbl 0xf(%rax), %edx
xorl %eax, %eax
movl $0x10, %ecx
cmpl $0x3, %edx
cmovel %ecx, %eax
orl %eax, %esi
callq 0x22ce0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x21680
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x21701
jmp 0x21682
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x216ce
movl -0x1c(%rbp), %ecx
andl $0x200, %ecx            # imm = 0x200
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x216ce
movq -0x10(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x10(%rbp), %rdi
callq 0x24510
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x216cc
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x21701
jmp 0x216ce
jmp 0x216e0
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
jmp 0x216e2
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rdi
callq 0x24730
movq -0x10(%rbp), %rax
movb $0x4, 0x13(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)