// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11Newfs
static int rt11Newfs(
  struct mountedFS *mount,
  size_t size
)
{
  struct RT11data *data = &mount->rt11data;
  int i;
  uint16_t checksum = 0, extra = 0;;

  /*
   * Check for device type override.
   */
  if (SWISSET('t')) {
    if (strcmp("rx01", SWGETVAL('t')) == 0) {
      mount->skip = RT11_RX0xNSECT * RT11_RX01SS;
      data->sectorsz = RT11_RX01SS;
    }
    if (strcmp("rx02", SWGETVAL('t')) == 0) {
      mount->skip = RT11_RX0xNSECT * RT11_RX02SS;
      data->sectorsz = RT11_RX02SS;
    }
  }

  /*
   * Check for extra bytes on each directory entry, rounded up to nearest
   * even value.
   */
  if (SWISSET('e')) {
    char *endptr;

    extra = strtoul(SWGETVAL('e'), &endptr, 10);
    extra = (extra + 1) & ~1;

    if ((extra > 63) || (*endptr != '\0')) {
      fprintf(stderr, "newfs: bad -e switch value \"%s\" - ignored\n",
              SWGETVAL('e'));
      extra = 0;
    }
  }

  /*
   * Remove possible first track
   */
  size = (size - mount->skip) / RT11_BLOCKSIZE;
  //  size = ((size * RT11_BLOCKSIZE) - mount->skip) / RT11_BLOCKSIZE;

  /*
   * Mark partition 0 as valid
   */
  memset(data->valid, 0, sizeof(data->valid));
  data->valid[0] = 1;
  data->maxblk[0] = size;
  data->first[0] = RT11_DSSTART;
  data->filesystems = 1;

  /*
   * Build and write the Home Block.
   */
  memset(&data->buf[0], 0, RT11_BLOCKSIZE);

  data->buf[RT11_HB_PCS] = htole16(1);
  data->buf[RT11_HB_FIRST] = htole16(RT11_DSSTART);
  data->buf[RT11_HB_SYSVER] = htole16(RT11_SYSVER_V05);

  memcpy((char *)&data->buf[RT11_HB_VOLID], RT11_VOLID, strlen(RT11_VOLID));
  memcpy((char *)&data->buf[RT11_HB_OWNER], RT11_OWNER, strlen(RT11_OWNER));
  memcpy((char *)&data->buf[RT11_HB_SYSID], RT11_SYSID, strlen(RT11_SYSID));

  for (i = 0; i < 255; i++)
    checksum += le16toh(data->buf[i]);

  data->buf[RT11_HB_CHKSUM] = htole16(checksum);

  if (rt11WriteBlock(mount, 0, RT11_HOME, NULL) == 0)
    return 0;

  /*
   * Build the maximum # of directory segments
   */
  for (i = 1; i <= RT11_DS_MAX; i++) {
    memset(data->buf, 0, sizeof(data->buf));

    data->buf[RT11_DH_COUNT] = htole16(RT11_DS_MAX);
    data->buf[RT11_DH_HIGHEST] = htole16(1);
    data->buf[RT11_DH_EXTRA] = htole16(extra);
    data->buf[RT11_DH_START] = htole16(RT11_DSSTART + (2 * RT11_DS_MAX));

    if (i == 1) {
      data->buf[RT11_DH_SIZE + RT11_DI_STATUS] = htole16(RT11_E_MPTY);
      data->buf[RT11_DH_SIZE + RT11_DI_LENGTH] =
        htole16(size - (RT11_DSSTART + (2 * RT11_DS_MAX)));
      data->buf[RT11_DH_SIZE + RT11_DI_SIZE + RT11_DI_STATUS] =
        htole16(RT11_E_EOS);
    } else data->buf[RT11_DH_SIZE + RT11_DI_STATUS] = htole16(RT11_E_EOS);

    if (rt11WriteDirSegment(mount, 0, i) == 0)
      return 0;
  }
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %r14
leaq 0xe012(%rip), %rax      # 0x17fd8
movl (%rax), %ebx
btl $0x13, %ebx
jae 0xa022
leaq 0xe00b(%rip), %rax      # 0x17fe0
movq 0x98(%rax), %r15
leaq 0x71b5(%rip), %rdi      # 0x11198
movq %r15, %rsi
callq 0x31d0
testl %eax, %eax
jne 0x9fff
movq $0xd00, 0x40(%r14)      # imm = 0xD00
movl $0x80, 0x4c(%r14)
leaq 0x7197(%rip), %rdi      # 0x1119d
movq %r15, %rsi
callq 0x31d0
testl %eax, %eax
jne 0xa022
movq $0x1a00, 0x40(%r14)     # imm = 0x1A00
movl $0x100, 0x4c(%r14)      # imm = 0x100
testb $0x10, %bl
jne 0xa031
movq $0x0, (%rsp)
jmp 0xa072
leaq 0xdfa8(%rip), %rbx      # 0x17fe0
movq 0x20(%rbx), %rdi
leaq 0x10(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x32a0
incl %eax
movq %rax, (%rsp)
cmpw $0x3f, %ax
ja 0xa21c
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax)
jne 0xa21c
movq (%rsp), %rax
andl $0x3e, %eax
movq %rax, (%rsp)
subl 0x40(%r14), %r12d
shrl $0x9, %r12d
xorps %xmm0, %xmm0
movups %xmm0, 0x54(%r14)
movups %xmm0, 0x62(%r14)
movq %r12, 0x8(%rsp)
movw %r12w, 0x72(%r14)
movw $0x6, 0x272(%r14)
movl $0x10001, 0x50(%r14)    # imm = 0x10001
leaq 0x472(%r14), %rdi
xorl %ebx, %ebx
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3170
movl $0x60001, 0x644(%r14) # imm = 0x60001
movw $0x8e53, 0x648(%r14)    # imm = 0x8E53
movabsq $0x2020204131315452, %rax # imm = 0x2020204131315452
movq %rax, 0x64a(%r14)
movl $0x20202020, %eax       # imm = 0x20202020
movl %eax, 0x652(%r14)
movabsq $0x2020202020202020, %rcx # imm = 0x2020202020202020
movq %rcx, 0x656(%r14)
movl %eax, 0x65e(%r14)
movabsq $0x4131315452434544, %rcx # imm = 0x4131315452434544
movq %rcx, 0x662(%r14)
movl %eax, 0x66a(%r14)
movl $0x239, %eax            # imm = 0x239
cmpq $0x338, %rax            # imm = 0x338
je 0xa131
addw (%r14,%rax,2), %bx
incq %rax
jmp 0xa11f
movw %bx, 0x670(%r14)
xorl %r13d, %r13d
pushq $0x1
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x87b2
testl %eax, %eax
je 0xa20a
leaq 0x47c(%r14), %rbp
movq 0x8(%rsp), %rax
addl $-0x44, %eax
movq %rax, 0x8(%rsp)
leaq 0x48a(%r14), %rbx
leaq 0x474(%r14), %r12
pushq $-0x1f
popq %r15
testl %r15d, %r15d
je 0xa204
movl $0x3fe, %edx            # imm = 0x3FE
movq %r12, %rdi
xorl %esi, %esi
callq 0x3170
movw $0x1f, 0x472(%r14)
movw $0x1, 0x476(%r14)
movq (%rsp), %rax
movw %ax, 0x478(%r14)
movw $0x44, 0x47a(%r14)
movq %rbp, %rax
cmpl $-0x1f, %r15d
jne 0xa1de
movw $0x200, 0x47c(%r14)     # imm = 0x200
movq 0x8(%rsp), %rax
movw %ax, 0x484(%r14)
movq %rbx, %rax
movw $0x800, (%rax)          # imm = 0x800
leal 0x20(%r15), %eax
movzwl %ax, %edx
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
callq 0x8a1d
incl %r15d
testl %eax, %eax
jne 0xa179
jmp 0xa20a
pushq $0x1
popq %rax
movl %eax, %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xcdd5(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movq 0x20(%rbx), %rdx
leaq 0x70e3(%rip), %rsi      # 0x11314
movq $0x0, (%rsp)
xorl %eax, %eax
callq 0x31e0
jmp 0xa072