// File: /workspace/github_c_makefile_star2_O3/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), %rax
movq %rax, 0x8(%rsp)
leaq 0x10(%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 0x15d(%rip), %rdx       # 0x13132
movq %rax, %rsi
callq 0xd98b
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
leaq 0x1b6(%rip), %rcx       # 0x131a4
movl $0x8, %edx
callq 0x3620
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
leaq 0x19b(%rip), %rcx       # 0x131a4
movl $0x8, %edx
callq 0x3620
leaq 0x60(%rsp), %rdi
callq 0xdc4a
movq 0x28(%rsp), %r13
testq %r13, %r13
je 0x13064
movq 0x20(%rsp), %rbp
shlq $0x3, %r13
xorl %r12d, %r12d
leaq 0x60(%rsp), %r14
movq (%rbp,%r12), %r15
movq %r15, %rdi
callq 0x3150
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xdb34
movq %r14, %rdi
callq 0xddcb
addq $0x8, %r12
cmpq %r12, %r13
jne 0x13038
movq 0x40(%rsp), %r13
testq %r13, %r13
je 0x130ab
movq 0x38(%rsp), %rbp
shlq $0x3, %r13
xorl %r12d, %r12d
leaq 0x60(%rsp), %r14
movq (%rbp,%r12), %r15
movq %r15, %rdi
callq 0x3150
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xdb34
movq %r14, %rdi
callq 0xddcb
addq $0x8, %r12
cmpq %r12, %r13
jne 0x1307f
leaq 0x60(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xdc61
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
je 0x130e1
movq 0x18(%rsp), %rdi
callq 0xe88a
leaq 0x38(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x130f5
movq 0x18(%rsp), %rdi
callq 0xe88a
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rbx)
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rdx
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x13120
jmp 0x13120
jmp 0x13120
movq 0x8(%rsp), %rcx
movq %rcx, 0x18(%rbx)
movq %rax, %rdi
callq 0x35e0
nop