// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/cache1d.c
// Function: klistpath
CACHE1D_FIND_REC *klistpath(const char *_path, const char *mask, int type)
{
	CACHE1D_FIND_REC *rec = NULL;
	char *path;
	
	// pathsearchmode == 0: enumerates a path in the virtual filesystem
	// pathsearchmode == 1: enumerates the system filesystem path passed in
	
	path = strdup(_path);
	if (!path) return NULL;

	// we don't need any leading dots and slashes or trailing slashes either
	{
		int i,j;
		for (i=0; path[i] == '.' || toupperlookup[(int)(unsigned char)path[i]] == '/'; ) i++;
		for (j=0; (path[j] = path[i]); j++,i++) ;
		while (j>0 && toupperlookup[(int)(unsigned char)path[j-1]] == '/') j--;
		path[j] = 0;
		//buildprintf("Cleaned up path = \"%s\"\n",path);
	}
	
	if (*path && (type & CACHE1D_FIND_DIR)) {
		if (klistaddentry(&rec, "..", CACHE1D_FIND_DIR, CACHE1D_SOURCE_CURDIR) < 0) goto failure;
	}
	
	if (!(type & CACHE1D_OPT_NOSTACK)) {	// current directory and paths in the search stack
		searchpath_t *search = NULL;
		BDIR *dir;
		struct Bdirent *dirent;
		const char *d = ".";
		int stackdepth = CACHE1D_SOURCE_CURDIR;
		char buf[BMAX_PATH];

		if (pathsearchmode) d = _path;

		do {
			if (!pathsearchmode) {
				strcpy(buf, path);
				if (*path) strcat(buf, "/");
				strcat(buf, d);
			} else strcpy(buf, d);
			dir = Bopendir(buf);
			if (dir) {
				while ((dirent = Breaddir(dir))) {
					if ((dirent->name[0] == '.' && dirent->name[1] == 0) ||
						(dirent->name[0] == '.' && dirent->name[1] == '.' && dirent->name[2] == 0))
						continue;
					if ((type & CACHE1D_FIND_DIR) && !(dirent->mode & BS_IFDIR)) continue;
					if ((type & CACHE1D_FIND_FILE) && (dirent->mode & BS_IFDIR)) continue;
					if (!Bwildmatch(dirent->name, mask)) continue;
					switch (klistaddentry(&rec, dirent->name,
								(dirent->mode & BS_IFDIR) ? CACHE1D_FIND_DIR : CACHE1D_FIND_FILE,
										  stackdepth)) {
						case -1: goto failure;
						//case 1: buildprintf("%s:%s dropped for lower priority\n", d,dirent->name); break;
						//case 0: buildprintf("%s:%s accepted\n", d,dirent->name); break;
						default: break;
					}
				}
				Bclosedir(dir);
			}
			
			if (pathsearchmode) break;

			if (!search) {
				search = searchpathhead;
				stackdepth = CACHE1D_SOURCE_PATH;
			} else {
				search = search->next;
				stackdepth++;
			}
			if (search) d = search->path;
		} while (search);
	}

#ifdef WITHKPLIB
	if (!pathsearchmode) {	// next, zip files
		char buf[BMAX_PATH];
		int i, j, ftype;
		strcpy(buf,path);
		if (*path) strcat(buf,"/");
		strcat(buf,mask);
		for (kzfindfilestart(buf); kzfindfile(buf); ) {
			if (buf[0] != '|') continue;	// local files we don't need
			
			// scan for the end of the string and shift
			// everything left a char in the process
			for (i=1; (buf[i-1]=buf[i]); i++) ;
			i-=2;

			// if there's a slash at the end, this is a directory entry
			if (toupperlookup[(int)(unsigned char)buf[i]] == '/') { ftype = CACHE1D_FIND_DIR; buf[i] = 0; }
			else ftype = CACHE1D_FIND_FILE;

			// skip over the common characters at the beginning of the base path and the zip entry
			for (j=0; buf[j] && path[j]; j++) {
				if (toupperlookup[(int)(unsigned char)path[j] ] == toupperlookup[(int)(unsigned char)buf[j] ]) continue;
				break;
			}
			// we've now hopefully skipped the common path component at the beginning.
			// if that's true, we should be staring at a null byte in path and either any character in buf
			// if j==0, or a slash if j>0
			if ((!path[0] && buf[j]) || (!path[j] && toupperlookup[(int)(unsigned char)buf[j] ] == '/')) {
				if (j>0) j++;
				
				// yep, so now we shift what follows back to the start of buf and while we do that,
				// keep an eye out for any more slashes which would mean this entry has sub-entities
				// and is useless to us.
				for (i = 0; (buf[i] = buf[j]) && toupperlookup[(int)(unsigned char)buf[j]] != '/'; i++,j++) ;
				if (toupperlookup[(int)(unsigned char)buf[j]] == '/') continue;	// damn, try next entry
			} else {
				// if we're here it means we have a situation where:
				//   path = foo
				//   buf = foobar...
				// or
				//   path = foobar
				//   buf = foo...
				// which would mean the entry is higher up in the directory tree and is also useless
				continue;
			}

			if ((type & CACHE1D_FIND_DIR) && ftype != CACHE1D_FIND_DIR) continue;
			if ((type & CACHE1D_FIND_FILE) && ftype != CACHE1D_FIND_FILE) continue;
			
			// the entry is in the clear
			switch (klistaddentry(&rec, buf, ftype, CACHE1D_SOURCE_ZIP)) {
				case -1: goto failure;
				//case 1: buildprintf("<ZIP>:%s dropped for lower priority\n", buf); break;
				//case 0: buildprintf("<ZIP>:%s accepted\n", buf); break;
				default: break;
			}
		}
	}
#endif
	
	// then, grp files
	if (!pathsearchmode && !*path && (type & CACHE1D_FIND_FILE)) {
		char buf[13];
		int i,j;
		buf[12] = 0;
		for (i=0;i<MAXGROUPFILES;i++) {
			if (groupfil[i] == -1) continue;
			for(j=gnumfiles[i]-1;j>=0;j--)
			{
				Bmemcpy(buf,&gfilelist[i][j<<4],12);
				if (!Bwildmatch(buf,mask)) continue;
				switch (klistaddentry(&rec, buf, CACHE1D_FIND_FILE, CACHE1D_SOURCE_GRP)) {
					case -1: goto failure;
					//case 1: buildprintf("<GRP>:%s dropped for lower priority\n", workspace); break;
					//case 0: buildprintf("<GRP>:%s accepted\n", workspace); break;
					default: break;
				}
			}
		}
	}
	
	if (pathsearchmode && (type & CACHE1D_FIND_DRIVE)) {
		char *drives, *drp;
		drives = Bgetsystemdrives();
		if (drives) {
			for (drp=drives; *drp; drp+=strlen(drp)+1) {
				if (klistaddentry(&rec, drp, CACHE1D_FIND_DRIVE, CACHE1D_SOURCE_DRIVE) < 0) {
					free(drives);
					goto failure;
				}
			}
			free(drives);
		}
	}
	
	free(path);
	return rec;
failure:
	free(path);
	klistfree(rec);
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq $0x0, 0x8(%rsp)
callq 0x6060
testq %rax, %rax
je 0x20c78
movq %rax, %rbx
movq $-0x1, %rcx
movb 0x1(%rbx,%rcx), %al
andb $-0x2, %al
incq %rcx
cmpb $0x2e, %al
je 0x20716
movl %ecx, %eax
movb (%rbx,%rax), %al
movb %al, (%rbx)
testb %al, %al
je 0x20745
xorl %eax, %eax
leaq (%rbx,%rax), %rdx
movb 0x1(%rcx,%rdx), %dl
movb %dl, 0x1(%rbx,%rax)
incq %rax
testb %dl, %dl
jne 0x20730
jmp 0x20747
xorl %eax, %eax
testl %eax, %eax
jle 0x20760
movl %eax, %ecx
cmpb $0x2f, -0x1(%rbx,%rcx)
jne 0x20760
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
jg 0x2074b
xorl %eax, %eax
cltq
movb $0x0, (%rbx,%rax)
movb (%rbx), %al
movb %al, 0x7(%rsp)
testb %al, %al
sete %al
testb $0x2, %bpl
sete %cl
orb %al, %cl
jne 0x2079f
leaq 0x51bfb(%rip), %rsi     # 0x7237e
leaq 0x8(%rsp), %rdi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x20c9a
testl %eax, %eax
js 0x20c50
btl $0x8, %ebp
jb 0x20914
cmpl $0x0, 0x192f50(%rip)    # 0x1b3700
leaq 0x51bc8(%rip), %rax     # 0x7237f
cmoveq %rax, %r14
movl $0x1, %r12d
xorl %r15d, %r15d
leaq 0x20(%rsp), %r13
cmpl $0x0, 0x192f30(%rip)    # 0x1b3700
je 0x207df
movq %r13, %rdi
movq %r14, %rsi
callq 0x6430
jmp 0x2080b
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6430
cmpb $0x0, 0x7(%rsp)
je 0x20800
movq %r13, %rdi
callq 0x6420
movw $0x2f, 0x20(%rsp,%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x6710
movq %r13, %rdi
callq 0x21ef9
testq %rax, %rax
je 0x208bd
movq %rax, %r13
movq %r14, 0x18(%rsp)
movq %rax, %rdi
callq 0x21f56
testq %rax, %rax
je 0x208b0
movq %rax, %r14
movq 0x8(%r14), %rdi
movb (%rdi), %al
cmpb $0x2e, %al
jne 0x20854
cmpb $0x0, 0x1(%rdi)
je 0x208a0
cmpb $0x2e, %al
jne 0x20854
cmpb $0x2e, 0x1(%rdi)
jne 0x20854
cmpb $0x0, 0x2(%rdi)
je 0x208a0
testb $0x2, %bpl
je 0x20861
testb $0x40, 0x11(%r14)
je 0x208a0
testb $0x1, %bpl
je 0x2086e
testb $0x40, 0x11(%r14)
jne 0x208a0
movq 0x10(%rsp), %rsi
callq 0x22006
testl %eax, %eax
je 0x208a0
btl $0xe, 0x10(%r14)
movq 0x8(%r14), %rsi
movl $0x0, %edx
adcl $0x1, %edx
leaq 0x8(%rsp), %rdi
movl %r12d, %ecx
callq 0x20c9a
cmpl $-0x1, %eax
je 0x208fe
movq %r13, %rdi
callq 0x21f56
movq %rax, %r14
testq %rax, %rax
jne 0x20834
movq %r13, %rdi
callq 0x21fee
movq 0x18(%rsp), %r14
xorl %eax, %eax
cmpl $0x0, 0x192e3a(%rip)    # 0x1b3700
jne 0x20903
incl %r12d
testq %r15, %r15
leaq 0x192e33(%rip), %rcx    # 0x1b3708
cmoveq %rcx, %r15
movl $0x2, %ecx
cmovel %ecx, %r12d
movq (%r15), %r15
testq %r15, %r15
je 0x208ee
movq 0x8(%r15), %r14
testq %r15, %r15
leaq 0x20(%rsp), %r13
jne 0x207c9
jmp 0x20903
movl $0x9, %eax
cmpl $0x9, %eax
je 0x20c50
testl %eax, %eax
jne 0x20c7b
cmpl $0x0, 0x192de5(%rip)    # 0x1b3700
je 0x209ef
cmpl $0x0, 0x192dd8(%rip)    # 0x1b3700
jne 0x20bcf
cmpb $0x0, 0x7(%rsp)
setne %al
testb $0x1, %bpl
sete %cl
orb %al, %cl
jne 0x20bcf
leaq 0x20(%rsp), %r15
movb $0x0, 0xc(%r15)
xorl %r14d, %r14d
leaq 0x826e7(%rip), %rax     # 0xa3040
cmpl $-0x1, (%rax,%r14,4)
je 0x209db
leaq 0x192dc9(%rip), %rax    # 0x1b3730
movslq (%rax,%r14,4), %r13
testq %r13, %r13
jle 0x209db
movq %r13, %r12
incq %r12
shll $0x4, %r13d
addl $-0x10, %r13d
leaq 0x192dbb(%rip), %rax    # 0x1b3740
movq (%rax,%r14,8), %rax
movslq %r13d, %rcx
movq (%rax,%rcx), %rdx
movq %rdx, 0x20(%rsp)
movl 0x8(%rax,%rcx), %eax
movl %eax, 0x28(%rsp)
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x22006
testl %eax, %eax
je 0x209ce
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movl $0x1, %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
callq 0x20c9a
cmpl $-0x1, %eax
je 0x20bb9
decq %r12
addl $-0x10, %r13d
cmpq $0x1, %r12
jg 0x2097e
incq %r14
cmpq $0x4, %r14
jne 0x20952
xorl %eax, %eax
jmp 0x20bbe
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x6430
cmpb $0x0, 0x7(%rsp)
je 0x20a14
leaq 0x20(%rsp), %rdi
callq 0x6420
movw $0x2f, 0x20(%rsp,%rax)
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x6710
movq %r15, %rdi
callq 0x45887
movq %r15, %rdi
callq 0x458c2
testl %eax, %eax
je 0x20ba1
movabsq $-0x200000000, %r13     # imm = 0xFFFFFFFE00000000
movabsq $0x100000000, %r12      # imm = 0x100000000
leaq 0x547a7(%rip), %r14     # 0x75200
cmpb $0x7c, 0x20(%rsp)
jne 0x20b91
leaq 0x21(%rsp), %rax
movq %r13, %rcx
addq %r12, %rcx
movb (%rax), %dl
movb %dl, -0x1(%rax)
incq %rax
testb %dl, %dl
jne 0x20a6c
sarq $0x20, %rcx
movb 0x20(%rsp,%rcx), %al
movl $0x1, %edx
cmpb $0x2f, %al
jne 0x20a96
movb $0x0, 0x20(%rsp,%rcx)
movl $0x2, %edx
movb 0x20(%rsp), %r8b
testb %r8b, %r8b
setne %cl
je 0x20adf
movb (%rbx), %r9b
xorl %edi, %edi
testb %r9b, %r9b
je 0x20ae8
movq %rdi, %rsi
movzbl %r9b, %edi
movb (%rdi,%r14), %dil
movzbl %r8b, %r9d
cmpb (%r9,%r14), %dil
jne 0x20aec
movb 0x21(%rsp,%rsi), %r8b
testb %r8b, %r8b
setne %cl
je 0x20af0
movb 0x1(%rbx,%rsi), %r9b
leaq 0x1(%rsi), %rdi
testb %r9b, %r9b
jne 0x20aad
jmp 0x20af3
xorl %edi, %edi
xorl %esi, %esi
xorl %r8d, %r8d
jmp 0x20afe
xorl %esi, %esi
jmp 0x20af6
movl %esi, %edi
jmp 0x20af6
leal 0x1(%rsi), %edi
incq %rsi
cmpb $0x2f, %r8b
sete %r8b
cmpb $0x0, 0x7(%rsp)
jne 0x20b0a
testb $0x1, %cl
jne 0x20b1a
cmpb $0x0, (%rbx,%rsi)
setne %cl
xorb $0x1, %r8b
orb %cl, %r8b
jne 0x20b91
leal 0x1(%rdi), %ecx
testl %edi, %edi
cmovel %edi, %ecx
movb 0x20(%rsp,%rcx), %sil
movb %sil, 0x20(%rsp)
testb %sil, %sil
je 0x20b52
movl %ecx, %esi
movq %r15, %rcx
cmpb $0x2f, (%rcx,%rsi)
je 0x20b4d
movb 0x1(%rcx,%rsi), %dil
movb %dil, 0x1(%rcx)
incq %rcx
testb %dil, %dil
jne 0x20b36
addq %rsi, %rcx
jmp 0x20b59
addq %rsp, %rcx
addq $0x20, %rcx
cmpb $0x2f, %al
sete %sil
setne %al
testb %sil, %bpl
jne 0x20b91
movl %ebp, %esi
shrb %sil
andb %al, %sil
jne 0x20b91
cmpb $0x2f, (%rcx)
je 0x20b91
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movl $0x7ffffffe, %ecx       # imm = 0x7FFFFFFE
callq 0x20c9a
cmpl $-0x1, %eax
je 0x20c90
movq %r15, %rdi
callq 0x458c2
testl %eax, %eax
jne 0x20a59
xorl %eax, %eax
cmpl $0x9, %eax
je 0x20c50
testl %eax, %eax
je 0x20921
jmp 0x20c7b
movl $0x9, %eax
cmpl $0x9, %eax
je 0x20c50
testl %eax, %eax
jne 0x20c7b
testb $0x4, %bpl
je 0x20c41
cmpl $0x0, 0x192b24(%rip)    # 0x1b3700
je 0x20c41
callq 0x21ecb
testq %rax, %rax
je 0x20c27
movq %rax, %r14
cmpb $0x0, (%rax)
je 0x20c23
leaq 0x8(%rsp), %r15
movq %r14, %r12
movq %r15, %rdi
movq %r12, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20c9a
testl %eax, %eax
js 0x20c2b
movq %r12, %rdi
callq 0x6420
addq %rax, %r12
incq %r12
cmpb $0x0, (%r12)
jne 0x20bf8
xorl %ebp, %ebp
jmp 0x20c30
xorl %ebp, %ebp
jmp 0x20c38
movl $0x9, %ebp
movq %r14, %rdi
callq 0x61c0
cmpl $0x9, %ebp
je 0x20c50
testl %ebp, %ebp
jne 0x20c7b
movq %rbx, %rdi
callq 0x61c0
movq 0x8(%rsp), %r14
jmp 0x20c7b
movq %rbx, %rdi
callq 0x61c0
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x20c78
xorl %r14d, %r14d
movq 0x10(%rdi), %rbx
callq 0x61c0
movq %rbx, %rdi
testq %rbx, %rbx
jne 0x20c65
jmp 0x20c7b
xorl %r14d, %r14d
movq %r14, %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x9, %eax
jmp 0x20ba3