// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rdi
callq 0x3200
leaq 0x18(%rsp), %rsi
movl %eax, %edi
callq 0x32e0
xorl %r13d, %r13d
testl %eax, %eax
je 0x859d
movl %r13d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x58(%rsp), %rax
cmpq $0x12bf, %rax           # imm = 0x12BF
jg 0x85b2
movq $0x80, 0x28(%rbx)
cmpq $0x12c1, %rax           # imm = 0x12C1
jl 0x85c2
movq $0x400, 0x28(%rbx)      # imm = 0x400
movq 0x48(%rsp), %rax
xorl %r13d, %r13d
xorl %edx, %edx
divq 0x28(%rbx)
movl %eax, 0x48(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
je 0x8588
movzwl 0x554(%rbx), %ebp
movzwl 0x558(%rbx), %r12d
movzwl 0x556(%rbx), %eax
movl %eax, 0x4(%rsp)
leaq 0x4c(%rbx), %rax
movq %rax, 0x8(%rsp)
movw $0x0, 0x4c(%rbx)
leaq 0x558(%rbx), %r13
leaq 0xfef3(%rip), %r14      # 0x18510
xorl %r15d, %r15d
movzwl %r12w, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
je 0x870a
movzwl (%r13), %edi
cmpw $0x3c, %di
jne 0x87fe
movl $0x2ae, %eax            # imm = 0x2AE
movzwl (%rbx,%rax,2), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x866f
movl %ecx, %edx
shrl $0x8, %edx
movzbl (%r14,%rdx), %edx
addl %edx, %r15d
movzbl %cl, %ecx
movzbl (%r14,%rcx), %ecx
addl %ecx, %r15d
incq %rax
cmpq $0x2ea, %rax            # imm = 0x2EA
jne 0x864a
movzwl 0x4c(%rbx), %eax
leal 0x1(%rax), %ecx
movw %cx, 0x4c(%rbx)
movw %r12w, 0x4e(%rbx,%rax,2)
movzwl 0x554(%rbx), %r12d
testw %r12w, %r12w
jne 0x8620
jmp 0x86f1
movq 0x28(%rbx), %rax
shrq %rax
addq $-0x3, %rax
cmpq $0x2, %rax
jb 0x86e5
movl $0x1, %r14d
movl $0x3, %ecx
movzwl 0x554(%rbx,%r14,2), %esi
testl %esi, %esi
je 0x86d9
movl %ecx, %r12d
movzwl 0x554(%rbx,%r12,2), %edx
cmpl $0x9, %edx
jne 0x8788
leal 0x2(%rcx), %r14d
addl $0x4, %ecx
cmpq %r14, %rax
ja 0x86b7
movzwl 0x554(%rbx), %ebp
testw %bp, %bp
je 0x8712
movzwl %bp, %esi
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
jne 0x869b
jmp 0x8588
xorl %r13d, %r13d
jmp 0x8588
movl $0x0, 0x14e(%rbx)
movb $0x0, 0x152(%rbx)
movzwl 0x4e(%rbx), %esi
leaq 0x154(%rbx), %rdx
movq %rbx, %rdi
callq 0x78a2
testl %eax, %eax
je 0x8588
leaq 0x1086f(%rip), %rax     # 0x18fb4
cmpl $0x0, (%rax)
jne 0x8774
leaq 0x8(%rbx), %rsi
leaq 0x93ce(%rip), %rdi      # 0x11b23
xorl %eax, %eax
callq 0x3160
movl 0x48(%rbx), %esi
leaq 0x93d7(%rip), %rdi      # 0x11b3d
movl %r15d, %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x3160
movw $0x101, 0x954(%rbx)     # imm = 0x101
movl $0x1, %r13d
jmp 0x8588
movq 0xf861(%rip), %rax      # 0x17ff0
movq (%rax), %rdi
movl %esi, %ecx
shrl $0x8, %ecx
movzbl %sil, %r8d
leaq 0x9351(%rip), %rsi      # 0x11af3
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x31d0
leaq 0x10805(%rip), %rax     # 0x18fb8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8588
movq 0xf7f2(%rip), %rax      # 0x17fb8
cmpq (%rax), %rdi
je 0x8588
movzwl 0x554(%rbx,%r12,2), %edx
movzwl 0x554(%rbx,%r14,2), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
leaq 0x9304(%rip), %rsi      # 0x11af3
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x31d0
jmp 0x8588
leaq 0x14(%rsp), %rbx
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x39e6
movl (%rbx), %r13d
jmp 0x8588