// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %r15, %rdi
callq 0x3120
testq %rax, %rax
je 0x9275
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
pushq $0xa
popq %r14
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x3320
movslq %ebp, %r13
testq %rax, %rax
je 0x924a
movq %rax, %rbx
addq $0x13, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8eb0
leaq 0xb4f3(%rip), %rdi      # 0x146a0
leaq 0x18(%rsp), %rsi
callq 0x32c0
movl 0x30(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x9180
cmpl %r14d, %ebp
jl 0x91e7
addl %r14d, %r14d
movslq %r14d, %rax
imulq $0x50, %rax, %rsi
movq 0x8(%rsp), %rdi
callq 0x33b0
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x3130
cmpl $0x28, %eax
jl 0x920a
leaq 0x50ad(%rip), %rdi      # 0xe2a8
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3170
jmp 0x9180
imulq $0x50, %r13, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax), %r13
addq $0x18, %r13
movq %r13, %rdi
movq %rbx, %rsi
pushq $0x28
popq %rdx
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)
incl %ebp
jmp 0x9180
movq %r12, %rdi
callq 0x31f0
leaq 0x4f(%rip), %rcx        # 0x92a8
pushq $0x50
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x30d0
movq 0x10(%rsp), %rax
movl %ebp, (%rax)
jmp 0x9293
leaq 0x9d4c(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x500e(%rip), %rdi      # 0xe294
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq