// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rdx, %r14
movl %esi, %r15d
movq %rdi, %r13
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x3200
movq %rax, 0x58(%rsp)
leaq 0x2e(%rsp), %rax
movq %rax, (%rsp)
leaq 0x2a(%rsp), %rcx
leaq 0x16(%rsp), %r8
leaq 0x14(%rsp), %r9
movq %r13, 0x18(%rsp)
movq %r13, %rdi
movl %r15d, %esi
movl %ebp, 0x30(%rsp)
movl %ebp, %edx
callq 0x9211
movzbl %r15b, %ebp
testl %eax, %eax
je 0x909c
movq %r14, 0x50(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x60(%rsp), %r14
movq %r14, %rsi
callq 0x30b0
movl $0xffb8, %eax           # imm = 0xFFB8
addl 0x14(%r14), %eax
movl %eax, 0x3c(%rsp)
movl 0xc(%r14), %eax
movl %eax, 0x40(%rsp)
movl 0x10(%r14), %eax
movl %eax, 0x44(%rsp)
movzwl 0x16(%rsp), %r13d
movzwl %r13w, %r14d
movq 0x18(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x89c6
testl %eax, %eax
je 0x9087
movl %r14d, 0x34(%rsp)
movl %r15d, 0x38(%rsp)
movq %rbx, 0x48(%rsp)
movq 0x18(%rsp), %rdi
movzwl 0x478(%rdi), %r12d
movl %r12d, %r14d
shrl %r14d
addl $0x7, %r14d
movzwl 0x14(%rsp), %r15d
movzwl %r15w, %edx
movl %r14d, %esi
callq 0x8a74
testl %eax, %eax
je 0x8d83
movl 0x3c(%rsp), %edx
movl %edx, %eax
andl $0x60, %eax
shll $0x9, %eax
andl $0x1f, %edx
movl 0x40(%rsp), %ecx
shll $0x5, %ecx
orl %edx, %ecx
orl %eax, %ecx
movl 0x44(%rsp), %esi
shll $0xa, %esi
addl $0x400, %esi            # imm = 0x400
orl %ecx, %esi
movq 0x18(%rsp), %rdi
movzwl 0x478(%rdi), %eax
movzwl %r15w, %r14d
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)
andw $0x0, 0x47c(%rdi,%r14,2)
shrl %eax
addl %r15d, %eax
addl $0x7, %eax
movw %si, 0x47e(%rdi,%r14,2)
movl 0x30(%rsp), %ecx
testw %cx, %cx
je 0x8fe2
movw %cx, 0x47a(%rdi,%r14,2)
movzwl %ax, %eax
subw %cx, 0x47a(%rdi,%rax,2)
jmp 0x8fff
movq 0x18(%rsp), %rsi
movzwl 0x2e(%rsp), %r9d
testw %r9w, %r9w
je 0x9150
movl %ebp, %r8d
xorl %ecx, %ecx
movw $0x1fb, %ax             # imm = 0x1FB
xorl %edx, %edx
divw %r14w
movzwl %ax, %eax
movq %rsi, %rdi
movzwl 0x474(%rsi), %edx
movw %dx, 0x2c(%rsp)
movzwl 0x47a(%rsi), %r15d
shrl %eax
movw $0x5, %r13w
movzwl %r13w, %ebp
cmpw %ax, %cx
jbe 0x8de3
movzwl 0x472(%rdi,%rbp,2), %ebx
movl %ebx, %edx
andl $0x500, %edx            # imm = 0x500
jne 0x8df3
addw 0x47a(%rdi,%rbp,2), %r15w
incl %ecx
addl %r14d, %r13d
jmp 0x8dc8
movw $0x800, 0x472(%rdi,%rbp,2) # imm = 0x800
movw %r9w, 0x28(%rsp)
movw %r9w, 0x474(%rdi)
movl %r8d, %esi
movl %r8d, 0x24(%rsp)
movl 0x34(%rsp), %edx
callq 0x8a1d
testl %eax, %eax
je 0x90f4
movq 0x18(%rsp), %rdi
leaq 0x474(%rdi), %rax
movzwl 0x2c(%rsp), %ecx
movw %cx, 0x474(%rdi)
movw %r15w, 0x47a(%rdi)
movw %bx, 0x472(%rdi,%rbp,2)
shrl %r12d
addq $0x6, %r12
movw $0x5, %cx
movl 0x24(%rsp), %esi
movzwl 0x28(%rsp), %r10d
movzwl %cx, %edx
cmpw $0x200, %r13w           # imm = 0x200
jae 0x8ebf
movzwl %r13w, %r9d
movzwl 0x472(%rdi,%r9,2), %r8d
movw %r8w, 0x472(%rdi,%rdx,2)
btl $0xb, %r8d
jb 0x8ebf
movl $0x200, %r8d            # imm = 0x200
subl %r13d, %r8d
cmpw %r14w, %r8w
jb 0x8ebf
leaq (%rax,%rdx,2), %rdx
leaq (%rax,%r9,2), %r11
xorl %r9d, %r9d
cmpq %r9, %r12
je 0x8eb7
movzwl (%r11,%r9,2), %r8d
movw %r8w, (%rdx,%r9,2)
incq %r9
jmp 0x8ea3
addl %r14d, %ecx
addl %r14d, %r13d
jmp 0x8e61
movw $0x800, 0x472(%rdi,%rdx,2) # imm = 0x800
movzwl %r10w, %edx
callq 0x8a1d
testl %eax, %eax
je 0x90f4
pushq $0x1
popq %rdx
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %esi
callq 0x89c6
testl %eax, %eax
je 0x90f4
movq 0x18(%rsp), %rdi
movzwl 0x28(%rsp), %eax
movw %ax, 0x476(%rdi)
pushq $0x1
popq %rdx
movl 0x24(%rsp), %esi
callq 0x8a1d
testl %eax, %eax
je 0x90f4
andq $0x0, (%rsp)
movzwl 0x30(%rsp), %edx
leaq 0x2a(%rsp), %rcx
leaq 0x16(%rsp), %r8
leaq 0x14(%rsp), %r9
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %ebp
movl %ebp, %esi
callq 0x9211
testl %eax, %eax
je 0x91b5
movzwl 0x16(%rsp), %r13d
movzwl %r13w, %edx
movq 0x18(%rsp), %rdi
movl %ebp, %esi
callq 0x89c6
testl %eax, %eax
je 0x90ef
movzwl 0x14(%rsp), %r15d
movzwl %r14w, %esi
movzwl %r15w, %edx
movq 0x18(%rsp), %rdi
callq 0x8a74
testl %eax, %eax
jne 0x8cd2
movq 0xe06a(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movq 0x18(%rsp), %rdx
addq $0x8, %rdx
leaq 0x818d(%rip), %rsi      # 0x1112e
movq %rdx, %rbx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xf014(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8fdb
movq 0xdffd(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x8fdb
leaq 0x815f(%rip), %rsi      # 0x1112e
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
pushq $0x2
jmp 0x920b
movzwl %ax, %eax
movzwl 0x47a(%rdi,%rax,2), %ecx
movw %cx, 0x47a(%rdi,%r14,2)
andw $0x0, 0x47a(%rdi,%rax,2)
xorl %r12d, %r12d
movzwl %r13w, %edx
movl %ebp, %esi
movq %rdi, %rbx
callq 0x8a1d
testl %eax, %eax
je 0x9087
movzwl 0x472(%rbx,%r14,2), %eax
movq 0x48(%rsp), %rcx
movw %ax, (%rcx)
movzwl 0x474(%rbx,%r14,2), %eax
movw %ax, 0x2(%rcx)
movzwl 0x476(%rbx,%r14,2), %eax
movw %ax, 0x4(%rcx)
movzwl 0x478(%rbx,%r14,2), %eax
movw %ax, 0x6(%rcx)
movzwl 0x47a(%rbx,%r14,2), %eax
movw %ax, 0x8(%rcx)
movzwl 0x47e(%rbx,%r14,2), %eax
movw %ax, 0xa(%rcx)
movb %r13b, 0xc(%rcx)
movw %r15w, 0xe(%rcx)
movq %rbx, 0x18(%rcx)
movl 0x38(%rsp), %eax
movb %al, 0x20(%rcx)
movzwl 0x2a(%rsp), %eax
movw %ax, 0x36(%rcx)
pushq $0x1
popq %r12
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xdf55(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movq 0x18(%rsp), %rdx
addq $0x8, %rdx
leaq 0x7fc7(%rip), %rsi      # 0x1107d
xorl %r12d, %r12d
movq %rdx, %rbx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xeefc(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x9087
movq 0xdee5(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x9087
movq %rbx, %rdx
leaq 0x7f93(%rip), %rsi      # 0x1107d
jmp 0x91a4
xorl %r12d, %r12d
jmp 0x9087
movq 0xdefd(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movq 0x18(%rsp), %rdx
addq $0x8, %rdx
leaq 0x7fc9(%rip), %rsi      # 0x110d7
xorl %r12d, %r12d
movq %rdx, %rbx
movl 0x24(%rsp), %ebp
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xeea0(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x9087
movq 0xde85(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x9087
movq %rbx, %rdx
leaq 0x7f89(%rip), %rsi      # 0x110d7
jmp 0x91a4
movq 0xdea1(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x7f3a(%rip), %rsi      # 0x110a2
xorl %r12d, %r12d
movq %rdx, %rbx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xee4a(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x9087
movq 0xde2f(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x9087
movq %rbx, %rdx
leaq 0x7efe(%rip), %rsi      # 0x110a2
xorl %r12d, %r12d
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
jmp 0x9087
movq 0xde3c(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movq 0x18(%rsp), %rdx
addq $0x8, %rdx
leaq 0x7f36(%rip), %rsi      # 0x11105
movq %rdx, %rbx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xede6(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x9209
movq 0xddcf(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x9209
leaq 0x7f08(%rip), %rsi      # 0x11105
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
pushq $0x1
popq %rdi
callq 0x32c0