// File: /workspace/github_cpp_makefile_star2_O1/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 0x2156(%rip), %r15      # 0x8320
leaq 0x18(%rsp), %rbx
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %ebp, %ebp
movslq (%r15,%r14,4), %rdi
addq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x2370
testl %eax, %eax
js 0x63a1
movq 0x18(%rsp), %rsi
addq %r12, %rsi
js 0x632c
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x22d0
testq %rax, %rax
je 0x6334
movq %rax, %r13
movq 0x18(%rsp), %rax
addq %r12, %rax
movl $0x8, %ecx
cmpq %rax, %r12
setb %bpl
jae 0x6373
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x2280
movl $0x64, %esi
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x212e(%rip), %rdx      # 0x8381
movq %rax, %rcx
xorl %eax, %eax
callq 0x2190
movq %rbx, %rdi
callq 0x23a0
movq %rax, (%r13,%r12,8)
movq %r12, %rbx
testq %rax, %rax
je 0x62ef
leaq 0x8(,%r12,8), %rax
addq %r13, %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
leaq (%r12,%rbx), %rax
incq %rax
movq 0x18(%rsp), %rcx
addq %r12, %rcx
cmpq %rcx, %rax
setb %bpl
jae 0x6362
movq 0x20(%rsp), %rax
movq 0x8(%rax,%rbx,8), %rdi
callq 0x2280
movl $0x64, %esi
movq %r13, %r15
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x20bb(%rip), %rdx      # 0x8381
movq %rax, %rcx
xorl %eax, %eax
callq 0x2190
movq %r13, %rdi
movq %r15, %r13
callq 0x23a0
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
testq %rax, %rax
jne 0x6284
addq %r12, %rbx
testq %rbx, %rbx
je 0x6309
xorl %r15d, %r15d
movq (%r13,%r15,8), %rdi
callq 0x2040
incq %r15
cmpq %r15, %rbx
jne 0x62f7
movq %r13, %rdi
callq 0x2040
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x2030
movl $0x1, %ecx
leaq 0x1ff6(%rip), %r15      # 0x8320
jmp 0x6373
movl $0x2, %r15d
jmp 0x6388
testq %r12, %r12
je 0x634d
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %rdi
callq 0x2040
incq %rbx
cmpq %rbx, %r12
jne 0x633b
movq %rbp, %rdi
callq 0x2040
movl $0x1, %r15d
leaq 0x18(%rsp), %rbx
jmp 0x6388
leaq 0x1fb7(%rip), %r15      # 0x8320
leaq 0x18(%rsp), %rbx
movl $0x8, %ecx
testb %bpl, %bpl
je 0x637d
movq %r13, %rbp
jmp 0x639d
addq 0x18(%rsp), %r12
xorl %r15d, %r15d
movq %r13, %rbp
movq %rbx, %rdi
callq 0x2030
movq %rbp, %r13
movl %r15d, %ecx
leaq 0x1f83(%rip), %r15      # 0x8320
testl %ecx, %ecx
jne 0x63b6
incq %r14
cmpq $0x3, %r14
jne 0x61d7
movl $0x2, %ecx
movq %rbp, %r13
movq $-0xc, %rax
cmpl $0x2, %ecx
jne 0x63cd
movq 0x8(%rsp), %rax
movq %r13, (%rax)
movq %r12, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq