// File: 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
subq $0x190, %rsp            # imm = 0x190
movb %sil, %al
movq %rdi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x18f2c
movq -0x20(%rbp), %rdi
callq 0x20b0
addq $0x1, %rax
movq %rax, -0x178(%rbp)
jmp 0x18f37
xorl %eax, %eax
movq %rax, -0x178(%rbp)
jmp 0x18f37
movq -0x178(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x140(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x167a0
movb -0x11(%rbp), %cl
leaq 0x533b(%rip), %rsi      # 0x1e29a
leaq 0x5ed1(%rip), %rax      # 0x1ee37
testb $0x1, %cl
cmovneq %rax, %rsi
movq -0x10(%rbp), %rdx
leaq -0x148(%rbp), %rdi
movb $0x0, %al
callq 0x167c0
leaq -0x140(%rbp), %rdi
callq 0x2300
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x150(%rbp)
je 0x190d8
jmp 0x18fa2
movq -0x150(%rbp), %rdi
callq 0x2100
movq %rax, -0x158(%rbp)
cmpq $0x0, %rax
je 0x190c6
cmpq $0x0, -0x20(%rbp)
jne 0x19000
movq -0x28(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x158(%rbp), %rdi
addq $0x13, %rdi
callq 0x15990
movq -0x180(%rbp), %rdi
movq %rax, -0x160(%rbp)
leaq -0x160(%rbp), %rsi
callq 0x161d0
jmp 0x190c1
movq -0x158(%rbp), %rdi
addq $0x13, %rdi
callq 0x20b0
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
cmpq -0x30(%rbp), %rax
jbe 0x190bf
movq -0x30(%rbp), %rcx
movq -0x168(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x168(%rbp)
movq -0x158(%rbp), %rax
movq -0x168(%rbp), %rcx
movsbl 0x13(%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0x190bd
movq -0x158(%rbp), %rdi
addq $0x13, %rdi
addq -0x168(%rbp), %rdi
addq $0x1, %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
subq $0x1, %rdx
callq 0x20d0
cmpl $0x0, %eax
jne 0x190bd
movq -0x28(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x158(%rbp), %rdi
addq $0x13, %rdi
movq -0x168(%rbp), %rsi
callq 0x16150
movq -0x188(%rbp), %rdi
movq %rax, -0x170(%rbp)
leaq -0x170(%rbp), %rsi
callq 0x161d0
jmp 0x190bf
jmp 0x190c1
jmp 0x18fa2
movq -0x150(%rbp), %rdi
callq 0x2370
movb $0x1, -0x1(%rbp)
jmp 0x190dc
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopw (%rax,%rax)