// File: /workspace/github_c_makefile_star2_O1/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 $0x218, %rsp            # imm = 0x218
movl %esi, %ebp
movq %rdi, %rbx
callq 0x6060
testq %rax, %rax
je 0x21d38
movq %rax, %r14
movl %ebp, 0x4(%rsp)
decq %rax
cmpb $0x0, 0x1(%rax)
leaq 0x1(%rax), %rax
jne 0x21bf6
xorl %ebp, %ebp
movl $0x0, %r13d
cmpq %r14, %rax
jbe 0x21c18
xorl %r13d, %r13d
cmpb $0x2f, -0x1(%rax)
sete %r13b
movb (%r14), %al
movb %al, 0x3(%rsp)
leaq 0x534e2(%rip), %r15     # 0x75108
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x21d4f
testq %rax, %rax
je 0x21c8b
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x21c70
cmpl $0x2e, %ecx
jne 0x21c66
cmpb $0x0, 0x1(%rax)
je 0x21c70
cmpb $0x2e, %cl
jne 0x21c66
cmpb $0x2e, 0x1(%rax)
jne 0x21c66
cmpb $0x0, 0x2(%rax)
je 0x21c7c
movslq %ebp, %rcx
incl %ebp
movq %rax, 0x10(%rsp,%rcx,8)
movl $0x0, %edi
cmpl $0x40, %ebp
jl 0x21c2e
jmp 0x21c8b
cmpl $0x2, %ebp
movl $0x1, %eax
cmovll %eax, %ebp
decl %ebp
jmp 0x21c70
movb 0x3(%rsp), %r8b
cmpb $0x2f, %r8b
sete %sil
testl %r13d, %r13d
sete %al
cmpl $0x0, 0x4(%rsp)
setne %dl
cmpl $0x2, %ebp
movl $0x1, %edi
movl $0x1, %ecx
cmovgel %ebp, %ecx
decl %ecx
testb %al, %dl
cmovel %ebp, %ecx
cmovnel %edi, %r13d
testl %ecx, %ecx
sete %al
testl %r13d, %r13d
setne %dl
andb %sil, %dl
cmpb $0x2f, %r8b
jne 0x21cdb
movb $0x2f, (%rbx)
incq %rbx
andb %dl, %al
testl %ecx, %ecx
jle 0x21d19
movl %ecx, %ecx
xorl %edx, %edx
testq %rdx, %rdx
je 0x21cf0
movb $0x2f, (%rbx)
incq %rbx
movq 0x10(%rsp,%rdx,8), %rsi
movb (%rsi), %dil
testb %dil, %dil
je 0x21d11
incq %rsi
movb %dil, (%rbx)
incq %rbx
movb (%rsi), %dil
incq %rsi
testb %dil, %dil
jne 0x21d00
incq %rdx
cmpq %rcx, %rdx
jne 0x21ce5
testl %r13d, %r13d
sete %cl
orb %cl, %al
jne 0x21d29
movb $0x2f, (%rbx)
incq %rbx
movb $0x0, (%rbx)
movq %r14, %rdi
callq 0x61c0
xorl %eax, %eax
jmp 0x21d3d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq