// File: /workspace/github_c_makefile_star2_O0/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
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)
movw $0x0, -0x26(%rbp)
movw $0x0, -0x28(%rbp)
leaq 0x107d9(%rip), %rax     # 0x1f1f8
movl (%rax), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0xea99
leaq 0x107ce(%rip), %rax     # 0x1f200
movq 0x98(%rax), %rsi
leaq 0xb7a6(%rip), %rdi      # 0x1a1e6
callq 0x31b0
cmpl $0x0, %eax
jne 0xea61
movq -0x10(%rbp), %rax
movq $0xd00, 0x40(%rax)      # imm = 0xD00
movq -0x20(%rbp), %rax
movl $0x80, 0x4(%rax)
leaq 0x10798(%rip), %rax     # 0x1f200
movq 0x98(%rax), %rsi
leaq 0xb775(%rip), %rdi      # 0x1a1eb
callq 0x31b0
cmpl $0x0, %eax
jne 0xea97
movq -0x10(%rbp), %rax
movq $0x1a00, 0x40(%rax)     # imm = 0x1A00
movq -0x20(%rbp), %rax
movl $0x100, 0x4(%rax)       # imm = 0x100
jmp 0xea99
leaq 0x10758(%rip), %rax     # 0x1f1f8
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0xeb15
leaq 0x1074f(%rip), %rax     # 0x1f200
movq 0x20(%rax), %rdi
leaq -0x30(%rbp), %rsi
movl $0xa, %edx
callq 0x3280
movw %ax, -0x28(%rbp)
movzwl -0x28(%rbp), %eax
addl $0x1, %eax
andl $-0x2, %eax
movw %ax, -0x28(%rbp)
movzwl -0x28(%rbp), %eax
cmpl $0x3f, %eax
jg 0xeaea
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0xeb13
movq 0xf507(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
leaq 0x10705(%rip), %rax     # 0x1f200
movq 0x20(%rax), %rdx
leaq 0xb85c(%rip), %rsi      # 0x1a362
movb $0x0, %al
callq 0x31c0
movw $0x0, -0x28(%rbp)
jmp 0xeb15
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
subq 0x40(%rcx), %rax
shrq $0x9, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rdi
addq $0xa, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3170
movq -0x20(%rbp), %rax
movw $0x1, 0xa(%rax)
movq -0x18(%rbp), %rax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x2a(%rax)
movq -0x20(%rbp), %rax
movw $0x6, 0x22a(%rax)
movq -0x20(%rbp), %rax
movw $0x1, 0x8(%rax)
movq -0x20(%rbp), %rdi
addq $0x42a, %rdi            # imm = 0x42A
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3170
movl $0x1, %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x5fc(%rax)
movl $0x6, %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x5fe(%rax)
movl $0x8e53, %edi           # imm = 0x8E53
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x600(%rax)
movq -0x20(%rbp), %rax
movq 0xb7b6(%rip), %rcx      # 0x1a38d
movq %rcx, 0x602(%rax)
movl 0xb7b1(%rip), %ecx      # 0x1a395
movl %ecx, 0x60a(%rax)
movq -0x20(%rbp), %rax
movq 0xb7a5(%rip), %rcx      # 0x1a39a
movq %rcx, 0x60e(%rax)
movl 0xb7a0(%rip), %ecx      # 0x1a3a2
movl %ecx, 0x616(%rax)
movq -0x20(%rbp), %rax
movq 0xb703(%rip), %rcx      # 0x1a316
movq %rcx, 0x61a(%rax)
movl 0xb6fe(%rip), %ecx      # 0x1a31e
movl %ecx, 0x622(%rax)
movl $0x0, -0x24(%rbp)
cmpl $0xff, -0x24(%rbp)
jge 0xec63
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
movzwl -0x26(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x26(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xec2d
movzwl -0x26(%rbp), %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x628(%rax)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0xc0d0
cmpl $0x0, %eax
jne 0xec9f
movl $0x0, -0x4(%rbp)
jmp 0xedca
movl $0x1, -0x24(%rbp)
cmpl $0x1f, -0x24(%rbp)
jg 0xedc3
movq -0x20(%rbp), %rdi
addq $0x42a, %rdi            # imm = 0x42A
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x3170
movl $0x1f, %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x42a(%rax)
movl $0x1, %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x42e(%rax)
movzwl -0x28(%rbp), %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x430(%rax)
movl $0x44, %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x432(%rax)
cmpl $0x1, -0x24(%rbp)
jne 0xed7c
movl $0x200, %edi            # imm = 0x200
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x434(%rax)
movq -0x18(%rbp), %rax
subq $0x44, %rax
movzwl %ax, %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x43c(%rax)
movl $0x800, %edi            # imm = 0x800
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x442(%rax)
jmp 0xed94
movl $0x800, %edi            # imm = 0x800
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x434(%rax)
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %eax
xorl %esi, %esi
movzwl %ax, %edx
callq 0xc490
cmpl $0x0, %eax
jne 0xedb3
movl $0x0, -0x4(%rbp)
jmp 0xedca
jmp 0xedb5
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xeca6
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)