// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x31f0
movl %eax, %edi
leaq -0xa8(%rbp), %rsi
callq 0x32d0
cmpl $0x0, %eax
jne 0x95ab
cmpq $0x12c0, -0x68(%rbp)    # imm = 0x12C0
jge 0x8fff
movq -0x10(%rbp), %rax
movq $0x80, 0x28(%rax)
cmpq $0x12c0, -0x68(%rbp)    # imm = 0x12C0
jle 0x9015
movq -0x10(%rbp), %rax
movq $0x400, 0x28(%rax)      # imm = 0x400
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
xorl %edx, %edx
divq 0x28(%rcx)
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x904e
movl $0x0, -0x4(%rbp)
jmp 0x95b2
movq -0x18(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0xaa(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x50e(%rax), %edi
callq 0x6c50
movw %ax, -0xae(%rbp)
movq -0x18(%rbp), %rax
movw $0x0, 0x4(%rax)
movq -0x18(%rbp), %rax
movzwl 0x510(%rax), %edi
callq 0x6c50
movw %ax, -0xac(%rbp)
movq -0x10(%rbp), %rdi
movzwl -0xac(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x90c2
movl $0x0, -0x4(%rbp)
jmp 0x95b2
movq -0x18(%rbp), %rax
movzwl 0x510(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x3c, %eax
je 0x9195
movq 0x14f13(%rip), %rax     # 0x1dff8
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x510(%rax), %edi
callq 0x6c50
movq -0xc8(%rbp), %rdi
movzwl %ax, %edx
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %ecx
addl $0x1, %ecx
leaq 0x109f5(%rip), %rsi     # 0x19b10
movb $0x0, %al
callq 0x31c0
leaq 0x160bf(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x9189
leaq 0x160b2(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x14e80(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x9189
leaq 0x1609c(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x510(%rax), %edi
callq 0x6c50
movq -0xd0(%rbp), %rdi
movzwl %ax, %edx
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %ecx
addl $0x1, %ecx
leaq 0x1098e(%rip), %rsi     # 0x19b10
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x95b2
movl $0x0, -0xb4(%rbp)
cmpl $0x3c, -0xb4(%rbp)
jae 0x9242
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x4, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0xba(%rbp)
movzwl -0xba(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x922c
movzwl -0xba(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movslq %eax, %rcx
leaq 0x1531a(%rip), %rax     # 0x1e510
movzbl (%rax,%rcx), %eax
addl -0xb8(%rbp), %eax
movl %eax, -0xb8(%rbp)
movzwl -0xba(%rbp), %eax
andl $0xff, %eax
movslq %eax, %rcx
leaq 0x152f4(%rip), %rax     # 0x1e510
movzbl (%rax,%rcx), %eax
addl -0xb8(%rbp), %eax
movl %eax, -0xb8(%rbp)
jmp 0x922e
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x919f
movw -0xac(%rbp), %dx
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rsi
movw 0x4(%rsi), %cx
movw %cx, %di
addw $0x1, %di
movw %di, 0x4(%rsi)
movzwl %cx, %ecx
movw %dx, 0x6(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0xac(%rbp)
movzwl -0xac(%rbp), %eax
cmpl $0x0, %eax
jne 0x909d
jmp 0x9291
movq -0x10(%rbp), %rdi
movzwl -0xaa(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x92b6
movl $0x0, -0x4(%rbp)
jmp 0x95b2
movl $0x1, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
shrq %rcx
subq $0x3, %rcx
cmpq %rcx, %rax
jae 0x94d3
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
je 0x94bd
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x9, %eax
je 0x94bb
movq 0x14cc3(%rip), %rax     # 0x1dff8
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movl %eax, -0xd4(%rbp)
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movq -0xe0(%rbp), %rdi
movl -0xd8(%rbp), %edx
movl -0xd4(%rbp), %ecx
movzwl %ax, %r8d
andl $0xff, %r8d
leaq 0x1076b(%rip), %rsi     # 0x19b3d
movb $0x0, %al
callq 0x31c0
leaq 0x15e08(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x94af
leaq 0x15df7(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x14bc5(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x94af
leaq 0x15ddd(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0xe8(%rbp)
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
sarl $0x8, %eax
andl $0xff, %eax
movl %eax, -0xe4(%rbp)
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movq -0xf0(%rbp), %rdi
movl -0xe8(%rbp), %edx
movl -0xe4(%rbp), %ecx
movzwl %ax, %r8d
andl $0xff, %r8d
leaq 0x10695(%rip), %rsi     # 0x19b3d
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x95b2
jmp 0x94bd
jmp 0x94bf
movl -0xb4(%rbp), %eax
addl $0x4, %eax
movl %eax, -0xb4(%rbp)
jmp 0x92c0
movq -0x18(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movw %ax, -0xaa(%rbp)
movzwl -0xaa(%rbp), %eax
cmpl $0x0, %eax
jne 0x9291
movq -0x18(%rbp), %rax
movw $0x0, 0x106(%rax)
movq -0x18(%rbp), %rax
movw $0x0, 0x108(%rax)
movq -0x18(%rbp), %rax
movb $0x0, 0x10a(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x6(%rax), %esi
movq -0x18(%rbp), %rdx
addq $0x10c, %rdx            # imm = 0x10C
callq 0x7380
cmpl $0x0, %eax
jne 0x9549
movl $0x0, -0x4(%rbp)
jmp 0x95b2
leaq 0x15c94(%rip), %rax     # 0x1f1e4
cmpl $0x0, (%rax)
jne 0x958c
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
leaq 0x10609(%rip), %rdi     # 0x19b6d
movb $0x0, %al
callq 0x3160
movq -0x18(%rbp), %rax
movl (%rax), %esi
movl -0xb8(%rbp), %edx
movzwl -0xae(%rbp), %ecx
leaq 0x10602(%rip), %rdi     # 0x19b87
movb $0x0, %al
callq 0x3160
movq -0x18(%rbp), %rax
movb $0x1, 0x90c(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x90d(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x95b2
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nop