// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]tundra/src/ScanCache.cpp
// Function: t2::ScanCacheSave(t2::ScanCache*, char const*, t2::MemAllocHeap*)
bool ScanCacheSave(ScanCache* self, const char* fn, MemAllocHeap* heap)
{
  TimingScope timing_scope(nullptr, &g_Stats.m_ScanCacheSaveTime);
  ProfilerScope prof_scope("Tundra SaveScanCache", 0);

  MemAllocLinear* scratch = self->m_Allocator;

  MemAllocLinearScope scratch_scope(scratch);

  HashTable<BinaryLocator, kFlagPathStrings> string_pool;
  HashTableInit(&string_pool, heap);

  ScanCacheWriter writer;
  ScanCacheWriterInit(&writer, heap);

  // Save new view of the scan cache
  //
  // Algorithm:
  // 
  // - Get all records from the dynamic table (stuff we put in this session)
  const uint32_t      record_count = self->m_RecordCount;
  ScanCache::Record **dyn_records  = LinearAllocateArray<ScanCache::Record*>(scratch, record_count);

  {
    uint32_t records_out = 0;
    for (uint32_t ti = 0, tsize = self->m_TableSize; ti < tsize; ++ti)
    {
      ScanCache::Record* chain = self->m_Table[ti];
      while (chain)
      {
        dyn_records[records_out++] = chain;
        chain                      = chain->m_Next;
      }
    }

    CHECK(records_out == record_count);
  }

  // - Sort these records in key order (by SHA-1 hash)
  std::sort(dyn_records, dyn_records + record_count, SortRecordsByHash);

  const ScanData       *scan_data      = self->m_FrozenData;
  uint32_t              frozen_count   = scan_data ? scan_data->m_EntryCount : 0;
  const HashDigest     *frozen_digests = scan_data ? scan_data->m_Keys.Get() : nullptr;
  const ScanCacheEntry *frozen_entries = scan_data ? scan_data->m_Data.Get() : nullptr;
  const uint64_t       *frozen_times   = scan_data ? scan_data->m_AccessTimes.Get() : nullptr;
  const uint8_t        *frozen_access  = self->m_FrozenAccess;

  const uint64_t now = time(nullptr);

  // Keep old entries for a week.
  const uint64_t timestamp_cutoff = now - 60 * 60 * 24 * 7;

  auto key_dynamic = [=](size_t index) -> const HashDigest* { return &dyn_records[index]->m_Key; };
  auto key_frozen = [=](size_t index) { return frozen_digests + index; };

  auto save_dynamic = [&writer, dyn_records, now, &string_pool](size_t index)
  {
    SaveRecord(
        &writer,
        &string_pool,
        &dyn_records[index]->m_Key,
        dyn_records[index]->m_Includes,
        dyn_records[index]->m_IncludeCount,
        dyn_records[index]->m_FileTimestamp,
        now);
  };

  auto save_frozen = [&](size_t index)
  {
    uint64_t timestamp = frozen_times[index];
    if (frozen_access[index])
      timestamp = now;

    if (timestamp > timestamp_cutoff)
    {
      SaveRecord(
          &writer,
          &string_pool,
          frozen_digests + index, 
          frozen_entries[index].m_IncludedFiles.GetArray(),
          frozen_entries[index].m_IncludedFiles.GetCount(),
          frozen_entries[index].m_FileTimestamp,
          timestamp);
    }
  };

  TraverseSortedArrays(record_count, save_dynamic, key_dynamic, frozen_count, save_frozen, key_frozen);

  self->m_FrozenData = nullptr;

  bool result = ScanCacheWriterFlush(&writer, fn);

  ScanCacheWriterDestroy(&writer);

  HashTableDestroy(&string_pool);

  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rdx, %rbx
movq %rsi, 0x30(%rsp)
movq %rdi, %r14
movq $0x0, 0xc8(%rsp)
leaq 0xd9c9(%rip), %rax      # 0x1d5a0
addq $0x10, %rax
movq %rax, 0xd0(%rsp)
callq 0x5f2a
movq %rax, 0xd8(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0xda81(%rip), %rax      # 0x1d680
cmpb $0x1, (%rax)
jne 0xfc12
leaq 0x74a0(%rip), %rdi      # 0x170ab
xorl %esi, %esi
callq 0xf4a3
movq %r14, 0x18(%rsp)
movq 0x48(%r14), %r14
movq 0x18(%r14), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x14c(%rsp)
movaps %xmm0, 0x140(%rsp)
movq %rbx, 0x160(%rsp)
movq $0x0, 0x168(%rsp)
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r14, 0x28(%rsp)
callq 0x134bc
leaq 0x40(%rsp), %rdi
callq 0x136d3
leaq 0x40(%rsp), %rdi
movq %rax, 0x20(%rdi)
callq 0x136d3
leaq 0x40(%rsp), %rdi
movq %rax, 0x28(%rdi)
callq 0x136d3
leaq 0x40(%rsp), %rdi
movq %rax, 0x30(%rdi)
callq 0x136d3
leaq 0x40(%rsp), %rdi
movq %rax, 0x38(%rdi)
callq 0x136d3
leaq 0x40(%rsp), %rdi
movq %rax, 0x40(%rdi)
callq 0x136d3
movq %rax, 0x88(%rsp)
movq 0x68(%rsp), %rdi
callq 0x134b5
movl %eax, 0x90(%rsp)
movq %rdx, 0x98(%rsp)
movq 0x70(%rsp), %rdi
callq 0x134b5
movl %eax, 0xa0(%rsp)
movq %rdx, 0xa8(%rsp)
movq 0x78(%rsp), %rdi
callq 0x134b5
movl %eax, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
movl $0x0, 0xc0(%rsp)
movq 0x18(%rsp), %rax
movl 0x50(%rax), %ecx
movq %rcx, (%rsp)
leaq (,%rcx,8), %rsi
movl $0x8, %edx
movq %r14, %rdi
movq %rax, %r14
callq 0xe950
movq %rax, %r12
movl 0x54(%r14), %eax
testq %rax, %rax
je 0xfd62
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x58(%r14), %rsi
movq (%rsi,%rcx,8), %rsi
testq %rsi, %rsi
je 0xfd5a
movl %edx, %edi
incl %edx
movq %rsi, (%r12,%rdi,8)
movq 0x28(%rsi), %rsi
jmp 0xfd47
incq %rcx
cmpq %rax, %rcx
jne 0xfd3f
cmpq $0x0, (%rsp)
je 0xfda2
movq (%rsp), %rax
leaq (%r12,%rax,8), %rbx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x35e(%rip), %rcx       # 0x100e3
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1050d
leaq 0x34c(%rip), %rdx       # 0x100e3
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1063c
movq (%r14), %rdx
testq %rdx, %rdx
je 0xfde4
movl 0x4(%rdx), %eax
movq %rax, 0x8(%rsp)
movslq 0x8(%rdx), %rcx
testq %rcx, %rcx
leaq 0x8(%rcx,%rdx), %rax
cmoveq %rcx, %rax
movslq 0xc(%rdx), %rsi
testq %rsi, %rsi
leaq 0xc(%rsi,%rdx), %rcx
cmoveq %rsi, %rcx
movslq 0x10(%rdx), %rsi
testq %rsi, %rsi
leaq 0x10(%rsi,%rdx), %rdx
cmoveq %rsi, %rdx
jmp 0xfdf3
xorl %eax, %eax
xorl %ecx, %ecx
movq $0x0, 0x8(%rsp)
xorl %edx, %edx
leaq 0x190(%rsp), %r14
movq %rax, (%r14)
leaq 0x38(%rsp), %rax
movq %rcx, (%rax)
leaq 0x188(%rsp), %r15
movq %rdx, (%r15)
movq 0x18(%rsp), %rax
movq 0x60(%rax), %rax
leaq 0x180(%rsp), %r13
movq %rax, (%r13)
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x3300
leaq 0x178(%rsp), %rcx
movq %rax, (%rcx)
leaq -0x93a80(%rax), %rdx
leaq 0x170(%rsp), %rsi
movq %rdx, (%rsi)
movq (%r14), %rbx
leaq 0x40(%rsp), %rdx
movq %rdx, 0xe0(%rsp)
movq %r12, 0xe8(%rsp)
movq %rax, 0xf0(%rsp)
leaq 0x140(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq %r15, 0x100(%rsp)
movq %r13, 0x108(%rsp)
movq %rcx, 0x110(%rsp)
movq %rsi, 0x118(%rsp)
movq %rdx, 0x120(%rsp)
movq %rax, 0x128(%rsp)
movq %r14, 0x130(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0x138(%rsp)
cmpq $0x0, (%rsp)
je 0xff66
movl $0x0, %r13d
cmpq $0x0, 0x8(%rsp)
je 0xff69
xorl %r13d, %r13d
xorl %r14d, %r14d
movq (%r12,%r13,8), %rax
movq %r14, %rcx
shlq $0x4, %rcx
movq (%rax), %rdx
xorl %esi, %esi
cmpq (%rbx,%rcx), %rdx
seta %sil
sbbl $0x0, %esi
movq 0x8(%rax), %rax
xorl %r15d, %r15d
cmpq 0x8(%rbx,%rcx), %rax
seta %r15b
sbbl $0x0, %r15d
testl %esi, %esi
cmovnel %esi, %r15d
testl %r15d, %r15d
jle 0xff35
movl $0x1, %ebp
leaq 0x100(%rsp), %rdi
movq %r14, %rsi
callq 0x1025e
jmp 0xff51
leaq 0xe0(%rsp), %rdi
movq %r13, %rsi
callq 0x10108
incq %r13
xorl %ebp, %ebp
testl %r15d, %r15d
sete %bpl
addq %r14, %rbp
cmpq (%rsp), %r13
jae 0xff69
movq %rbp, %r14
cmpq 0x8(%rsp), %rbp
jb 0xfee4
jmp 0xff69
xorl %r13d, %r13d
cmpq (%rsp), %r13
movq 0x28(%rsp), %r14
jae 0xff90
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x10108
incq %r13
cmpq %r13, (%rsp)
jne 0xff7c
cmpq 0x8(%rsp), %rbp
jae 0xffb4
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1025e
incq %rbp
cmpq %rbp, 0x8(%rsp)
jne 0xff9f
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
movq 0x60(%rsp), %rdi
leaq 0x100(%rsp), %rsi
movl $0x6d072210, (%rsi)     # imm = 0x6D072210
movl $0x4, %edx
callq 0x1343d
movq 0x60(%rsp), %rdi
movl 0xc0(%rsp), %eax
leaq 0x100(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
callq 0x1343d
movq 0x60(%rsp), %rdi
movl 0x90(%rsp), %esi
movq 0x98(%rsp), %rdx
callq 0x13469
movq 0x60(%rsp), %rdi
movl 0xa0(%rsp), %esi
movq 0xa8(%rsp), %rdx
callq 0x13469
movq 0x60(%rsp), %rdi
movl 0xb0(%rsp), %esi
movq 0xb8(%rsp), %rdx
callq 0x13469
leaq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x1359a
movl %eax, %ebx
leaq 0x40(%rsp), %rdi
callq 0x134cf
leaq 0x140(%rsp), %rdi
callq 0x104cf
movq 0x20(%rsp), %rax
movq %rax, 0x18(%r14)
leaq 0x14(%rsp), %rdi
callq 0x5a06
leaq 0xc8(%rsp), %rdi
callq 0x5a30
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x100ce
jmp 0x100b3
jmp 0x100b3
jmp 0x100b3
jmp 0x100b3
jmp 0x100b3
movq %rax, %rbx
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x14(%rsp), %rdi
callq 0x5a06
leaq 0xc8(%rsp), %rdi
callq 0x5a30
movq %rbx, %rdi
callq 0x35e0