// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x910, %edx            # imm = 0x910
callq 0x3170
movq -0x18(%rbp), %rax
shrq $0x8, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movw $0x5, 0x4(%rax)
movq -0x20(%rbp), %rax
movw $0x3, 0x6(%rax)
movq -0x20(%rbp), %rax
movw $0x4, 0x8(%rax)
movq -0x20(%rbp), %rax
movw $0x5, 0xa(%rax)
movq -0x20(%rbp), %rax
movw $0x6, 0xc(%rax)
movq -0x20(%rbp), %rax
movw $0x7, 0xe(%rax)
movq -0x20(%rbp), %rax
movw $0xffff, 0x106(%rax)    # imm = 0xFFFF
movq -0x20(%rbp), %rdi
addq $0x50c, %rdi            # imm = 0x50C
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
xorl %esi, %esi
callq 0x3170
movl $0x2, %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x50c(%rax)
movl $0x1, %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x50e(%rax)
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x510(%rax)
movq -0x20(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x512(%rax)
movq -0x20(%rbp), %rax
movzwl 0xa(%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x514(%rax)
movq -0x20(%rbp), %rax
movzwl 0xc(%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x516(%rax)
movq -0x20(%rbp), %rax
movzwl 0xe(%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x518(%rax)
xorl %edi, %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x51a(%rax)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
cmpl $0x0, %eax
jne 0x976e
movl $0x0, -0x4(%rbp)
jmp 0x9972
movq -0x20(%rbp), %rdi
addq $0x50c, %rdi            # imm = 0x50C
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
xorl %esi, %esi
callq 0x3170
movq -0x10(%rbp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
cmpl $0x0, %eax
jne 0x97ab
movl $0x0, -0x4(%rbp)
jmp 0x9972
movq -0x20(%rbp), %rdi
addq $0x50c, %rdi            # imm = 0x50C
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
xorl %esi, %esi
callq 0x3170
movl $0x1, -0x24(%rbp)
cmpl $0x6, -0x24(%rbp)
jge 0x9895
cmpl $0x5, -0x24(%rbp)
jne 0x97e3
xorl %eax, %eax
movl %eax, -0x28(%rbp)
jmp 0x97fb
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzwl 0x6(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x50c(%rax)
movl -0x24(%rbp), %eax
movzwl %ax, %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x50e(%rax)
movl $0x3c, %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x510(%rax)
movl $0x3, %edi
callq 0x6c50
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x512(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x6(%rax,%rcx,2), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x75a0
cmpl $0x0, %eax
jne 0x9885
movl $0x0, -0x4(%rbp)
jmp 0x9972
jmp 0x9887
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x97cc
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xb2e0
cmpl $0x0, %eax
jne 0x98b1
movl $0x0, -0x4(%rbp)
jmp 0x9972
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xb2e0
cmpl $0x0, %eax
jne 0x98d0
movl $0x0, -0x4(%rbp)
jmp 0x9972
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xb2e0
cmpl $0x0, %eax
jne 0x98ef
movl $0x0, -0x4(%rbp)
jmp 0x9972
movl $0x1, -0x24(%rbp)
cmpl $0x6, -0x24(%rbp)
jge 0x992c
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %eax
addl $0x3, %eax
subl $0x1, %eax
movzwl %ax, %esi
callq 0xb2e0
cmpl $0x0, %eax
jne 0x991f
movl $0x0, -0x4(%rbp)
jmp 0x9972
jmp 0x9921
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x98f6
movl $0x12c0, -0x24(%rbp)    # imm = 0x12C0
cmpl $0x12c0, -0x24(%rbp)    # imm = 0x12C0
jge 0x9966
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %eax
movzwl %ax, %esi
callq 0xb2e0
cmpl $0x0, %eax
jne 0x9959
movl $0x0, -0x4(%rbp)
jmp 0x9972
jmp 0x995b
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x9933
movq -0x10(%rbp), %rdi
callq 0x8280
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)