// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rdi, %rbp
movq (%rsi), %rdi
movq %rbp, %rsi
callq *0x8(%rbx)
movq %rbp, %rdi
callq 0x31fd9
testq %rax, %rax
je 0x2b008
movq %rax, %r15
leaq 0x9b52b(%rip), %r14     # 0xc63e0
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
movq %r15, %rdi
callq 0x3201e
testq %rax, %rax
je 0x2b014
movq %rax, %r13
movq (%rbx), %rdi
callq *0x10(%rbx)
testl %eax, %eax
jne 0x2b014
movq 0x8(%r13), %rax
cmpb $0x2e, (%rax)
jne 0x2af02
movzbl 0x1(%rax), %ecx
testl %ecx, %ecx
je 0x2aec4
cmpl $0x2e, %ecx
jne 0x2af02
cmpb $0x0, 0x2(%rax)
je 0x2aec4
movl 0x10(%r13), %eax
testw $0xc000, %ax            # imm = 0xC000
je 0x2aec4
testw %ax, %ax
jns 0x2af32
xorl %r12d, %r12d
cmpq $0xa0, %r12
je 0x2aec4
movq (%r12,%r14), %rsi
movq 0x8(%r13), %rdi
callq 0x320c8
addq $0x8, %r12
testl %eax, %eax
je 0x2af14
movq %rbp, %rdi
callq 0x85a0
movq (%r13), %rcx
leaq (%rax,%rcx), %r12
addq $0x2, %r12
movq %r12, %rdi
callq 0x8d50
testq %rax, %rax
je 0x2b00c
movq %rbp, %rcx
movq %rax, %rbp
movq 0x8(%r13), %r8
movq %rax, %rdi
movq %r12, %rsi
leaq 0x56014(%rip), %rdx     # 0x80f82
movq %rcx, %r12
xorl %eax, %eax
callq 0x8710
testb $0x40, 0x11(%r13)
jne 0x2afb0
movl 0x18(%r13), %esi
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x2b03e
cmpl $-0x1, %eax
je 0x2afe1
cmpl $0x2, %eax
je 0x2afc7
cmpl $0x1, %eax
jne 0x2aff8
leaq 0x57cee(%rip), %rdi     # 0x82c92
movq %rbp, %rsi
xorl %eax, %eax
callq 0x33127
jmp 0x2aff8
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2ae80
cmpl $-0x1, %eax
je 0x2aff2
cmpl $0x2, %eax
je 0x2afd8
jmp 0x2aff8
leaq 0x57cd0(%rip), %rdi     # 0x82c9e
movq %rbp, %rsi
xorl %eax, %eax
callq 0x33127
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0x2aff8
leaq 0x57cc3(%rip), %rdi     # 0x82cab
movq %rbp, %rsi
xorl %eax, %eax
callq 0x33127
pushq $0x1
popq %rax
movl %eax, (%rsp)
movq %rbp, %rdi
callq 0x8270
movq %r12, %rbp
jmp 0x2aec4
xorl %eax, %eax
jmp 0x2b02f
movq %r15, %rdi
callq 0x320b0
movq %r15, %rdi
callq 0x320b0
cmpl $0x0, 0x4(%rsp)
je 0x2b028
pushq $0x2
popq %rax
jmp 0x2b02f
xorl %eax, %eax
negl (%rsp)
sbbl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq