// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/fsio/dosmt.c
// Function: dosmtParseFilespec
int dosmtParseFilespec(
  struct mountedFS *mount,
  char *ptr,
  struct dosmtFileSpec *spec,
  int wildcard
)
{
  struct DOSMTdata *data = &mount->dosmtdata;
  char term[] = { '\0', '.', '[', ',', ']' };
  char flags[] =
    { 0, DOSMT_WC_NAME, DOSMT_WC_EXT, DOSMT_WC_PROJ, DOSMT_WC_PROG };
  int state = P_NAME;
  unsigned int uic, i, namemax = ((mount->flags & FS_DOSMTEXT) != 0) ? 9 : 6;
  char filename[9], ext[3];

  memset(spec, 0, sizeof(struct dosmtFileSpec));
  spec->proj = data->proj;
  spec->prog = data->prog;

  memset(&filename, ' ', sizeof(filename));
  memset(&ext, ' ', sizeof(ext));

  if (wildcard == DOSMT_M_NONAME)
    spec->flags = DOSMT_WC_NAME | DOSMT_WC_EXT | DOSMT_WC_PROJ | DOSMT_WC_PROG;

  while (state != P_DONE) {
    if (wildcard) {
      if (*ptr == '*') {
        spec->flags |= flags[state];
        ptr++;
        if (*ptr == '\0')
          state = P_DONE;
        else if (*ptr == term[state]) {
          if (state == P_PROG)
            state = P_DONE;
          else state++;
        } else return 0;
        ptr++;
        continue;
      }
    }

    i = 0;

    switch (state) {
      case P_NAME:
        while ((*ptr != '\0') &&
               (*ptr != '.') &&
               (strchr(rad50, toupper(*ptr)) != NULL) &&
               (i < namemax)) {
          spec->flags &= ~DOSMT_WC_NAME;
          filename[i++] = toupper(*ptr++);
        }

        switch (*ptr++) {
          case '\0':
            state = P_DONE;
            break;

          case '.':
            state = P_EXT;
            break;

          case '[':
            state = P_PROJ;
            break;

          default:
            return 0;
        }
        break;

      case P_EXT:
        while ((*ptr != '\0') &&
               (strchr(rad50, toupper(*ptr)) != NULL) &&
               (i < sizeof(ext))) {
          spec->flags &= ~DOSMT_WC_EXT;
          ext[i++] = toupper(*ptr++);
        }

        switch (*ptr++) {
          case '\0':
            state = P_DONE;
            break;

          case '[':
            state = P_PROJ;
            break;

          default:
            return 0;
        }
        break;

      case P_PROJ:
        spec->flags &= ~(DOSMT_WC_PROJ | DOSMT_WC_PROG);
        uic = 0;
        while ((strchr("01234567", *ptr) != NULL) && (i < 3)) {
          uic = (uic << 3) | (*ptr++ - '0');
          i++;
        }
        if ((uic == 0) || (uic > 0377))
          return 0;
        spec->proj = uic & 0377;

        if (*ptr++ != ',')
          return 0;

        state = P_PROG;
        break;

      case P_PROG:
        uic = 0;
        while ((strchr("01234567", *ptr) != NULL) && (i < 3)) {
          uic = (uic << 3) | (*ptr++ - '0');
          i++;
        }
        if ((uic == 0) || (uic > 0377))
          return 0;
        spec->prog = uic & 0377;

        if (*ptr++ != ']')
          return 0;

        state = P_DONE;
        break;
    }
  }

  spec->name[0] = ascR50(&filename[0]);
  spec->name[1] = ascR50(&filename[3]);
  spec->name[2] = ascR50(&filename[6]);
  spec->ext = ascR50(&ext[0]);
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r12d
movq %rdx, %rbx
movq %rsi, %r14
movzwl 0x30(%rdi), %eax
movl $0x0, 0x8(%rdx)
movq $0x0, (%rdx)
movb 0x250(%rdi), %cl
movb %cl, 0xa(%rdx)
movb 0x251(%rdi), %cl
movb %cl, 0xb(%rdx)
movb $0x20, %cl
movb %cl, 0x18(%rsp)
movabsq $0x2020202020202020, %rdx # imm = 0x2020202020202020
movq %rdx, 0x10(%rsp)
movb %cl, 0xa(%rsp)
movw $0x2020, 0x8(%rsp)      # imm = 0x2020
cmpl $0x2, %r12d
jne 0xc120
movb $0xf, (%rbx)
shrl $0x8, %eax
andl $0x1, %eax
leaq (%rax,%rax,2), %rax
addq $0x6, %rax
movq %rax, 0x20(%rsp)
movl $0x1, %eax
movl %r12d, 0xc(%rsp)
testl %r12d, %r12d
je 0xc185
cmpb $0x2a, (%r14)
jne 0xc185
movl %eax, %ecx
leaq 0x62f0(%rip), %rdx      # 0x12441
movb (%rcx,%rdx), %dl
orb %dl, (%rbx)
movb 0x1(%r14), %dl
testb %dl, %dl
je 0xc364
leaq 0x62d3(%rip), %rsi      # 0x1243c
cmpb (%rcx,%rsi), %dl
jne 0xc426
cmpl $0x4, %eax
leal 0x1(%rax), %eax
movl $0x0, %ecx
cmovel %ecx, %eax
jmp 0xc366
decl %eax
leaq 0x629e(%rip), %rcx      # 0x1242c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %r13d, %r13d
movzbl (%r14,%r13), %ebp
movsbq %bpl, %r15
testl %ebp, %ebp
je 0xc33d
cmpl $0x2e, %ebp
je 0xc33d
callq 0x3030
movq (%rax), %r12
movl (%r12,%r15,4), %esi
leaq 0xc449(%rip), %rdi      # 0x18610
callq 0x3150
testq %rax, %rax
je 0xc33d
cmpq 0x20(%rsp), %r13
jae 0xc33d
andb $-0x2, (%rbx)
movsbq (%r14,%r13), %rax
movb (%r12,%rax,4), %al
movb %al, 0x10(%rsp,%r13)
incq %r13
jmp 0xc19a
andb $-0xd, (%rbx)
movsbl (%r14), %ebp
movl $0x9, %edx
leaq 0x56ff(%rip), %rdi      # 0x11908
movl %ebp, %esi
callq 0x31a0
xorl %ecx, %ecx
testq %rax, %rax
je 0xc36c
xorl %r15d, %r15d
xorl %r13d, %r13d
movl %r13d, %eax
shll $0x3, %eax
addl $-0x30, %ebp
movl %ebp, %r13d
orl %eax, %r13d
movsbl 0x1(%r14,%r15), %ebp
movl $0x9, %edx
leaq 0x56c6(%rip), %rdi      # 0x11908
movl %ebp, %esi
callq 0x31a0
leaq 0x1(%r15), %rcx
testq %rax, %rax
je 0xc25b
cmpl $0x2, %r15d
movq %rcx, %r15
jb 0xc221
addq %rcx, %r14
xorl %ecx, %ecx
jmp 0xc36f
movsbl (%r14), %ebp
movl $0x9, %edx
leaq 0x5693(%rip), %rdi      # 0x11908
movl %ebp, %esi
callq 0x31a0
xorl %ecx, %ecx
testq %rax, %rax
je 0xc399
xorl %r15d, %r15d
xorl %r13d, %r13d
movl %r13d, %eax
shll $0x3, %eax
addl $-0x30, %ebp
movl %ebp, %r13d
orl %eax, %r13d
movsbl 0x1(%r14,%r15), %ebp
movl $0x9, %edx
leaq 0x565a(%rip), %rdi      # 0x11908
movl %ebp, %esi
callq 0x31a0
leaq 0x1(%r15), %rcx
testq %rax, %rax
je 0xc2c7
cmpl $0x2, %r15d
movq %rcx, %r15
jb 0xc28d
addq %rcx, %r14
xorl %ecx, %ecx
jmp 0xc39c
movb (%r14), %bpl
testb %bpl, %bpl
je 0xc3ca
callq 0x3030
movq %rax, %r13
xorl %r15d, %r15d
movq (%r13), %r12
movsbq %bpl, %rax
movl (%r12,%rax,4), %esi
leaq 0xc315(%rip), %rdi      # 0x18610
callq 0x3150
testq %rax, %rax
je 0xc3c2
cmpq $0x2, %r15
ja 0xc3c2
andb $-0x3, (%rbx)
movsbq (%r14,%r15), %rax
movb (%r12,%rax,4), %cl
leaq 0x1(%r15), %rax
movb %cl, 0x8(%rsp,%r15)
movb 0x1(%r14,%r15), %bpl
movq %rax, %r15
testb %bpl, %bpl
jne 0xc2e8
addq %rax, %r14
jmp 0xc3c5
addq %r13, %r14
incq %r14
cmpb $0x5b, %bpl
movl 0xc(%rsp), %r12d
je 0xc3da
movzbl %r15b, %eax
cmpl $0x2e, %eax
je 0xc3bb
testl %eax, %eax
je 0xc3b7
jmp 0xc426
xorl %eax, %eax
addq $0x2, %r14
jmp 0xc3df
xorl %r13d, %r13d
leal -0x100(%r13), %eax
cmpl $0xffffff01, %eax       # imm = 0xFFFFFF01
jb 0xc428
movb %r13b, 0xa(%rbx)
cmpb $0x2c, (%r14)
jne 0xc428
incq %r14
movl $0x4, %eax
jmp 0xc3df
xorl %r13d, %r13d
leal -0x100(%r13), %eax
cmpl $0xffffff01, %eax       # imm = 0xFFFFFF01
jb 0xc428
movb %r13b, 0xb(%rbx)
cmpb $0x5d, (%r14)
jne 0xc428
incq %r14
xorl %eax, %eax
jmp 0xc3df
movl $0x2, %eax
jmp 0xc3df
addq %r15, %r14
movl 0xc(%rsp), %r12d
incq %r14
movzbl %bpl, %eax
testb %al, %al
je 0xc3df
cmpl $0x5b, %eax
jne 0xc426
movl $0x3, %eax
testl %eax, %eax
jne 0xc13d
leaq 0x10(%rsp), %rdi
callq 0x66bc
movw %ax, 0x2(%rbx)
leaq 0x13(%rsp), %rdi
callq 0x66bc
movw %ax, 0x4(%rbx)
leaq 0x16(%rsp), %rdi
callq 0x66bc
movw %ax, 0x6(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x66bc
movw %ax, 0x8(%rbx)
movl $0x1, %ecx
jmp 0xc428
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq