// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11ParseFilespec
int dos11ParseFilespec(
  char *ptr,
  struct dos11FileSpec *spec,
  unsigned char user,
  unsigned char group,
  int wildcard
)
{
  char term[] = { '\0', '.', '[', ',', ']' };
  char flags[] =
    { 0, DOS11_WC_NAME, DOS11_WC_EXT, DOS11_WC_GROUP, DOS11_WC_USER };
  int state = P_NAME;
  unsigned int uic, i;
  char filename[6], ext[3];

  memset(spec, 0, sizeof(struct dos11FileSpec));
  spec->user = user;
  spec->group = group;

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

  if (wildcard == DOS11_M_NONAME)
    if ((*ptr == '\0') || (*ptr == '['))
      spec->flags = DOS11_WC_NAME | DOS11_WC_EXT;

  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_USER)
            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 < sizeof(filename)))
          filename[i++] = toupper(*ptr++);

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

          case '.':
            state = P_EXT;
            break;

          case '[':
            state = P_GROUP;
            break;

          default:
            return 0;
        }
        break;

      case P_EXT:
        while ((*ptr != '\0') &&
               (strchr(rad50, toupper(*ptr)) != NULL) &&
               (i < sizeof(ext)))
          ext[i++] = toupper(*ptr++);
        
        switch (*ptr++) {
          case '\0':
            state = P_DONE;
            break;

          case '[':
            state = P_GROUP;
            break;

          default:
            return 0;
        }
        break;

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

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

        state = P_USER;
        break;

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

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

        state = P_DONE;
        break;
    }
  }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %r13d
