// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: pager_end_transaction
static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
  int rc = SQLITE_OK;      /* Error code from journal finalization operation */
  int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */

  /* Do nothing if the pager does not have an open write transaction
  ** or at least a RESERVED lock. This function may be called when there
  ** is no write-transaction active but a RESERVED or greater lock is
  ** held under two circumstances:
  **
  **   1. After a successful hot-journal rollback, it is called with
  **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
  **
  **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE 
  **      lock switches back to locking_mode=normal and then executes a
  **      read-transaction, this function is called with eState==PAGER_READER 
  **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
  */
  assert( assert_pager_state(pPager) );
  assert( pPager->eState!=PAGER_ERROR );
  if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
    return SQLITE_OK;
  }

  releaseAllSavepoints(pPager);
  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
  if( isOpen(pPager->jfd) ){
    assert( !pagerUseWal(pPager) );

    /* Finalize the journal file. */
    if( sqlite3IsMemJournal(pPager->jfd) ){
      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
      sqlite3OsClose(pPager->jfd);
    }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
      if( pPager->journalOff==0 ){
        rc = SQLITE_OK;
      }else{
        rc = sqlite3OsTruncate(pPager->jfd, 0);
      }
      pPager->journalOff = 0;
    }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
      || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
    ){
      rc = zeroJournalHdr(pPager, hasMaster);
      pPager->journalOff = 0;
    }else{
      /* This branch may be executed with Pager.journalMode==MEMORY if
      ** a hot-journal was just rolled back. In this case the journal
      ** file should be closed and deleted. If this connection writes to
      ** the database file, it will do so using an in-memory journal. 
      */
      int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
      assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
           || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
      );
      sqlite3OsClose(pPager->jfd);
      if( bDelete ){
        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
      }
    }
  }

#ifdef SQLITE_CHECK_PAGES
  sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
  if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
    PgHdr *p = pager_lookup(pPager, 1);
    if( p ){
      p->pageHash = 0;
      sqlite3PagerUnref(p);
    }
  }
#endif

  sqlite3BitvecDestroy(pPager->pInJournal);
  pPager->pInJournal = 0;
  pPager->nRec = 0;
  sqlite3PcacheCleanAll(pPager->pPCache);
  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);

  if( pagerUseWal(pPager) ){
    /* Drop the WAL write-lock, if any. Also, if the connection was in 
    ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 
    ** lock held on the database file.
    */
    rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
    assert( rc2==SQLITE_OK );
  }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
    /* This branch is taken when committing a transaction in rollback-journal
    ** mode if the database file on disk is larger than the database image.
    ** At this point the journal has been finalized and the transaction 
    ** successfully committed, but the EXCLUSIVE lock is still held on the
    ** file. So it is safe to truncate the database file to its minimum
    ** required size.  */
    assert( pPager->eLock==EXCLUSIVE_LOCK );
    rc = pager_truncate(pPager, pPager->dbSize);
  }

  if( !pPager->exclusiveMode 
   && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
  ){
    rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
    pPager->changeCountDone = 0;
  }
  pPager->eState = PAGER_READER;
  pPager->setMaster = 0;

  return (rc==SQLITE_OK?rc2:rc);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r14d
movl %esi, %r12d
movq %rdi, %rbx
cmpb $0x1, 0x13(%rdi)
ja 0x1b745
xorl %r13d, %r13d
cmpb $0x2, 0x14(%rbx)
jb 0x1b90e
movq %rbx, %rdi
callq 0x1b591
movq 0x50(%rbx), %r13
movq (%r13), %rax
xorl %r15d, %r15d
testq %rax, %rax
je 0x1b88c
leaq 0x6a738(%rip), %rcx     # 0x85ea0
cmpq %rcx, %rax
je 0x1b792
movzbl 0x9(%rbx), %ecx
cmpl $0x1, %ecx
je 0x1b7af
cmpl $0x3, %ecx
jne 0x1b7a4
cmpq $0x0, 0x60(%rbx)
je 0x1b7dd
movq %r13, %rdi
xorl %esi, %esi
callq *0x20(%rax)
movl %eax, %r15d
jmp 0x1b887
movq %r13, %rdi
callq 0x18eda
andq $0x0, (%r13)
jmp 0x1b88c
cmpb $0x5, %cl
je 0x1b7e5
cmpb $0x0, 0x8(%rbx)
je 0x1b7e5
cmpq $0x0, 0x60(%rbx)
je 0x1b7dd
movq 0xc8(%rbx), %rcx
testl %r12d, %r12d
movq %rcx, %r12
setne %cl
testq %r12, %r12
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x1b811
movq %r13, %rdi
xorl %esi, %esi
callq *0x20(%rax)
jmp 0x1b823
xorl %r15d, %r15d
jmp 0x1b887
movb 0x10(%rbx), %r12b
movq %r13, %rdi
callq *0x8(%rax)
andq $0x0, (%r13)
testb %r12b, %r12b
jne 0x1b88c
movq (%rbx), %rdi
movq 0xd8(%rbx), %rsi
xorl %edx, %edx
callq *0x30(%rdi)
movl %eax, %r15d
jmp 0x1b88c
leaq 0x4cb58(%rip), %rsi     # 0x68370
pushq $0x1c
popq %rdx
movq %r13, %rdi
xorl %ecx, %ecx
callq *0x18(%rax)
movl %eax, %r15d
testl %eax, %eax
jne 0x1b887
xorl %r15d, %r15d
cmpb $0x0, 0xb(%rbx)
jne 0x1b847
movq 0x50(%rbx), %rdi
movzbl 0xf(%rbx), %esi
orl $0x10, %esi
movq (%rdi), %rax
callq *0x28(%rax)
movl %eax, %r15d
testl %r15d, %r15d
sete %al
testq %r12, %r12
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1b887
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq -0x30(%rbp), %rsi
callq *0x30(%rax)
movl %eax, %r15d
testl %eax, %eax
jne 0x1b887
xorl %r15d, %r15d
cmpq %r12, -0x30(%rbp)
jle 0x1b887
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
movq %r12, %rsi
jmp 0x1b787
andq $0x0, 0x60(%rbx)
movq 0x40(%rbx), %rdi
callq 0x1b557
andq $0x0, 0x40(%rbx)
andl $0x0, 0x30(%rbx)
movq 0x110(%rbx), %rdi
callq 0x1d06f
movq 0x110(%rbx), %rdi
movl 0x1c(%rbx), %esi
callq 0x1a7b8
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
je 0x1b920
callq 0x1b46e
xorl %r13d, %r13d
cmpb $0x0, 0x8(%rbx)
jne 0x1b8ff
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
je 0x1b8ed
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x1d08e
testl %eax, %eax
je 0x1b8ff
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1b646
movl %eax, %r13d
movb $0x0, 0x15(%rbx)
movb $0x1, 0x13(%rbx)
movb $0x0, 0x16(%rbx)
testl %r15d, %r15d
cmovnel %r15d, %r13d
movl %r13d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r14d, %r14d
je 0x1b8ca
testl %r15d, %r15d
jne 0x1b8ca
movl 0x1c(%rbx), %esi
xorl %r15d, %r15d
cmpl %esi, 0x24(%rbx)
jbe 0x1b8ca
movq %rbx, %rdi
callq 0x1cfc2
movl %eax, %r15d
jmp 0x1b8ca