// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/util.c
// Function: GetSortedDir
DirEntry_t * GetSortedDir(char * Directory, int * NumFiles)
{
    DirEntry_t * FileNames;
    int NumFileNames;
    int NumAllocated;
    DIR * dirp;

    NumAllocated = 10;
    FileNames = malloc(sizeof (DirEntry_t) * NumAllocated);

    NumFileNames = 0;
  
    dirp = opendir(Directory);
    if (dirp == NULL){
        fprintf(Log, "could not open dir\n");
        return NULL;
    }

    for (;;){
        struct dirent * dp;
        struct stat buf;
        int l;
        dp = readdir(dirp);
        if (dp == NULL) break;
        //printf("name: %s %d %d\n",dp->d_name, (int)dp->d_off, (int)dp->d_reclen);

        // Check that it's a regular file.
        stat(CatPath(Directory, dp->d_name), &buf);
        if (!S_ISREG(buf.st_mode)) continue; // not a file.

        if (NumFileNames >= NumAllocated){
            NumAllocated *= 2;
            FileNames = realloc(FileNames, sizeof (DirEntry_t) * NumAllocated);
        }
        l = strlen(dp->d_name);
        if (l >= 40){
            printf("Filename too long %s",dp->d_name);
            continue;
        }
        strncpy(FileNames[NumFileNames].FileName,dp->d_name,40);
        FileNames[NumFileNames].MTime = buf.st_mtime;
        FileNames[NumFileNames].ATime = buf.st_atime;
        FileNames[NumFileNames].FileSize = buf.st_size;
        NumFileNames += 1;
    }
    closedir(dirp);

    // Now sort the names (could be in random order)
    qsort(FileNames, NumFileNames, sizeof(DirEntry_t), fncmpfunc);

    *NumFiles = NumFileNames;
    return FileNames;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movl $0x320, %edi            # imm = 0x320
callq 0x3340
movq %rax, %r13
movq %r15, %rdi
callq 0x3120
testq %rax, %rax
je 0x9bf0
movq %rax, %r12
movq %rbx, 0x10(%rsp)
movq %rax, %rdi
callq 0x3320
testq %rax, %rax
je 0x9c15
movq %rax, %rbx
movl $0xa, %r14d
xorl %ebp, %ebp
addq $0x13, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x9828
leaq 0xab70(%rip), %rdi      # 0x14690
leaq 0x18(%rsp), %rsi
callq 0x32c0
movl 0x30(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x9bda
cmpl %r14d, %ebp
jl 0x9b5e
addl %r14d, %r14d
movslq %r14d, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rsi
movq %r13, %rdi
callq 0x33a0
movq %rax, %r13
movq %rbx, %rdi
callq 0x3130
cmpl $0x28, %eax
jl 0x9b7e
leaq 0x4766(%rip), %rdi      # 0xe2d8
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
jmp 0x9bda
movslq %ebp, %rax
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
movl %ebp, 0xc(%rsp)
movl %r14d, %ebp
movq %r12, %r14
movq %r15, %r12
movq %r13, %r15
leaq 0x18(%r13,%rax), %r13
movl $0x28, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3090
movq 0x70(%rsp), %rax
movq %rax, -0x10(%r13)
movq 0x60(%rsp), %rax
movq %rax, -0x8(%r13)
movl 0x48(%rsp), %eax
movl %eax, -0x18(%r13)
movq %r15, %r13
movq %r12, %r15
movq %r14, %r12
movl %ebp, %r14d
movl 0xc(%rsp), %ebp
incl %ebp
movq %r12, %rdi
callq 0x3320
movq %rax, %rbx
testq %rax, %rax
jne 0x9b0a
jmp 0x9c17
leaq 0x93c1(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x46c3(%rip), %rdi      # 0xe2c4
movl $0x13, %esi
movl $0x1, %edx
callq 0x34a0
xorl %r13d, %r13d
jmp 0x9c40
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x31f0
movslq %ebp, %rbx
leaq 0x2c(%rip), %rcx        # 0x9c55
movl $0x50, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x30d0
movq 0x10(%rsp), %rax
movl %ebx, (%rax)
movq %r13, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq