// File: /workspace/github_cpp_makefile_star2_O1/DoubleString[P]RtConverter/src/Rtklib/rtkcmn.cpp
// Function: bamboo::expath(char const*, char**, int)
extern int expath(const char *path, char *paths[], int nmax)
{
    int i,j,n=0;
    char tmp[1024];
#ifdef WIN32
    WIN32_FIND_DATA file;
    HANDLE h;
    char dir[1024]="",*p;
    
    trace(3,"expath  : path=%s nmax=%d\n",path,nmax);
    
    if ((p=strrchr(path,'\\'))) {
        strncpy(dir,path,p-path+1); dir[p-path+1]='\0';
    }
    if ((h=FindFirstFile((LPCTSTR)path,&file))==INVALID_HANDLE_VALUE) {
        strcpy(paths[0],path);
        return 1;
    }
    sprintf(paths[n++],"%s%s",dir,file.cFileName);
    while (FindNextFile(h,&file)&&n<nmax) {
        if (file.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) continue;
        sprintf(paths[n++],"%s%s",dir,file.cFileName);
    }
    FindClose(h);
#else
    struct dirent *d;
    DIR *dp;
    const char *file=path;
    char dir[1024]="",s1[1024],s2[1024],*p,*q,*r;
    
    trace(3,"expath  : path=%s nmax=%d\n",path,nmax);
    char p_path[1024] = {0};
    strcpy(p_path,path);
    if ((p=strrchr(p_path,'/'))||(p=strrchr(p_path,'\\'))) {
        file=p+1; strncpy(dir,p_path,p-p_path+1); dir[p-p_path+1]='\0';
    }
    if (!(dp=opendir(*dir?dir:"."))) return 0;
    while ((d=readdir(dp))) {
        if (*(d->d_name)=='.') continue;
        sprintf(s1,"^%s$",d->d_name);
        sprintf(s2,"^%s$",file);
        for (p=s1;*p;p++) *p=(char)tolower((int)*p);
        for (p=s2;*p;p++) *p=(char)tolower((int)*p);
        
        for (p=s1,q=strtok_r(s2,"*",&r);q;q=strtok_r(NULL,"*",&r)) {
            if ((p=strstr(p,q))) p+=strlen(q); else break;
        }
        if (p&&n<nmax) sprintf(paths[n++],"%s%s",dir,d->d_name);
    }
    closedir(dp);
#endif
    /* sort paths in alphabetical order */
    for (i=0;i<n-1;i++) {
        for (j=i+1;j<n;j++) {
            if (strcmp(paths[i],paths[j])>0) {
                strcpy(tmp,paths[i]);
                strcpy(paths[i],paths[j]);
                strcpy(paths[j],tmp);
            }
        }
    }
    for (i=0;i<n;i++) trace(3,"expath  : file=%s\n",paths[i]);
    
    return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1438, %rsp           # imm = 0x1438
movl %edx, %ebx
movq %rsi, -0x48(%rbp)
movq %rdi, %r13
leaq -0x460(%rbp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
xorl %esi, %esi
callq 0x8390
leaq -0x1060(%rbp), %r15
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
xorl %esi, %esi
callq 0x8390
movq %r15, %rdi
movq %r13, %rsi
callq 0x85c0
movq %r15, %rdi
movl $0x2f, %esi
callq 0x86a0
testq %rax, %rax
jne 0x53f7a
leaq -0x1060(%rbp), %rdi
movl $0x5c, %esi
callq 0x86a0
testq %rax, %rax
je 0x53fa1
movq %rax, %r12
leaq 0x1(%rax), %r13
subq %r15, %r12
leaq 0x1(%r12), %rdx
leaq -0x460(%rbp), %rdi
movq %r15, %rsi
callq 0x8350
movb $0x0, -0x45f(%rbp,%r12)
movq %r13, -0x38(%rbp)
cmpb $0x0, -0x460(%rbp)
leaq 0x152a1(%rip), %rax     # 0x69254
cmoveq %rax, %r14
movq %r14, %rdi
callq 0x8940
testq %rax, %rax
je 0x541c8
movq %rax, %r13
movq $0x0, -0x30(%rbp)
movq %r13, %rdi
callq 0x8960
testq %rax, %rax
je 0x54110
movq %rax, %r12
cmpb $0x2e, 0x13(%rax)
je 0x53fd3
movl %ebx, -0x40(%rbp)
addq $0x13, %r12
leaq -0xc60(%rbp), %rdi
leaq 0x21382(%rip), %rbx     # 0x75384
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x8120
leaq -0x860(%rbp), %rdi
movq %rbx, %rsi
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x8120
movb -0xc60(%rbp), %al
leaq -0xc5f(%rbp), %rbx
testb %al, %al
je 0x54047
movsbl %al, %edi
callq 0x88f0
movb %al, -0x1(%rbx)
movb (%rbx), %al
incq %rbx
jmp 0x54031
movb -0x860(%rbp), %al
leaq -0x85f(%rbp), %rbx
testb %al, %al
je 0x5406a
movsbl %al, %edi
callq 0x88f0
movb %al, -0x1(%rbx)
movb (%rbx), %al
incq %rbx
jmp 0x54054
leaq -0x860(%rbp), %rdi
leaq 0x21311(%rip), %rsi     # 0x75389
leaq -0x60(%rbp), %rdx
callq 0x8ba0
testq %rax, %rax
movl -0x40(%rbp), %ebx
je 0x540cf
movq %rax, %r15
leaq -0xc60(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x8160
testq %rax, %rax
je 0x53fd3
movq %rax, %r14
movq %r15, %rdi
callq 0x8290
addq %rax, %r14
xorl %edi, %edi
leaq 0x212cb(%rip), %rsi     # 0x75389
leaq -0x60(%rbp), %rdx
callq 0x8ba0
movq %rax, %r15
testq %rax, %rax
jne 0x54093
cmpl %ebx, -0x30(%rbp)
jge 0x53fd3
movq -0x30(%rbp), %rax
movq %rax, %rbx
incq %rbx
movl %eax, %eax
movq -0x48(%rbp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0x1b981(%rip), %rsi     # 0x6fa74
leaq -0x460(%rbp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x8120
movq %rbx, -0x30(%rbp)
movl -0x40(%rbp), %ebx
jmp 0x53fd3
movq %r13, %rdi
callq 0x8a20
movq -0x30(%rbp), %rax
cmpl $0x2, %eax
jb 0x541ca
leal -0x1(%rax), %ecx
movq %rcx, -0x50(%rbp)
xorl %ecx, %ecx
movq %rax, %r14
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq %rcx, %rbx
leaq 0x1(%rcx), %rax
movq %rax, -0x58(%rbp)
movl $0x1, %r13d
movq %rcx, -0x40(%rbp)
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %r15
movq -0x38(%rbp), %rax
movq (%rax,%r13,8), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x8890
testl %eax, %eax
jle 0x541a4
movq %r14, %rbx
leaq -0x1460(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x85c0
movq %r15, %rdi
movq %r12, %rsi
callq 0x85c0
movq -0x38(%rbp), %rax
movq (%rax,%r13,8), %rdi
movq %r14, %rsi
movq %rbx, %r14
movq -0x40(%rbp), %rbx
callq 0x85c0
incq %r13
cmpq %r13, %r14
jne 0x5414e
addq $0x8, -0x38(%rbp)
decq %r14
movq -0x58(%rbp), %rcx
cmpq -0x50(%rbp), %rcx
jne 0x54139
movq -0x30(%rbp), %rax
jmp 0x541ca
xorl %eax, %eax
addq $0x1438, %rsp           # imm = 0x1438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq