// File: /workspace/github_c_makefile_star2_O3/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 0x2686
movl $0x1, %eax
leaq 0xe30(%rip), %rbx       # 0x32b3
xorl %r13d, %r13d
movslq %eax, %rcx
movq (%rsi,%rcx,8), %rdx
cmpb $0x2d, (%rdx)
jne 0x24c8
movzbl 0x1(%rdx), %r8d
cmpl $0x65, %r8d
je 0x24bb
cmpl $0x64, %r8d
je 0x24b2
cmpl $0x63, %r8d
jne 0x24c2
incl %eax
movq 0x8(%rsi,%rcx,8), %r13
jmp 0x24c2
incl %eax
movq 0x8(%rsi,%rcx,8), %rbx
jmp 0x24c2
cmpb $0x1, 0x2(%rdx)
adcl $0x0, %eax
incl %eax
cmpl %edi, %eax
jl 0x2486
testq %r13, %r13
je 0x2686
movq %rbx, %rdi
callq 0x10c0
testq %rax, %rax
je 0x267e
movq %rax, %r15
leaq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x130c
movq %rax, %r14
testq %rax, %rax
je 0x2668
movq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x1d96
testl %eax, %eax
jne 0x26ae
movq %r14, (%rsp)
xorl %r13d, %r13d
leaq 0x2bea(%rip), %rbp      # 0x5110
movq 0x10(%rsp,%r13,8), %r12
testq %r12, %r12
je 0x2573
cmpb $0x0, (%r12)
je 0x2573
movq %r12, %rdi
movl $0x2f, %esi
callq 0x1110
testq %rax, %rax
je 0x2554
incq %rax
movq %rax, 0x10(%rsp,%r13,8)
movq %rax, %r12
xorl %r14d, %r14d
movq (%r14,%rbp), %rsi
movq %r12, %rdi
callq 0x1040
testl %eax, %eax
je 0x2573
addq $0x8, %r14
cmpq $0x48, %r14
jne 0x2557
jmp 0x257c
movq $0x0, 0x10(%rsp,%r13,8)
incq %r13
cmpq $0x19, %r13
jne 0x2526
movq %rbx, %rdi
callq 0x10d0
leaq 0x101(%rax), %rdi
callq 0x1190
testq %rax, %rax
je 0x266d
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq %rbx, %rdi
callq 0x10d0
movq %rax, %r13
movb $0x2f, (%r12,%rax)
movq %r15, %rdi
callq 0x1180
testq %rax, %rax
je 0x2631
movq %rax, %rbp
addq %r12, %r13
incq %r13
addq $0x13, %rbp
xorl %r14d, %r14d
movq 0x10(%rsp,%r14,8), %rsi
testq %rsi, %rsi
je 0x2618
movq %rbp, %rdi
callq 0x1040
testl %eax, %eax
jne 0x2618
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1070
leal 0x1(%r14), %esi
movq %r12, %rdi
callq 0x2277
movq $0x0, 0x10(%rsp,%r14,8)
incq %r14
cmpq $0x19, %r14
jne 0x25dd
movq %r15, %rdi
callq 0x1180
movq %rax, %rbp
testq %rax, %rax
jne 0x25d6
movq 0x29c0(%rip), %r14      # 0x4ff8
leaq 0xb91(%rip), %r13       # 0x31d0
xorl %ebp, %ebp
movq 0x10(%rsp,%rbp,8), %rcx
testq %rcx, %rcx
jne 0x2656
incq %rbp
cmpq $0x19, %rbp
jne 0x2641
jmp 0x2670
movq (%r14), %rdi
movq %r13, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1160
jmp 0x264b
xorl %r12d, %r12d
jmp 0x2674
xorl %r12d, %r12d
movq (%rsp), %r14
movq %r15, %rdi
callq 0x1140
jmp 0x268c
movq %rbx, %rdi
callq 0x11c0
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r14, %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 0x2943(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xaf4(%rip), %rsi       # 0x31b3
xorl %r12d, %r12d
movq %r13, %rdx
xorl %eax, %eax
callq 0x1160
jmp 0x2674