// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movq %rcx, %r12
movq %rdx, %r14
movl %esi, %ebx
movq %rdi, %r15
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x31f0
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 %ebx, %esi
movl %ebp, %edx
callq 0xa752
testl %eax, %eax
je 0xa6e0
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)
movzbl %bl, %r15d
movzwl 0x16(%rsp), %edx
movq 0x18(%rsp), %rdi
movl %r15d, %esi
movl %edx, 0x34(%rsp)
callq 0xa037
testl %eax, %eax
je 0xa723
movl %r15d, 0x10(%rsp)
movq %r14, 0x50(%rsp)
movl %ebx, 0x30(%rsp)
movq 0x18(%rsp), %rdi
movzwl 0x478(%rdi), %r15d
movl %r15d, %r14d
shrl %r14d
leal 0x7(%r14), %r13d
movzwl 0x14(%rsp), %edx
movl %r13d, %esi
callq 0xa0e9
testl %eax, %eax
jne 0xa570
movq 0x18(%rsp), %rdi
movzwl 0x2a(%rsp), %r9d
testw %r9w, %r9w
je 0xa70f
movl %ebp, 0x2c(%rsp)
movq %r12, 0x48(%rsp)
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 %r14w, %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 0xa39a
movzwl %si, %ebp
movzwl 0x472(%rdi,%rbp,2), %ebx
movl %ebx, %esi
andl $0x500, %esi            # imm = 0x500
je 0xa39a
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 0xa090
testl %eax, %eax
je 0xa6f7
movq 0x18(%rsp), %rdi
leaq 0x474(%rdi), %rax
movzwl 0x28(%rsp), %ecx
movw %cx, 0x474(%rdi)
movw %r12w, 0x47a(%rdi)
movw %bx, 0x472(%rdi,%rbp,2)
shrl %r15d
addq $0x6, %r15
movw $0x5, %cx
movzwl 0x24(%rsp), %r11d
movzwl %r14w, %esi
cmpl $0x1ff, %esi            # imm = 0x1FF
ja 0xa49d
movzwl 0x472(%rdi,%rsi,2), %edx
movzwl %cx, %r9d
movw %dx, 0x472(%rdi,%r9,2)
btl $0xb, %edx
setae %r8b
movl $0x200, %edx            # imm = 0x200
subl %esi, %edx
cmpw %r13w, %dx
setae %dl
andb %r8b, %dl
cmpb $0x1, %dl
jne 0xa499
leaq (%rax,%r9,2), %r10
leaq (%rax,%rsi,2), %rsi
xorl %r8d, %r8d
movzwl (%rsi,%r8,2), %r9d
movw %r9w, (%r10,%r8,2)
incq %r8
cmpq %r8, %r15
jne 0xa481
addl %r13d, %ecx
addl %r13d, %r14d
testb %dl, %dl
jne 0xa437
movzwl %cx, %eax
movw $0x800, 0x472(%rdi,%rax,2) # imm = 0x800
movzwl %r11w, %edx
movl 0x10(%rsp), %esi
callq 0xa090
testl %eax, %eax
je 0xa6f7
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movl $0x1, %edx
callq 0xa037
testl %eax, %eax
je 0xa6f7
movq 0x18(%rsp), %rdi
movzwl 0x24(%rsp), %eax
movw %ax, 0x476(%rdi)
movl 0x10(%rsp), %esi
movl $0x1, %edx
callq 0xa090
testl %eax, %eax
je 0xa6f7
movq $0x0, (%rsp)
movl 0x2c(%rsp), %ebp
movzwl %bp, %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 0xa752
testl %eax, %eax
je 0xa738
movzwl 0x16(%rsp), %edx
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0xa037
testl %eax, %eax
movq 0x48(%rsp), %r12
je 0xa6db
movzwl 0x14(%rsp), %edx
movzwl %r13w, %esi
movq 0x18(%rsp), %rdi
callq 0xa0e9
testl %eax, %eax
je 0xa744
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
movzwl 0x14(%rsp), %r14d
movq 0x18(%rsp), %rdi
movzwl 0x478(%rdi), %eax
shrl %eax
addl %r14d, %eax
addl $0x7, %eax
movw $0x100, 0x472(%rdi,%r14,2) # imm = 0x100
movq 0x50(%rsp), %rdx
movzwl 0x2(%rdx), %ecx
movw %cx, 0x474(%rdi,%r14,2)
movzwl 0x4(%rdx), %ecx
movw %cx, 0x476(%rdi,%r14,2)
movzwl 0x6(%rdx), %ecx
movw %cx, 0x478(%rdi,%r14,2)
movw $0x0, 0x47c(%rdi,%r14,2)
movw %si, 0x47e(%rdi,%r14,2)
testw %bp, %bp
je 0xa619
movw %bp, 0x47a(%rdi,%r14,2)
movzwl %ax, %eax
subw %bp, 0x47a(%rdi,%rax,2)
jmp 0xa637
movzwl %ax, %eax
movzwl 0x47a(%rdi,%rax,2), %ecx
movw %cx, 0x47a(%rdi,%r14,2)
movw $0x0, 0x47a(%rdi,%rax,2)
xorl %r13d, %r13d
movl 0x10(%rsp), %esi
movzwl 0x16(%rsp), %ebp
movzwl %bp, %edx
movq %rdi, %rbx
callq 0xa090
testl %eax, %eax
je 0xa723
movzwl 0x472(%rbx,%r14,2), %eax
movw %ax, (%r12)
movzwl 0x474(%rbx,%r14,2), %eax
movw %ax, 0x2(%r12)
movzwl 0x476(%rbx,%r14,2), %eax
movw %ax, 0x4(%r12)
movzwl 0x478(%rbx,%r14,2), %eax
movw %ax, 0x6(%r12)
movzwl 0x47a(%rbx,%r14,2), %eax
movw %ax, 0x8(%r12)
movzwl 0x47e(%rbx,%r14,2), %eax
movw %ax, 0xa(%r12)
movb %bpl, 0xc(%r12)
movw %r14w, 0xe(%r12)
movq %rbx, 0x18(%r12)
movl 0x30(%rsp), %eax
movb %al, 0x20(%r12)
movzwl 0x26(%rsp), %eax
movw %ax, 0x36(%r12)
movl $0x1, %r13d
jmp 0xa723
xorl %r13d, %r13d
jmp 0xa723
movzbl %bl, %esi
leaq 0x60(%rsp), %r14
movq 0x18(%rsp), %rdi
movq %r14, %rdx
callq 0x4032
jmp 0xa720
leaq 0x44(%rsp), %r14
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movq %r14, %rdx
callq 0x3f68
jmp 0xa720
leaq 0x44(%rsp), %r14
movl 0x10(%rsp), %esi
movq %r14, %rdx
callq 0x3fcd
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 0x3f07
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
callq 0x3ea6