// File: /workspace/github_c_makefile_star2_O1/kyz[P]amostools/listamos.c
// Function: read_config_extensions
void read_config_extensions(int argc, char *argv[]) {
    char *dir = ".", *config = NULL, *fullname = NULL, *fp;
    char *slots[AMOS_EXTENSION_SLOTS];
    uint8_t *buf = NULL;
    size_t len;
    int i, j;

    DIR *d = NULL;
    struct dirent *de;

    /* look for -c and -d options */
    for (i = 1; i < argc; i++) {
        if (argv[i][0] != '-') break; /* end of arg processing */
        if (argv[i][1] == 'c') config = argv[++i];
        else if (argv[i][1] == 'd') dir = argv[++i];
        else if (argv[i][1] == 'e' && !argv[i][2]) i++;
    }

    /* if no '-c' flag, do nothing */
    if (!config) goto error_handler;

    /* open '-d' directory */
    if (!(d = opendir(dir))) {
        perror(dir);
        goto error_handler;
    }

    /* read '-c' config file */
    if (!(buf = read_file(config, &len))) {
        goto error_handler;
    }
    if (AMOS_parse_config(buf, len, slots)) {
        fprintf(stderr, "%s: not an AMOS config file\n", config);
        goto error_handler;
    }

    /* reduce the extension names to just NULL or a filename,
     * and remove default extensions */
    for (i = 0; i < AMOS_EXTENSION_SLOTS; i++) {
        if (slots[i] && *slots[i]) {
            char *fname = strrchr(slots[i], '/');
            if (fname) slots[i] = fname + 1;
            /* ignore extensions loaded by default */
            for (j = 0; j < NUM_DEFAULT_EXTENSIONS; j++) {
                if (!strcasecmp(slots[i], default_extensions[j])) {
                    slots[i] = NULL;
                    break;
                }
            }
        }
        else {
            slots[i] = NULL;
        }
    }

    /* allocate space for dir + "/" + fname, pre-fill dir + "/" */
    fullname = malloc(strlen(dir) + 257);
    if (!fullname) goto error_handler;
    strcpy(fullname, dir);
    fp = &fullname[strlen(dir)];
    *fp++ = '/';

    /* find files in the directory that are listed as extensions */
    while ((de = readdir(d))) {
        /* compare each extension's filename to the current dir entry */
        for (i = 0; i < AMOS_EXTENSION_SLOTS; i++) {
            if (!slots[i] || strcasecmp(de->d_name, slots[i])) continue;
            /* build the full path and load extension */
            strncpy(fp, de->d_name, 256);
            read_extension_file(fullname, i + 1);
            slots[i] = NULL;
        }
    }

    /* complain about any listed extensions not loaded */
    for (i = 0; i < AMOS_EXTENSION_SLOTS; i++) {
        if (slots[i]) fprintf(stderr, "%s/%s: no such file\n", dir, slots[i]);
    }

 error_handler:
    if (d) closedir(d);
    free(buf);
    free(fullname);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
cmpl $0x2, %edi
jl 0x2512
movl $0x1, %eax
leaq 0xdeb(%rip), %rbx       # 0x32b6
xorl %r13d, %r13d
movslq %eax, %rcx
movq (%rsi,%rcx,8), %rdx
cmpb $0x2d, (%rdx)
jne 0x251c
movzbl 0x1(%rdx), %r8d
cmpl $0x65, %r8d
je 0x2503
cmpl $0x64, %r8d
je 0x24fa
cmpl $0x63, %r8d
jne 0x250a
incl %eax
movq 0x8(%rsi,%rcx,8), %r13
jmp 0x250a
incl %eax
movq 0x8(%rsi,%rcx,8), %rbx
jmp 0x250a
cmpb $0x1, 0x2(%rdx)
adcl $0x0, %eax
incl %eax
cmpl %edi, %eax
jl 0x24ce
jmp 0x251c
leaq 0xd9d(%rip), %rbx       # 0x32b6
xorl %r13d, %r13d
testq %r13, %r13
je 0x26d2
movq %rbx, %rdi
callq 0x10c0
testq %rax, %rax
je 0x26ca
movq %rax, %r14
leaq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x131c
testq %rax, %rax
je 0x26b9
movq %rax, %r15
movq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rax, %rdi
callq 0x1d84
testl %eax, %eax
jne 0x270a
movq %r15, (%rsp)
xorl %r13d, %r13d
leaq 0x2b96(%rip), %rbp      # 0x5110
movq 0x10(%rsp,%r13,8), %rdi
testq %rdi, %rdi
je 0x25c4
cmpb $0x0, (%rdi)
je 0x25c4
movl $0x2f, %esi
callq 0x1110
testq %rax, %rax
je 0x25a0
incq %rax
movq %rax, 0x10(%rsp,%r13,8)
movq 0x10(%rsp,%r13,8), %r12
xorl %r15d, %r15d
movq (%r15,%rbp), %rsi
movq %r12, %rdi
callq 0x1040
testl %eax, %eax
je 0x25c4
addq $0x8, %r15
cmpq $0x48, %r15
jne 0x25a8
jmp 0x25cd
movq $0x0, 0x10(%rsp,%r13,8)
incq %r13
cmpq $0x19, %r13
jne 0x257a
movq %rbx, %rdi
callq 0x10d0
leaq 0x101(%rax), %rdi
callq 0x11a0
testq %rax, %rax
je 0x26c1
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq %rbx, %rdi
callq 0x10d0
movq %rax, %r13
movb $0x2f, (%r12,%rax)
movq %r14, %rdi
callq 0x1190
testq %rax, %rax
je 0x2682
movq %rax, %rbp
addq %r12, %r13
incq %r13
addq $0x13, %rbp
xorl %r15d, %r15d
movq 0x10(%rsp,%r15,8), %rsi
testq %rsi, %rsi
je 0x2669
movq %rbp, %rdi
callq 0x1040
testl %eax, %eax
jne 0x2669
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1070
leal 0x1(%r15), %esi
movq %r12, %rdi
callq 0x22cd
movq $0x0, 0x10(%rsp,%r15,8)
incq %r15
cmpq $0x19, %r15
jne 0x262e
movq %r14, %rdi
callq 0x1190
movq %rax, %rbp
testq %rax, %rax
jne 0x2627
movq 0x296f(%rip), %r15      # 0x4ff8
leaq 0xb43(%rip), %r13       # 0x31d3
xorl %ebp, %ebp
movq 0x10(%rsp,%rbp,8), %rcx
testq %rcx, %rcx
jne 0x26a7
incq %rbp
cmpq $0x19, %rbp
jne 0x2692
jmp 0x26c4
movq (%r15), %rdi
movq %r13, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0x269c
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x26db
xorl %r12d, %r12d
movq (%rsp), %r15
jmp 0x26db
movq %rbx, %rdi
callq 0x11d0
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
testq %r14, %r14
je 0x26e8
movq %r14, %rdi
callq 0x1140
movq %r15, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28e7(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xa9b(%rip), %rsi       # 0x31b6
xorl %r12d, %r12d
movq %r13, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0x26db