// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11Mount
static int dos11Mount(
  struct mountedFS *mount
)
{
  struct DOS11data *data = &mount->dos11data;
  struct stat stat;
  uint16_t mfdblk, mapblk, interleave;
  unsigned int i, freeblocks = 0;

  if (fstat(fileno(mount->container), &stat) == 0) {
    if (stat.st_blocks < DISKSIZE_RK05)
      mount->blocksz = BLOCKSIZE_RF11 * 2;
    if (stat.st_blocks > DISKSIZE_RK05)
      mount->blocksz = BLOCKSIZE_RP03 * 2;

    data->blocks = stat.st_size / mount->blocksz;

    /*
     * Build a list of the bitmap blocks.
     */
    if (dos11ReadBlock(mount, MFD1_BLOCK, NULL) == 0)
      return 0;

    mfdblk = le16toh(data->buf[MFD1_MFD2BLOCK]);
    interleave = le16toh(data->buf[MFD1_INTERLEAVE]);

    data->bitmaps = 0;
    mapblk = le16toh(data->buf[MFD1_BMSTART]);

    do {
      if (dos11ReadBlock(mount, mapblk, NULL) == 0)
        return 0;

      if (le16toh(data->buf[MAP_WORDS]) != MAP_LEN) {
        ERROR("mount: wrong bitmap size (%d) for bitmap %d\n",
              le16toh(data->buf[MAP_WORDS]),
              data->bitmaps + 1);
        return 0;
      }

      /*
       * Compute the # of free blocks in the bitmap.
       */
      for (i = 0; i < MAP_LEN; i++) {
        uint16_t bmentry = le16toh(data->buf[i + MAP_BMSTART]);

        if (bmentry != 0177777) {
          freeblocks += zeroes[(bmentry >> 8) & 0377];
          freeblocks += zeroes[bmentry & 0377];
        }
      }
      data->bmblk[data->bitmaps++] = mapblk;

      mapblk = le16toh(data->buf[MAP_LINK]);
    } while (mapblk != 0);

    do {
      if (dos11ReadBlock(mount, mfdblk, NULL) == 0)
        return 0;

      for (i = MFD2_HEADER; i < EODIR(mount, MFD2_SIZE); i += MFD2_SIZE)
        if (le16toh(data->buf[i + MFD2_UFDUIC]) != 0)
          if (le16toh(data->buf[i + MFD2_UFDSIZE]) != UFD_LEN) {
            ERROR("mount: wrong directory size (%d) for [%3o,%3o]\n",
                  le16toh(data->buf[i + MFD2_UFDSIZE]),
                  (le16toh(data->buf[i + MFD2_UFDUIC]) >> 8) & 0377,
                  le16toh(data->buf[i + MFD2_UFDUIC]) & 0377);
            return 0;
          }
      mfdblk = le16toh(data->buf[MFD2_LINK]);
    } while (mfdblk != 0);

    /*
     * Preload bitmap 0
     */
    data->bmindex = 0;
    data->bmscan = 0;
    data->bmdirty = 0;

    if (dos11ReadBlock(mount, data->bmblk[0], data->bmbuf) == 0)
      return 0;

    if (!quiet) {
      printf("%s: successfully mounted\n", mount->name);
      printf("Total blocks: %d, Free blocks: %d, Interleave: %d\n",
             data->blocks, freeblocks, interleave);
    }

    /*
     * Set up default parameters
     */
    data->group = 1;
    data->user = 1;
    return 1;
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rdi
callq 0x3210
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x32f0
xorl %r14d, %r14d
testl %eax, %eax
je 0x6ded
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rsp), %rax
cmpq $0x12bf, %rax           # imm = 0x12BF
jg 0x6e09
movq $0x80, 0x28(%rbx)
movl $0x80, %ecx
jmp 0x6e24
cmpq $0x12c0, %rax           # imm = 0x12C0
jne 0x6e17
movq 0x28(%rbx), %rcx
jmp 0x6e24
movq $0x400, 0x28(%rbx)      # imm = 0x400
movl $0x400, %ecx            # imm = 0x400
movq 0x38(%rsp), %rax
xorl %r14d, %r14d
xorl %edx, %edx
divq %rcx
movl %eax, 0x48(%rbx)
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5ffa
testl %eax, %eax
je 0x6dd8
movzwl 0x554(%rbx), %r13d
movzwl 0x558(%rbx), %r15d
movzwl 0x556(%rbx), %eax
movl %eax, 0x4(%rsp)
andw $0x0, 0x4c(%rbx)
xorl %r14d, %r14d
leaq 0x106b1(%rip), %rbp     # 0x17520
xorl %r12d, %r12d
movzwl %r15w, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5ffa
testl %eax, %eax
je 0x6dd8
movzwl 0x558(%rbx), %edx
cmpl $0x3c, %edx
jne 0x703e
movl $0x2ae, %eax            # imm = 0x2AE
cmpq $0x2ea, %rax            # imm = 0x2EA
je 0x6ecf
movzwl (%rbx,%rax,2), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x6eca
movl %ecx, %edx
shrl $0x8, %edx
movzbl (%rbp,%rdx), %edx
addl %edx, %r12d
movzbl %cl, %ecx
movzbl (%rbp,%rcx), %ecx
addl %ecx, %r12d
incq %rax
jmp 0x6e9d
movzwl 0x4c(%rbx), %eax
leal 0x1(%rax), %ecx
movw %cx, 0x4c(%rbx)
movw %r15w, 0x4e(%rbx,%rax,2)
movzwl 0x554(%rbx), %r15d
testw %r15w, %r15w
jne 0x6e72
pushq $0x3
popq %rbp
movzwl %r13w, %esi
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5ffa
testl %eax, %eax
je 0x6dd8
movq 0x28(%rbx), %rax
shrq %rax
addq $-0x3, %rax
movl %ebp, %ecx
leal -0x2(%rcx), %r15d
cmpq %r15, %rax
jbe 0x6f47
movzwl 0x554(%rbx,%r15,2), %esi
testl %esi, %esi
je 0x6f42
movl %ecx, %r13d
movzwl 0x554(%rbx,%r13,2), %edx
cmpl $0x9, %edx
jne 0x6fc8
addl $0x4, %ecx
jmp 0x6f17
movzwl 0x554(%rbx), %r13d
testw %r13w, %r13w
jne 0x6ef1
andl $0x0, 0x14e(%rbx)
movb $0x0, 0x152(%rbx)
movzwl 0x4e(%rbx), %esi
leaq 0x154(%rbx), %rdx
movq %rbx, %rdi
callq 0x5ffa
testl %eax, %eax
pushq $0x1
popq %rbp
je 0x6dd8
leaq 0x1103c(%rip), %rax     # 0x17fc4
cmpl $0x0, (%rax)
jne 0x6fb7
leaq 0x8(%rbx), %rsi
leaq 0x9b8b(%rip), %rdi      # 0x10b23
xorl %eax, %eax
callq 0x3160
movl 0x48(%rbx), %esi
leaq 0x9b94(%rip), %rdi      # 0x10b3d
movl %r12d, %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x3160
movw $0x101, 0x954(%rbx)     # imm = 0x101
movl %ebp, %r14d
jmp 0x6dd8
movq 0x10029(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
movl %esi, %ecx
shrl $0x8, %ecx
movzbl %sil, %r8d
leaq 0x9b11(%rip), %rsi      # 0x10af3
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x31e0
leaq 0x10fd5(%rip), %rax     # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x6dd8
movq 0xffba(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x6dd8
movzwl 0x554(%rbx,%r13,2), %edx
movzwl 0x554(%rbx,%r15,2), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
leaq 0x9ac4(%rip), %rsi      # 0x10af3
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x31e0
jmp 0x6dd8
movq 0xffb3(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movzwl 0x4c(%rbx), %ecx
incl %ecx
leaq 0x9a71(%rip), %rsi      # 0x10ac6
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x31e0
leaq 0x10f62(%rip), %rax     # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x6dd8
movq 0xff47(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x6dd8
movzwl 0x558(%rbx), %edx
movzwl 0x4c(%rbx), %ecx
incl %ecx
leaq 0x9a30(%rip), %rsi      # 0x10ac6
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x31e0
jmp 0x6dd8