// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11Newfs
static int dos11Newfs(
  struct mountedFS *mount,
  size_t size
)
{
  struct DOS11data *data = &mount->dos11data;
  int i;

#define MFD2_BLOCK      2
#define MAP_BLOCK       3

  memset(data, 0, sizeof(*data));

  data->blocks = size / BLOCKSIZE_RK11;
  data->bitmaps = 5;
  data->bmblk[0] = MAP_BLOCK;
  data->bmblk[1] = MAP_BLOCK + 1;
  data->bmblk[2] = MAP_BLOCK + 2;
  data->bmblk[3] = MAP_BLOCK + 3;
  data->bmblk[4] = MAP_BLOCK + 4;
  data->bmindex = 0177777;

  /*
   * Build and write MFD Block #1:
   *
   *   - second MFD block is at block 2
   *   - 5 bitmaps starting at block (filesys->blocks - 5).
   */
  memset(data->buf, 0, mount->blocksz);
  data->buf[MFD1_MFD2BLOCK] = htole16(MFD2_BLOCK);
  data->buf[MFD1_INTERLEAVE] = htole16(1);
  data->buf[MFD1_BMSTART] = htole16(data->bmblk[0]);
  data->buf[MFD1_BMSTART + 1] = htole16(data->bmblk[1]);
  data->buf[MFD1_BMSTART + 2] = htole16(data->bmblk[2]);
  data->buf[MFD1_BMSTART + 3] = htole16(data->bmblk[3]);
  data->buf[MFD1_BMSTART + 4] = htole16(data->bmblk[4]);
  data->buf[MFD1_BMSTART + 5] = htole16(0);

  if (dos11WriteBlock(mount, MFD1_BLOCK, NULL) == 0)
    return 0;

  /*
   * Build and write MFD Block #2:
   *
   *   - no UFDs present
   */
  memset(data->buf, 0, mount->blocksz);

  if (dos11WriteBlock(mount, MFD2_BLOCK, NULL) == 0)
    return 0;

  /*
   * Build and write 5 bitmap blocks.
   */
  memset(data->buf, 0, mount->blocksz);

  for (i = 1; i < 6; i++) {
    data->buf[MAP_LINK] =
      i == 5 ? 0 : htole16(data->bmblk[i]);
    data->buf[MAP_MAP] = htole16(i);
    data->buf[MAP_WORDS] = htole16(MAP_LEN);
    data->buf[MAP_FIRST] = htole16(MAP_BLOCK);

    if (dos11WriteBlock(mount, data->bmblk[i - 1], NULL) == 0)
      return 0;
  }

  /*
   * Reserve used blocks in the bitmap(s).
   */
  if (bitmapSetBit(mount, BOOT_BLOCK) == 0)
    return 0;
  if (bitmapSetBit(mount, MFD1_BLOCK) == 0)
    return 0;
  if (bitmapSetBit(mount, MFD2_BLOCK) == 0)
    return 0;

  for (i = 1; i < 6; i++)
    if (bitmapSetBit(mount, MAP_BLOCK + i - 1) == 0)
      return 0;

  /*
   * Reserve all blocks past the end of the disk.
   */
  for (i = 4800; i < (5 * MAP_BLOCKS); i++)
    if (bitmapSetBit(mount, i) == 0)
    return 0;

  return bitmapFlush(mount);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rsi, %r14
movq %rdi, %rbx
addq $0x58, %rdi
movl $0x900, %edx            # imm = 0x900
xorl %esi, %esi
callq 0x3170
shrq $0x8, %r14
movl %r14d, 0x48(%rbx)
movabsq $0x5000400030005, %rax  # imm = 0x5000400030005
movq %rax, 0x4c(%rbx)
movl $0x70006, 0x54(%rbx)    # imm = 0x70006
orw $-0x1, 0x14e(%rbx)
leaq 0x554(%rbx), %r14
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
movaps 0x9768(%rip), %xmm0     # 0x10870
movups %xmm0, 0x554(%rbx)
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x616b
testl %eax, %eax
je 0x719c
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
pushq $0x2
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x616b
testl %eax, %eax
je 0x719c
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
leaq 0x50(%rbx), %r14
pushq $0x1
popq %r15
cmpq $0x5, %r15
je 0x7167
cmpq $0x6, %r15
je 0x71a4
movzwl (%r14), %eax
jmp 0x7169
xorl %eax, %eax
movw %ax, 0x554(%rbx)
movw %r15w, 0x556(%rbx)
movl $0x3003c, 0x558(%rbx)   # imm = 0x3003C
movzwl -0x2(%r14), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x616b
incq %r15
addq $0x2, %r14
testl %eax, %eax
jne 0x7155
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
retq
movq %rbx, %rdi
xorl %esi, %esi
callq 0x81f5
testl %eax, %eax
je 0x719c
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x81f5
testl %eax, %eax
je 0x719c
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x81f5
testl %eax, %eax
je 0x719c
pushq $-0x5
popq %r14
testl %r14d, %r14d
je 0x71f1
leal 0x8(%r14), %eax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x81f5
incl %r14d
testl %eax, %eax
jne 0x71d4
jmp 0x719c
movq %rbx, %rdi
popq %rbx
popq %r14
popq %r15
jmp 0x667c