// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]tundra/src/FileSign.cpp
// Function: t2::CalculateGlobSignatureFor(char const*, t2::MemAllocHeap*, t2::MemAllocLinear*)
t2::HashDigest CalculateGlobSignatureFor(const char* path, t2::MemAllocHeap* heap, t2::MemAllocLinear* scratch)
{
    // Helper for directory iteration + memory allocation of strings.  We need to
    // buffer the filenames as we need them in sorted order to ensure the results
    // are consistent between runs.
    struct IterContext
    {
      MemAllocLinear       *m_Allocator;
      MemAllocHeap         *m_Heap;
      Buffer<const char *>  m_Dirs;
      Buffer<const char *>  m_Files;

      void Init(MemAllocHeap* heap, MemAllocLinear* linear)
      {
        m_Allocator = linear;
        m_Heap      = heap;
        BufferInit(&m_Dirs);
        BufferInit(&m_Files);
      }

      void Destroy()
      {
        BufferDestroy(&m_Files, m_Heap);
        BufferDestroy(&m_Dirs, m_Heap);
      }

      static void Callback(void* user_data, const FileInfo& info, const char* path)
      {
        IterContext* self = (IterContext*) user_data;
        char* data = StrDup(self->m_Allocator, path);
        Buffer<const char*>* target = info.IsDirectory() ? &self->m_Dirs : &self->m_Files;
        BufferAppendOne(target, self->m_Heap, data);
      }

      static int SortStringPtrs(const void* l, const void* r)
      {
        return strcmp(*(const char**)l, *(const char**)r);
      }
    };

    // Set up to rewind allocator for each loop iteration
    MemAllocLinearScope mem_scope(scratch);

    // Set up context
    IterContext ctx;
    ctx.Init(heap, scratch);

    // Get directory data
    ListDirectory(path, &ctx, IterContext::Callback);

    // Sort data
    qsort(ctx.m_Dirs.m_Storage, ctx.m_Dirs.m_Size, sizeof(const char*), IterContext::SortStringPtrs);
    qsort(ctx.m_Files.m_Storage, ctx.m_Files.m_Size, sizeof(const char*), IterContext::SortStringPtrs);

    // Compute digest
    HashState h;
    HashInit(&h);
    for (const char* p : ctx.m_Dirs)
    {
      HashAddPath(&h, p);
      HashAddSeparator(&h);
    }

    for (const char* p : ctx.m_Files)
    {
      HashAddPath(&h, p);
      HashAddSeparator(&h);
    }

    HashDigest digest;
    HashFinalize(&h, &digest);

    ctx.Destroy();
    return digest;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, %rbx
movq 0x18(%rdx), %r14
leaq 0x20(%rsp), %rax
movq %rdx, (%rax)
movq %rsi, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
leaq 0x120(%rip), %rdx       # 0x11848
movq %rax, %rsi
callq 0xca82
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x13f(%rip), %rcx       # 0x11880
pushq $0x8
popq %rdx
callq 0x3620
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
leaq 0x126(%rip), %rcx       # 0x11880
pushq $0x8
popq %rdx
callq 0x3620
movq %r14, 0x8(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xcd00
leaq 0x48(%rsp), %r15
movq -0x18(%r15), %rbp
movq -0x10(%r15), %r14
shlq $0x3, %r14
xorl %r13d, %r13d
leaq 0x60(%rsp), %r12
cmpq %r13, %r14
je 0x117aa
movq (%rbp,%r13), %rsi
movq %r12, %rdi
callq 0x557d
movq %r12, %rdi
callq 0xce0b
addq $0x8, %r13
jmp 0x1178a
movq 0x48(%rsp), %rbp
movq 0x50(%rsp), %r14
shlq $0x3, %r14
xorl %r13d, %r13d
leaq 0x60(%rsp), %r12
cmpq %r13, %r14
je 0x117e0
movq (%rbp,%r13), %rsi
movq %r12, %rdi
callq 0x557d
movq %r12, %rdi
callq 0xce0b
addq $0x8, %r13
jmp 0x117c0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xcd14
movq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0xc214
movq 0x28(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0xc214
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rbx)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1183b
jmp 0x11836
jmp 0x11836
movq 0x8(%rsp), %r14
movq %r14, 0x18(%rbx)
movq %rax, %rdi
callq 0x35e0
nop