// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rsi, %rbp
movq %rdi, %r12
movq 0x4110(%rdi), %rdi
movslq %r8d, %r15
movq %r15, %rsi
callq 0x4144
movq %rax, %r14
movq %rbp, %rdi
callq 0x47a9
leaq 0x66742(%rip), %rcx     # 0x6b116
testb %al, %al
cmoveq %r13, %rcx
leaq 0x61984(%rip), %rdx     # 0x66365
xorl %r13d, %r13d
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %r8
xorl %eax, %eax
callq 0x3170
movq 0x4110(%r12), %rdi
shlq $0x3, %r15
movq %r15, %rsi
callq 0x4144
xorl %edx, %edx
movzbl (%r14), %ecx
cmpl $0x9, %ecx
je 0x4a4e
cmpl $0x20, %ecx
je 0x4a4e
cmpl $0x2e, %ecx
je 0x4a73
cmpl $0x2f, %ecx
je 0x4a2d
cmpl $0x5c, %ecx
je 0x4a2d
testl %ecx, %ecx
jne 0x4a50
jmp 0x4a93
xorl %ecx, %ecx
cmpl $0x1, %edx
je 0x4a89
cmpl $0x2, %edx
jne 0x4a79
xorl %ecx, %ecx
cmpl $0x2, %r13d
jl 0x4aa0
movl %r13d, %edx
decl %r13d
movq -0x10(%rax,%rdx,8), %rbx
jmp 0x4a86
xorl %edx, %edx
incq %rbx
movl %edx, %ecx
testl %ecx, %ecx
jle 0x4a64
movb $0x2e, -0x1(%rbx)
decl %ecx
incq %rbx
jmp 0x4a55
movl %edx, %ecx
sarl $0x1f, %ecx
andl %edx, %ecx
movb (%r14), %dl
movb %dl, -0x1(%rbx)
jmp 0x4a89
incl %edx
movl %edx, %ecx
jmp 0x4a89
movslq %r13d, %rdx
incl %r13d
movq %rbx, (%rax,%rdx,8)
movb $0x2f, (%rbx)
incq %rbx
incq %r14
movl %ecx, %edx
jmp 0x4a0a
testl %edx, %edx
jle 0x4a9b
xorl %ecx, %ecx
jmp 0x4aa0
movb $0x0, (%rbx)
movb $0x1, %cl
movl %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq