// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/kplib.c
// Function: kzfindfile
int kzfindfile (char *filnam)
{
	int i;

	filnam[0] = 0;
	if (srchstat == -3)
	{
		if (!wildst[0]) { srchstat = -1; return(0); }
		do
		{
			srchstat = -2;

				//Extract directory from wildcard string for pre-pending
			wildstpathleng = 0;
			for(i=0;wildst[i];i++)
				if ((wildst[i] == '/') || (wildst[i] == '\\'))
					wildstpathleng = i+1;

			memcpy(filnam,wildst,wildstpathleng);

#if defined(__DOS__)
			if (_dos_findfirst(wildst,_A_SUBDIR,&findata))
				{ if (!kzhashbuf) return(0); srchstat = kzlastfnam; continue; }
			i = wildstpathleng;
			if (findata.attrib&16)
				if ((findata.name[0] == '.') && (!findata.name[1])) continue;
			strcpy(&filnam[i],findata.name);
			if (findata.attrib&16) strcat(&filnam[i],"\\");
#elif defined(_WIN32)
			hfind = FindFirstFile(wildst,&findata);
			if (hfind == INVALID_HANDLE_VALUE)
				{ if (!kzhashbuf) return(0); srchstat = kzlastfnam; continue; }
			if (findata.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) continue;
			i = wildstpathleng;
			if (findata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
				if ((findata.cFileName[0] == '.') && (!findata.cFileName[1])) continue;
			strcpy(&filnam[i],findata.cFileName);
			if (findata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) strcat(&filnam[i],"\\");
#else
			if (!hfind)
			{
				char *s = ".";
				if (wildstpathleng > 0) {
					filnam[wildstpathleng] = 0;
					s = filnam;
				}
				hfind = opendir(s);
				if (!hfind) { if (!kzhashbuf) return 0; srchstat = kzlastfnam; continue; }
			}
			break;   // process srchstat == -2
#endif
			return(1);
		} while (0);
	}
	if (srchstat == -2)
		while (1)
		{
			memcpy(filnam,wildst,wildstpathleng);
#if defined(__DOS__)
			if (_dos_findnext(&findata))
				{ if (!kzhashbuf) return(0); srchstat = kzlastfnam; break; }
			i = wildstpathleng;
			if (findata.attrib&16)
				if ((findata.name[0] == '.') && (!findata.name[1])) continue;
			strcpy(&filnam[i],findata.name);
			if (findata.attrib&16) strcat(&filnam[i],"\\");
#elif defined(_WIN32)
			if (!FindNextFile(hfind,&findata))
				{ FindClose(hfind); hfind = INVALID_HANDLE_VALUE; if (!kzhashbuf) return(0); srchstat = kzlastfnam; break; }
			if (findata.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) continue;
			i = wildstpathleng;
			if (findata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
				if ((findata.cFileName[0] == '.') && (!findata.cFileName[1])) continue;
			strcpy(&filnam[i],findata.cFileName);
			if (findata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) strcat(&filnam[i],"\\");
#else
			struct stat st;
			if ((findata = readdir(hfind)) == NULL)
				{ closedir(hfind); hfind = NULL; if (!kzhashbuf) return 0; srchstat = kzlastfnam; break; }
			i = wildstpathleng;
			if (fstatat(dirfd(hfind), findata->d_name, &st, 0)) continue;
			if (st.st_mode & S_IFDIR)
				{ if (findata->d_name[0] == '.' && !findata->d_name[1]) continue; } //skip .
			else if ((st.st_mode & S_IFREG) || (st.st_mode & S_IFLNK))
				{ if (findata->d_name[0] == '.') continue; } //skip hidden (dot) files
			else continue; //skip devices and fifos and such
			if (!wildmatch(findata->d_name,&wildst[wildstpathleng])) continue;
			strcpy(&filnam[i],findata->d_name);
			if (st.st_mode & S_IFDIR) strcat(&filnam[i],"/");
#endif
			return(1);
		}
	while (srchstat >= 0)
	{
		if (wildmatch(&kzhashbuf[srchstat+16],wildst))
		{
			//strcpy(filnam,&kzhashbuf[srchstat+16]);
			filnam[0] = '|'; strcpy(&filnam[1],&kzhashbuf[srchstat+16]);
			srchstat = *(int *)&kzhashbuf[srchstat+4];
			return(1);
		}
		srchstat = *(int *)&kzhashbuf[srchstat+4];
	}
	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movb $0x0, (%rdi)
movl 0x5d337(%rip), %eax     # 0x9dc74
cmpl $-0x3, %eax
jne 0x40a07
movb 0x56f6e4(%rip), %dl     # 0x5b0030
testb %dl, %dl
je 0x409c1
andl $0x0, 0x56f7dd(%rip)    # 0x5b0134
movl $0xfffffffe, 0x5d313(%rip) # imm = 0xFFFFFFFE
xorl %r14d, %r14d
leaq 0x56f6c5(%rip), %rax    # 0x5b0030
xorl %ecx, %ecx
cmpb $0x2f, %dl
je 0x40983
movzbl %dl, %edx
cmpl $0x5c, %edx
je 0x40983
testl %edx, %edx
je 0x40994
incq %rcx
jmp 0x4098f
incq %rcx
movl %ecx, 0x56f7a8(%rip)    # 0x5b0134
movl %ecx, %r14d
movb (%rcx,%rax), %dl
jmp 0x4096d
movslq %r14d, %rdx
leaq 0x56f692(%rip), %rsi    # 0x5b0030
movq %rbx, %rdi
callq 0x60c0
cmpq $0x0, 0x56f67a(%rip)    # 0x5b0028
jne 0x40a10
testl %r14d, %r14d
jle 0x409cd
movl %r14d, %eax
movb $0x0, (%rbx,%rax)
movq %rbx, %rdi
jmp 0x409d4
orl $-0x1, 0x5d2ac(%rip)    # 0x9dc74
jmp 0x40b65
leaq 0x279ab(%rip), %rdi     # 0x6837f
callq 0x6610
movq %rax, 0x56f648(%rip)    # 0x5b0028
testq %rax, %rax
je 0x409ed
movl 0x5d289(%rip), %eax     # 0x9dc74
jmp 0x40a07
cmpq $0x0, 0x56f1b3(%rip)    # 0x5afba8
je 0x40b65
movl 0x56f5bf(%rip), %eax    # 0x5affc0
movl %eax, 0x5d26d(%rip)     # 0x9dc74
cmpl $-0x2, %eax
jne 0x40b2f
leaq 0x56f619(%rip), %r14    # 0x5b0030
leaq 0x8(%rsp), %r15
movslq 0x56f711(%rip), %rdx    # 0x5b0134
movq %rbx, %rdi
movq %r14, %rsi
callq 0x60c0
movq 0x56f5f3(%rip), %rdi    # 0x5b0028
callq 0x6810
movq %rax, 0x56f6f7(%rip)    # 0x5b0138
testq %rax, %rax
je 0x40b01
movslq 0x56f6e3(%rip), %r13    # 0x5b0134
movq 0x56f5d0(%rip), %rdi    # 0x5b0028
callq 0x6a80
movq 0x56f6d4(%rip), %rsi    # 0x5b0138
addq $0x13, %rsi
movl %eax, %edi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x6410
testl %eax, %eax
jne 0x40a1c
movl 0x20(%rsp), %ebp
btl $0xe, %ebp
jb 0x40a9a
testw $0xa000, %bp            # imm = 0xA000
je 0x40a1c
movq 0x56f6a8(%rip), %r12    # 0x5b0138
cmpb $0x2e, 0x13(%r12)
je 0x40a1c
jmp 0x40ab5
movq 0x56f697(%rip), %r12    # 0x5b0138
cmpb $0x2e, 0x13(%r12)
jne 0x40ab5
cmpb $0x0, 0x14(%r12)
je 0x40a1c
addq $0x13, %r12
movslq 0x56f674(%rip), %rsi    # 0x5b0134
addq %r14, %rsi
movq %r12, %rdi
callq 0x40ba3
testl %eax, %eax
je 0x40a1c
addq %r13, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6430
btl $0xe, %ebp
pushq $0x1
popq %rbp
jae 0x40b8f
movq %rbx, %rdi
callq 0x6420
movw $0x2f, (%rbx,%rax)
jmp 0x40b8f
movq 0x56f520(%rip), %rdi    # 0x5b0028
callq 0x65e0
andq $0x0, 0x56f513(%rip)    # 0x5b0028
movq 0x56f08c(%rip), %r15    # 0x5afba8
testq %r15, %r15
je 0x40b65
movl 0x56f499(%rip), %eax    # 0x5affc0
movl %eax, 0x5d147(%rip)     # 0x9dc74
jmp 0x40b36
movq 0x56f072(%rip), %r15    # 0x5afba8
leaq 0x10(%r15), %r12
leaq 0x56f4ef(%rip), %r14    # 0x5b0030
testl %eax, %eax
js 0x40b65
movl %eax, %r13d
leaq (%r12,%r13), %rdi
movq %r14, %rsi
callq 0x40ba3
testl %eax, %eax
jne 0x40b69
movl 0x4(%r15,%r13), %eax
movl %eax, 0x5d111(%rip)     # 0x9dc74
jmp 0x40b41
xorl %ebp, %ebp
jmp 0x40b8f
movb $0x7c, (%rbx)
incq %rbx
leaq (%r15,%r13), %r14
addq $0x10, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6430
movl -0xc(%r14), %eax
movl %eax, 0x5d0e8(%rip)     # 0x9dc74
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq