// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movw %r8w, %ax
movq %rdi, -0x10(%rbp)
movb %sil, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movw %ax, -0x2a(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x38(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x31e0
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %dl
leaq -0x82(%rbp), %rcx
leaq -0x84(%rbp), %r8
leaq -0x86(%rbp), %r9
leaq -0x88(%rbp), %rax
movzbl %dl, %esi
movzwl -0x2a(%rbp), %edx
movq %rax, (%rsp)
callq 0xcf80
cmpl $0x0, %eax
jne 0xc91b
movq 0x1174d(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd80a(%rip), %rsi      # 0x1a0cb
movb $0x0, %al
callq 0x31c0
leaq 0x12919(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xc90f
leaq 0x1290c(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x116da(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xc90f
leaq 0x128f6(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd7c3(%rip), %rsi      # 0x1a0cb
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0xcf67
leaq -0x78(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x30a0
movl -0x5c(%rbp), %eax
subl $0x48, %eax
movw %ax, -0x7a(%rbp)
movzwl -0x7a(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
shll $0xe, %eax
movl %eax, -0x8c(%rbp)
movzwl -0x7a(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl -0x8c(%rbp), %eax
orl %edx, %eax
movw %ax, -0x7c(%rbp)
movl -0x64(%rbp), %ecx
shll $0x5, %ecx
movl -0x60(%rbp), %eax
addl $0x1, %eax
shll $0xa, %eax
orl %eax, %ecx
movzwl -0x7c(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x7c(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x84(%rbp), %edx
callq 0xc3e0
cmpl $0x0, %eax
jne 0xc9a1
movl $0x0, -0x4(%rbp)
jmp 0xcf67
movq -0x38(%rbp), %rax
movzwl 0x430(%rax), %edi
callq 0xc6b0
movzwl %ax, %eax
sarl %eax
addl $0x7, %eax
movw %ax, -0x7e(%rbp)
movq -0x10(%rbp), %rdi
movw -0x7e(%rbp), %ax
movzwl %ax, %esi
movzwl -0x86(%rbp), %edx
callq 0xc540
cmpl $0x0, %eax
jne 0xcc6a
movzwl -0x88(%rbp), %eax
cmpl $0x0, %eax
jne 0xca60
movq 0x11608(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd6ea(%rip), %rsi      # 0x1a0f0
movb $0x0, %al
callq 0x31c0
leaq 0x127d4(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xca54
leaq 0x127c7(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x11595(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xca54
leaq 0x127b1(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd6a3(%rip), %rsi      # 0x1a0f0
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0xcf67
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %dl
movw -0x7e(%rbp), %cx
movw -0x84(%rbp), %ax
movzbl %dl, %esi
movzwl %cx, %edx
movzwl %ax, %ecx
movzwl -0x88(%rbp), %r8d
callq 0xd210
cmpl $0x0, %eax
jne 0xcb04
movq 0x11564(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd67b(%rip), %rsi      # 0x1a125
movb $0x0, %al
callq 0x31c0
leaq 0x12730(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xcaf8
leaq 0x12723(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x114f1(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xcaf8
leaq 0x1270d(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd634(%rip), %rsi      # 0x1a125
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0xcf67
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
leaq -0x82(%rbp), %rcx
leaq -0x84(%rbp), %r8
leaq -0x86(%rbp), %r9
xorl %edx, %edx
movzbl %al, %esi
movzwl -0x2a(%rbp), %edx
movq $0x0, (%rsp)
callq 0xcf80
cmpl $0x0, %eax
jne 0xcbb0
movq 0x114b6(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd5fb(%rip), %rsi      # 0x1a153
movb $0x0, %al
callq 0x31c0
leaq 0x12682(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xcba6
leaq 0x12675(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x11443(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xcba6
leaq 0x1265f(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd5b4(%rip), %rsi      # 0x1a153
movb $0x0, %al
callq 0x31c0
movl $0x1, %edi
callq 0x32a0
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x84(%rbp), %edx
callq 0xc3e0
cmpl $0x0, %eax
jne 0xcbd7
movl $0x0, -0x4(%rbp)
jmp 0xcf67
movq -0x10(%rbp), %rdi
movw -0x7e(%rbp), %ax
movzwl %ax, %esi
movzwl -0x86(%rbp), %edx
callq 0xc540
cmpl $0x0, %eax
jne 0xcc68
movq 0x113fe(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd56c(%rip), %rsi      # 0x1a17c
movb $0x0, %al
callq 0x31c0
leaq 0x125ca(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xcc5e
leaq 0x125bd(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x1138b(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xcc5e
leaq 0x125a7(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xd525(%rip), %rsi      # 0x1a17c
movb $0x0, %al
callq 0x31c0
movl $0x2, %edi
callq 0x32a0
jmp 0xcc6a
movzwl -0x86(%rbp), %eax
addl $0x7, %eax
movl %eax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x430(%rax), %edi
callq 0xc6b0
movw %ax, %cx
movl -0x90(%rbp), %eax
movzwl %cx, %ecx
sarl %ecx
addl %ecx, %eax
movw %ax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw $0x100, 0x42a(%rax,%rcx,2) # imm = 0x100
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movw $0x0, 0x42a(%rax,%rcx,2)
movzwl -0x7c(%rbp), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x6, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movzwl -0x2a(%rbp), %eax
cmpl $0x0, %eax
jne 0xcdc6
movq -0x38(%rbp), %rax
movzwl -0x80(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %dx
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x38(%rbp), %rax
movzwl -0x80(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw $0x0, 0x42a(%rax,%rcx,2)
jmp 0xce30
movzwl -0x2a(%rbp), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x38(%rbp), %rax
movzwl -0x80(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
movzwl -0x2a(%rbp), %ecx
subl %ecx, %eax
movzwl %ax, %edi
callq 0xc6b0
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x80(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x84(%rbp), %edx
callq 0xc490
cmpl $0x0, %eax
jne 0xce57
movl $0x0, -0x4(%rbp)
jmp 0xcf67
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x6(%rax)
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x38(%rbp), %rax
movzwl -0x86(%rbp), %ecx
addl $0x6, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0xa(%rax)
movw -0x84(%rbp), %ax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0xc(%rax)
movw -0x86(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0xe(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movb -0x11(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x20(%rax)
movw -0x82(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x36(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)