// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movl %ecx, %r15d
movq %rsi, %r14
movzwl 0x30(%rdi), %eax
andl $0x0, 0x8(%rdx)
andq $0x0, (%rdx)
movb 0x250(%rdi), %cl
movb %cl, 0xa(%rdx)
movb 0x251(%rdi), %cl
movq %rdx, 0x10(%rsp)
movb %cl, 0xb(%rdx)
movb $0x20, %cl
movb %cl, 0x28(%rsp)
movabsq $0x2020202020202020, %rdx # imm = 0x2020202020202020
movq %rdx, 0x20(%rsp)
movb %cl, 0xa(%rsp)
movw $0x2020, 0x8(%rsp)      # imm = 0x2020
movl $0x0, %r13d
cmpl $0x2, %r15d
jne 0xaaf6
movq 0x10(%rsp), %rcx
movb $0xf, (%rcx)
movb $0xf, %r13b
shrl $0x8, %eax
andl $0x1, %eax
leaq (%rax,%rax,2), %rax
addq $0x6, %rax
movq %rax, 0x30(%rsp)
pushq $0x1
popq %rcx
movl %r15d, 0xc(%rsp)
testl %ecx, %ecx
je 0xadae
testl %r15d, %r15d
je 0xab6a
cmpb $0x2a, (%r14)
jne 0xab6a
movl %ecx, %eax
leaq 0x6914(%rip), %rdx      # 0x11441
orb (%rax,%rdx), %r13b
movq 0x10(%rsp), %rdx
movb %r13b, (%rdx)
movb 0x1(%r14), %dl
testb %dl, %dl
je 0xad60
leaq 0x68f0(%rip), %rsi      # 0x1143c
cmpb (%rax,%rsi), %dl
jne 0xadf0
cmpl $0x4, %ecx
leal 0x1(%rcx), %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movl %eax, %ecx
jmp 0xad62
decl %ecx
leaq 0x68b9(%rip), %rdx      # 0x1142c
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq %r13, 0x18(%rsp)
movl %r13d, %ebp
andb $-0x2, %bpl
xorl %ebx, %ebx
movzbl (%r14,%rbx), %r13d
movsbq %r13b, %r12
testl %r13d, %r13d
je 0xad32
cmpl $0x2e, %r13d
je 0xad32
callq 0x3030
movq (%rax), %r15
movl (%r15,%r12,4), %esi
leaq 0xca67(%rip), %rdi      # 0x17620
callq 0x3150
testq %rax, %rax
je 0xad32
cmpq 0x30(%rsp), %rbx
jae 0xad32
movq 0x10(%rsp), %rax
movb %bpl, (%rax)
movsbq (%r14,%rbx), %rax
movb (%r15,%rax,4), %al
movb %al, 0x20(%rsp,%rbx)
incq %rbx
movl %ebp, %eax
movq %rax, 0x18(%rsp)
jmp 0xab8a
andb $-0xd, %r13b
movq 0x10(%rsp), %rax
movb %r13b, (%rax)
incq %r14
xorl %r12d, %r12d
xorl %ebx, %ebx
movsbl -0x1(%r14), %ebp
leaq 0x5cf5(%rip), %rdi      # 0x10908
movl %ebp, %esi
pushq $0x9
popq %rdx
callq 0x31a0
testq %rax, %rax
je 0xac38
cmpl $0x2, %r12d
ja 0xac38
shll $0x3, %ebx
addl $-0x30, %ebp
orl %ebp, %ebx
incl %r12d
incq %r14
jmp 0xac07
leal -0x100(%rbx), %ecx
xorl %eax, %eax
cmpl $0xffffff01, %ecx       # imm = 0xFFFFFF01
jb 0xadf2
movq 0x10(%rsp), %rcx
movb %bl, 0xa(%rcx)
cmpb $0x2c, -0x1(%r14)
pushq $0x4
popq %rcx
je 0xab11
jmp 0xadf2
incq %r14
xorl %r12d, %r12d
xorl %ebx, %ebx
movsbl -0x1(%r14), %ebp
leaq 0x5c8d(%rip), %rdi      # 0x10908
movl %ebp, %esi
pushq $0x9
popq %rdx
callq 0x31a0
testq %rax, %rax
je 0xaca0
cmpl $0x2, %r12d
ja 0xaca0
shll $0x3, %ebx
addl $-0x30, %ebp
orl %ebp, %ebx
incl %r12d
incq %r14
jmp 0xac6f
leal -0x100(%rbx), %eax
cmpl $0xffffff01, %eax       # imm = 0xFFFFFF01
jb 0xadf0
movq 0x10(%rsp), %rax
movb %bl, 0xb(%rax)
xorl %ecx, %ecx
cmpb $0x5d, -0x1(%r14)
movl $0x0, %eax
je 0xab11
jmp 0xadf2
movq %r13, 0x18(%rsp)
movl %r13d, %ebx
andb $-0x3, %bl
xorl %ebp, %ebp
leaq (%r14,%rbp), %r12
incq %r12
movsbq -0x1(%r12), %r13
testq %r13, %r13
je 0xad6b
callq 0x3030
movq (%rax), %r15
movl (%r15,%r13,4), %esi
leaq 0xc91e(%rip), %rdi      # 0x17620
callq 0x3150
testq %rax, %rax
je 0xad72
cmpq $0x2, %rbp
ja 0xad72
movq 0x10(%rsp), %rax
movb %bl, (%rax)
movsbq (%r14,%rbp), %rax
movb (%r15,%rax,4), %al
movb %al, 0x8(%rsp,%rbp)
incq %rbp
movl %ebx, %eax
movq %rax, 0x18(%rsp)
jmp 0xacdd
addq %rbx, %r14
incq %r14
cmpb $0x5b, %r13b
je 0xad9a
movzbl %r12b, %eax
cmpl $0x2e, %eax
movl 0xc(%rsp), %r15d
movq 0x18(%rsp), %r13
je 0xad90
testl %eax, %eax
jne 0xadf0
xorl %ecx, %ecx
jmp 0xab11
xorl %ecx, %ecx
addq $0x2, %r14
jmp 0xab11
xorl %ecx, %ecx
movq %r12, %r14
jmp 0xad9f
pushq $0x3
popq %rax
movl %eax, %ecx
movq %r12, %r14
cmpb $0x5b, %r13b
movl 0xc(%rsp), %r15d
movq 0x18(%rsp), %r13
je 0xab11
jmp 0xadf0
pushq $0x2
popq %rax
movl %eax, %ecx
jmp 0xab11
pushq $0x3
popq %rax
movl %eax, %ecx
movl 0xc(%rsp), %r15d
movq 0x18(%rsp), %r13
jmp 0xab11
leaq 0x20(%rsp), %rdi
callq 0x5119
movq 0x10(%rsp), %rbx
movw %ax, 0x2(%rbx)
leaq 0x23(%rsp), %rdi
callq 0x5119
movw %ax, 0x4(%rbx)
leaq 0x26(%rsp), %rdi
callq 0x5119
movw %ax, 0x6(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x5119
movw %ax, 0x8(%rbx)
pushq $0x1
popq %rax
jmp 0xadf2
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq