// File: /workspace/github_c_makefile_star2_O3/serge-404[P]CPM32/cpm.c
// Function: FindFirstFile
DIR *FindFirstFile(char* lpFileName,	// pointer to name of file to search for  
    WIN32_FIND_DATA *lpFindFileData) 	// pointer to returned information 
{
    DIR *pDir;
    struct dirent *pDirent;
    struct stat statbuf;
	char *cDir, *cName=lpFileName;
	int szDir;
	cDir=strrchr(lpFileName, '|');
	if (cDir==NULL)
		cDir=strrchr(lpFileName, '/');
	if ((cDir)!=NULL) { 
		cName=cDir+1;
		cDir=lpFileName;
		szDir=cName-cDir-1;
		strncpy(ffDir, cDir, szDir);
		ffDir[szDir]=0;
	}
	else {
		strncpy(ffDir, cpm_drive[ cpm_disk_no], sizeof(ffDir));
	}
	strncpy(ffName, cName, sizeof(ffName)-1);
	if (strncmp(ffName,"*.*",4)==0) {
		*ffName='*';
		ffName[1]=0;
	}
	CharUpperX(ffName);
	pDir = opendir(ffDir);
	if (pDir == NULL) 
        return hFindFile=INVALID_HANDLE_VALUE;
    while ((pDirent = readdir(pDir)) != NULL) {
       	strncpy(lpFindFileData->cFileName, pDirent->d_name, _MAX_PATH-1);
		if (fnmatch(ffName, CharUpperX(pDirent->d_name), FNM_CASEFOLD)==0) {
			if (stat(buildname(ffDir,lpFindFileData->cFileName), &statbuf)!=0) { 
				closedir(pDir);
				DEBUGOUT(stderr, "FindFirst ERROR: failed while stat file `%s`\n",lpFindFileData->cFileName);
	 			return hFindFile=INVALID_HANDLE_VALUE;
			}
			if (!(statbuf.st_mode & S_IFDIR)) {
				lpFindFileData->cAlternateFileName[0]=0;
				lpFindFileData->dwFileAttributes=(*(lpFindFileData->cFileName)=='.' ? FILE_ATTRIBUTE_HIDDEN : 0);	
				lpFindFileData->nFileSizeHigh=0;
				lpFindFileData->nFileSizeLow=(DWORD)statbuf.st_size;
				return pDir;
			}
		}
    }
    closedir(pDir);
    return hFindFile=INVALID_HANDLE_VALUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, (%rsp)
movq %rdi, %r14
movl $0x7c, %esi
callq 0x2110
testq %rax, %rax
jne 0x93dc
movq %r14, %rdi
movl $0x2f, %esi
callq 0x2110
testq %rax, %rax
je 0x9557
movq %rax, %rbx
incq %rbx
movl %r14d, %eax
notl %eax
addl %ebx, %eax
movslq %eax, %r15
leaq 0x10cafd(%rip), %r12    # 0x115ef0
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2070
movb $0x0, (%r15,%r12)
movq %rbx, %r14
leaq 0x10cbf0(%rip), %rdi    # 0x116000
movl $0x103, %edx            # imm = 0x103
movq %r14, %rsi
callq 0x2070
movl 0x10cbdd(%rip), %ebx    # 0x116000
cmpl $0x2a2e2a, %ebx         # imm = 0x2A2E2A
jne 0x9438
movw $0x2a, 0x10cbcc(%rip)   # 0x116000
movb $0x2a, %bl
jmp 0x943c
testb %bl, %bl
je 0x9460
leaq 0x10cbbe(%rip), %r14    # 0x116001
callq 0x2030
movq (%rax), %rcx
movsbq %bl, %rdx
movb (%rcx,%rdx,4), %cl
movb %cl, -0x1(%r14)
movb (%r14), %bl
incq %r14
testb %bl, %bl
jne 0x9448
leaq 0x10ca89(%rip), %rdi    # 0x115ef0
callq 0x20d0
testq %rax, %rax
je 0x9534
movq %rax, %r14
movq %rax, %rdi
callq 0x2270
testq %rax, %rax
je 0x952c
movq %rax, %rbx
movq (%rsp), %rax
leaq 0x18(%rax), %r15
leaq 0x10cb65(%rip), %r12    # 0x116000
leaq 0x13(%rbx), %r13
movl $0x103, %edx            # imm = 0x103
movq %r15, %rdi
movq %r13, %rsi
callq 0x2070
movb 0x13(%rbx), %bpl
testb %bpl, %bpl
je 0x94d9
callq 0x2030
addq $0x14, %rbx
movq (%rax), %rcx
movsbq %bpl, %rdx
movb (%rcx,%rdx,4), %cl
movb %cl, -0x1(%rbx)
movb (%rbx), %bpl
incq %rbx
testb %bpl, %bpl
jne 0x94c1
movq %r12, %rdi
movq %r13, %rsi
movl $0x10, %edx
callq 0x2140
testl %eax, %eax
jne 0x9518
leaq 0x10c9fc(%rip), %rdi    # 0x115ef0
movq %r15, %rsi
callq 0x89d9
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x2230
testl %eax, %eax
jne 0x957f
testb $0x40, 0x21(%rsp)
je 0x95ad
movq %r14, %rdi
callq 0x2270
movq %rax, %rbx
testq %rax, %rax
jne 0x949b
movq %r14, %rdi
callq 0x21a0
movq $0x0, 0x10c9a1(%rip) # 0x115ee0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0xacaa(%rip), %eax      # 0x14208
leaq 0x10aedb(%rip), %rcx    # 0x114440
movq (%rcx,%rax,8), %rsi
leaq 0x10c980(%rip), %rdi    # 0x115ef0
movl $0x104, %edx            # imm = 0x104
callq 0x2070
jmp 0x9409
movq %r14, %rdi
callq 0x21a0
cmpl $0x0, 0x10ad86(%rip)    # 0x114314
je 0x9534
movq 0xaa61(%rip), %rax      # 0x13ff8
movq (%rax), %rdi
leaq 0x7856(%rip), %rsi      # 0x10df7
movq %r15, %rdx
xorl %eax, %eax
callq 0x2200
jmp 0x9534
movq (%rsp), %rcx
movb $0x0, 0x11c(%rcx)
xorl %eax, %eax
cmpb $0x2e, 0x18(%rcx)
sete %al
addl %eax, %eax
movq %rax, (%rcx)
movq $0x0, 0x8(%rcx)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rcx)
jmp 0x9542