// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/src/datascan.c
// Function: ImportFilesFromDir
static int ImportFilesFromDir(const char *path, struct importdatameta *cbs)
{
    BDIR *dir;
    struct Bdirent *dirent;
    int found = 0, errors = 0;
    size_t subpathlen;
    char *subpath;
    static const char *pats[] = {
        "tiles???.art",
        "beach?.map",
        "city?.map",
        "final?.map",
        "free?.map",
        "inds?.map",
        "level?.map",
        "load.map",
        "matrix.map",
        "mid?.map",
        "net?.map",
        "park?.map",
        "sewer?.map",
        "subway?.map",
        "ware*.map",
        "lookup.dat",
        "nlookup.dat",
        "songs",
        "sounds",
        "*.smk",
        NULL
    };

    cbs->progress(cbs->data, path);

    dir = Bopendir(path);
    if (!dir) return IMPORTDATA_OK;    // Maybe no permission.

    while ((dirent = Breaddir(dir))) {
        if (cbs->cancelled(cbs->data)) break;

        if (dirent->name[0] == '.' &&
            (dirent->name[1] == 0 || (dirent->name[1] == '.' && dirent->name[2] == 0)))
                continue;
        if (!(dirent->mode & (BS_IFDIR|BS_IFREG))) continue; // Ignore non-directories and non-regular files.
        if (dirent->mode & BS_IFREG) {
            int i;
            for (i=0; pats[i]; i++) {
                if (Bwildmatch(dirent->name, pats[i])) break;
            }
            if (!pats[i]) continue;    // No match to any pattern.
        }

        subpathlen = strlen(path) + 1 + dirent->namlen + 1;
        subpath = malloc(subpathlen);
        if (!subpath) { Bclosedir(dir); break; }
        snprintf(subpath, subpathlen, "%s/%s", path, dirent->name);

        if (dirent->mode & BS_IFDIR) {
            switch (ImportFilesFromDir(subpath, cbs)) {
                case IMPORTDATA_COPIED: found = 1; break;
                case IMPORTDATA_ERROR: errors = 1; break;
            }
        }
        else {
            switch (ImportDataFromFile(subpath, (int)dirent->size, cbs)) {
                case IMPORTDATA_SKIPPED: buildprintf("Skipped %s\n", subpath); break;
                case IMPORTDATA_COPIED: buildprintf("Imported %s\n", subpath); found = 1; break;
                case IMPORTDATA_ERROR: buildprintf("Error importing %s\n", subpath); errors = 1; break;
            }
        }
        free(subpath);
    }
    Bclosedir(dir);
    if (found) return IMPORTDATA_COPIED;   // Finding anything is considered fine.
    else if (errors) return IMPORTDATA_ERROR; // Finding nothing but errors reports back errors.
    return IMPORTDATA_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %r13
movq (%rsi), %rdi
movq %r13, %rsi
callq *0x8(%r14)
movq %r13, %rdi
callq 0x36011
testq %rax, %rax
je 0x2ef08
movq %rax, %rbx
movq %rax, %rdi
callq 0x3605d
testq %rax, %rax
je 0x2ef0c
movq %rax, %rbp
leaq 0xa163e(%rip), %r15     # 0xd03e0
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
movq (%r14), %rdi
callq *0x10(%r14)
testl %eax, %eax
jne 0x2ef20
movq 0x8(%rbp), %rax
cmpb $0x2e, (%rax)
jne 0x2eddc
movzbl 0x1(%rax), %ecx
testl %ecx, %ecx
je 0x2ee0c
cmpl $0x2e, %ecx
jne 0x2eddc
cmpb $0x0, 0x2(%rax)
je 0x2ee0c
movl 0x10(%rbp), %eax
testl $0xc000, %eax           # imm = 0xC000
je 0x2ee0c
testw %ax, %ax
jns 0x2ee21
xorl %r12d, %r12d
movq (%r12,%r15), %rsi
movq 0x8(%rbp), %rdi
callq 0x3610d
testl %eax, %eax
jne 0x2ee21
addq $0x8, %r12
cmpq $0xa0, %r12
jne 0x2edee
movq %rbx, %rdi
callq 0x3605d
movq %rax, %rbp
testq %rax, %rax
jne 0x2edb1
jmp 0x2ef20
movq %r13, %rdi
callq 0x85a0
movq (%rbp), %rcx
leaq (%rax,%rcx), %r12
addq $0x2, %r12
movq %r12, %rdi
callq 0x8d50
testq %rax, %rax
je 0x2ef18
movq %r13, %rcx
movq %rax, %r13
movq 0x8(%rbp), %r8
movq %rax, %rdi
movq %r12, %rsi
leaq 0x60125(%rip), %rdx     # 0x8ef82
movq %rcx, %r12
xorl %eax, %eax
callq 0x8710
testb $0x40, 0x11(%rbp)
jne 0x2ee9d
movl 0x18(%rbp), %esi
movq %r13, %rdi
movq %r14, %rdx
callq 0x2ef4c
cmpl $-0x1, %eax
je 0x2eece
cmpl $0x2, %eax
je 0x2eeb4
cmpl $0x1, %eax
jne 0x2eee7
leaq 0x61f39(%rip), %rdi     # 0x90dca
movq %r13, %rsi
xorl %eax, %eax
callq 0x372d1
jmp 0x2eee7
movq %r13, %rdi
movq %r14, %rsi
callq 0x2ed58
cmpl $-0x1, %eax
je 0x2eedf
cmpl $0x2, %eax
je 0x2eec5
jmp 0x2eee7
leaq 0x61f1b(%rip), %rdi     # 0x90dd6
movq %r13, %rsi
xorl %eax, %eax
callq 0x372d1
movl $0x1, (%rsp)
jmp 0x2eee7
leaq 0x61f0e(%rip), %rdi     # 0x90de3
movq %r13, %rsi
xorl %eax, %eax
callq 0x372d1
movl $0x1, 0x4(%rsp)
movq %r13, %rdi
callq 0x8270
movq %rbx, %rdi
callq 0x3605d
movq %rax, %rbp
testq %rax, %rax
movq %r12, %r13
jne 0x2edb1
jmp 0x2ef20
xorl %eax, %eax
jmp 0x2ef3d
movq %rbx, %rdi
callq 0x360f5
xorl %ecx, %ecx
jmp 0x2ef37
movq %rbx, %rdi
callq 0x360f5
movq %rbx, %rdi
callq 0x360f5
movl $0x2, %eax
cmpl $0x0, (%rsp)
movl 0x4(%rsp), %ecx
jne 0x2ef3d
xorl %eax, %eax
negl %ecx
sbbl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq