// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x26f0, %rsp           # imm = 0x26F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x6e0(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x63a0
movl $0x0, -0x26e4(%rbp)
movl $0x0, -0x26e8(%rbp)
movq -0x8(%rbp), %rsi
leaq 0xab686(%rip), %rdi     # 0xe419b
callq 0x2d3d0
cmpl $0x0, %eax
je 0x38b3e
movq -0x8(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x2f, %eax
je 0x38b3e
movl $0x1, -0x26e4(%rbp)
movq -0x8(%rbp), %rax
movb $0x5f, 0x1(%rax)
movq -0x10(%rbp), %rsi
leaq 0xab652(%rip), %rdi     # 0xe419b
callq 0x2d3d0
cmpl $0x0, %eax
je 0x38b72
movq -0x10(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x2f, %eax
je 0x38b72
movl $0x1, -0x26e8(%rbp)
movq -0x10(%rbp), %rax
movb $0x5f, 0x1(%rax)
movq -0x8(%rbp), %rdi
leaq -0x220(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x385d0
movq -0x10(%rbp), %rdi
leaq -0x420(%rbp), %rsi
leaq -0x4a0(%rbp), %rdx
leaq -0x4e0(%rbp), %rcx
callq 0x385d0
leaq -0x220(%rbp), %rdi
leaq -0x16e0(%rbp), %rdx
movl $0x2f, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x2eb50
movl %eax, -0x20(%rbp)
leaq -0x420(%rbp), %rdi
leaq -0x26e0(%rbp), %rdx
movl $0x2f, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x2eb50
movl %eax, -0x1c(%rbp)
cmpl $0x1, -0x26e4(%rbp)
jne 0x38bfa
movq -0x16d8(%rbp), %rax
movb $0x2f, (%rax)
cmpl $0x1, -0x26e8(%rbp)
jne 0x38c0d
movq -0x26d8(%rbp), %rax
movb $0x2f, (%rax)
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x26ec(%rbp)
movl -0x20(%rbp), %eax
subl $0x1, %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x38c3b
movl -0x20(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x26f0(%rbp)
jmp 0x38c47
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x26f0(%rbp)
movl -0x26ec(%rbp), %eax
movl -0x26f0(%rbp), %ecx
cmpl %ecx, %eax
jge 0x38c88
movslq -0x14(%rbp), %rax
movq -0x16e0(%rbp,%rax,8), %rdi
movslq -0x14(%rbp), %rax
movq -0x26e0(%rbp,%rax,8), %rsi
callq 0x6a40
cmpl $0x0, %eax
je 0x38c7b
jmp 0x38c88
jmp 0x38c7d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x38c14
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x38cbe
leaq -0x6e0(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0xa46c0(%rip), %rdx     # 0xdd36e
callq 0x311e0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x38c8e
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x38d11
leaq -0x6e0(%rbp), %rdi
movslq -0x18(%rbp), %rax
movq -0x26e0(%rbp,%rax,8), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x311e0
leaq -0x6e0(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0xa637e(%rip), %rdx     # 0xdf07f
callq 0x311e0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x38cc4
leaq -0x6e0(%rbp), %rdi
leaq -0x4a0(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x311e0
leaq -0x6e0(%rbp), %rdi
leaq -0x4e0(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x311e0
leaq -0x6e0(%rbp), %rdi
callq 0x2a450
addq $0x26f0, %rsp           # imm = 0x26F0
popq %rbp
retq
nopw %cs:(%rax,%rax)