// File: /workspace/github_c_makefile_star2_O3/vtereshkov[P]umka-lang/src/umka_common.c
// Function: moduleRegularizePath
bool moduleRegularizePath(const Modules *modules, const char *path, const char *curFolder, char *regularizedPath, int size)
{
    char *absolutePath = storageAdd(modules->storage, size);
    snprintf(absolutePath, size, "%s%s", modulePathIsAbsolute(path) ? "" : curFolder, path);

    char **separators = storageAdd(modules->storage, size * sizeof(char *));
    int numSeparators = 0;

    const char *readCh = absolutePath;
    char *writeCh = regularizedPath;
    int numDots = 0;

    while (*readCh)
    {
        switch (*readCh)
        {
            case '/':
            case '\\':
            {
                if (numDots == 1)   // "./" or ".\"
                {
                    numDots = 0;
                    break;
                }

                if (numDots == 2)   // "../" or "..\"
                {
                    if (numSeparators < 2)
                        return false;

                    numSeparators--;
                    writeCh = separators[numSeparators - 1] + 1;

                    numDots = 0;
                    break;
                }

                separators[numSeparators++] = writeCh;
                *(writeCh++) = '/';

                numDots = 0;
                break;
            }

            case '.':
            {
                numDots++;
                break;
            }

            case ' ':
            case '\t':
            {
                numDots = 0;
                // fallthrough
            }

            default:
            {
                while (numDots > 0)
                {
                    *(writeCh++) = '.';
                    numDots--;
                }

                *(writeCh++) = *readCh;
                break;
            }
        }

        readCh++;
    }

    if (numDots > 0)
        return false;

    *writeCh = 0;
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %r12
movq 0x4110(%rdi), %rdi
movslq %r8d, %r15
movq %r15, %rsi
callq 0x7a70
movq %rax, %r14
testq %r13, %r13
je 0x83be
movq %r13, %rax
movzbl (%rax), %ecx
cmpl $0x9, %ecx
je 0x83ab
cmpl $0x20, %ecx
jne 0x83b0
incq %rax
jmp 0x839e
cmpl $0x2f, %ecx
jne 0x83be
leaq 0x6edba(%rip), %rcx     # 0x77176
jmp 0x83c1
movq %rbp, %rcx
leaq 0x69f9d(%rip), %rdx     # 0x72365
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %r8
xorl %eax, %eax
callq 0x3170
movq 0x4110(%r12), %rdi
shlq $0x3, %r15
movq %r15, %rsi
callq 0x7a70
movq %rax, %r15
xorl %ecx, %ecx
movb (%r14), %al
movzbl %al, %edx
cmpl $0x2d, %edx
jle 0x842b
cmpl $0x2e, %edx
je 0x845f
cmpl $0x5c, %edx
je 0x840d
cmpl $0x2f, %edx
jne 0x8439
xorl %eax, %eax
cmpl $0x1, %ecx
je 0x8474
cmpl $0x2, %ecx
jne 0x8465
xorl %eax, %eax
cmpl $0x2, %ebp
jl 0x848b
movl %ebp, %ecx
decl %ebp
movq -0x10(%r15,%rcx,8), %rbx
jmp 0x8471
cmpl $0x9, %edx
je 0x8456
cmpl $0x20, %edx
je 0x8456
testl %edx, %edx
je 0x847e
testl %ecx, %ecx
jle 0x8458
movl %ecx, %r12d
movq %rbx, %rdi
movl $0x2e, %esi
movq %r12, %rdx
callq 0x3190
addq %r12, %rbx
movb (%r14), %al
xorl %ecx, %ecx
movb %al, (%rbx)
incq %rbx
jmp 0x8461
incl %ecx
movl %ecx, %eax
jmp 0x8474
movslq %ebp, %rcx
incl %ebp
movq %rbx, (%r15,%rcx,8)
movb $0x2f, (%rbx)
incq %rbx
incq %r14
movl %eax, %ecx
jmp 0x83f3
testl %ecx, %ecx
jle 0x8486
xorl %eax, %eax
jmp 0x848b
movb $0x0, (%rbx)
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq