// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, %r12
pushq $0x3a
popq %rsi
movq %rbp, %rdi
callq 0x3150
testq %rax, %rax
movq %r15, 0x28(%rsp)
je 0x4f00
movq %rax, %r13
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
incq %r13
movb $0x0, (%rax)
leaq 0x30(%rsp), %rdi
pushq $0x20
popq %rdx
movq %rbp, %rsi
callq 0x3090
movq %rbp, %rdi
callq 0x3130
movq %rax, %r14
xorl %eax, %eax
movq %r12, (%rsp)
movl %eax, %ebx
cmpq %rbx, %r14
jbe 0x4f28
callq 0x3300
movq (%rax), %rax
movsbq (%rbp,%rbx), %rcx
movzwl (%rax,%rcx,2), %ecx
leal 0x1(%rbx), %eax
btl $0xa, %ecx
jb 0x4e8e
btl $0xb, %ecx
jae 0x501a
leaq (%rbx,%rbp), %r15
leaq 0x50(%rsp), %r12
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x32a0
movq (%r12), %rcx
cmpb $0x0, (%rcx)
jne 0x502d
cmpq $0x100, %rax            # imm = 0x100
jae 0x5040
movq %rax, 0x10(%rsp)
movb $0x0, (%r15)
movb $0x1, %al
movq %rax, 0x8(%rsp)
jmp 0x4f3a
leaq 0xa535(%rip), %rdi      # 0xf43c
callq 0x3040
testq %rax, %rax
jne 0x4ff9
leaq 0x141ac(%rip), %r12     # 0x190c8
xorl %ecx, %ecx
xorl %edx, %edx
movq %rbp, %r13
jmp 0x4fda
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
leaq 0x1417f(%rip), %r12     # 0x190c0
movq (%r12), %r12
testq %r12, %r12
je 0x4fa2
leaq 0x8(%r12), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x31d0
testl %eax, %eax
jne 0x4f41
cmpq %rbx, %r14
jbe 0x4fc6
movq 0x20(%r12), %rax
testb $0x1, 0x18(%rax)
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
jne 0x4fda
movq 0x1206f(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xa464(%rip), %rsi      # 0xf3f7
xorl %r12d, %r12d
movq (%rsp), %rdx
movq %r15, %rcx
jmp 0x505d
movq 0x1204f(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xa46c(%rip), %rsi      # 0xf41f
xorl %r12d, %r12d
leaq 0x30(%rsp), %rcx
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x31e0
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
movq 0x28(%rsp), %rax
movb %cl, (%rax)
movb %dl, (%r14)
movq %r13, (%rbx)
movq %r12, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x11ff8(%rip), %rax     # 0x16ff8
movq (%rax), %rcx
leaq 0xa441(%rip), %rdi      # 0xf44b
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
xorl %r12d, %r12d
jmp 0x4fe7
movq 0x11fd7(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xa39e(%rip), %rsi      # 0xf3c9
jmp 0x5051
movq 0x11fc4(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xa33f(%rip), %rsi      # 0xf37d
jmp 0x5051
movq 0x11fb1(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xa351(%rip), %rsi      # 0xf3a2
xorl %r12d, %r12d
leaq 0x30(%rsp), %rcx
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0x4fe7
nop