// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11Mount
static int rt11Mount(
  struct mountedFS *mount
)
{
  struct RT11data *data = &mount->rt11data;
  struct stat stat;

  data->sectorsz = 0;

  /*
   * Check for device type override.
   */
  if (SWISSET('t')) {
    if (strcmp("rx01", SWGETVAL('t')) == 0) {
      mount->skip = RT11_RX0xNSECT * RT11_RX01SS;
      data->sectorsz = RT11_RX01SS;
    }
    if (strcmp("rx02", SWGETVAL('t')) == 0) {
      mount->skip = RT11_RX0xNSECT * RT11_RX02SS;
      data->sectorsz = RT11_RX02SS;
    }

    if (data->sectorsz == 0)
      fprintf(stderr,
              "mount: Ignoring unknown disk type \"%s\"\n", SWGETVAL('t'));
  }

  memset(&data->valid, 0, sizeof(data->valid));

  if (fstat(fileno(mount->container), &stat) == 0) {
    uint16_t i, count, lastsz, validcount = 0;;

    data->blocks = stat.st_size / RT11_BLOCKSIZE;

    count = data->blocks / RT11_MAXPARTSZ;
    lastsz = data->blocks % RT11_MAXPARTSZ;

    if (lastsz >= RT11_MINPARTSZ)
      count++;

    /*
     * Check each file system for validity.
     */
    for (i = 0; i < count; i++) {
      int type;

      /*
       * Assume valid and maximal size
       */
      data->valid[i / 16] |= 1 << (i % 16);
      data->maxblk[i] = RT11_MAXPARTSZ - 1;

      type = validate(mount, i, &data->maxblk[i], &data->first[i]);

      if (type != RT11_NOPART)
        validcount++;
      else data->valid[i / 16] &= ~(1 << (i % 16));

      if (type == RT11_SINGLE)
        break;
    }

    data->filesystems = validcount;

    if (validcount != 0) {
      if (!quiet)
        printf("%s: successfully mounted (%d partition%s)\n\n",
               mount->name, validcount, validcount == 1 ? "" : "s");

      for (i = 0; (i < 256) && (validcount != 0); i++)
        if (RT11_PARTITIONVALID(data, i)) {
          uint16_t entrysz, freeblks = 0, dsseg = 1;
          uint16_t highest = 0;

          do {
            uint16_t off = RT11_DH_SIZE;

            if (rt11ReadDirSegment(mount, i, dsseg) == 0)
              return 0;

            if (highest == 0)
              highest = le16toh(data->buf[RT11_DH_HIGHEST]);

            entrysz = RT11_DI_SIZE + (le16toh(data->buf[RT11_DH_EXTRA]) >> 1);

            while (!RT11EOS(le16toh(data->buf[off + RT11_DI_STATUS])) &&
                   ((RT11_DS_SIZE - off) >= entrysz)) {
              uint16_t status = le16toh(data->buf[off + RT11_DI_STATUS]);

              if ((status & RT11_E_MPTY) != 0)
                freeblks += le16toh(data->buf[off + RT11_DI_LENGTH]);

              off += entrysz;
            }
            dsseg = le16toh(data->buf[RT11_DH_NEXT]);
          } while (dsseg != 0);

          if (!quiet) {
            char vers[4], *version = NULL;
            uint16_t cnt, extra;

            cnt = le16toh(data->buf[RT11_DH_COUNT]);
            extra = le16toh(data->buf[RT11_DH_EXTRA]);

            if (rt11ReadBlock(mount, i, RT11_HOME, NULL) == 0)
              return 0;

            /*
             * Special handling of volumes created by non-RT-11 systenms
             * (e.g. VMS Exchange).
             */
            if (strncmp((char *)&data->buf[RT11_HB_SYSID], RT11_VMSSYSID, strlen(RT11_VMSSYSID)) == 0) {
              r50Asc(le16toh(data->buf[RT11_HB_SYSVER]), vers);
              version = vers;
            } else {
              switch (le16toh(data->buf[RT11_HB_SYSVER])) {
                case RT11_SYSVER_V3A:
                  version = "V3A";
                  break;

                case RT11_SYSVER_V04:
                  version = "V04";
                  break;

                case RT11_SYSVER_V05:
                  version = "V05";
                  break;
              }
            }

            printf("%s%o:\n", mount->name, i);
            if (version != NULL)
              printf("  Version: %s,        System ID: %12s\n",
                     version, (char *)&data->buf[RT11_HB_SYSID]);
            printf("  Total blocks: %5d, Free blocks: %5d\n"
                   "  Directory segments: %2d (Highest in use: %d)\n"
                   "  Extra bytes/directory entry: %d\n",
                   data->maxblk[i] + 1, freeblks, cnt, highest, extra);
            if (data->sectorsz != 0)
              printf("  Sector size: %d\n", data->sectorsz);
          }
          validcount--;
        }
      return 1;
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
andl $0x0, 0x4c(%rdi)
leaq 0xe604(%rip), %rax      # 0x17fd8
testb $0x8, 0x2(%rax)
je 0x9a4f
leaq 0xe5ff(%rip), %rax      # 0x17fe0
movq 0x98(%rax), %r14
leaq 0x77a9(%rip), %rdi      # 0x11198
movq %r14, %rsi
callq 0x31d0
movl %eax, %ebp
testl %eax, %eax
jne 0x9a0c
movq $0xd00, 0x40(%rbx)      # imm = 0xD00
movl $0x80, 0x4c(%rbx)
leaq 0x778a(%rip), %rdi      # 0x1119d
movq %r14, %rsi
callq 0x31d0
testl %eax, %eax
je 0x9a40
testl %ebp, %ebp
je 0x9a4f
movq 0xd5ce(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x776e(%rip), %rsi      # 0x111a2
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0x9a4f
movq $0x1a00, 0x40(%rbx)     # imm = 0x1A00
movl $0x100, 0x4c(%rbx)      # imm = 0x100
xorps %xmm0, %xmm0
movups %xmm0, 0x62(%rbx)
movups %xmm0, 0x52(%rbx)
movq 0x38(%rbx), %rdi
callq 0x3210
leaq 0x68(%rsp), %rsi
movl %eax, %edi
callq 0x32f0
testl %eax, %eax
je 0x9a8b
xorl %r15d, %r15d
movl %r15d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x52(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x98(%rsp), %rax
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
movl %eax, 0x48(%rbx)
movl %eax, %ecx
shrl $0x10, %ecx
andl $0xfff8, %eax           # imm = 0xFFF8
cmpq $0x1, %rax
sbbl $-0x1, %ecx
leaq 0x662(%rbx), %rax
movq %rax, 0x18(%rsp)
movzwl %cx, %eax
movq %rax, 0x38(%rsp)
xorl %esi, %esi
movl $0x0, 0x10(%rsp)
cmpq 0x38(%rsp), %rsi
je 0x9cef
movl %esi, %ecx
andb $0xf, %cl
pushq $0x1
popq %rdx
movl %edx, %eax
shll %cl, %eax
movl %esi, %ecx
shrl $0x4, %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movl %eax, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
orw %ax, 0x52(%rbx,%rcx,2)
orw $-0x1, 0x72(%rbx,%rsi,2)
movq %rsi, 0x8(%rsp)
movzbl %sil, %esi
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x859e
testl %eax, %eax
je 0x9b61
leaq 0xe4ac(%rip), %rax      # 0x17fd8
testb $0x20, (%rax)
jne 0x9b82
movq 0x18(%rsp), %rdi
leaq 0x778b(%rip), %rsi      # 0x112c8
pushq $0xc
popq %rdx
callq 0x30a0
testl %eax, %eax
je 0x9b8e
movq 0x18(%rsp), %rdi
leaq 0x76a1(%rip), %rsi      # 0x111f6
pushq $0xc
popq %rdx
callq 0x30a0
testl %eax, %eax
je 0x9ba5
movl 0x28(%rsp), %ecx
notl %ecx
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rdx
andw %cx, (%rax,%rdx,2)
movq 0x8(%rsp), %rsi
incq %rsi
jmp 0x9ad8
movb $0x1, %al
movl %eax, 0x14(%rsp)
movw $0x6, %ax
jmp 0x9bcf
movzwl 0x648(%rbx), %eax
cmpb $0x0, 0x8(%rsp)
je 0x9ba9
cmpl $0x8e53, %eax           # imm = 0x8E53
jne 0x9b49
jmp 0x9bc2
movb $0x1, %cl
jmp 0x9bc4
movb $0x1, %cl
cmpl $0x8e52, %eax           # imm = 0x8E52
je 0x9bc4
cmpl $0x8e53, %eax           # imm = 0x8E53
je 0x9bc2
cmpl $0x8ea9, %eax           # imm = 0x8EA9
jne 0x9b49
jmp 0x9bc4
xorl %ecx, %ecx
movl %ecx, 0x14(%rsp)
movzwl 0x646(%rbx), %eax
movq 0x8(%rsp), %rsi
movw %ax, 0x272(%rbx,%rsi,2)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movw $0x1, %bp
xorl %r13d, %r13d
movzbl %sil, %esi
movzwl %bp, %r14d
movq %rbx, %rdi
movl %r14d, %edx
callq 0x89c6
testl %eax, %eax
je 0x9b61
movb 0x40(%rsp,%r14), %al
leal 0x1(%rax), %ecx
movb %cl, 0x40(%rsp,%r14)
testb %al, %al
jne 0x9b61
movzwl 0x478(%rbx), %edx
movzwl 0x472(%rbx), %eax
movzwl 0x47a(%rbx), %ecx
cmpw $0x1, %bp
jne 0x9c59
movzwl 0x476(%rbx), %r15d
cmpw $0x1f, %r15w
ja 0x9b61
cmpw %ax, %r15w
jbe 0x9c63
jmp 0x9b61
cmpw %ax, %r12w
jne 0x9b61
shrl %edx
addl $0x7, %edx
movl $0x200, %esi            # imm = 0x200
subl %edx, %esi
movw $0x5, %di
movzwl %di, %edi
movzwl 0x472(%rbx,%rdi,2), %r8d
btl $0xb, %r8d
jb 0x9cad
cmpl %edi, %esi
jl 0x9cad
movl %edi, %r9d
movzwl 0x47a(%rbx,%r9,2), %r9d
addl %r9d, %ecx
btl $0x9, %r8d
jb 0x9cad
addl %edx, %edi
addw %cx, %r9w
jae 0x9c73
jmp 0x9b61
movzwl 0x474(%rbx), %ebp
cmpw %r15w, %bp
ja 0x9b61
cmpw %r13w, %cx
cmoval %ecx, %r13d
movl %eax, %r12d
testw %bp, %bp
movq 0x8(%rsp), %rsi
jne 0x9bf0
decl %r13d
movw %r13w, 0x72(%rbx,%rsi,2)
incl 0x10(%rsp)
cmpb $0x0, 0x14(%rsp)
je 0x9b7a
movl 0x10(%rsp), %ebp
movw %bp, 0x50(%rbx)
testw %bp, %bp
je 0x9a73
leaq 0xe2bd(%rip), %rax      # 0x17fc4
cmpl $0x0, (%rax)
jne 0x9d37
movzwl %bp, %edx
leaq 0x8(%rbx), %rsi
cmpw $0x1, %bp
leaq 0x7d62(%rip), %rax      # 0x11a80
leaq 0x7356(%rip), %rcx      # 0x1107b
cmoveq %rax, %rcx
leaq 0x749a(%rip), %rdi      # 0x111ca
xorl %eax, %eax
callq 0x3160
leaq 0x8(%rbx), %rax
movq %rax, 0x28(%rsp)
xorl %edx, %edx
xorl %r15d, %r15d
cmpq $0xff, %rdx
ja 0x9f21
testw %bp, %bp
je 0x9f21
movq %rdx, %rax
shrq $0x4, %rax
movq 0x30(%rsp), %rcx
movzwl (%rcx,%rax,2), %eax
movl %edx, %ecx
andl $0xf, %ecx
btl %ecx, %eax
jae 0x9f19
movl %ebp, 0x10(%rsp)
movw $0x1, %ax
movq %rdx, 0x8(%rsp)
movzbl %dl, %r14d
xorl %r12d, %r12d
xorl %ebp, %ebp
movzwl %ax, %edx
movq %rbx, %rdi
movl %r14d, %esi
callq 0x89c6
testl %eax, %eax
je 0x9a76
testw %bp, %bp
jne 0x9db1
movzwl 0x476(%rbx), %ebp
movzwl 0x478(%rbx), %r13d
movl %r13d, %eax
shrl %eax
movl $0x1f9, %ecx            # imm = 0x1F9
subl %eax, %ecx
addl $0x7, %eax
pushq $0x5
popq %rdx
movzwl %dx, %edx
movzwl 0x472(%rbx,%rdx,2), %esi
btl $0xb, %esi
jb 0x9df5
cmpl %ecx, %edx
jg 0x9df5
btl $0x9, %esi
jae 0x9df1
movl %edx, %esi
addw 0x47a(%rbx,%rsi,2), %r12w
addl %eax, %edx
jmp 0x9dcb
movzwl 0x474(%rbx), %eax
testw %ax, %ax
jne 0x9d8f
leaq 0xe1bc(%rip), %rax      # 0x17fc4
cmpl $0x0, (%rax)
jne 0x9f0e
movzwl 0x472(%rbx), %eax
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
movl %r14d, %esi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
callq 0x859e
testl %eax, %eax
je 0x9a76
movq 0x18(%rsp), %rdi
leaq 0x73b6(%rip), %rsi      # 0x111f6
pushq $0xc
popq %rdx
callq 0x30a0
movzwl 0x648(%rbx), %edi
testl %eax, %eax
je 0x9e79
cmpl $0x8e52, %edi           # imm = 0x8E52
je 0x9e91
cmpl $0x8e53, %edi           # imm = 0x8E53
movq 0x8(%rsp), %rdx
je 0x9e88
cmpl $0x8ea9, %edi           # imm = 0x8EA9
jne 0x9e9f
leaq 0x738c(%rip), %r14      # 0x11203
jmp 0x9ea2
leaq 0x40(%rsp), %r14
movq %r14, %rsi
callq 0x5068
jmp 0x9e98
leaq 0x737c(%rip), %r14      # 0x1120b
jmp 0x9ea2
leaq 0x736f(%rip), %r14      # 0x11207
movq 0x8(%rsp), %rdx
jmp 0x9ea2
xorl %r14d, %r14d
leaq 0x7366(%rip), %rdi      # 0x1120f
movq 0x28(%rsp), %rsi
xorl %eax, %eax
callq 0x3160
testq %r14, %r14
je 0x9ed0
leaq 0x7355(%rip), %rdi      # 0x11216
movq %r14, %rsi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x3160
movq 0x8(%rsp), %rax
movzwl 0x72(%rbx,%rax,2), %esi
incl %esi
movzwl %r12w, %edx
movzwl %bp, %r8d
leaq 0x7352(%rip), %rdi      # 0x1123d
movl 0x20(%rsp), %ecx
movl %r13d, %r9d
xorl %eax, %eax
callq 0x3160
movl 0x4c(%rbx), %esi
testl %esi, %esi
je 0x9f0e
leaq 0x73ae(%rip), %rdi      # 0x112b5
xorl %eax, %eax
callq 0x3160
movl 0x10(%rsp), %ebp
decl %ebp
movq 0x8(%rsp), %rdx
incq %rdx
jmp 0x9d45
pushq $0x1
popq %r15
jmp 0x9a76