// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]tundra/src/DigestCache.cpp
// Function: t2::DigestCacheSave(t2::DigestCache*, t2::MemAllocHeap*, char const*, char const*)
bool DigestCacheSave(DigestCache* self, MemAllocHeap* serialization_heap, const char* filename, const char* tmp_filename)
{
  TimingScope timing_scope(nullptr, &g_Stats.m_DigestCacheSaveTimeCycles);

  BinaryWriter writer;
  BinaryWriterInit(&writer, serialization_heap);

  BinarySegment *main_seg   = BinaryWriterAddSegment(&writer);
  BinarySegment *array_seg  = BinaryWriterAddSegment(&writer);
  BinarySegment *string_seg = BinaryWriterAddSegment(&writer);
  BinaryLocator  array_ptr  = BinarySegmentPosition(array_seg);

  auto save_record = [=](size_t index, uint32_t hash, const char* path, const DigestCacheRecord& r)
  {
    BinarySegmentWriteUint64(array_seg, r.m_Timestamp);
    BinarySegmentWriteUint64(array_seg, r.m_AccessTime);
    BinarySegmentWriteUint32(array_seg, hash);
    BinarySegmentWrite(array_seg, &r.m_ContentDigest, sizeof(r.m_ContentDigest));
    BinarySegmentWritePointer(array_seg, BinarySegmentPosition(string_seg));
    BinarySegmentWriteStringData(string_seg, path);
    BinarySegmentWriteUint32(array_seg, 0); // m_Padding
#if ENABLED(USE_FAST_HASH)
    BinarySegmentWriteUint32(array_seg, 0); // m_Padding
#endif
  };

  HashTableWalk(&self->m_Table, save_record);

  BinarySegmentWriteUint32(main_seg, DigestCacheState::MagicNumber);
  BinarySegmentWriteInt32(main_seg, (int) self->m_Table.m_RecordCount);
  BinarySegmentWritePointer(main_seg, array_ptr);

  // Unmap old state to avoid sharing conflicts on Windows.
  MmapFileUnmap(&self->m_StateFile);
  self->m_State = nullptr;

  bool success = BinaryWriterFlush(&writer, tmp_filename);

  if (success)
  {
    success = RenameFile(tmp_filename, filename);
  }
  else
  {
    remove(tmp_filename);
  }

  BinaryWriterDestroy(&writer);

  return success;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, %r15
movq %rdx, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
andq $0x0, 0x50(%rsp)
movl $0xa0, %eax
addq 0xbfeb(%rip), %rax      # 0x1cfc0
movq %rax, 0x58(%rsp)
callq 0x5b35
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x11ba0
leaq 0x68(%rsp), %rdi
callq 0x11d4e
movq %rax, %r13
leaq 0x68(%rsp), %rdi
callq 0x11d4e
movq %rax, %r14
leaq 0x68(%rsp), %rdi
callq 0x11d4e
movq %rax, %rbx
movq %r13, 0x30(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, %rdi
callq 0x11b99
movl %eax, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
movq 0xa0(%r12), %rax
movq %rax, 0x40(%rsp)
movq 0xa8(%r12), %rax
movq %rax, 0x38(%rsp)
movq 0xc8(%r12), %r13
movq %r12, 0x28(%rsp)
movl 0xb0(%r12), %eax
movq %rax, 0x48(%rsp)
xorl %ebp, %ebp
cmpq %rbp, 0x48(%rsp)
je 0x110f9
movq 0x40(%rsp), %rax
movl (%rax,%rbp,4), %r15d
testl %r15d, %r15d
je 0x110ed
movq 0x38(%rsp), %rax
movq (%rax,%rbp,8), %r12
movq 0x10(%r13), %rsi
movq %r14, %rdi
callq 0x7e30
movq 0x18(%r13), %rsi
movq %r14, %rdi
callq 0x7e30
movq %r14, %rdi
movl %r15d, %esi
callq 0x7df6
movq %r14, %rdi
movq %r13, %rsi
pushq $0x10
popq %rdx
callq 0x11b32
movq %rbx, %rdi
callq 0x11b99
movq %r14, %rdi
movl %eax, %esi
callq 0x11b5e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7d4a
movq %r14, %rdi
xorl %esi, %esi
callq 0x7df6
movq %r14, %rdi
xorl %esi, %esi
callq 0x7df6
incq %rbp
addq $0x20, %r13
jmp 0x11069
movq 0x30(%rsp), %r14
movq %r14, %rdi
movl $0x6a683db8, %esi       # imm = 0x6A683DB8
callq 0x7df6
movq 0x28(%rsp), %rbx
movl 0xb8(%rbx), %esi
movq %r14, %rdi
callq 0x7e14
movq %r14, %rdi
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %rdx
callq 0x11b5e
leaq 0x80(%rbx), %rdi
callq 0xd8b4
andq $0x0, 0x38(%rbx)
leaq 0x68(%rsp), %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x11c22
testb %al, %al
je 0x11167
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x5c5d
movl %eax, %ebx
jmp 0x11171
movq %rbx, %rdi
callq 0x3600
xorl %ebx, %ebx
leaq 0x68(%rsp), %rdi
callq 0x11bb0
leaq 0x50(%rsp), %rdi
callq 0x55d6
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x111a7
jmp 0x111a7
jmp 0x111a7
jmp 0x111a7
jmp 0x111a7
jmp 0x111a7
jmp 0x111a7
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x55d6
movq %rbx, %rdi
callq 0x35e0