// File: /workspace/github_c_makefile_star2_O1/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 0x1c923
movl %eax, %r12d
testl %eax, %eax
jne 0x1afae
cmpb $0x0, 0xb(%rbx)
jne 0x1af86
movq 0x50(%rbx), %rax
cmpq $0x0, (%rax)
je 0x1af7e
cmpb $0x4, 0x9(%rbx)
jne 0x1afc0
movq 0x60(%rbx), %rax
movq %rax, 0x68(%rbx)
movq 0x110(%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x1af9f
andb $-0x5, 0x2c(%rcx)
movq 0x38(%rcx), %rcx
jmp 0x1af90
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%rax)
movb $0x4, 0x13(%rbx)
xorl %r12d, %r12d
movl %r12d, %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
jb 0x1b0b6
movabsq $-0x289c5edf06fa2a27, %rax # imm = 0xD763A120F905D5D9
movq %rax, -0x38(%rbp)
movl 0x30(%rbx), %eax
bswapl %eax
movl %eax, -0x30(%rbp)
movq 0x60(%rbx), %rax
testq %rax, %rax
je 0x1b010
decq %rax
movl 0xb8(%rbx), %ecx
cqto
idivq %rcx
movq %rax, %r13
incq %r13
imulq %rcx, %r13
jmp 0x1b013
xorl %r13d, %r13d
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq -0x40(%rbp), %rsi
movl $0x8, %edx
movq %r13, %rcx
callq *0x10(%rax)
movl %eax, %r12d
testl %eax, %eax
jne 0x1b05f
xorl %r12d, %r12d
movabsq $-0x289c5edf06fa2a27, %rax # imm = 0xD763A120F905D5D9
cmpq %rax, -0x40(%rbp)
jne 0x1b05f
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq 0x5b305(%rip), %rsi     # 0x76356
movl $0x1, %edx
movq %r13, %rcx
callq *0x18(%rax)
movl %eax, %r12d
cmpl $0x20a, %r12d           # imm = 0x20A
je 0x1b06d
testl %r12d, %r12d
jne 0x1b08f
btl $0xa, %r15d
jb 0x1b093
cmpb $0x0, 0xc(%rbx)
je 0x1b093
movq 0x50(%rbx), %rdi
movzbl 0xf(%rbx), %esi
movq (%rdi), %rax
callq *0x28(%rax)
testl %eax, %eax
je 0x1b093
movl %eax, %r12d
xorl %eax, %eax
jmp 0x1b0b2
movq 0x50(%rbx), %rdi
movq 0x68(%rbx), %rcx
movq (%rdi), %rax
leaq -0x38(%rbp), %rsi
movl $0xc, %edx
callq *0x18(%rax)
movl %eax, %r12d
testl %eax, %eax
sete %al
testb %al, %al
je 0x1b111
btl $0xa, %r15d
jb 0x1b0dd
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 0x1b106
movq 0x60(%rbx), %rax
movq %rax, 0x68(%rbx)
testl %r14d, %r14d
je 0x1b10d
andl $0x200, %r15d           # imm = 0x200
jne 0x1b10d
movl $0x0, 0x30(%rbx)
movq %rbx, %rdi
callq 0x1c987
testl %eax, %eax
je 0x1b10d
xorl %ecx, %ecx
movl %eax, %r12d
jmp 0x1b113
movb $0x1, %cl
jmp 0x1b113
xorl %ecx, %ecx
testb %cl, %cl
jne 0x1af86
jmp 0x1afae