// File: /workspace/github_cpp_makefile_star2_O2/Fohlen[P]cubescript-standalone/stream.cpp
// Function: listdir(char const*, bool, char const*, vector<char*>&)
bool listdir(const char *dirname, bool rel, const char *ext, vector<char *> &files)
{
    size_t extsize = ext ? strlen(ext)+1 : 0;
#ifdef WIN32
    defformatstring(pathname)(rel ? ".\\%s\\*.%s" : "%s\\*.%s", dirname, ext ? ext : "*");
    WIN32_FIND_DATA FindFileData;
    HANDLE Find = FindFirstFile(pathname, &FindFileData);
    if(Find != INVALID_HANDLE_VALUE)
    {
        do {
            if(!ext) files.add(newstring(FindFileData.cFileName));
            else
            {
                size_t namelen = strlen(FindFileData.cFileName); 
                if(namelen > extsize) 
                { 
                    namelen -= extsize;
                    if(FindFileData.cFileName[namelen] == '.' && strncmp(FindFileData.cFileName+namelen+1, ext, extsize-1)==0)
                        files.add(newstring(FindFileData.cFileName, namelen));
                }
            }
        } while(FindNextFile(Find, &FindFileData));
        FindClose(Find);
        return true;
    }
#else
    defformatstring(pathname)(rel ? "./%s" : "%s", dirname);
    DIR *d = opendir(pathname);
    if(d)
    {
        struct dirent *de;
        while((de = readdir(d)) != NULL)
        {
            if(!ext) files.add(newstring(de->d_name));
            else
            {
                size_t namelen = strlen(de->d_name);
                if(namelen > extsize)
                {
                    namelen -= extsize;
                    if(de->d_name[namelen] == '.' && strncmp(de->d_name+namelen+1, ext, extsize-1)==0)
                        files.add(newstring(de->d_name, namelen));
                }
            }
        }
        closedir(d);
        return true;
    }
#endif
    else return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rcx, -0x38(%rbp)
movq %rdx, %r14
movl %esi, %r13d
movq %rdi, %r12
testq %rdx, %rdx
je 0xe7d3
movq %r14, %rdi
callq 0x20a0
movq %rax, %r15
incq %r15
jmp 0xe7d6
xorl %r15d, %r15d
leaq -0x150(%rbp), %rbx
leaq -0x30(%rbp), %rdi
movq %rbx, (%rdi)
leaq 0x3fdc(%rip), %rax      # 0x127c7
leaq 0x3438(%rip), %rsi      # 0x11c2a
testb %r13b, %r13b
cmovneq %rax, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xd450
movq %rbx, %rdi
callq 0x22d0
movq %rax, %r12
testq %rax, %rax
je 0xe897
leaq -0x1(%r15), %rax
movq %rax, -0x40(%rbp)
movq %r12, %rdi
callq 0x20f0
testq %rax, %rax
je 0xe88f
movq %rax, %r13
addq $0x13, %r13
testq %r14, %r14
je 0xe874
movq %r13, %rdi
callq 0x20a0
movq %rax, %rbx
subq %r15, %rbx
jbe 0xe81f
cmpb $0x2e, (%r13,%rbx)
jne 0xe81f
leaq (%rbx,%r13), %rdi
incq %rdi
movq %r14, %rsi
movq -0x40(%rbp), %rdx
callq 0x20c0
testl %eax, %eax
jne 0xe81f
movq %r13, %rdi
movq %rbx, %rsi
callq 0xd339
jmp 0xe87c
movq %r13, %rdi
callq 0xd0ec
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xd364
jmp 0xe81f
movq %r12, %rdi
callq 0x2350
testq %r12, %r12
setne %al
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq