// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/fsio.c
// Function: findMount
static struct mountedFS *findMount(
  char *cmd,
  char *dev,
  uint8_t *unit,
  uint8_t *present,
  char **fname
)
{
  struct mountedFS *fs = &localMount;
  unsigned long unitno = 0;
  uint8_t unitpresent = 0;
  unsigned int i;
  char *endptr, *ptr;
  char origdev[32];

  if ((ptr = strchr(dev, ':')) != NULL) {
    *ptr++ = '\0';

    strncpy(origdev, dev, sizeof(origdev));

    /*
     * Determine validity of the device name and whether a unit number is
     * present.
     */
    for (i = 0; i < strlen(dev); i++)
      if (!isalpha(dev[i])) {
        if (isdigit(dev[i])) {
          unitno = strtoul(&dev[i], &endptr, 8);
          if (*endptr != '\0') {
            fprintf(stderr,
                    "%s: Device \"%s\" unit number invalid\n", cmd, origdev);
            return NULL;
          }

          if (unitno > 0377) {
            fprintf(stderr,
                    "%s: Device \"%s\" unit number too large\n", cmd, origdev);
            return NULL;
          }
          dev[i] ='\0';
          unitpresent = 1;
          break;
        }
        fprintf(stderr,
                "%s: Device \"%s\" contains non-alpha character\n",
                cmd, origdev);
        return NULL;
      }

    /*
     * Search the mounted file system list.
     */
    for (fs = mounts; fs != NULL; fs = fs->next)
      if (strcmp(dev, fs->name) == 0) {
        if ((unitpresent == 0) ||
            ((fs->filesys->flags & FS_UNITVALID) != 0))
          break;
        fprintf(stderr,
                "%s: \"%s\" does not support unit numbers\n", cmd, fs->name);
        return NULL;
      }
  
    if (fs == NULL)
      fprintf(stderr, "%s: Device \"%s\" not mounted\n", cmd, origdev);
  } else {
    if (getenv("FSioForceLocal") != NULL) {
      fprintf(stderr, "Local file system access requires use of \"local:\"\n");
      return NULL;
    }
    ptr = dev;
  }

  /*
   * No device present - assume local file system usage
   */
  *unit = unitno & 0377;
  *present = unitpresent;
  *fname = ptr;
  return fs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r15
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r12
movq %rsi, %rdi
movl $0x3a, %esi
callq 0x3150
testq %rax, %rax
movq %r15, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
je 0x6413
movq %rax, %r13
movq %r12, 0x8(%rsp)
movb $0x0, (%rax)
leaq 0x30(%rsp), %rdi
movl $0x20, %edx
movq %rbp, %rsi
callq 0x3090
movq %rbp, %rdi
callq 0x3130
testq %rax, %rax
je 0x6403
movq %rax, %rbx
callq 0x3300
movq (%rax), %rax
movl $0x1, %ecx
xorl %r12d, %r12d
movsbq (%rbp,%r12), %rdx
movzwl (%rax,%rdx,2), %edx
btl $0xa, %edx
jae 0x643c
movl %ecx, %r12d
incl %ecx
cmpq %r12, %rbx
ja 0x63e9
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x6489
leaq 0xa022(%rip), %rdi      # 0x1043c
callq 0x3040
testq %rax, %rax
jne 0x6512
leaq 0x13c99(%rip), %r12     # 0x1a0c8
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %rbp, %r13
jmp 0x64c2
btl $0xb, %edx
jae 0x653a
addq %rbp, %r12
leaq 0x50(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x32a0
movq %rax, %rbx
movq (%r14), %rax
cmpb $0x0, (%rax)
jne 0x654d
cmpq $0x100, %rbx            # imm = 0x100
jae 0x6560
movb $0x0, (%r12)
movb $0x1, %al
movq %rax, 0x10(%rsp)
movb $0x1, %r15b
incq %r13
leaq 0x13c2d(%rip), %r12     # 0x1a0c0
movq (%r12), %r12
testq %r12, %r12
je 0x64eb
leaq 0x8(%r12), %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x31d0
testl %eax, %eax
jne 0x6493
cmpb $0x0, 0x10(%rsp)
je 0x64c2
movq 0x20(%r12), %rax
testb $0x1, 0x18(%rax)
je 0x651c
movq 0x18(%rsp), %rax
movb %bl, (%rax)
movq 0x20(%rsp), %rax
movb %r15b, (%rax)
movq 0x28(%rsp), %rax
movq %r13, (%rax)
movq %r12, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x11afe(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9f23(%rip), %rsi      # 0x1041f
xorl %r12d, %r12d
leaq 0x30(%rsp), %rcx
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0x64c2
callq 0x36fe
xorl %r12d, %r12d
jmp 0x64d9
movq 0x11acd(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9eca(%rip), %rsi      # 0x103f7
xorl %r12d, %r12d
movq 0x8(%rsp), %rdx
movq %r14, %rcx
jmp 0x657e
movq 0x11aaf(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9e7e(%rip), %rsi      # 0x103c9
jmp 0x6571
movq 0x11a9c(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9e1f(%rip), %rsi      # 0x1037d
jmp 0x6571
movq 0x11a89(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9e31(%rip), %rsi      # 0x103a2
xorl %r12d, %r12d
leaq 0x30(%rsp), %rcx
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0x64d9
nop