// File: /workspace/github_c_makefile_star2_O1/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, %rbx
leaq 0x197955(%rip), %r14    # 0x1b78bc
xorl %r15d, %r15d
leaq 0x830df(%rip), %r12     # 0xa3050
cmpl $-0x1, 0xfc(%r12,%r15,4)
je 0x1ffa1
decq %r15
addq $-0x104, %r14           # imm = 0xFEFC
cmpq $-0x40, %r15
jne 0x1ff71
leaq 0x551bd(%rip), %rdi     # 0x75150
xorl %eax, %eax
callq 0x24cb4
xorl %edi, %edi
callq 0x66a0
leaq 0x3f(%r15), %rax
movq %rax, (%rsp)
testb %bpl, %bpl
je 0x2010b
decq %rbx
cmpb $0x2f, 0x1(%rbx)
leaq 0x1(%rbx), %rbx
je 0x1ffb5
movl 0x8318b(%rip), %eax     # 0xa3150
movl %r15d, %ecx
subl %eax, %ecx
cmpl $-0x3f, %ecx
je 0x1fffc
callq 0x47c82
testl %eax, %eax
js 0x1fffc
cmpl $0x0, 0x83171(%rip)     # 0xa3150
js 0x1fff7
callq 0x47c82
movslq 0x83163(%rip), %rcx     # 0xa3150
leaq 0x1937cc(%rip), %rdx    # 0x1b37c0
movl %eax, (%rdx,%rcx,4)
callq 0x47ce0
cmpb $0x1, %bpl
je 0x20054
movq %rbx, %rdi
callq 0x20173
testl %eax, %eax
je 0x20054
movq (%rsp), %r13
movl %r13d, 0x83137(%rip)    # 0xa3150
leaq 0x193760(%rip), %rcx    # 0x1b3780
movb $-0x2, 0x3f(%r15,%rcx)
movl %eax, 0xfc(%r12,%r15,4)
leaq 0x19378b(%rip), %rax    # 0x1b37c0
movl $0x0, 0xfc(%rax,%r15,4)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6430
movl %r13d, %eax
jmp 0x20164
movl 0x1936be(%rip), %ecx    # 0x1b3718
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jle 0x20164
leaq 0x55192(%rip), %r9      # 0x75200
decl %ecx
cmpb $0x1, %bpl
movl $0x0, %edx
cmovel %edx, %ecx
leaq 0x82fbd(%rip), %rdx     # 0xa3040
cmpl $0x0, (%rdx,%rcx,4)
js 0x20101
leaq 0x1936a0(%rip), %rdx    # 0x1b3730
movslq (%rdx,%rcx,4), %r8
testq %r8, %r8
jle 0x20101
leaq 0x1936a0(%rip), %rdx    # 0x1b3740
movq (%rdx,%rcx,8), %r11
movl %r8d, %r14d
shll $0x4, %r14d
addl $-0x10, %r14d
movq %r8, %r10
movslq %r14d, %rdx
addq %r11, %rdx
decq %r8
xorl %edi, %edi
movzbl (%rbx,%rdi), %esi
testq %rsi, %rsi
je 0x200e6
movb (%rsi,%r9), %sil
movzbl (%rdx,%rdi), %r13d
cmpb (%r13,%r9), %sil
jne 0x200f7
incq %rdi
cmpq $0xd, %rdi
jne 0x200bd
movl $0xd, %edi
jmp 0x200ec
cmpb $0x0, (%rdx,%rdi)
jne 0x200f7
cmpl $0xd, %edi
jne 0x20139
cmpb $0x0, 0xd(%rbx)
je 0x20139
addl $-0x10, %r14d
cmpq $0x1, %r10
jg 0x200af
testl %ecx, %ecx
jg 0x2006e
jmp 0x20164
movq %rbx, %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1f923
testl %eax, %eax
js 0x1ffb2
leaq 0x193657(%rip), %rcx    # 0x1b3780
movb $-0x1, 0x3f(%r15,%rcx)
movl %eax, 0xfc(%r12,%r15,4)
jmp 0x2014d
leaq 0x193640(%rip), %rax    # 0x1b3780
movb %cl, 0x3f(%r15,%rax)
movl %r8d, 0xfc(%r12,%r15,4)
leaq 0x19366c(%rip), %rax    # 0x1b37c0
movl $0x0, 0xfc(%rax,%r15,4)
movq (%rsp), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq