// File: /workspace/github_c_makefile_star2_O2/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 %r12d, %r12d
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x63b0
leaq 0x65211(%rip), %rdi     # 0x890a6
movq %r14, %rsi
callq 0x1db41
testl %eax, %eax
je 0x23eb5
cmpb $0x2f, 0x2(%r14)
jne 0x23ead
xorl %r12d, %r12d
jmp 0x23eb5
movb $0x5f, 0x1(%r14)
movb $0x1, %r12b
leaq 0x651ea(%rip), %rdi     # 0x890a6
movq %rbx, %rsi
callq 0x1db41
testl %eax, %eax
je 0x23ece
cmpb $0x2f, 0x2(%rbx)
jne 0x23ed3
xorl %r13d, %r13d
jmp 0x23eda
movb $0x5f, 0x1(%rbx)
movb $0x1, %r13b
leaq 0x4c0(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x23b2e
leaq 0x2c0(%rsp), %r14
leaq 0x40(%rsp), %rdx
movq %rsp, %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x23b2e
pushq $0x2f
popq %rbp
leaq 0x16c0(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x1e6d7
movl %eax, %ebx
leaq 0x6c0(%rsp), %rdx
movq %r14, %rdi
movl %ebp, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x1e6d7
movl %eax, %ebp
testb %r12b, %r12b
je 0x23f51
movq 0x16c8(%rsp), %rax
movb $0x2f, (%rax)
testb %r13b, %r13b
je 0x23f61
movq 0x6c8(%rsp), %rax
movb $0x2f, (%rax)
decl %ebx
decl %ebp
cmpl %ebp, %ebx
movl %ebp, %eax
cmovll %ebx, %eax
movslq %eax, %r14
cmpl $0x2, %r14d
pushq $0x1
popq %r12
cmovgel %r14d, %r12d
pushq $0x1
popq %r15
cmpq %r14, %r15
jge 0x23fa5
movq 0x16c0(%rsp,%r15,8), %rdi
movq 0x6c0(%rsp,%r15,8), %rsi
callq 0x6a50
testl %eax, %eax
jne 0x23fa2
incq %r15
jmp 0x23f7f
movl %r15d, %r12d
leaq 0x5e50a(%rip), %r14     # 0x824b6
leaq 0xc0(%rsp), %r15
movl %r12d, %r13d
cmpl %ebx, %r13d
jge 0x23fd1
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movq %r14, %rdx
callq 0x1fb92
incl %r13d
jmp 0x23fb7
movl %r12d, %r15d
movslq %ebp, %r12
leaq 0xc0(%rsp), %rbx
leaq 0x6018f(%rip), %r14     # 0x84175
cmpq %r12, %r15
jge 0x24015
movq 0x6c0(%rsp,%r15,8), %rdx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x1fb92
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1fb92
incq %r15
jmp 0x23fe6
leaq 0xc0(%rsp), %rbx
leaq 0x40(%rsp), %rdx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x1fb92
movq %rsp, %rdx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x1fb92
movq %rbx, %rdi
callq 0x1c306
addq $0x26c8, %rsp           # imm = 0x26C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)