// File: /workspace/github_c_makefile_star2_O1/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 0xda24(%rip), %rax      # 0x18fc8
movl (%rax), %ebx
btl $0x13, %ebx
jae 0xb600
leaq 0xda1d(%rip), %rax      # 0x18fd0
movq 0x98(%rax), %r15
leaq 0x6bd7(%rip), %rdi      # 0x12198
movq %r15, %rsi
callq 0x31c0
testl %eax, %eax
jne 0xb5dd
movq $0xd00, 0x40(%r14)      # imm = 0xD00
movl $0x80, 0x4c(%r14)
leaq 0x6bb9(%rip), %rdi      # 0x1219d
movq %r15, %rsi
callq 0x31c0
testl %eax, %eax
jne 0xb600
movq $0x1a00, 0x40(%r14)     # imm = 0x1A00
movl $0x100, 0x4c(%r14)      # imm = 0x100
testb $0x10, %bl
jne 0xb60a
xorl %r15d, %r15d
jmp 0xb647
leaq 0xd9bf(%rip), %rax      # 0x18fd0
movq 0x20(%rax), %rdi
leaq 0x10(%rsp), %rsi
movl $0xa, %edx
callq 0x3290
movq %rax, %r15
incl %r15d
cmpw $0x3f, %r15w
ja 0xb7e3
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax)
jne 0xb7e3
andl $0x3e, %r15d
subl 0x40(%r14), %r12d
shrl $0x9, %r12d
xorps %xmm0, %xmm0
movups %xmm0, 0x52(%r14)
movups %xmm0, 0x62(%r14)
movw $0x1, %ax
movw %ax, 0x52(%r14)
movq %r12, (%rsp)
movw %r12w, 0x72(%r14)
movw $0x6, 0x272(%r14)
movw %ax, 0x50(%r14)
leaq 0x472(%r14), %r12
xorl %ebx, %ebx
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
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
addw (%r14,%rax,2), %bx
incq %rax
cmpq $0x338, %rax            # imm = 0x338
jne 0xb6fc
movw %bx, 0x670(%r14)
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x9e88
testl %eax, %eax
je 0xb7d1
leaq 0x47c(%r14), %rbp
movq (%rsp), %rax
addl $-0x44, %eax
movq %rax, (%rsp)
leaq 0x48a(%r14), %rax
movq %rax, 0x8(%rsp)
movl $0xffffffe1, %ebx       # imm = 0xFFFFFFE1
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x3170
movw $0x1f, 0x472(%r14)
movw $0x1, 0x476(%r14)
movw %r15w, 0x478(%r14)
movw $0x44, 0x47a(%r14)
movq %rbp, %rax
cmpl $-0x1f, %ebx
jne 0xb7ab
movw $0x200, 0x47c(%r14)     # imm = 0x200
movq (%rsp), %rax
movw %ax, 0x484(%r14)
movq 0x8(%rsp), %rax
movw $0x800, (%rax)          # imm = 0x800
leal 0x20(%rbx), %eax
movzwl %ax, %edx
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
callq 0xa090
testl %eax, %eax
je 0xb7d1
incl %ebx
jne 0xb753
movl $0x1, %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4222
jmp 0xb605