// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbp
movq (%rsi), %rdi
movq %rbp, %rsi
callq *0x8(%r14)
movq %rbp, %rdi
callq 0x34d65
testq %rax, %rax
je 0x2dbbb
movq %rax, %rbx
movq %rax, %rdi
callq 0x34dc2
testq %rax, %rax
je 0x2dbbf
movq %rax, %r13
leaq 0x9f974(%rip), %r15     # 0xcd3b0
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq (%r14), %rdi
callq *0x10(%r14)
testl %eax, %eax
jne 0x2dbcf
movq 0x8(%r13), %rax
cmpb $0x2e, (%rax)
jne 0x2da77
movzbl 0x1(%rax), %ecx
testl %ecx, %ecx
je 0x2dac5
cmpl $0x2e, %ecx
jne 0x2da77
cmpb $0x0, 0x2(%rax)
je 0x2dac5
movl 0x10(%r13), %eax
testl $0xc000, %eax           # imm = 0xC000
je 0x2dac5
testw %ax, %ax
jns 0x2dade
movq 0x8(%r13), %rdi
leaq 0x600f1(%rip), %rsi     # 0x8db83
callq 0x34e72
testl %eax, %eax
jne 0x2dade
movl $0x1, %ecx
cmpq $0x14, %rcx
je 0x2dac5
movq %rcx, %r12
movq (%r15,%rcx,8), %rsi
movq 0x8(%r13), %rdi
callq 0x34e72
leaq 0x1(%r12), %rcx
testl %eax, %eax
je 0x2daa0
cmpq $0x14, %r12
jne 0x2dade
movq %rbx, %rdi
callq 0x34dc2
movq %rax, %r13
testq %rax, %rax
jne 0x2da4c
jmp 0x2dbcf
movq %rbp, %rdi
callq 0x85a0
movq (%r13), %rcx
movq %rbp, 0x10(%rsp)
leaq (%rax,%rcx), %rbp
addq $0x2, %rbp
movq %rbp, %rdi
callq 0x8d50
testq %rax, %rax
je 0x2dbfa
movq %rax, %r12
movq 0x8(%r13), %r8
movq %rax, %rdi
movq %rbp, %rsi
leaq 0x5e452(%rip), %rdx     # 0x8bf6e
movq 0x10(%rsp), %rbp
movq %rbp, %rcx
xorl %eax, %eax
callq 0x8710
testb $0x40, 0x11(%r13)
jne 0x2db63
movl 0x18(%r13), %esi
movq %r12, %rdi
movq %r14, %rdx
callq 0x2dc04
cmpl $-0x1, %eax
je 0x2db95
cmpl $0x2, %eax
je 0x2db7a
cmpl $0x1, %eax
jne 0x2dbae
leaq 0x600bb(%rip), %rdi     # 0x8dc12
movq %r12, %rsi
xorl %eax, %eax
callq 0x36050
jmp 0x2dbae
movq %r12, %rdi
movq %r14, %rsi
callq 0x2d9ef
cmpl $-0x1, %eax
je 0x2dba6
cmpl $0x2, %eax
je 0x2db8b
jmp 0x2dbae
leaq 0x6009d(%rip), %rdi     # 0x8dc1e
movq %r12, %rsi
xorl %eax, %eax
callq 0x36050
movl $0x1, 0xc(%rsp)
jmp 0x2dbae
leaq 0x6008f(%rip), %rdi     # 0x8dc2b
movq %r12, %rsi
xorl %eax, %eax
callq 0x36050
movl $0x1, 0x8(%rsp)
movq %r12, %rdi
callq 0x8270
jmp 0x2dac5
xorl %eax, %eax
jmp 0x2dbeb
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq %rbx, %rdi
callq 0x34e5a
movl $0x2, %eax
cmpl $0x0, 0xc(%rsp)
jne 0x2dbeb
xorl %eax, %eax
negl 0x8(%rsp)
sbbl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x34e5a
jmp 0x2dbcf