// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/cache1d.c
// Function: kopen4load
int kopen4load(const char *filename, char searchfirst)
{
	int i, j, k, fil, newhandle;
	char bad, *gfileptr;

	newhandle = MAXOPENFILES-1;
	while (filehan[newhandle] != -1)
	{
		newhandle--;
		if (newhandle < 0)
		{
			buildprintf("TOO MANY FILES OPEN IN FILE GROUPING SYSTEM!");
			exit(0);
		}
	}

	if (searchfirst == 0)
		if ((fil = openfrompath(filename,BO_BINARY|BO_RDONLY,S_IREAD)) >= 0)
		{
			filegrp[newhandle] = 255;
			filehan[newhandle] = fil;
			filepos[newhandle] = 0;
			return(newhandle);
		}

	for (; toupperlookup[(int)(unsigned char)*filename] == '/'; filename++);
	
#ifdef WITHKPLIB
	if ((kzcurhand != newhandle) && (kztell() >= 0))
	{
		if (kzcurhand >= 0) filepos[kzcurhand] = kztell();
		kzclose();
	}
	if (searchfirst != 1 && (i = kzipopen(filename)) != 0) {
		kzcurhand = newhandle;
		filegrp[newhandle] = 254;
		filehan[newhandle] = i;
		filepos[newhandle] = 0;
		strcpy(filenamsav[newhandle],filename);
		return newhandle;
	}
#endif

	for(k=numgroupfiles-1;k>=0;k--)
	{
		if (searchfirst == 1) k = 0;
		if (groupfil[k] >= 0)
		{
			for(i=gnumfiles[k]-1;i>=0;i--)
			{
				gfileptr = (char *)&gfilelist[k][i<<4];

				bad = 0;
				for(j=0;j<13;j++)
				{
					if (!filename[j]) break;
					if (toupperlookup[(int)(unsigned char)filename[j]] != toupperlookup[(int)(unsigned char)gfileptr[j]])
						{ bad = 1; break; }
				}
				if (bad) continue;
				if (j<13 && gfileptr[j]) continue;   // JBF: because e1l1.map might exist before e1l1
				if (j==13 && filename[j]) continue;   // JBF: long file name

				filegrp[newhandle] = k;
				filehan[newhandle] = i;
				filepos[newhandle] = 0;
				return(newhandle);
			}
		}
	}
	return(-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %r14
movl $0x3f, %ebx
leaq 0x83bf8(%rip), %r15     # 0xa4050
movl %ebx, %r12d
cmpl $-0x1, (%r15,%r12,4)
je 0x2047c
addl $-0x1, %ebx
jb 0x20458
leaq 0x55dfe(%rip), %rdi     # 0x7626c
xorl %eax, %eax
callq 0x25221
xorl %edi, %edi
callq 0x66a0
testb %bpl, %bpl
je 0x205dd
decq %r14
cmpb $0x2f, 0x1(%r14)
leaq 0x1(%r14), %r14
je 0x20488
cmpl %ebx, 0x83cb7(%rip)     # 0xa4150
je 0x204c8
callq 0x49f1b
testl %eax, %eax
js 0x204c8
cmpl $0x0, 0x83ca5(%rip)     # 0xa4150
js 0x204c3
callq 0x49f1b
movslq 0x83c97(%rip), %rcx     # 0xa4150
leaq 0x194300(%rip), %rdx    # 0x1b47c0
movl %eax, (%rdx,%rcx,4)
callq 0x49f79
cmpb $0x1, %bpl
je 0x20521
movq %r14, %rdi
callq 0x20635
testl %eax, %eax
je 0x20521
movl %ebx, 0x83c70(%rip)     # 0xa4150
leaq 0x194299(%rip), %rcx    # 0x1b4780
movb $-0x2, (%r12,%rcx)
movl %eax, (%r15,%r12,4)
leaq 0x1942c9(%rip), %rax    # 0x1b47c0
movl $0x0, (%rax,%r12,4)
movq %r12, %rax
shlq $0x8, %rax
leaq (%rax,%r12,4), %rax
leaq 0x1943af(%rip), %rdi    # 0x1b48c0
addq %rax, %rdi
movq %r14, %rsi
callq 0x6430
jmp 0x20624
movl 0x1941f1(%rip), %eax    # 0x1b4718
testl %eax, %eax
jle 0x205d6
leaq 0x55dda(%rip), %r8      # 0x76310
movl %ebp, 0x4(%rsp)
decl %eax
cmpb $0x1, %bpl
movl %eax, %r9d
movl $0x0, %ecx
cmovel %ecx, %r9d
leaq 0x83aed(%rip), %rcx     # 0xa4040
cmpl $0x0, (%rcx,%r9,4)
js 0x205c9
leaq 0x1941cf(%rip), %rcx    # 0x1b4730
movslq (%rcx,%r9,4), %rdi
testq %rdi, %rdi
jle 0x205c9
leaq 0x1941cf(%rip), %rcx    # 0x1b4740
movq (%rcx,%r9,8), %r11
movl %edi, %r13d
shll $0x4, %r13d
addl $-0x10, %r13d
movq %rdi, %r10
movslq %r13d, %rsi
addq %r11, %rsi
decq %rdi
xorl %ecx, %ecx
movzbl (%r14,%rcx), %edx
testq %rdx, %rdx
je 0x205b9
movb (%rdx,%r8), %dl
movzbl (%rsi,%rcx), %ebp
cmpb (%rbp,%r8), %dl
jne 0x205bf
incq %rcx
cmpq $0xd, %rcx
jne 0x2058e
cmpb $0x0, 0xd(%r14)
jne 0x205bf
jmp 0x20606
cmpb $0x0, (%rsi,%rcx)
je 0x20606
addl $-0x10, %r13d
cmpq $0x1, %r10
jg 0x20580
testl %r9d, %r9d
movl 0x4(%rsp), %ebp
jg 0x2053a
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x20624
movq %r14, %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1fe10
testl %eax, %eax
js 0x20485
leaq 0x194185(%rip), %rcx    # 0x1b4780
movb $-0x1, (%r12,%rcx)
movl %eax, (%r15,%r12,4)
jmp 0x20615
leaq 0x194173(%rip), %rax    # 0x1b4780
movb %r9b, (%r12,%rax)
movl %edi, (%r15,%r12,4)
leaq 0x1941a4(%rip), %rax    # 0x1b47c0
movl $0x0, (%rax,%r12,4)
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq