// File: /workspace/github_c_makefile_star2_O2/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, 0x20(%rsp)
andq $0x0, 0xc8(%rsp)
movq %rdi, %r12
leaq 0xecd4(%rip), %rax      # 0x1d5a0
addq $0x10, %rax
movq %rax, 0xd0(%rsp)
callq 0x5b35
movq %rax, 0xd8(%rsp)
andl $0x0, 0xc(%rsp)
leaq 0x67b2(%rip), %rdi      # 0x150a3
xorl %esi, %esi
callq 0x551c
movq 0x48(%r12), %r14
movq 0x18(%r14), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0xec(%rsp)
movaps %xmm0, 0xe0(%rsp)
movq %rbx, 0x100(%rsp)
andq $0x0, 0x108(%rsp)
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r14, 0x10(%rsp)
movq %r15, 0x18(%rsp)
callq 0x11ba0
leaq 0x40(%rsp), %rdi
callq 0x11d4e
leaq 0x40(%rsp), %rdi
movq %rax, 0x20(%rdi)
callq 0x11d4e
leaq 0x40(%rsp), %rdi
movq %rax, 0x28(%rdi)
callq 0x11d4e
leaq 0x40(%rsp), %rdi
movq %rax, 0x30(%rdi)
callq 0x11d4e
leaq 0x40(%rsp), %rdi
movq %rax, 0x38(%rdi)
callq 0x11d4e
leaq 0x40(%rsp), %rdi
movq %rax, 0x40(%rdi)
callq 0x11d4e
movq %rax, 0x88(%rsp)
movq 0x68(%rsp), %rdi
callq 0x11b99
movl %eax, 0x90(%rsp)
movq %rdx, 0x98(%rsp)
movq 0x70(%rsp), %rdi
callq 0x11b99
movl %eax, 0xa0(%rsp)
movq %rdx, 0xa8(%rsp)
movq 0x78(%rsp), %rdi
callq 0x11b99
movl %eax, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
andl $0x0, 0xc0(%rsp)
movl 0x50(%r12), %ebx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xf110
movq %rax, %r13
movq %r12, (%rsp)
movl 0x54(%r12), %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rax, %rcx
je 0xea38
leaq (,%rcx,8), %rsi
movq (%rsp), %rdi
addq 0x58(%rdi), %rsi
movq (%rsi), %rsi
testq %rsi, %rsi
je 0xea33
movl %edx, %edi
incl %edx
movq %rsi, (%r13,%rdi,8)
addq $0x28, %rsi
jmp 0xea1c
incq %rcx
jmp 0xea07
leaq (,%rbx,8), %rsi
addq %r13, %rsi
leaq 0x2ac(%rip), %rdx       # 0xecf6
movq %r13, %rdi
callq 0xf16b
movq (%rsp), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0xea94
movl 0x4(%rcx), %r12d
movslq 0x8(%rcx), %rax
testq %rax, %rax
leaq 0x8(%rax,%rcx), %rsi
cmoveq %rax, %rsi
movslq 0xc(%rcx), %rdx
testq %rdx, %rdx
leaq 0xc(%rdx,%rcx), %rax
cmoveq %rdx, %rax
movslq 0x10(%rcx), %rdx
testq %rdx, %rdx
leaq 0x10(%rdx,%rcx), %rcx
cmoveq %rdx, %rcx
jmp 0xea9d
xorl %esi, %esi
xorl %eax, %eax
xorl %r12d, %r12d
xorl %ecx, %ecx
leaq 0x38(%rsp), %rdx
movq %rsi, 0x28(%rsp)
movq %rsi, (%rdx)
leaq 0x30(%rsp), %rdx
movq %rax, (%rdx)
leaq 0x130(%rsp), %r15
movq %rcx, (%r15)
movq (%rsp), %rax
movq 0x60(%rax), %rax
leaq 0x128(%rsp), %rbp
movq %rax, (%rbp)
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x3300
leaq 0x120(%rsp), %rcx
movq %rax, (%rcx)
leaq -0x93a80(%rax), %rdx
leaq 0x118(%rsp), %rsi
movq %rdx, (%rsi)
leaq 0x138(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %rdx, (%rdi)
movq %r13, 0x8(%rdi)
movq %rax, 0x10(%rdi)
leaq 0xe0(%rsp), %rax
movq %rax, 0x18(%rdi)
leaq 0x158(%rsp), %rdi
movq %r15, (%rdi)
movq %rbp, 0x8(%rdi)
movq %rcx, 0x10(%rdi)
movq %rsi, 0x18(%rdi)
movq %rdx, 0x20(%rdi)
movq %rax, 0x28(%rdi)
leaq 0x38(%rsp), %rax
movq %rax, 0x30(%rdi)
leaq 0x30(%rsp), %rax
movq %rax, 0x38(%rdi)
xorl %ebp, %ebp
cmpq %rbx, %rbp
jae 0xebab
cmpq %r12, %r14
jae 0xebab
movq (%r13,%rbp,8), %rdi
movq %r14, %rsi
shlq $0x4, %rsi
addq 0x28(%rsp), %rsi
callq 0x55b1
testl %eax, %eax
jle 0xeb88
leaq 0x158(%rsp), %rdi
movq %r14, %rsi
callq 0xee24
pushq $0x1
popq %rax
jmp 0xeba6
movl %eax, %r15d
leaq 0x138(%rsp), %rdi
movq %rbp, %rsi
callq 0xecfc
incq %rbp
xorl %eax, %eax
testl %r15d, %r15d
sete %al
addq %rax, %r14
jmp 0xeb4f
movq 0x10(%rsp), %r15
movq (%rsp), %r13
cmpq %rbx, %rbp
jae 0xebce
leaq 0x138(%rsp), %rdi
movq %rbp, %rsi
callq 0xecfc
incq %rbp
jmp 0xebb4
leaq 0x158(%rsp), %rbx
cmpq %r12, %r14
jae 0xebeb
movq %rbx, %rdi
movq %r14, %rsi
callq 0xee24
incq %r14
jmp 0xebd6
andq $0x0, (%r13)
movq 0x60(%rsp), %rdi
movl $0x6d072210, %esi       # imm = 0x6D072210
callq 0x7df6
movq 0x60(%rsp), %rdi
movl 0xc0(%rsp), %esi
callq 0x7df6
movq 0x60(%rsp), %rdi
movl 0x90(%rsp), %esi
movq 0x98(%rsp), %rdx
callq 0x11b5e
movq 0x60(%rsp), %rdi
movl 0xa0(%rsp), %esi
movq 0xa8(%rsp), %rdx
callq 0x11b5e
movq 0x60(%rsp), %rdi
movl 0xb0(%rsp), %esi
movq 0xb8(%rsp), %rdx
callq 0x11b5e
leaq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x11c22
movl %eax, %ebx
leaq 0x40(%rsp), %rdi
callq 0x11bb0
leaq 0xe0(%rsp), %rdi
callq 0xf11c
movq 0x18(%rsp), %rax
movq %rax, 0x18(%r15)
leaq 0xc(%rsp), %rdi
callq 0x5488
leaq 0xc8(%rsp), %rdi
callq 0x55d6
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 0xece1
jmp 0xecc6
jmp 0xecc6
jmp 0xecc6
jmp 0xecc6
jmp 0xecc6
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x18(%rax)
leaq 0xc(%rsp), %rdi
callq 0x5488
leaq 0xc8(%rsp), %rdi
callq 0x55d6
movq %rbx, %rdi
callq 0x35e0