// File: /workspace/github_c_makefile_star2_O2/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 $0xe8, %rsp
leaq 0xfe0(%rip), %rbx       # 0x32b3
pushq $0x1
popq %rax
xorl %r13d, %r13d
cmpl %edi, %eax
jge 0x231d
movslq %eax, %rcx
movq (%rsi,%rcx,8), %rdx
cmpb $0x2d, (%rdx)
jne 0x231d
movzbl 0x1(%rdx), %r8d
cmpl $0x65, %r8d
je 0x2312
cmpl $0x64, %r8d
je 0x2309
cmpl $0x63, %r8d
jne 0x2319
incl %eax
movq 0x8(%rsi,%rcx,8), %r13
jmp 0x2319
incl %eax
movq 0x8(%rsi,%rcx,8), %rbx
jmp 0x2319
cmpb $0x1, 0x2(%rdx)
adcl $0x0, %eax
incl %eax
jmp 0x22d9
testq %r13, %r13
je 0x24d2
movq %rbx, %rdi
callq 0x10c0
testq %rax, %rax
je 0x24ca
movq %rax, %r15
leaq 0x18(%rsp), %rsi
movq %r13, %rdi
callq 0x12dc
movq %rax, %r14
testq %rax, %rax
je 0x247a
movq 0x18(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
callq 0x1c59
testl %eax, %eax
jne 0x24fa
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
xorl %r13d, %r13d
pushq $0x2f
popq %rbp
leaq 0x2d7c(%rip), %r14      # 0x5100
cmpq $0x19, %r13
je 0x23dc
movq 0x20(%rsp,%r13,8), %r12
testq %r12, %r12
je 0x23d1
cmpb $0x0, (%r12)
je 0x23d1
movq %r12, %rdi
movl %ebp, %esi
callq 0x1110
testq %rax, %rax
je 0x23b5
incq %rax
movq %rax, 0x20(%rsp,%r13,8)
movq %rax, %r12
xorl %ebx, %ebx
cmpq $0x48, %rbx
je 0x23d7
movq (%rbx,%r14), %rsi
movq %r12, %rdi
callq 0x1040
addq $0x8, %rbx
testl %eax, %eax
jne 0x23b7
andq $0x0, 0x20(%rsp,%r13,8)
incq %r13
jmp 0x2384
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x10d0
leaq 0x101(%rax), %rdi
callq 0x1180
testq %rax, %rax
je 0x24b8
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq %rbx, %rdi
callq 0x10d0
leaq (%r12,%rax), %r13
incq %r13
movb $0x2f, (%r12,%rax)
movq %r15, %rdi
callq 0x1170
testq %rax, %rax
je 0x247f
movq %rax, %rbp
addq $0x13, %rbp
xorl %r14d, %r14d
cmpq $0x19, %r14
je 0x2420
movq 0x20(%rsp,%r14,8), %rsi
testq %rsi, %rsi
je 0x2475
movq %rbp, %rdi
callq 0x1040
testl %eax, %eax
jne 0x2475
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1070
leal 0x1(%r14), %esi
movq %r12, %rdi
callq 0x20ea
andq $0x0, 0x20(%rsp,%r14,8)
incq %r14
jmp 0x2437
xorl %r12d, %r12d
jmp 0x24c0
movq 0x2b72(%rip), %r14      # 0x4ff8
leaq 0xd43(%rip), %r13       # 0x31d0
xorl %ebp, %ebp
cmpq $0x19, %rbp
je 0x24bb
movq 0x20(%rsp,%rbp,8), %rcx
testq %rcx, %rcx
jne 0x24a4
incq %rbp
jmp 0x248f
movq (%r14), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1150
jmp 0x249f
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movq %r15, %rdi
callq 0x1130
jmp 0x24d8
movq %rbx, %rdi
callq 0x11b0
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2af7(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xca8(%rip), %rsi       # 0x31b3
xorl %r12d, %r12d
movq %r13, %rdx
xorl %eax, %eax
callq 0x1150
jmp 0x24c0