// File: /workspace/github_c_makefile_star2_O3/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 0x71696(%rip), %rdi     # 0x9a326
movq %r14, %rsi
callq 0x21b23
testl %eax, %eax
je 0x28caf
cmpb $0x2f, 0x2(%r14)
jne 0x28ca7
xorl %ebp, %ebp
jmp 0x28caf
movb $0x5f, 0x1(%r14)
movb $0x1, %bpl
leaq 0x71670(%rip), %rdi     # 0x9a326
movq %rbx, %rsi
callq 0x21b23
testl %eax, %eax
je 0x28cc8
cmpb $0x2f, 0x2(%rbx)
jne 0x28ccd
xorl %r13d, %r13d
jmp 0x28cd4
movb $0x5f, 0x1(%rbx)
movb $0x1, %r13b
leaq 0x4c0(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x288ae
leaq 0x2c0(%rsp), %r12
leaq 0x40(%rsp), %rdx
movq %rsp, %rcx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x288ae
leaq 0x16c0(%rsp), %rdx
movq %r15, %rdi
movl $0x2f, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x22862
movl %eax, %r14d
leaq 0x6c0(%rsp), %rdx
movq %r12, %rdi
movl $0x2f, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x22862
movl %eax, %ebx
testb %bpl, %bpl
je 0x28d4f
movq 0x16c8(%rsp), %rax
movb $0x2f, (%rax)
testb %r13b, %r13b
je 0x28d5f
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 0x28da7
movl %r15d, %r13d
movl $0x1, %r12d
movq 0x16c0(%rsp,%r12,8), %rdi
movq 0x6c0(%rsp,%r12,8), %rsi
callq 0x6a50
testl %eax, %eax
jne 0x28da7
incq %r12
cmpq %r12, %r13
jne 0x28d83
movl %r15d, %r12d
cmpl %ebp, %r12d
jge 0x28dd7
movl %r12d, %ebp
notl %ebp
addl %r14d, %ebp
leaq 0x6a7fb(%rip), %r14     # 0x935b6
leaq 0xc0(%rsp), %r15
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
movq %r14, %rdx
callq 0x24013
decl %ebp
jne 0x28dc3
cmpl %ebx, %r12d
jge 0x28e26
movl %r12d, %eax
leaq (%rsp,%rax,8), %r13
addq $0x6c0, %r13            # imm = 0x6C0
leaq 0xc0(%rsp), %r14
leaq 0x6c47c(%rip), %r15     # 0x95275
movq (%r13), %rdx
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdi
callq 0x24013
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdi
movq %r15, %rdx
callq 0x24013
addq $0x8, %r13
incl %r12d
cmpl %r12d, %ebx
jg 0x28df9
leaq 0xc0(%rsp), %rbx
leaq 0x40(%rsp), %rdx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x24013
movq %rsp, %rdx
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x24013
movq %rbx, %rdi
callq 0x1feba
addq $0x26c8, %rsp           # imm = 0x26C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop