// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc8, %rsp
movq %rdi, 0x8(%rsp)
leaq 0x2843(%rip), %rbx      # 0x831c
leaq 0x18(%rsp), %r15
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x5c00
movslq (%rbx,%r12,4), %rdi
addq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x2370
testl %eax, %eax
js 0x5bae
movq 0x18(%rsp), %rsi
addq %r13, %rsi
js 0x5bf6
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x22d0
testq %rax, %rax
je 0x5c0a
movq %rax, %r14
movq %r12, 0x10(%rsp)
leaq (%rax,%r13,8), %r12
xorl %ebp, %ebp
movq %r13, %r15
addq %rbp, %r15
movq 0x18(%rsp), %rbx
addq %r13, %rbx
cmpq %rbx, %r15
jae 0x5b8f
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rdi
callq 0x2280
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
pushq $0x64
popq %rsi
leaq 0x280e(%rip), %rdx      # 0x837d
movq %rax, %rcx
xorl %eax, %eax
callq 0x2190
movq %rbx, %rdi
callq 0x23a0
movq %rax, (%r12,%rbp,8)
testq %rax, %rax
je 0x5bb6
incq %rbp
jmp 0x5b3c
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x2030
movq %r14, %rbp
movq %rbx, %r13
leaq 0x2773(%rip), %rbx      # 0x831c
movq 0x10(%rsp), %r12
incq %r12
jmp 0x5ae6
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x5bcb
movq (%r14,%rbx,8), %rdi
callq 0x2040
incq %rbx
jmp 0x5bb8
movq %r14, %rdi
callq 0x2040
leaq 0x18(%rsp), %rdi
callq 0x2030
pushq $-0xc
popq %r13
movq %r13, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%rsp), %rdi
callq 0x2030
movq 0x8(%rsp), %rax
movq %rbp, (%rax)
jmp 0x5be1
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x5c21
movq (%rbp,%r14,8), %rdi
callq 0x2040
incq %r14
jmp 0x5c0d
movq %rbp, %rdi
jmp 0x5bce