// File: /workspace/github_c_makefile_star2_O3/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 0x3210
leaq 0x18(%rsp), %rsi
movl %eax, %edi
callq 0x32f0
xorl %r13d, %r13d
testl %eax, %eax
je 0x82b3
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 0x82cf
movq $0x80, 0x28(%rbx)
movl $0x80, %ecx
jmp 0x82ea
cmpq $0x12c0, %rax           # imm = 0x12C0
jne 0x82dd
movq 0x28(%rbx), %rcx
jmp 0x82ea
movq $0x400, 0x28(%rbx)      # imm = 0x400
movl $0x400, %ecx            # imm = 0x400
movq 0x48(%rsp), %rax
xorl %r13d, %r13d
xorl %edx, %edx
divq %rcx
movl %eax, 0x48(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x829e
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 0x101dc(%rip), %r14     # 0x18520
xorl %r15d, %r15d
movzwl %r12w, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x8431
movzwl (%r13), %edi
cmpw $0x3c, %di
jne 0x8525
movl $0x2ae, %eax            # imm = 0x2AE
movzwl (%rbx,%rax,2), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x8396
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 0x8371
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 0x8347
jmp 0x8418
movq 0x28(%rbx), %rax
shrq %rax
addq $-0x3, %rax
cmpq $0x2, %rax
jb 0x840c
movl $0x1, %r14d
movl $0x3, %ecx
movzwl 0x554(%rbx,%r14,2), %esi
testl %esi, %esi
je 0x8400
movl %ecx, %r12d
movzwl 0x554(%rbx,%r12,2), %edx
cmpl $0x9, %edx
jne 0x84af
leal 0x2(%rcx), %r14d
addl $0x4, %ecx
cmpq %r14, %rax
ja 0x83de
movzwl 0x554(%rbx), %ebp
testw %bp, %bp
je 0x8439
movzwl %bp, %esi
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
jne 0x83c2
jmp 0x829e
xorl %r13d, %r13d
jmp 0x829e
movl $0x0, 0x14e(%rbx)
movb $0x0, 0x152(%rbx)
movzwl 0x4e(%rbx), %esi
leaq 0x154(%rbx), %rdx
movq %rbx, %rdi
callq 0x7621
testl %eax, %eax
je 0x829e
leaq 0x10b58(%rip), %rax     # 0x18fc4
cmpl $0x0, (%rax)
jne 0x849b
leaq 0x8(%rbx), %rsi
leaq 0x96a7(%rip), %rdi      # 0x11b23
xorl %eax, %eax
callq 0x3160
movl 0x48(%rbx), %esi
leaq 0x96b0(%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 0x829e
movq 0xfb3a(%rip), %rax      # 0x17ff0
movq (%rax), %rdi
movl %esi, %ecx
shrl $0x8, %ecx
movzbl %sil, %r8d
leaq 0x962a(%rip), %rsi      # 0x11af3
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x31e0
leaq 0x10aee(%rip), %rax     # 0x18fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x829e
movq 0xfacb(%rip), %rax      # 0x17fb8
cmpq (%rax), %rdi
je 0x829e
movzwl 0x554(%rbx,%r12,2), %edx
movzwl 0x554(%rbx,%r14,2), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
leaq 0x95dd(%rip), %rsi      # 0x11af3
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x31e0
jmp 0x829e
leaq 0x14(%rsp), %rbx
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x3abe
movl (%rbx), %r13d
jmp 0x829e