// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/compat.c
// Function: Bcorrectfilename
int Bcorrectfilename(char *filename, int removefn)
{
	char *fn;
	const int MAXTOKARR = 64;
	char *tokarr[64], *first, *next, *token;
	int i, ntok = 0, leadslash = 0, trailslash = 0;
	
	fn = strdup(filename);
	if (!fn) return -1;
	
	// find the end of the string
	for (first=fn; *first; first++) {
#ifdef _WIN32
		// translating backslashes to forwardslashes on the way
		if (*first == '\\') *first = '/';
#endif
	}
	leadslash = (*fn == '/');
	trailslash = (first>fn && first[-1] == '/');
	
	// carve up the string into pieces by directory, and interpret
	// the . and .. components
	first = fn;
	do {
		token = Bstrtoken(first, "/", &next, 1);
		first = NULL;
		if (!token) break;
		else if (token[0] == 0) continue;
		else if (token[0] == '.' && token[1] == 0) continue;
		else if (token[0] == '.' && token[1] == '.' && token[2] == 0) ntok = max(0,ntok-1);
		else tokarr[ntok++] = token;
	} while (ntok < MAXTOKARR);
	
	if (!trailslash && removefn) { ntok = max(0,ntok-1); trailslash = 1; }
	if (ntok == 0 && trailslash && leadslash) trailslash = 0;
	
	// rebuild the filename
	first = filename;
	if (leadslash) *(first++) = '/';
	for (i=0; i<ntok; i++) {
		if (i>0) *(first++) = '/';
		for (token=tokarr[i]; *token; token++)
			*(first++) = *token;
	}
	if (trailslash) *(first++) = '/';
	*(first++) = 0;
	
	free(fn);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %esi, %ebp
movq %rdi, %rbx
callq 0x6060
testq %rax, %rax
je 0x1f7b7
movq %rax, %rdi
movl %ebp, 0xc(%rsp)
decq %rax
cmpb $0x0, 0x1(%rax)
leaq 0x1(%rax), %rax
jne 0x1f684
xorl %r14d, %r14d
movl $0x0, %ebp
cmpq %rdi, %rax
jbe 0x1f6a5
xorl %ebp, %ebp
cmpb $0x2f, -0x1(%rax)
sete %bpl
movb (%rdi), %al
movb %al, 0xb(%rsp)
leaq 0x4ba86(%rip), %r12     # 0x6b138
leaq 0x18(%rsp), %r13
pushq $0x1
popq %r15
movq %rdi, 0x10(%rsp)
movq %r12, %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x1f7cc
testq %rax, %rax
je 0x1f717
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x1f6fd
cmpl $0x2e, %ecx
jne 0x1f6f2
movzbl 0x1(%rax), %ecx
testl %ecx, %ecx
je 0x1f6fd
cmpl $0x2e, %ecx
jne 0x1f6f2
cmpb $0x0, 0x2(%rax)
je 0x1f70a
movslq %r14d, %rcx
incl %r14d
movq %rax, 0x20(%rsp,%rcx,8)
movl $0x0, %edi
cmpl $0x40, %r14d
jl 0x1f6c0
jmp 0x1f717
cmpl $0x2, %r14d
cmovll %r15d, %r14d
decl %r14d
jmp 0x1f6fd
movb 0xb(%rsp), %dil
cmpb $0x2f, %dil
sete %sil
testl %ebp, %ebp
sete %cl
cmpl $0x0, 0xc(%rsp)
setne %dl
cmpl $0x2, %r14d
movl %r15d, %eax
cmovgel %r14d, %eax
decl %eax
testb %cl, %dl
cmovel %r14d, %eax
cmovnel %r15d, %ebp
testl %eax, %eax
sete %cl
testl %ebp, %ebp
setne %dl
andb %sil, %dl
cmpb $0x2f, %dil
jne 0x1f761
movb $0x2f, (%rbx)
incq %rbx
andb %dl, %cl
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rax, %rdx
je 0x1f797
testq %rdx, %rdx
je 0x1f77a
movb $0x2f, (%rbx)
incq %rbx
movq 0x20(%rsp,%rdx,8), %rsi
movb (%rsi), %dil
testb %dil, %dil
je 0x1f792
movb %dil, (%rbx)
incq %rbx
incq %rsi
jmp 0x1f77f
incq %rdx
jmp 0x1f76a
testl %ebp, %ebp
sete %al
orb %al, %cl
jne 0x1f7a6
movb $0x2f, (%rbx)
incq %rbx
movq 0x10(%rsp), %rdi
movb $0x0, (%rbx)
callq 0x61c0
xorl %eax, %eax
jmp 0x1f7ba
pushq $-0x1
popq %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq