// File: /workspace/github_c_makefile_star2_O3/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, 0x4(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
movq $0x0, 0x8(%rsp)
callq 0x6060
testq %rax, %rax
je 0x21007
movq %rax, %rbx
movq $-0x1, %rax
movb 0x1(%rbx,%rax), %cl
andb $-0x2, %cl
incq %rax
cmpb $0x2e, %cl
je 0x20be7
movl %eax, %ecx
movb (%rbx,%rcx), %cl
movb %cl, (%rbx)
testb %cl, %cl
je 0x20c2d
xorl %ecx, %ecx
leaq (%rbx,%rcx), %rdx
movb 0x1(%rax,%rdx), %dl
movb %dl, 0x1(%rbx,%rcx)
incq %rcx
testb %dl, %dl
jne 0x20c03
jmp 0x20c22
leal -0x1(%rcx), %eax
cmpl $0x1, %ecx
movl %eax, %ecx
jle 0x20c31
movl %ecx, %eax
cmpb $0x2f, -0x1(%rbx,%rax)
je 0x20c18
jmp 0x20c33
xorl %eax, %eax
jmp 0x20c36
xorl %ecx, %ecx
movslq %ecx, %rax
movb $0x0, (%rbx,%rax)
movb (%rbx), %al
movb %al, 0x3(%rsp)
testb %al, %al
sete %al
movl 0x4(%rsp), %ebp
andl $0x2, %ebp
sete %cl
orb %al, %cl
jne 0x20c76
leaq 0x52770(%rip), %rsi     # 0x733ca
leaq 0x8(%rsp), %rdi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x210d1
testl %eax, %eax
js 0x20fe0
btl $0x8, 0x4(%rsp)
movl 0x193a7e(%rip), %eax    # 0x1b4700
jb 0x20dd5
testl %eax, %eax
sete %al
leaq 0x52737(%rip), %rcx     # 0x733cb
cmoveq %rcx, %r13
movl $0x1, %r15d
xorl %r14d, %r14d
leaq 0x20(%rsp), %r12
movq %r12, %rdi
testb $0x1, %al
je 0x20cd8
movq %rbx, %rsi
callq 0x6430
cmpb $0x0, 0x3(%rsp)
je 0x20ccb
movq %r12, %rdi
callq 0x6420
movw $0x2f, 0x20(%rsp,%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x6710
jmp 0x20ce0
movq %r13, %rsi
callq 0x6430
movq %r12, %rdi
callq 0x222d9
testq %rax, %rax
je 0x20d95
movq %rax, %r13
movq %rax, %rdi
callq 0x22325
testq %rax, %rax
je 0x20d88
movq %rax, %r12
movq 0x8(%r12), %rdi
cmpb $0x2e, (%rdi)
jne 0x20d25
movzbl 0x1(%rdi), %eax
testl %eax, %eax
je 0x20d78
cmpl $0x2e, %eax
jne 0x20d25
cmpb $0x0, 0x2(%rdi)
je 0x20d78
testl %ebp, %ebp
je 0x20d31
testb $0x40, 0x11(%r12)
je 0x20d78
testb $0x1, 0x4(%rsp)
je 0x20d40
testb $0x40, 0x11(%r12)
jne 0x20d78
movq 0x10(%rsp), %rsi
callq 0x223d5
testl %eax, %eax
je 0x20d78
btl $0xe, 0x10(%r12)
movq 0x8(%r12), %rsi
movl $0x0, %edx
adcl $0x1, %edx
leaq 0x8(%rsp), %rdi
movl %r15d, %ecx
callq 0x210d1
cmpl $-0x1, %eax
je 0x20fe0
movq %r13, %rdi
callq 0x22325
movq %rax, %r12
testq %rax, %rax
jne 0x20d08
movq %r13, %rdi
callq 0x223bd
leaq 0x20(%rsp), %r12
movl 0x193965(%rip), %eax    # 0x1b4700
testl %eax, %eax
jne 0x20dd5
testq %r14, %r14
movq %r14, %rcx
leaq 0x19395c(%rip), %rdx    # 0x1b4708
cmoveq %rdx, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x20dd5
incl %r15d
testq %r14, %r14
movl $0x2, %eax
cmovel %eax, %r15d
movq 0x8(%rcx), %r13
movb $0x1, %al
movq %rcx, %r14
jmp 0x20ca6
testl %eax, %eax
jne 0x20f59
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x6430
cmpb $0x0, 0x3(%rsp)
je 0x20e02
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 0x47afd
movq %r15, %rdi
callq 0x47b38
testl %eax, %eax
je 0x20f50
movl 0x4(%rsp), %eax
andl $0x1, %eax
movl %eax, 0x1c(%rsp)
leaq 0x21(%rsp), %r13
movabsq $0x100000000, %r14      # imm = 0x100000000
leaq 0x20(%rsp), %r12
leaq 0x554be(%rip), %r15     # 0x76310
cmpb $0x7c, 0x20(%rsp)
jne 0x20f40
movq %r13, %rax
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
addq %r14, %rcx
movb (%rax), %dl
movb %dl, -0x1(%rax)
incq %rax
testb %dl, %dl
jne 0x20e6a
sarq $0x20, %rcx
movb 0x20(%rsp,%rcx), %al
movl $0x1, %edx
cmpb $0x2f, %al
jne 0x20e94
movb $0x0, 0x20(%rsp,%rcx)
movl $0x2, %edx
movb 0x20(%rsp), %sil
testb %sil, %sil
je 0x20f40
movl $0x1, %ecx
movzbl -0x1(%rbx,%rcx), %edi
testq %rdi, %rdi
je 0x20ece
movb (%rdi,%r15), %dil
movzbl %sil, %r8d
cmpb (%r8,%r15), %dil
jne 0x20ece
movb 0x20(%rsp,%rcx), %sil
incq %rcx
testb %sil, %sil
jne 0x20ea7
jmp 0x20f40
cmpb $0x0, 0x3(%rsp)
je 0x20ee2
cmpb $0x2f, %sil
jne 0x20f40
cmpb $0x0, -0x1(%rbx,%rcx)
jne 0x20f40
cmpq $0x1, %rcx
movl %ecx, %ecx
movl $0x0, %esi
cmoveq %rsi, %rcx
movb 0x20(%rsp,%rcx), %sil
movb %sil, 0x20(%rsp)
testb %sil, %sil
je 0x20f18
movq %r13, %rdi
cmpb $0x2f, %sil
je 0x20f40
movb (%rdi,%rcx), %sil
movb %sil, (%rdi)
incq %rdi
testb %sil, %sil
jne 0x20f03
cmpb $0x2f, %al
movl %ebp, %eax
cmovel 0x1c(%rsp), %eax
testl %eax, %eax
jne 0x20f40
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
movl $0x7ffffffe, %ecx       # imm = 0x7FFFFFFE
callq 0x210d1
cmpl $-0x1, %eax
je 0x20fe0
movq %r12, %rdi
callq 0x47b38
testl %eax, %eax
jne 0x20e52
cmpl $0x0, 0x1937a9(%rip)    # 0x1b4700
je 0x20fb5
xorl %eax, %eax
testb $0x4, 0x4(%rsp)
je 0x20fc9
testb %al, %al
jne 0x20fc9
callq 0x222ab
testq %rax, %rax
je 0x20fc9
movq %rax, %r14
cmpb $0x0, (%rax)
je 0x20fab
leaq 0x8(%rsp), %r15
movq %r14, %r12
movq %r15, %rdi
movq %r12, %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x210d1
testl %eax, %eax
js 0x20fd8
movq %r12, %rdi
callq 0x6420
addq %rax, %r12
incq %r12
cmpb $0x0, (%r12)
jne 0x20f80
movq %r14, %rdi
callq 0x61c0
jmp 0x20fc9
cmpb $0x0, 0x3(%rsp)
setne %al
testb $0x1, 0x4(%rsp)
sete %cl
orb %al, %cl
je 0x2101e
movq %rbx, %rdi
callq 0x61c0
movq 0x8(%rsp), %rbx
jmp 0x21009
movq %r14, %rdi
callq 0x61c0
movq %rbx, %rdi
callq 0x61c0
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x21007
xorl %ebx, %ebx
movq 0x10(%rdi), %r14
callq 0x61c0
movq %r14, %rdi
testq %r14, %r14
jne 0x20ff4
jmp 0x21009
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %r15
movb $0x0, 0xc(%r15)
xorl %r14d, %r14d
leaq 0x19370e(%rip), %rbp    # 0x1b4740
leaq 0x83007(%rip), %rax     # 0xa4040
cmpl $-0x1, (%rax,%r14,4)
je 0x210b5
leaq 0x1936e9(%rip), %rax    # 0x1b4730
movslq (%rax,%r14,4), %r12
testq %r12, %r12
jle 0x210b5
movq %r12, %r13
incq %r13
shll $0x4, %r12d
addl $-0x10, %r12d
movq (%rbp,%r14,8), %rax
movslq %r12d, %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 0x223d5
testl %eax, %eax
je 0x210a8
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movl $0x1, %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
callq 0x210d1
cmpl $-0x1, %eax
je 0x20fe0
decq %r13
addl $-0x10, %r12d
cmpq $0x1, %r13
ja 0x2105e
incq %r14
cmpq $0x4, %r14
jne 0x21032
cmpl $0x0, 0x193637(%rip)    # 0x1b4700
sete %al
jmp 0x20f5b