// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x6e40
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x205e0
jmp 0x203c7
movq -0x18(%rbp), %rdi
leaq 0x25e(%rip), %rdx       # 0x20630
leaq -0x78(%rbp), %rsi
callq 0x16b70
jmp 0x203dd
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq 0x2c4(%rip), %rcx       # 0x206b0
movl $0x8, %edx
callq 0x3600
jmp 0x203f8
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq 0x2a9(%rip), %rcx       # 0x206b0
movl $0x8, %edx
callq 0x3600
jmp 0x20413
leaq -0x130(%rbp), %rdi
callq 0x16f20
jmp 0x20421
leaq -0x68(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rdi
callq 0x1d7d0
movq %rax, -0x178(%rbp)
jmp 0x20441
movq -0x178(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x138(%rbp), %rdi
callq 0x1d7f0
movq %rax, -0x180(%rbp)
jmp 0x20464
movq -0x180(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rax
cmpq -0x148(%rbp), %rax
je 0x204e9
movq -0x140(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rsi
leaq -0x130(%rbp), %rdi
callq 0x6ef0
jmp 0x204a8
leaq -0x130(%rbp), %rdi
callq 0x17130
jmp 0x204b6
jmp 0x204b8
movq -0x140(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x140(%rbp)
jmp 0x20472
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6f50
jmp 0x205cb
leaq -0x50(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rdi
callq 0x1d7d0
movq %rax, -0x188(%rbp)
jmp 0x20509
movq -0x188(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x158(%rbp), %rdi
callq 0x1d7f0
movq %rax, -0x190(%rbp)
jmp 0x2052c
movq -0x190(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x160(%rbp), %rax
cmpq -0x168(%rbp), %rax
je 0x20594
movq -0x160(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rsi
leaq -0x130(%rbp), %rdi
callq 0x6ef0
jmp 0x20570
leaq -0x130(%rbp), %rdi
callq 0x17130
jmp 0x2057e
jmp 0x20580
movq -0x160(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x160(%rbp)
jmp 0x2053a
leaq -0x130(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x16f70
jmp 0x205a6
leaq -0x78(%rbp), %rdi
callq 0x206e0
jmp 0x205b1
leaq -0x38(%rbp), %rdi
callq 0x6f50
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x35c0
nopw %cs:(%rax,%rax)