// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/lib/filePath.c
// Function: makeRelativePath
char *makeRelativePath(char *from, char *to)
/* Calculate a relative path from one absolute directory/file to another.
 * Assumptions: both from and to are canonicalized absolute paths beginning
 * at "/" or "//".  Filenames are okay, but all directory names must end with
 * a "/" to distinguish them from files.
 * e.g., /test/dir/ is a directory, but /test/dir is a file.
 */
{
int i, j, toCount, fromCount;
char fromDir[PATH_LEN];
char toDir[PATH_LEN], toFile[FILENAME_LEN], toExt[FILEEXT_LEN];
char relPath [PATH_LEN] = "";
char *fromDirList[PATH_LEN], *toDirList[PATH_LEN];
boolean fromStartsDoubleSlash = FALSE, toStartsDoubleSlash = FALSE;

if (startsWith("//", from) && from[2] != '/')
    {
    fromStartsDoubleSlash = TRUE;
    from[1] = '_'; // prevent initial // from being misinterpreted
    }
if (startsWith("//", to) && to[2] != '/')
    {
    toStartsDoubleSlash = TRUE;
    to[1] = '_'; // prevent initial // from being misinterpreted
    }

splitPath(from, fromDir, NULL, NULL);
splitPath(to, toDir, toFile, toExt);

fromCount = chopByChar(fromDir, '/', fromDirList, ArraySize(fromDirList));
toCount   = chopByChar(toDir,   '/', toDirList,   ArraySize(toDirList));

if (fromStartsDoubleSlash == TRUE)
    fromDirList[1][0] = '/';
if (toStartsDoubleSlash == TRUE)
    toDirList[1][0] = '/';
    

for (i=1; i < min(fromCount-1, toCount-1); i++)
    {
    if (!sameString(fromDirList[i], toDirList[i]))
        break;
    }
for (j=i; j < fromCount-1; j++)
    {
    safecat(relPath, sizeof relPath, "../");
    }
for (j=i; j < toCount-1; j++)
    {
    safecat(relPath, sizeof relPath, toDirList[j]);
    safecat(relPath, sizeof relPath, "/");
    }

safecat(relPath, sizeof relPath, toFile);
safecat(relPath, sizeof relPath, toExt);

return cloneString(relPath);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x26c8, %rsp           # imm = 0x26C8
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xc0(%rsp), %rdi
xorl %ebp, %ebp
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x63b0
leaq 0x717ea(%rip), %rdi     # 0x9a0f6
movq %r14, %rsi
callq 0x217c8
testl %eax, %eax
je 0x2892b
cmpb $0x2f, 0x2(%r14)
jne 0x28923
xorl %ebp, %ebp
jmp 0x2892b
movb $0x5f, 0x1(%r14)
movb $0x1, %bpl
leaq 0x717c4(%rip), %rdi     # 0x9a0f6
movq %rbx, %rsi
callq 0x217c8
testl %eax, %eax
je 0x28944
cmpb $0x2f, 0x2(%rbx)
jne 0x28949
xorl %r13d, %r13d
jmp 0x28950
movb $0x5f, 0x1(%rbx)
movb $0x1, %r13b
leaq 0x4c0(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2852a
leaq 0x2c0(%rsp), %r12
leaq 0x40(%rsp), %rdx
movq %rsp, %rcx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2852a
leaq 0x16c0(%rsp), %rdx
movq %r15, %rdi
movl $0x2f, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x2251a
movl %eax, %r14d
leaq 0x6c0(%rsp), %rdx
movq %r12, %rdi
movl $0x2f, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x2251a
movl %eax, %ebx
testb %bpl, %bpl
je 0x289cb
movq 0x16c8(%rsp), %rax
movb $0x2f, (%rax)
testb %r13b, %r13b
je 0x289db
movq 0x6c8(%rsp), %rax
movb $0x2f, (%rax)
leal -0x1(%r14), %ebp
decl %ebx
cmpl %ebx, %ebp
movl %ebx, %r15d
cmovll %ebp, %r15d
movl $0x1, %r12d
cmpl $0x2, %r15d
jl 0x28a23
movl %r15d, %r13d
movl $0x1, %r12d
movq 0x16c0(%rsp,%r12,8), %rdi
movq 0x6c0(%rsp,%r12,8), %rsi
callq 0x6a50
testl %eax, %eax
jne 0x28a23
incq %r12
cmpq %r12, %r13
jne 0x289ff
movl %r15d, %r12d
cmpl %ebp, %r12d
jge 0x28a53
movl %r12d, %ebp
notl %ebp
addl %r14d, %ebp
leaq 0x6aab2(%rip), %r14     # 0x934e9
leaq 0xc0(%rsp), %r15
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movq %r14, %rdx
callq 0x23cf8
decl %ebp
jne 0x28a3f
cmpl %ebx, %r12d
jge 0x28aa2
movl %r12d, %eax
leaq (%rsp,%rax,8), %r13
addq $0x6c0, %r13            # imm = 0x6C0
leaq 0xc0(%rsp), %r14
leaq 0x6c718(%rip), %r15     # 0x9518d
movq (%r13), %rdx
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdi
callq 0x23cf8
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdi
movq %r15, %rdx
callq 0x23cf8
addq $0x8, %r13
incl %r12d
cmpl %r12d, %ebx
jg 0x28a75
leaq 0xc0(%rsp), %rbx
leaq 0x40(%rsp), %rdx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x23cf8
movq %rsp, %rdx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x23cf8
movq %rbx, %rdi
callq 0x1f9f6
addq $0x26c8, %rsp           # imm = 0x26C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop