// File: 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
subq $0x1480, %rsp           # imm = 0x1480
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x448(%rbp)
leaq -0x850(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x93d0
movq -0x10(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl $0x3, %edi
leaq 0x324b5(%rip), %rsi     # 0xc7e53
movb $0x0, %al
callq 0x8b980
leaq -0x1470(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x93d0
leaq -0x1470(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x96f0
leaq -0x1470(%rbp), %rdi
movl $0x2f, %esi
callq 0x9820
movq %rax, -0x1058(%rbp)
cmpq $0x0, %rax
jne 0x95a04
leaq -0x1470(%rbp), %rdi
movl $0x5c, %esi
callq 0x9820
movq %rax, -0x1058(%rbp)
cmpq $0x0, %rax
je 0x95a57
movq -0x1058(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x448(%rbp)
leaq -0x850(%rbp), %rdi
leaq -0x1470(%rbp), %rsi
movq -0x1058(%rbp), %rdx
leaq -0x1470(%rbp), %rax
subq %rax, %rdx
addq $0x1, %rdx
callq 0x93b0
movq -0x1058(%rbp), %rax
leaq -0x1470(%rbp), %rcx
subq %rcx, %rax
movb $0x0, -0x84f(%rbp,%rax)
cmpb $0x0, -0x850(%rbp)
je 0x95a70
leaq -0x850(%rbp), %rax
movq %rax, -0x1478(%rbp)
jmp 0x95a80
leaq 0x25d1d(%rip), %rax     # 0xbb794
movq %rax, -0x1478(%rbp)
jmp 0x95a80
movq -0x1478(%rbp), %rdi
callq 0x9b10
movq %rax, -0x440(%rbp)
cmpq $0x0, %rax
jne 0x95aa5
movl $0x0, -0x4(%rbp)
jmp 0x95d8b
jmp 0x95aa7
movq -0x440(%rbp), %rdi
callq 0x9b30
movq %rax, -0x438(%rbp)
cmpq $0x0, %rax
je 0x95c88
movq -0x438(%rbp), %rax
movsbl 0x13(%rax), %eax
cmpl $0x2e, %eax
jne 0x95ad6
jmp 0x95aa7
leaq -0xc50(%rbp), %rdi
movq -0x438(%rbp), %rdx
addq $0x13, %rdx
leaq 0x3237f(%rip), %rsi     # 0xc7e6e
movb $0x0, %al
callq 0x9150
leaq -0x1050(%rbp), %rdi
movq -0x448(%rbp), %rdx
leaq 0x32363(%rip), %rsi     # 0xc7e6e
movb $0x0, %al
callq 0x9150
leaq -0xc50(%rbp), %rax
movq %rax, -0x1058(%rbp)
movq -0x1058(%rbp), %rax
cmpb $0x0, (%rax)
je 0x95b5a
movq -0x1058(%rbp), %rax
movsbl (%rax), %edi
callq 0x9ab0
movb %al, %cl
movq -0x1058(%rbp), %rax
movb %cl, (%rax)
movq -0x1058(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1058(%rbp)
jmp 0x95b20
leaq -0x1050(%rbp), %rax
movq %rax, -0x1058(%rbp)
movq -0x1058(%rbp), %rax
cmpb $0x0, (%rax)
je 0x95ba2
movq -0x1058(%rbp), %rax
movsbl (%rax), %edi
callq 0x9ab0
movb %al, %cl
movq -0x1058(%rbp), %rax
movb %cl, (%rax)
movq -0x1058(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1058(%rbp)
jmp 0x95b68
leaq -0xc50(%rbp), %rax
movq %rax, -0x1058(%rbp)
leaq -0x1050(%rbp), %rdi
leaq 0x322b5(%rip), %rsi     # 0xc7e73
leaq -0x1068(%rbp), %rdx
callq 0x9da0
movq %rax, -0x1060(%rbp)
cmpq $0x0, -0x1060(%rbp)
je 0x95c3b
movq -0x1058(%rbp), %rdi
movq -0x1060(%rbp), %rsi
callq 0x91b0
movq %rax, -0x1058(%rbp)
cmpq $0x0, %rax
je 0x95c17
movq -0x1060(%rbp), %rdi
callq 0x92e0
addq -0x1058(%rbp), %rax
movq %rax, -0x1058(%rbp)
jmp 0x95c19
jmp 0x95c3b
jmp 0x95c1b
xorl %eax, %eax
movl %eax, %edi
leaq 0x3224d(%rip), %rsi     # 0xc7e73
leaq -0x1068(%rbp), %rdx
callq 0x9da0
movq %rax, -0x1060(%rbp)
jmp 0x95bd1
cmpq $0x0, -0x1058(%rbp)
je 0x95c83
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x95c83
movq -0x18(%rbp), %rax
movl -0x28(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x28(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x850(%rbp), %rdx
movq -0x438(%rbp), %rcx
addq $0x13, %rcx
leaq 0x2c01d(%rip), %rsi     # 0xc1c99
movb $0x0, %al
callq 0x9150
jmp 0x95aa7
movq -0x440(%rbp), %rdi
callq 0x9be0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x95d4c
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x95d3c
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x9a60
cmpl $0x0, %eax
jle 0x95d2c
leaq -0x430(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x96f0
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x96f0
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x430(%rbp), %rsi
callq 0x96f0
jmp 0x95d2e
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x95cb5
jmp 0x95d3e
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x95c9b
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x95d85
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movl $0x3, %edi
leaq 0x32102(%rip), %rsi     # 0xc7e75
movb $0x0, %al
callq 0x8b980
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x95d53
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1480, %rsp           # imm = 0x1480
popq %rbp
retq
nopw (%rax,%rax)