// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r14d
movq %rdi, %rbx
callq 0x1a21d
testl %eax, %eax
jne 0x18b23
cmpb $0x0, 0xb(%rbx)
jne 0x18af9
movq 0x50(%rbx), %rax
cmpq $0x0, (%rax)
je 0x18af1
cmpb $0x4, 0x9(%rbx)
jne 0x18b32
movq 0x60(%rbx), %rax
movq %rax, 0x68(%rbx)
movq 0x110(%rbx), %rax
movq %rax, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x18b15
andb $-0x5, 0x2c(%rcx)
addq $0x38, %rcx
jmp 0x18b03
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%rax)
movb $0x4, 0x13(%rbx)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
movl %eax, %r15d
btl $0x9, %eax
jae 0x18b7f
movl %r15d, %r12d
andl $0x400, %r12d           # imm = 0x400
testl %r12d, %r12d
je 0x18bc8
movq 0x60(%rbx), %rax
movq %rax, 0x68(%rbx)
testl %r14d, %r14d
je 0x18af9
andl $0x200, %r15d           # imm = 0x200
jne 0x18af9
andl $0x0, 0x30(%rbx)
movq %rbx, %rdi
callq 0x1a262
testl %eax, %eax
jne 0x18b23
jmp 0x18af9
movabsq $-0x289c5edf06fa2a27, %r13 # imm = 0xD763A120F905D5D9
movq %r13, -0x38(%rbp)
movl 0x30(%rbx), %eax
bswapl %eax
movl %eax, -0x30(%rbp)
movq %rbx, %rdi
callq 0x1a23a
movq %rax, %r12
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq -0x40(%rbp), %rsi
pushq $0x8
popq %rdx
movq %r12, %rcx
callq *0x10(%rax)
testl %eax, %eax
je 0x18bf1
cmpl $0x20a, %eax            # imm = 0x20A
je 0x18bf7
testl %eax, %eax
je 0x18bf7
jmp 0x18b23
movq 0x50(%rbx), %rdi
movzbl 0xf(%rbx), %eax
xorl %esi, %esi
cmpl $0x3, %eax
sete %sil
shll $0x4, %esi
orl %eax, %esi
movq (%rdi), %rax
callq *0x28(%rax)
testl %eax, %eax
jne 0x18b23
jmp 0x18b54
cmpq %r13, -0x40(%rbp)
je 0x18c41
movl %r15d, %r12d
andl $0x400, %r12d           # imm = 0x400
jne 0x18c1f
cmpb $0x0, 0xc(%rbx)
je 0x18c1f
movq 0x50(%rbx), %rdi
movzbl 0xf(%rbx), %esi
movq (%rdi), %rax
callq *0x28(%rax)
testl %eax, %eax
jne 0x18b23
movq 0x50(%rbx), %rdi
movq 0x68(%rbx), %rcx
movq (%rdi), %rax
leaq -0x38(%rbp), %rsi
pushq $0xc
popq %rdx
callq *0x18(%rax)
testl %eax, %eax
jne 0x18b23
jmp 0x18b4f
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq 0x4f717(%rip), %rsi     # 0x68366
pushq $0x1
popq %rdx
movq %r12, %rcx
callq *0x18(%rax)
jmp 0x18bb8