// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movq %rcx, %r15
movq %rdx, 0x28(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
movq $0x0, 0x60(%rsp)
movl $0xa0, %eax
addq 0xa890(%rip), %rax      # 0x1cfc0
movq %rax, 0x68(%rsp)
callq 0x5f2a
movq %rax, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
callq 0x134bc
leaq 0x78(%rsp), %rdi
callq 0x136d3
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x136d3
movq %rax, %r14
movq %rbx, 0x40(%rsp)
movq %r15, 0x38(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x136d3
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x134b5
movq %rdx, 0x30(%rsp)
movl %eax, 0x14(%rsp)
movq %r12, 0x18(%rsp)
movl 0xb0(%r12), %eax
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x128b8
movq 0x18(%rsp), %rax
movq 0xa0(%rax), %rcx
movq %rcx, 0x50(%rsp)
movq 0xa8(%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0xc8(%rax), %rbp
xorl %r15d, %r15d
leaq 0x8(%rsp), %rbx
movq 0x50(%rsp), %rax
movl (%rax,%r15,4), %r13d
testl %r13d, %r13d
je 0x128a6
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %r12
movq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
movl $0x8, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1343d
movq 0x18(%rbp), %rax
movq %rax, 0x8(%rsp)
movl $0x8, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1343d
movl %r13d, 0x8(%rsp)
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1343d
movl $0x10, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1343d
movq 0x20(%rsp), %rdi
callq 0x134b5
movq %r14, %rdi
movl %eax, %esi
callq 0x13469
movq %r12, %rdi
callq 0x3150
leaq 0x1(%rax), %rdx
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x1343d
movl $0x0, 0x8(%rsp)
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1343d
movl $0x0, 0x8(%rsp)
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1343d
incq %r15
addq $0x20, %rbp
cmpq %r15, 0x58(%rsp)
jne 0x127d7
leaq 0x8(%rsp), %rsi
movl $0x6a683db8, (%rsi)     # imm = 0x6A683DB8
movl $0x4, %edx
movq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x1343d
movq 0x18(%rsp), %rbx
movl 0xb8(%rbx), %eax
leaq 0x8(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0x1343d
movq %r14, %rdi
movl 0x14(%rsp), %esi
movq 0x30(%rsp), %rdx
callq 0x13469
leaq 0x80(%rbx), %rdi
callq 0xe9a4
movq $0x0, 0x38(%rbx)
leaq 0x78(%rsp), %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
callq 0x1359a
testb %al, %al
je 0x12940
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x6054
movl %eax, %ebx
jmp 0x1294a
movq %rbx, %rdi
callq 0x3600
xorl %ebx, %ebx
leaq 0x78(%rsp), %rdi
callq 0x134cf
leaq 0x60(%rsp), %rdi
callq 0x5a30
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12980
jmp 0x12980
jmp 0x12980
jmp 0x12980
jmp 0x12980
jmp 0x12980
jmp 0x12980
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x5a30
movq %rbx, %rdi
callq 0x35e0