// File: /workspace/github_c_makefile_star2_O1/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 0xcca8
movq %rax, %r14
testq %r13, %r13
je 0xd622
movq %r13, %rax
movzbl (%rax), %ecx
cmpl $0x9, %ecx
je 0xd60c
cmpl $0x20, %ecx
jne 0xd611
incq %rax
jmp 0xd5ff
cmpl $0x2f, %ecx
movq %rbp, %rcx
jne 0xd625
leaq 0x6ab56(%rip), %rcx     # 0x78176
jmp 0xd625
movq %rbp, %rcx
leaq 0x65d39(%rip), %rdx     # 0x73365
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 0xcca8
movq %rax, %r15
xorl %r12d, %r12d
movzbl (%r14), %eax
cmpl $0x2d, %eax
jle 0xd690
cmpl $0x2e, %eax
je 0xd6c8
cmpl $0x5c, %eax
je 0xd670
cmpl $0x2f, %eax
jne 0xd69d
xorl %eax, %eax
cmpl $0x1, %r12d
je 0xd6df
cmpl $0x2, %r12d
jne 0xd6d0
xorl %eax, %eax
cmpl $0x2, %ebp
jl 0xd6fc
movl %ebp, %ecx
decl %ebp
movq -0x10(%r15,%rcx,8), %rbx
jmp 0xd6dc
cmpl $0x9, %eax
je 0xd69a
cmpl $0x20, %eax
jne 0xd6ea
xorl %r12d, %r12d
testl %r12d, %r12d
jle 0xd6be
movl %r12d, %edx
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x3190
decl %r12d
addq %r12, %rbx
incq %rbx
xorl %r12d, %r12d
movb (%r14), %al
movb %al, (%rbx)
incq %rbx
jmp 0xd6cb
incl %r12d
movl %r12d, %eax
jmp 0xd6df
movslq %ebp, %rcx
incl %ebp
movq %rbx, (%r15,%rcx,8)
movb $0x2f, (%rbx)
incq %rbx
incq %r14
movl %eax, %r12d
jmp 0xd658
testl %eax, %eax
jne 0xd69d
testl %r12d, %r12d
jle 0xd6f7
xorl %eax, %eax
jmp 0xd6fc
movb $0x0, (%rbx)
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq