// File: /workspace/github_c_makefile_star2_O1/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, %rbp
movl $0x320, %edi            # imm = 0x320
callq 0x3330
movq %rax, (%rsp)
movq %rbp, %rdi
callq 0x3120
testq %rax, %rax
je 0x9c26
movq %rax, %r12
movq %rbx, 0x8(%rsp)
movl $0xa, %r15d
xorl %r14d, %r14d
movq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x3310
movq %rax, %rbx
testq %rax, %rax
je 0x9bf1
leaq 0x13(%rbx), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x9834
leaq 0xab53(%rip), %rdi      # 0x14690
leaq 0x18(%rsp), %rsi
callq 0x32b0
movl 0x30(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x9bf1
cmpl %r15d, %r14d
jl 0x9b7d
addl %r15d, %r15d
movslq %r15d, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rsi
movq (%rsp), %rdi
callq 0x3390
movq %rax, (%rsp)
movq %r13, %rdi
callq 0x3130
cmpl $0x28, %eax
jl 0x9b9d
leaq 0x46d7(%rip), %rdi      # 0xe268
movq %r13, %rsi
xorl %eax, %eax
callq 0x3170
jmp 0x9bf1
movslq %r14d, %rax
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
movl %r15d, %r12d
movq %rbp, %r15
movq (%rsp), %rcx
leaq (%rcx,%rax), %rbp
addq $0x18, %rbp
movl $0x28, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3090
movq 0x70(%rsp), %rax
movq %rax, -0x10(%rbp)
movq 0x60(%rsp), %rax
movq %rax, -0x8(%rbp)
movl 0x48(%rsp), %eax
movl %eax, -0x18(%rbp)
movq %r15, %rbp
movl %r12d, %r15d
movq 0x10(%rsp), %r12
incl %r14d
testq %rbx, %rbx
jne 0x9b13
movq %r12, %rdi
callq 0x31f0
movslq %r14d, %rbx
leaq 0x58(%rip), %rcx        # 0x9c64
movl $0x50, %edx
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x30d0
movq 0x8(%rsp), %rax
movl %ebx, (%rax)
jmp 0x9c4e
leaq 0x938b(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x461d(%rip), %rdi      # 0xe254
movl $0x13, %esi
movl $0x1, %edx
callq 0x3490
movq $0x0, (%rsp)
movq (%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq