// File: /workspace/github_cpp_makefile_star2_O3/AndreRenaud[P]simple_uart/simple_uart.c
// Function: simple_uart_list
ssize_t simple_uart_list(char ***namesp)
{
#if defined(__linux__) || defined(__APPLE__)
    glob_t g;
    char **names = NULL;
    size_t count = 0;

#ifdef __linux__
    char *path_globs[] = {"/sys/class/tty/ttyS[0-9]*", "/dev/ttyUSB*", "/dev/ttyACM*"};
#endif

#ifdef __APPLE__
    char *path_globs[] = {"/dev/tty.*"};
#endif

    for (size_t path = 0; path < sizeof(path_globs) / sizeof(path_globs[0]); path++) {
        if (glob(path_globs[path], 0, NULL, &g) >= 0) {
            char buffer[100];
            char **new_names;
            if ((count + g.gl_pathc) > SSIZE_MAX) {
                globfree(&g);
                break; // abort to avoid memory leakage
            }
            new_names = realloc(names, (count + g.gl_pathc) * sizeof(char *));
            if (!new_names) {
                /* Free previously allocated strings to prevent memory leak */
                for (size_t j = 0; j < count; j++) {
                    free(names[j]);
                }
                free(names);
                globfree(&g);
                return -ENOMEM;
            }
            names = new_names;
            for (size_t i = count; i < count + g.gl_pathc; i++) {
                /* Use snprintf to prevent buffer overflow */
                snprintf(buffer, sizeof(buffer), "/dev/%s", basename(g.gl_pathv[i - count]));
                names[i] = strdup(buffer);
                if (!names[i]) {
                    /* Handle strdup failure */
                    for (size_t j = 0; j < i; j++) {
                        free(names[j]);
                    }
                    free(names);
                    globfree(&g);
                    return -ENOMEM;
                }
            }
            count += g.gl_pathc;
            globfree(&g);
        }
    }
    *namesp = names;
    return (ssize_t)count;

#else
    int pos = 0;
    char **names = NULL;

    for (int i = 0; i < 255; i++) {
        char buffer[10];
        char target[255];
        sprintf(buffer, "COM%d", i + 1);
        if (QueryDosDevice(buffer, target, sizeof(target)) > 0) {
            char **new_names = realloc(names, (DWORD)(pos + 1) * sizeof(char *));
            if (!new_names)
                continue;
            names = new_names;
            names[pos] = malloc(strlen(buffer) + 1);
            if (!names[pos]) {
                continue;
            }
            strcpy(names[pos], buffer);
            pos++;
        }
    }
    *namesp = names;
    return pos;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, (%rsp)
leaq 0x22ba(%rip), %rbx      # 0x8354
leaq 0x8(%rsp), %r12
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r14d, %r14d
movslq (%rbx,%r15,4), %rdi
addq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r12, %rcx
callq 0x2370
testl %eax, %eax
js 0x616a
movq 0x8(%rsp), %rsi
addq %r13, %rsi
js 0x61b0
shlq $0x3, %rsi
movq %r14, %rdi
callq 0x22d0
testq %rax, %rax
je 0x61d9
movq %rax, %rbp
movq 0x8(%rsp), %rbx
addq %r13, %rbx
cmpq %rbx, %r13
jae 0x614e
xorl %r12d, %r12d
movq %r13, %r14
movq 0x10(%rsp), %rax
movq (%rax,%r12), %rdi
callq 0x2280
movl $0x64, %esi
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x2297(%rip), %rdx      # 0x83b5
movq %rax, %rcx
xorl %eax, %eax
callq 0x2190
movq %rbx, %rdi
callq 0x23a0
movq %rax, (%rbp,%r14,8)
testq %rax, %rax
je 0x617c
incq %r14
movq 0x8(%rsp), %rbx
addq %r13, %rbx
addq $0x8, %r12
cmpq %rbx, %r14
jb 0x60fc
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x2030
movq %rbx, %r13
movq %rbp, %r14
leaq 0x21ec(%rip), %rbx      # 0x8354
jmp 0x616d
movq %r14, %rbp
incq %r15
cmpq $0x3, %r15
jne 0x60a8
jmp 0x61bd
testq %r14, %r14
je 0x6195
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %rdi
callq 0x2040
incq %rbx
cmpq %rbx, %r14
jne 0x6183
movq %rbp, %rdi
callq 0x2040
leaq 0x8(%rsp), %rdi
callq 0x2030
movq $-0xc, %r13
jmp 0x61c4
leaq 0x8(%rsp), %rdi
callq 0x2030
movq %r14, %rbp
movq (%rsp), %rax
movq %rbp, (%rax)
movq %r13, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r13, %r13
je 0x61f1
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x2040
incq %rbx
cmpq %rbx, %r13
jne 0x61e0
movq %r14, %rdi
jmp 0x6198