// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x3f
popq %rbx
leaq 0x7f400(%rip), %r15     # 0x9d050
movl %ebx, %r12d
cmpl $-0x1, (%r15,%r12,4)
je 0x1dc74
addl $-0x1, %ebx
jb 0x1dc50
leaq 0x4d51a(%rip), %rdi     # 0x6b180
xorl %eax, %eax
callq 0x22427
xorl %edi, %edi
callq 0x66a0
testb %bpl, %bpl
je 0x1ddbc
decq %r14
cmpb $0x2f, 0x1(%r14)
leaq 0x1(%r14), %r14
je 0x1dc80
cmpl %ebx, 0x7f4bf(%rip)     # 0x9d150
je 0x1dcc0
callq 0x41773
testl %eax, %eax
js 0x1dcc0
cmpl $0x0, 0x7f4ad(%rip)     # 0x9d150
js 0x1dcbb
callq 0x41773
movslq 0x7f49f(%rip), %rcx     # 0x9d150
leaq 0x18fb08(%rip), %rdx    # 0x1ad7c0
movl %eax, (%rdx,%rcx,4)
callq 0x417cf
cmpb $0x1, %bpl
je 0x1dd12
movq %r14, %rdi
callq 0x1de05
testl %eax, %eax
je 0x1dd12
movl %ebx, 0x7f478(%rip)     # 0x9d150
leaq 0x18faa1(%rip), %rcx    # 0x1ad780
movb $-0x2, (%r12,%rcx)
leaq 0x18fad5(%rip), %rcx    # 0x1ad7c0
andl $0x0, (%rcx,%r12,4)
movl %eax, (%r15,%r12,4)
imulq $0x104, %r12, %rax      # imm = 0x104
leaq 0x18fbbe(%rip), %rdi    # 0x1ad8c0
addq %rax, %rdi
movq %r14, %rsi
callq 0x6430
jmp 0x1ddef
movl 0x18fa00(%rip), %eax    # 0x1ad718
xorl %ecx, %ecx
leaq 0x7f31f(%rip), %rsi     # 0x9d040
leaq 0x18fa18(%rip), %r8     # 0x1ad740
leaq 0x4d501(%rip), %r9      # 0x6b230
testl %eax, %eax
jle 0x1de00
decl %eax
cmpb $0x1, %bpl
cmovel %ecx, %eax
cmpl $0x0, (%rsi,%rax,4)
js 0x1dd2f
leaq 0x18f9e3(%rip), %rcx    # 0x1ad730
movslq (%rcx,%rax,4), %r10
movl %r10d, %r11d
shll $0x4, %r11d
addl $-0x10, %r11d
movq %r10, %rcx
movslq %r11d, %r13
decq %r10
testq %rcx, %rcx
jle 0x1dda7
addq (%r8,%rax,8), %r13
xorl %ecx, %ecx
cmpq $0xd, %rcx
je 0x1dd95
movzbl (%r14,%rcx), %edi
testq %rdi, %rdi
je 0x1dd9e
movb (%rdi,%r9), %dil
movzbl (%r13,%rcx), %edx
cmpb (%rdx,%r9), %dil
jne 0x1dd58
incq %rcx
jmp 0x1dd70
cmpb $0x0, 0xd(%r14)
jne 0x1dd58
jmp 0x1ddab
cmpb $0x0, (%rcx,%r13)
jne 0x1dd58
jmp 0x1ddab
xorl %ecx, %ecx
jmp 0x1dd2f
leaq 0x18f9ce(%rip), %rcx    # 0x1ad780
movb %al, (%r12,%rcx)
movl %r10d, (%r15,%r12,4)
jmp 0x1dde3
movq %r14, %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1d714
testl %eax, %eax
js 0x1dc7d
leaq 0x18f9a6(%rip), %rcx    # 0x1ad780
movb $-0x1, (%r12,%rcx)
movl %eax, (%r15,%r12,4)
leaq 0x18f9d6(%rip), %rax    # 0x1ad7c0
andl $0x0, (%rax,%r12,4)
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rbx
jmp 0x1ddef