// File: /workspace/github_c_makefile_star2_O1/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rdx, %rbx
movq %rsi, 0x38(%rsp)
movq %rdi, %r14
movq $0x0, 0xd8(%rsp)
leaq 0xd2e4(%rip), %rax      # 0x1d5a0
addq $0x10, %rax
movq %rax, 0xe0(%rsp)
callq 0x60a6
movq %rax, 0xe8(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0xd39c(%rip), %rax      # 0x1d680
cmpb $0x1, (%rax)
jne 0x102f7
leaq 0x6dbb(%rip), %rdi      # 0x170ab
xorl %esi, %esi
callq 0xfb72
movq %r14, 0x18(%rsp)
movq 0x48(%r14), %r14
movq 0x18(%r14), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x15c(%rsp)
movaps %xmm0, 0x150(%rsp)
movq %rbx, 0x170(%rsp)
movq $0x0, 0x178(%rsp)
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
movq %r14, 0x28(%rsp)
callq 0x13bf4
leaq 0x50(%rsp), %rdi
callq 0x13e05
leaq 0x50(%rsp), %rdi
movq %rax, 0x20(%rdi)
callq 0x13e05
leaq 0x50(%rsp), %rdi
movq %rax, 0x28(%rdi)
callq 0x13e05
leaq 0x50(%rsp), %rdi
movq %rax, 0x30(%rdi)
callq 0x13e05
leaq 0x50(%rsp), %rdi
movq %rax, 0x38(%rdi)
callq 0x13e05
leaq 0x50(%rsp), %r13
movq %rax, 0x40(%r13)
movq %r13, %rdi
callq 0x13e05
movq %rax, 0x98(%rsp)
movq 0x78(%rsp), %rdi
callq 0x13bed
movl %eax, 0xa0(%rsp)
movq %rdx, 0xa8(%rsp)
movq 0x80(%rsp), %rdi
callq 0x13bed
movl %eax, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
movq 0x88(%rsp), %rdi
callq 0x13bed
movl %eax, 0xc0(%rsp)
movq %rdx, 0xc8(%rsp)
movl $0x0, 0xd0(%rsp)
movq 0x18(%rsp), %rax
movl 0x50(%rax), %r15d
leaq (,%r15,8), %rsi
movl $0x8, %edx
movq %r14, %rdi
movq %rax, %r14
callq 0xf030
movq %rax, %r12
movl 0x54(%r14), %eax
testq %rax, %rax
je 0x1044d
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x58(%r14), %rsi
movq (%rsi,%rcx,8), %rsi
testq %rsi, %rsi
je 0x10445
movl %edx, %edi
incl %edx
movq %rsi, (%r12,%rdi,8)
movq 0x28(%rsi), %rsi
jmp 0x10432
incq %rcx
cmpq %rax, %rcx
jne 0x1042a
testq %r15, %r15
je 0x10487
leaq (%r12,%r15,8), %rbx
bsrq %r15, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x37c(%rip), %rcx       # 0x107e6
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10c10
leaq 0x36a(%rip), %rdx       # 0x107e6
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10d3f
movq (%r14), %rax
testq %rax, %rax
je 0x10499
movl 0x4(%rax), %ecx
movq %rcx, 0x8(%rsp)
jmp 0x104a2
movq $0x0, 0x8(%rsp)
testq %rax, %rax
je 0x104ce
movslq 0x8(%rax), %rcx
testq %rcx, %rcx
leaq 0x8(%rcx,%rax), %rdx
cmoveq %rcx, %rdx
movq %rdx, 0x30(%rsp)
movslq 0xc(%rax), %rdx
testq %rdx, %rdx
leaq 0xc(%rdx,%rax), %rcx
cmoveq %rdx, %rcx
jmp 0x104d9
movq $0x0, 0x30(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x48(%rsp)
movq %r15, %rbx
testq %rax, %rax
je 0x104f8
movslq 0x10(%rax), %rcx
testq %rcx, %rcx
leaq 0x10(%rcx,%rax), %rax
cmoveq %rcx, %rax
jmp 0x104fa
xorl %eax, %eax
leaq 0x1a0(%rsp), %r14
movq %rax, (%r14)
movq 0x18(%rsp), %rax
movq 0x60(%rax), %rax
leaq 0x198(%rsp), %r15
movq %rax, (%r15)
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x3300
leaq 0x190(%rsp), %rcx
movq %rax, (%rcx)
leaq -0x93a80(%rax), %rdx
leaq 0x188(%rsp), %rsi
movq %rdx, (%rsi)
leaq 0x30(%rsp), %rdx
movq (%rdx), %rdi
movq %rdi, 0x40(%rsp)
movq %r13, 0xf0(%rsp)
movq %r12, 0xf8(%rsp)
movq %rax, 0x100(%rsp)
leaq 0x150(%rsp), %rax
movq %rax, 0x108(%rsp)
movq %r14, 0x110(%rsp)
movq %r15, 0x118(%rsp)
movq %rcx, 0x120(%rsp)
movq %rsi, 0x128(%rsp)
movq %r13, 0x130(%rsp)
movq %rax, 0x138(%rsp)
movq %rdx, 0x140(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x148(%rsp)
movq %rbx, %r15
testq %rbx, %rbx
je 0x1066b
movl $0x0, %r13d
cmpq $0x0, 0x8(%rsp)
je 0x1066e
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %r15, %rbx
movq (%r12,%r13,8), %rax
movq %rbp, %rcx
shlq $0x4, %rcx
movq (%rax), %rdx
xorl %esi, %esi
movq 0x40(%rsp), %rdi
cmpq (%rdi,%rcx), %rdx
seta %sil
sbbl $0x0, %esi
movq 0x8(%rax), %rax
xorl %r14d, %r14d
cmpq 0x8(%rdi,%rcx), %rax
seta %r14b
sbbl $0x0, %r14d
testl %esi, %esi
cmovnel %esi, %r14d
testl %r14d, %r14d
jle 0x10636
movl $0x1, %r15d
leaq 0x110(%rsp), %rdi
movq %rbp, %rsi
callq 0x10962
jmp 0x10653
leaq 0xf0(%rsp), %rdi
movq %r13, %rsi
callq 0x1080c
incq %r13
xorl %r15d, %r15d
testl %r14d, %r14d
sete %r15b
addq %r15, %rbp
movq %rbx, %r15
cmpq %rbx, %r13
jae 0x1066e
cmpq 0x8(%rsp), %rbp
jb 0x105dc
jmp 0x1066e
xorl %r13d, %r13d
cmpq %r15, %r13
movq 0x28(%rsp), %r14
jae 0x10693
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1080c
incq %r13
cmpq %r13, %r15
jne 0x10680
cmpq 0x8(%rsp), %rbp
jae 0x106b7
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x10962
incq %rbp
cmpq %rbp, 0x8(%rsp)
jne 0x106a2
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
movq 0x70(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movl $0x6d072210, (%rsi)     # imm = 0x6D072210
movl $0x4, %edx
callq 0x13b75
movq 0x70(%rsp), %rdi
movl 0xd0(%rsp), %eax
leaq 0x110(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
callq 0x13b75
movq 0x70(%rsp), %rdi
movl 0xa0(%rsp), %esi
movq 0xa8(%rsp), %rdx
callq 0x13ba1
movq 0x70(%rsp), %rdi
movl 0xb0(%rsp), %esi
movq 0xb8(%rsp), %rdx
callq 0x13ba1
movq 0x70(%rsp), %rdi
movl 0xc0(%rsp), %esi
movq 0xc8(%rsp), %rdx
callq 0x13ba1
leaq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x13ccc
movl %eax, %ebx
leaq 0x50(%rsp), %rdi
callq 0x13c07
leaq 0x150(%rsp), %rdi
callq 0x10bd2
movq 0x20(%rsp), %rax
movq %rax, 0x18(%r14)
leaq 0x14(%rsp), %rdi
callq 0x5b82
leaq 0xd8(%rsp), %rdi
callq 0x5bac
movl %ebx, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x107d1
jmp 0x107b6
jmp 0x107b6
jmp 0x107b6
jmp 0x107b6
jmp 0x107b6
movq %rax, %rbx
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x14(%rsp), %rdi
callq 0x5b82
leaq 0xd8(%rsp), %rdi
callq 0x5bac
movq %rbx, %rdi
callq 0x35e0