// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11CreateFile
int rt11CreateFile(
  struct mountedFS *mount,
  uint8_t unit,
  struct rt11FileSpec *spec,
  struct rt11OpenFile *file,
  uint16_t size
)
{
  struct RT11data *data = &mount->rt11data;
  struct tm tm;
  time_t now = time(NULL);
  uint16_t year, today;
  uint16_t entrysz, empty, dstart, dseg, doffset, next;

  /*
   * Check if suitable free space is available
   */
  if (rt11BestFit(mount, unit, size, &dstart, &dseg, &doffset, &next) == 0) {
    ERROR("Free space not available on \"%s%o:\"\n", mount->name, unit);
    return 0;
  }

  /*
   * Create an RT-11 timestamp for today
   */
  localtime_r(&now, &tm);

  year = tm.tm_year - 72;
  today = ((year / 32) << 14) | (year % 32);
  today |= (tm.tm_mday << 5) | ((tm.tm_mon + 1) << 10);

  if (rt11ReadDirSegment(mount, unit, dseg) == 0)
    return 0;

  entrysz = RT11_DI_SIZE + (le16toh(data->buf[RT11_DH_EXTRA]) >> 1);

  /*
   * Try to open up a new directory entry in the current directory segment
   */
  if (rt11MakeDirectoryEntry(mount, entrysz, doffset) == 0) {
    /*
     * No space for a new directory entry. Split the current directory
     * segment and try again.
     */
    if (next == 0) {
      ERROR("No directory segment available for split on \"%s%o:\"\n",
            mount->name, unit);
      return 0;
    }

    if (rt11SplitDirSegment(mount, unit, entrysz, dseg, next) == 0) {
      ERROR("Error splitting directory segment on \"%s%o:\"\n",
            mount->name, unit);
      return 0;
    }

    if (rt11BestFit(mount, unit, size, &dstart, &dseg, &doffset, NULL) == 0) {
      ERROR("Panic: second best fit failed on\"%s%o:\"\n", mount->name, unit);
      exit(1);
    }

    if (rt11ReadDirSegment(mount, unit, dseg) == 0)
      return 0;

    if (rt11MakeDirectoryEntry(mount, entrysz, doffset) == 0) {
      ERROR("Panic: second directory make operation failed on \"%s%o:\"\n",
            mount->name, unit);
      exit(2);
    }
  }

  /*
   * Fill in the newly created directory entry
   */
  empty = doffset + RT11_DI_SIZE + (le16toh(data->buf[RT11_DH_EXTRA]) >> 1);

  data->buf[doffset + RT11_DI_STATUS] = RT11_E_TENT;
  data->buf[doffset + RT11_DI_FNAME1] = htole16(spec->name[0]);
  data->buf[doffset + RT11_DI_FNAME2] = htole16(spec->name[1]);
  data->buf[doffset + RT11_DI_FTYPE] = htole16(spec->type);
  data->buf[doffset + RT11_DI_JOB_CHN] = 0;
  data->buf[doffset + RT11_DI_CREATE] = htole16(today);

  if (size == 0) {
    data->buf[doffset + RT11_DI_LENGTH] = data->buf[empty + RT11_DI_LENGTH];
    data->buf[empty + RT11_DI_LENGTH] = 0;
  } else {
    data->buf[doffset + RT11_DI_LENGTH] = htole16(size);
    data->buf[empty + RT11_DI_LENGTH] =
      htole16(le16toh(data->buf[empty + RT11_DI_LENGTH]) - size);
  }

  if (rt11WriteDirSegment(mount, unit, dseg) == 0)
    return 0;

  /*
   * Fill in the open file descriptor.
   */
  file->status = data->buf[doffset + RT11_DI_STATUS];
  file->name[0] = data->buf[doffset + RT11_DI_FNAME1];
  file->name[1] = data->buf[doffset + RT11_DI_FNAME2];
  file->type = data->buf[doffset + RT11_DI_FTYPE];
  file->length = data->buf[doffset + RT11_DI_LENGTH];
  file->creation = data->buf[doffset + RT11_DI_CREATE];

  file->segment = dseg;
  file->offset = doffset;

  file->mount = mount;
  file->unit = unit;

  file->start = dstart;

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r8d, %r14d
movq %rcx, %rbx
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r15
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x3200
movq %rax, 0x58(%rsp)
leaq 0x2a(%rsp), %rax
movq %rax, (%rsp)
leaq 0x26(%rsp), %rcx
leaq 0x16(%rsp), %r8
leaq 0x14(%rsp), %r9
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movl %ebp, %esi
movl %r14d, 0x2c(%rsp)
movl %r14d, %edx
callq 0xa441
testl %eax, %eax
je 0xa3ce
leaq 0x58(%rsp), %rdi
leaq 0x60(%rsp), %r15
movq %r15, %rsi
callq 0x30b0
movl $0xffb8, %eax           # imm = 0xFFB8
addl 0x14(%r15), %eax
movl %eax, 0x38(%rsp)
movl 0xc(%r15), %eax
movl %eax, 0x3c(%rsp)
movl 0x10(%r15), %eax
movl %eax, 0x40(%rsp)
movzwl 0x16(%rsp), %r14d
movzbl %bpl, %r15d
movzwl %r14w, %edx
movq 0x18(%rsp), %rdi
movl %r15d, %esi
movl %edx, 0x34(%rsp)
callq 0x9d20
testl %eax, %eax
je 0xa412
movl %r15d, 0x10(%rsp)
movq %r12, 0x50(%rsp)
movl %ebp, 0x30(%rsp)
movq %rbx, 0x48(%rsp)
movq 0x18(%rsp), %rdi
movzwl 0x478(%rdi), %r15d
movl %r15d, %ebp
shrl %ebp
leal 0x7(%rbp), %r13d
movzwl 0x14(%rsp), %r12d
movzwl %r12w, %edx
movl %r13d, %esi
callq 0x9dd2
testl %eax, %eax
jne 0xa26e
movq 0x18(%rsp), %rdi
movzwl 0x2a(%rsp), %r9d
testw %r9w, %r9w
je 0xa3fe
movw $0x1fb, %ax             # imm = 0x1FB
xorl %edx, %edx
divw %r13w
movzwl %ax, %eax
movzwl 0x474(%rdi), %ecx
movw %cx, 0x28(%rsp)
movzwl 0x47a(%rdi), %r12d
shrl %eax
movzwl %bp, %edx
addl $0xc, %edx
movw $0x5, %r14w
movw $0x1, %cx
movl %edx, %esi
movzwl %r14w, %edx
addw 0x47a(%rdi,%rdx,2), %r12w
addl %r13d, %r14d
leal 0x1(%rcx), %r8d
leal (%rsi,%r13), %edx
cmpw %ax, %cx
movl %r8d, %ecx
jbe 0xa08d
movzwl %si, %ebp
movzwl 0x472(%rdi,%rbp,2), %ebx
movl %ebx, %esi
andl $0x500, %esi            # imm = 0x500
je 0xa08d
movw $0x800, 0x472(%rdi,%rbp,2) # imm = 0x800
movw %r9w, 0x24(%rsp)
movw %r9w, 0x474(%rdi)
movl 0x10(%rsp), %esi
movl 0x34(%rsp), %edx
callq 0x9d79
testl %eax, %eax
je 0xa3e6
movq 0x18(%rsp), %rdi
movzwl 0x28(%rsp), %eax
movw %ax, 0x474(%rdi)
movw %r12w, 0x47a(%rdi)
movw %bx, 0x472(%rdi,%rbp,2)
movzwl %r14w, %eax
movl $0x5, %ecx
cmpl $0x1ff, %eax            # imm = 0x1FF
ja 0xa19a
shrl %r15d
addq $0x6, %r15
leaq 0x474(%rdi), %rax
movw $0x5, %cx
movzwl %cx, %ecx
movl %ecx, %edx
addl %edx, %edx
movzwl %r14w, %r9d
leal (%r9,%r9), %esi
movzwl 0x472(%rdi,%rsi), %r8d
movw %r8w, 0x472(%rdi,%rdx)
btl $0xb, %r8d
jb 0xa19a
movl $0x200, %r8d            # imm = 0x200
subl %r9d, %r8d
cmpl %r13d, %r8d
jb 0xa19a
addq %rax, %rdx
addq %rax, %rsi
xorl %r9d, %r9d
movzwl (%rsi,%r9,2), %r8d
movw %r8w, (%rdx,%r9,2)
incq %r9
cmpq %r9, %r15
jne 0xa173
addl %r13d, %ecx
addl %r13d, %r14d
movzwl %r14w, %edx
cmpl $0x200, %edx            # imm = 0x200
jb 0xa134
movzwl %cx, %ecx
movw $0x800, 0x472(%rdi,%rcx,2) # imm = 0x800
movzwl 0x24(%rsp), %edx
movl 0x10(%rsp), %esi
callq 0x9d79
testl %eax, %eax
je 0xa3e6
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movl $0x1, %edx
callq 0x9d20
testl %eax, %eax
je 0xa3e6
movq 0x18(%rsp), %rdi
movzwl 0x24(%rsp), %eax
movw %ax, 0x476(%rdi)
movl 0x10(%rsp), %esi
movl $0x1, %edx
callq 0x9d79
testl %eax, %eax
je 0xa3e6
movq $0x0, (%rsp)
movzwl 0x2c(%rsp), %edx
leaq 0x26(%rsp), %rcx
leaq 0x16(%rsp), %r8
leaq 0x14(%rsp), %r9
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %ebx
movl %ebx, %esi
callq 0xa441
testl %eax, %eax
je 0xa427
movzwl 0x16(%rsp), %r14d
movzwl %r14w, %edx
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x9d20
testl %eax, %eax
je 0xa3c9
movzwl 0x14(%rsp), %r12d
movzwl %r13w, %esi
movzwl %r12w, %edx
movq 0x18(%rsp), %rdi
callq 0x9dd2
testl %eax, %eax
je 0xa433
movl 0x38(%rsp), %edx
movl %edx, %eax
andl $0x60, %eax
shll $0x9, %eax
andl $0x1f, %edx
movl 0x3c(%rsp), %ecx
shll $0x5, %ecx
orl %edx, %ecx
orl %eax, %ecx
movl 0x40(%rsp), %esi
shll $0xa, %esi
addl $0x400, %esi            # imm = 0x400
orl %ecx, %esi
movq 0x18(%rsp), %rdi
movzwl 0x478(%rdi), %eax
shrl %eax
addl %r12d, %eax
addl $0x7, %eax
movzwl %r12w, %r15d
movw $0x100, 0x472(%rdi,%r15,2) # imm = 0x100
movq 0x50(%rsp), %rdx
movzwl 0x2(%rdx), %ecx
movw %cx, 0x474(%rdi,%r15,2)
movzwl 0x4(%rdx), %ecx
movw %cx, 0x476(%rdi,%r15,2)
movzwl 0x6(%rdx), %ecx
movw %cx, 0x478(%rdi,%r15,2)
movw $0x0, 0x47c(%rdi,%r15,2)
movw %si, 0x47e(%rdi,%r15,2)
movl 0x2c(%rsp), %ecx
testw %cx, %cx
je 0xa319
movw %cx, 0x47a(%rdi,%r15,2)
movzwl %ax, %eax
subw %cx, 0x47a(%rdi,%rax,2)
jmp 0xa337
movzwl %ax, %eax
movzwl 0x47a(%rdi,%rax,2), %ecx
movw %cx, 0x47a(%rdi,%r15,2)
movw $0x0, 0x47a(%rdi,%rax,2)
xorl %r13d, %r13d
movzwl %r14w, %edx
movl 0x10(%rsp), %esi
movq %rdi, %rbx
callq 0x9d79
testl %eax, %eax
je 0xa412
movzwl 0x472(%rbx,%r15,2), %eax
movq 0x48(%rsp), %rcx
movw %ax, (%rcx)
movzwl 0x474(%rbx,%r15,2), %eax
movw %ax, 0x2(%rcx)
movzwl 0x476(%rbx,%r15,2), %eax
movw %ax, 0x4(%rcx)
movzwl 0x478(%rbx,%r15,2), %eax
movw %ax, 0x6(%rcx)
movzwl 0x47a(%rbx,%r15,2), %eax
movw %ax, 0x8(%rcx)
movzwl 0x47e(%rbx,%r15,2), %eax
movw %ax, 0xa(%rcx)
movb %r14b, 0xc(%rcx)
movw %r12w, 0xe(%rcx)
movq %rbx, 0x18(%rcx)
movl 0x30(%rsp), %eax
movb %al, 0x20(%rcx)
movzwl 0x26(%rsp), %eax
movw %ax, 0x36(%rcx)
movl $0x1, %r13d
jmp 0xa412
xorl %r13d, %r13d
jmp 0xa412
movzbl %bpl, %esi
leaq 0x60(%rsp), %r14
movq 0x18(%rsp), %rdi
movq %r14, %rdx
callq 0x40e2
jmp 0xa40f
leaq 0x44(%rsp), %r14
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movq %r14, %rdx
callq 0x4018
jmp 0xa40f
leaq 0x44(%rsp), %r14
movl 0x10(%rsp), %esi
movq %r14, %rdx
callq 0x407d
movl (%r14), %r13d
movl %r13d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x3fb7
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
callq 0x3f56