// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq 0xff9e(%rip), %rdx      # 0x2f590
addq $0xa0, %rdx
xorl %eax, %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
callq 0x7220
movq -0x10(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x20e50
jmp 0x1f615
leaq -0x58(%rbp), %rdi
callq 0x21190
movq %rax, -0xd0(%rbp)
jmp 0x1f627
movq -0xd0(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x21190
movq %rax, -0xd8(%rbp)
jmp 0x1f644
movq -0xd8(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x21190
movq %rax, -0xe0(%rbp)
jmp 0x1f661
movq -0xe0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
callq 0x20e20
movq %rdx, -0xf0(%rbp)
movl %eax, -0xe4(%rbp)
jmp 0x1f684
movq -0xf0(%rbp), %rax
movl -0xe4(%rbp), %ecx
movl %ecx, -0x90(%rbp)
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
addq $0xa0, %rdi
movups -0xa0(%rbp), %xmm0
movaps %xmm0, -0xb0(%rbp)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
callq 0x1f800
jmp 0x1f6e2
movq -0x70(%rbp), %rdi
movl $0x6a683db8, %esi       # imm = 0x6A683DB8
callq 0xbaa0
jmp 0x1f6f2
movq -0x70(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0xb8(%rax), %esi
callq 0xbb00
jmp 0x1f707
movq -0x70(%rbp), %rdi
movups -0x90(%rbp), %xmm0
movaps %xmm0, -0xc0(%rbp)
movl -0xc0(%rbp), %esi
movq -0xb8(%rbp), %rdx
callq 0x20db0
jmp 0x1f72d
movq -0x8(%rbp), %rdi
subq $-0x80, %rdi
callq 0x18c60
jmp 0x1f73c
movq -0x8(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x20f60
movb %al, -0xf1(%rbp)
jmp 0x1f75d
movb -0xf1(%rbp), %al
andb $0x1, %al
movb %al, -0xc1(%rbp)
testb $0x1, -0xc1(%rbp)
je 0x1f7b0
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7f20
movb %al, -0xf2(%rbp)
jmp 0x1f789
movb -0xf2(%rbp), %al
andb $0x1, %al
movb %al, -0xc1(%rbp)
jmp 0x1f7b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7270
jmp 0x1f7ea
movq -0x20(%rbp), %rdi
callq 0x35e0
leaq -0x58(%rbp), %rdi
callq 0x20e80
jmp 0x1f7c4
movb -0xc1(%rbp), %al
movb %al, -0xf3(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7270
movb -0xf3(%rbp), %al
andb $0x1, %al
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x35c0
nopw %cs:(%rax,%rax)