// File: /workspace/github_c_makefile_star2_O3/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
movl $0x0, 0x4c(%rdi)
leaq 0xe427(%rip), %rax      # 0x18fd8
testb $0x8, 0x2(%rax)
je 0xac22
leaq 0xe422(%rip), %rax      # 0x18fe0
movq 0x98(%rax), %r14
leaq 0x75cc(%rip), %rdi      # 0x12198
movq %r14, %rsi
callq 0x31d0
testl %eax, %eax
je 0xac00
leaq 0x75be(%rip), %rdi      # 0x1219d
movq %r14, %rsi
callq 0x31d0
testl %eax, %eax
jne 0xb159
movq $0x1a00, 0x40(%rbx)     # imm = 0x1A00
movl $0x100, 0x4c(%rbx)      # imm = 0x100
jmp 0xac22
movq $0xd00, 0x40(%rbx)      # imm = 0xD00
movl $0x80, 0x4c(%rbx)
leaq 0x7587(%rip), %rdi      # 0x1219d
movq %r14, %rsi
callq 0x31d0
testl %eax, %eax
je 0xabef
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
xorl %ebp, %ebp
testl %eax, %eax
je 0xac5c
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x98(%rsp), %rax
leaq 0x1ff(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x9, %rcx
movl %ecx, 0x48(%rbx)
movl %ecx, %edx
shrl $0x10, %edx
xorl %eax, %eax
testl $0xfff8, %ecx           # imm = 0xFFF8
setne %al
addw %dx, %ax
je 0xb147
leaq 0x52(%rbx), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x662(%rbx), %rcx
movq %rcx, 0x18(%rsp)
movzwl %ax, %eax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movl $0x0, (%rsp)
movl %r12d, %ecx
andb $0xf, %cl
movl $0x1, %eax
shll %cl, %eax
movl %r12d, %ecx
shrl $0x4, %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movl %eax, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
orw %ax, 0x52(%rbx,%rcx,2)
movw $0xffff, 0x72(%rbx,%r12,2) # imm = 0xFFFF
movzbl %r12b, %esi
movq %rbx, %rdi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x99c2
testl %eax, %eax
je 0xad3f
leaq 0xe2d2(%rip), %rax      # 0x18fd8
testb $0x20, (%rax)
jne 0xad66
movl $0xc, %edx
movq 0x18(%rsp), %rdi
leaq 0x75ac(%rip), %rsi      # 0x122c8
callq 0x30a0
testl %eax, %eax
je 0xad72
movl $0xc, %edx
movq 0x18(%rsp), %rdi
leaq 0x74c0(%rip), %rsi      # 0x121f6
callq 0x30a0
testl %eax, %eax
je 0xad87
movl 0x8(%rsp), %ecx
notl %ecx
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rdx
andw %cx, (%rax,%rdx,2)
incq %r12
cmpq 0x38(%rsp), %r12
jne 0xacb9
jmp 0xaed4
movb $0x1, %al
movl %eax, 0x14(%rsp)
movw $0x6, %ax
jmp 0xadb1
movzwl 0x648(%rbx), %eax
testb %r12b, %r12b
je 0xad8b
cmpl $0x8e53, %eax           # imm = 0x8E53
jne 0xad25
jmp 0xada4
movb $0x1, %cl
jmp 0xada6
movb $0x1, %cl
cmpl $0x8e52, %eax           # imm = 0x8E52
je 0xada6
cmpl $0x8e53, %eax           # imm = 0x8E53
je 0xada4
cmpl $0x8ea9, %eax           # imm = 0x8EA9
jne 0xad25
jmp 0xada6
xorl %ecx, %ecx
movl %ecx, 0x14(%rsp)
movzwl 0x646(%rbx), %eax
movw %ax, 0x272(%rbx,%r12,2)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movw $0x1, %r14w
xorl %edx, %edx
movzbl %r12b, %esi
movl %esi, 0x2c(%rsp)
movl %edx, 0x4(%rsp)
movzwl %r14w, %r13d
movq %rbx, %rdi
movl %r13d, %edx
callq 0x9d20
testl %eax, %eax
je 0xad3f
movb 0x40(%rsp,%r13), %al
leal 0x1(%rax), %ecx
movb %cl, 0x40(%rsp,%r13)
testb %al, %al
jne 0xad3f
movzwl 0x478(%rbx), %edx
movzwl 0x472(%rbx), %eax
movzwl 0x47a(%rbx), %ecx
cmpw $0x1, %r14w
jne 0xae3d
movzwl 0x476(%rbx), %ebp
cmpw $0x1f, %bp
ja 0xad3f
cmpw %ax, %bp
jbe 0xae47
jmp 0xad3f
cmpw %ax, %r15w
jne 0xad3f
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 0xae91
cmpl %edi, %esi
jl 0xae91
movl %edi, %r9d
movzwl 0x47a(%rbx,%r9,2), %r9d
addl %r9d, %ecx
btl $0x9, %r8d
jb 0xae91
addl %edx, %edi
addw %cx, %r9w
jae 0xae57
jmp 0xad3f
movzwl 0x474(%rbx), %r14d
cmpw %bp, %r14w
ja 0xad3f
movl 0x4(%rsp), %edx
cmpw %dx, %cx
cmoval %ecx, %edx
movl %eax, %r15d
testw %r14w, %r14w
movl 0x2c(%rsp), %esi
jne 0xadd6
decl %edx
movw %dx, 0x72(%rbx,%r12,2)
incl (%rsp)
cmpb $0x0, 0x14(%rsp)
je 0xad53
movl (%rsp), %r14d
movw %r14w, 0x50(%rbx)
testw %r14w, %r14w
je 0xb152
leaq 0xe0d6(%rip), %rax      # 0x18fc4
cmpl $0x0, (%rax)
jne 0xaf20
movzwl %r14w, %edx
leaq 0x8(%rbx), %rsi
cmpw $0x1, %r14w
leaq 0x7b99(%rip), %rax      # 0x12aa0
leaq 0x716d(%rip), %rcx      # 0x1207b
cmoveq %rax, %rcx
leaq 0x72b1(%rip), %rdi      # 0x121ca
xorl %eax, %eax
callq 0x3160
leaq 0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
xorl %edx, %edx
movl %edx, %eax
shrl $0x4, %eax
movq 0x30(%rsp), %rcx
movzwl (%rcx,%rax,2), %eax
movl %edx, %ecx
andl $0xf, %ecx
btl %ecx, %eax
jae 0xb104
movl %r14d, (%rsp)
movw $0x1, %ax
xorl %ebp, %ebp
movq %rdx, 0x8(%rsp)
movzbl %dl, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movzwl %ax, %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x9d20
testl %eax, %eax
je 0xac48
testw %r12w, %r12w
jne 0xaf84
movzwl 0x476(%rbx), %r12d
movzwl 0x478(%rbx), %r13d
movzwl 0x47c(%rbx), %esi
btl $0xb, %esi
setae %al
cmpl $0x3ea, %r13d           # imm = 0x3EA
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xafe7
movl %r13d, %eax
shrl %eax
movl $0x1f9, %ecx            # imm = 0x1F9
subl %eax, %ecx
addl $0x7, %eax
movl $0x5, %edx
btl $0x9, %esi
jae 0xafd0
movl %edx, %esi
addw 0x47a(%rbx,%rsi,2), %r14w
addl %eax, %edx
movzwl %dx, %edx
movzwl 0x472(%rbx,%rdx,2), %esi
btl $0xb, %esi
jb 0xafe7
cmpl %ecx, %edx
jle 0xafbf
movzwl 0x474(%rbx), %eax
testw %ax, %ax
jne 0xaf60
leaq 0xdfc6(%rip), %rax      # 0x18fc4
cmpl $0x0, (%rax)
jne 0xb0f8
movzwl 0x472(%rbx), %eax
movl %eax, 0x4(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x99c2
testl %eax, %eax
je 0xac48
movl $0xc, %edx
movq 0x18(%rsp), %rdi
leaq 0x71b9(%rip), %rsi      # 0x121f6
callq 0x30a0
movzwl 0x648(%rbx), %edi
testl %eax, %eax
je 0xb079
leaq 0x71af(%rip), %r15      # 0x12203
cmpl $0x8ea9, %edi           # imm = 0x8EA9
je 0xb08f
cmpl $0x8e53, %edi           # imm = 0x8E53
je 0xb088
cmpl $0x8e52, %edi           # imm = 0x8E52
jne 0xb128
leaq 0x7190(%rip), %r15      # 0x12207
jmp 0xb08f
leaq 0x40(%rsp), %r15
movq %r15, %rsi
callq 0x658c
jmp 0xb08f
leaq 0x717c(%rip), %r15      # 0x1220b
leaq 0x7179(%rip), %rdi      # 0x1220f
movq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rbp
movl %ebp, %edx
xorl %eax, %eax
callq 0x3160
leaq 0x7166(%rip), %rdi      # 0x12216
movq %r15, %rsi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x3160
movl 0x4(%rsp), %ecx
movzwl 0x72(%rbx,%rbp,2), %esi
incl %esi
movzwl %r14w, %edx
movzwl %r12w, %r8d
leaq 0x7164(%rip), %rdi      # 0x1223d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x3160
movl 0x4c(%rbx), %esi
testl %esi, %esi
je 0xb0f8
leaq 0x71c4(%rip), %rdi      # 0x122b5
xorl %eax, %eax
callq 0x3160
movl (%rsp), %r14d
decl %r14d
movq 0x8(%rsp), %rdx
movl $0x1, %ebp
cmpq $0xfe, %rdx
ja 0xac48
incq %rdx
testw %r14w, %r14w
jne 0xaf2b
jmp 0xac48
leaq 0x70e0(%rip), %rdi      # 0x1220f
movq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rbp
movl %ebp, %edx
xorl %eax, %eax
callq 0x3160
jmp 0xb0bf
movw $0x0, 0x50(%rbx)
jmp 0xac48
xorl %ebp, %ebp
jmp 0xac48
movq 0xce90(%rip), %rax      # 0x17ff0
movq (%rax), %rdi
leaq 0x7038(%rip), %rsi      # 0x121a2
movq %r14, %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0xac22