movq %rsi, %r15
movq %rdi, %r14
movw $0x0, 0x8(%rsi)
movq $0x0, (%rsi)
movb %dl, 0x8(%rsi)
movb %cl, 0x9(%rsi)
movw $0x2020, %ax            # imm = 0x2020
movw %ax, 0xc(%rsp)
movl $0x20202020, 0x8(%rsp)  # imm = 0x20202020
movw %ax, 0x4(%rsp)
movb $0x20, 0x6(%rsp)
cmpl $0x2, %r8d
jne 0x7f1e
movzbl (%r14), %eax
cmpl $0x5b, %eax
je 0x7f1a
testl %eax, %eax
jne 0x7f1e
movb $0x3, (%r15)
movl $0x1, %eax
leaq 0x99de(%rip), %r12      # 0x11908
movq %r15, 0x10(%rsp)
movl %r13d, (%rsp)
testl %r13d, %r13d
je 0x7f7c
cmpb $0x2a, (%r14)
jne 0x7f7c
movl %eax, %ecx
leaq 0x991e(%rip), %rdx      # 0x11865
movb (%rcx,%rdx), %dl
orb %dl, (%r15)
movb 0x1(%r14), %dl
testb %dl, %dl
je 0x8134
leaq 0x9900(%rip), %rsi      # 0x11860
cmpb (%rcx,%rsi), %dl
jne 0x8202
cmpl $0x4, %eax
leal 0x1(%rax), %eax
movl $0x0, %ecx
cmovel %ecx, %eax
jmp 0x8136
decl %eax
leaq 0x98cb(%rip), %rcx      # 0x11850
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %r13d, %r13d
movzbl (%r14,%r13), %r15d
movsbq %r15b, %rbp
testl %r15d, %r15d
je 0x810d
cmpl $0x2e, %r15d
je 0x810d
callq 0x3030
movq (%rax), %rax
movl (%rax,%rbp,4), %ebx
leaq 0x10651(%rip), %rdi     # 0x18610
movl %ebx, %esi
callq 0x3150
testq %rax, %rax
je 0x810d
cmpq $0x5, %r13
ja 0x810d
movb %bl, 0x8(%rsp,%r13)
incq %r13
jmp 0x7f91
movsbl (%r14), %ebx
movl $0x9, %edx
movq %r12, %rdi
movl %ebx, %esi
callq 0x31a0
xorl %ecx, %ecx
testq %rax, %rax
je 0x813f
xorl %r13d, %r13d
xorl %ebp, %ebp
movl %ebp, %eax
shll $0x3, %eax
addl $-0x30, %ebx
movl %ebx, %ebp
orl %eax, %ebp
movsbl 0x1(%r14,%r13), %ebx
movl $0x9, %edx
movq %r12, %rdi
movl %ebx, %esi
callq 0x31a0
leaq 0x1(%r13), %rcx
testq %rax, %rax
je 0x8039
cmpl $0x2, %r13d
movq %rcx, %r13
jb 0x8006
addq %rcx, %r14
movl (%rsp), %r13d
xorl %ecx, %ecx
jmp 0x8141
movsbl (%r14), %ebx
movl $0x9, %edx
movq %r12, %rdi
movl %ebx, %esi
callq 0x31a0
xorl %ecx, %ecx
testq %rax, %rax
je 0x816a
xorl %r13d, %r13d
xorl %ebp, %ebp
movl %ebp, %eax
shll $0x3, %eax
addl $-0x30, %ebx
movl %ebx, %ebp
orl %eax, %ebp
movsbl 0x1(%r14,%r13), %ebx
movl $0x9, %edx
movq %r12, %rdi
movl %ebx, %esi
callq 0x31a0
leaq 0x1(%r13), %rcx
testq %rax, %rax
je 0x809d
cmpl $0x2, %r13d
movq %rcx, %r13
jb 0x806a
addq %rcx, %r14
movl (%rsp), %r13d
xorl %ecx, %ecx
jmp 0x816c
movb (%r14), %bpl
testb %bpl, %bpl
je 0x81b1
callq 0x3030
movq %rax, %rbx
xorl %r15d, %r15d
movq (%rbx), %rax
movsbq %bpl, %rcx
movl (%rax,%rcx,4), %r13d
leaq 0x1053c(%rip), %rdi     # 0x18610
movl %r13d, %esi
callq 0x3150
testq %rax, %rax
je 0x81a5
cmpq $0x2, %r15
ja 0x81a5
leaq 0x1(%r15), %rax
movb %r13b, 0x4(%rsp,%r15)
movb 0x1(%r14,%r15), %bpl
movq %rax, %r15
testb %bpl, %bpl
jne 0x80c2
addq %rax, %r14
jmp 0x81a8
addq %r13, %r14
incq %r14
cmpb $0x5b, %r15b
je 0x8195
movzbl %bpl, %eax
cmpl $0x2e, %eax
movq 0x10(%rsp), %r15
movl (%rsp), %r13d
je 0x818e
testl %eax, %eax
je 0x818a
jmp 0x8202
xorl %eax, %eax
addq $0x2, %r14
jmp 0x81c6
xorl %ebp, %ebp
leal -0x100(%rbp), %eax
cmpl $0xffffff01, %eax       # imm = 0xFFFFFF01
jb 0x8204
movb %bpl, 0x9(%r15)
cmpb $0x2c, (%r14)
jne 0x8204
incq %r14
movl $0x4, %eax
jmp 0x81c6
xorl %ebp, %ebp
leal -0x100(%rbp), %eax
cmpl $0xffffff01, %eax       # imm = 0xFFFFFF01
jb 0x8204
movb %bpl, 0x8(%r15)
cmpb $0x5d, (%r14)
jne 0x8204
incq %r14
xorl %eax, %eax
jmp 0x81c6
movl $0x2, %eax
jmp 0x81c6
movl $0x3, %eax
movq 0x10(%rsp), %r15
movl (%rsp), %r13d
jmp 0x81c6
addq %r15, %r14
movq 0x10(%rsp), %r15
movl (%rsp), %r13d
incq %r14
movzbl %bpl, %eax
testb %al, %al
je 0x81c6
cmpl $0x5b, %eax
jne 0x8202
movl $0x3, %eax
testl %eax, %eax
jne 0x7f33
leaq 0x8(%rsp), %rdi
callq 0x66bc
movw %ax, 0x2(%r15)
leaq 0xb(%rsp), %rdi
callq 0x66bc
movw %ax, 0x4(%r15)
leaq 0x4(%rsp), %rdi
callq 0x66bc
movw %ax, 0x6(%r15)
movl $0x1, %ecx
jmp 0x8204
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq