// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/fsio/os8.c
// Function: os8CreateFile
int os8CreateFile(
  struct mountedFS *mount,
  uint8_t unit,
  struct os8FileSpec *spec,
  struct os8OpenFile *file,
  uint16_t size
)
{
  struct OS8data *data = &mount->os8data;
  uint16_t segment, off, start, entries, extra, entrysz, space, left;

 retry:
  /*
   * Check if suitable free space is available
   */
  if (os8FindSpace(mount, unit, size, &segment, &off, &start) == 0) {
    ERROR("Free space not available on \"%s%o:\"\n", mount->name, unit);
    return 0;
  }

  /*
   * Check if there is space to create a new directory entry in this
   * directory segment. If not, split the directory segment in 2 and retry
   * from the beginning.
   */
  if (!os8CheckDirectory(mount)) {
    if (!os8SplitDirectory(mount, unit, segment)) {
      ERROR("No directory space on \"%s%o:\"\n", mount->name, unit);
      return 0;
    }
    goto retry;
  }

  entries = os8Neg(le16toh(data->buf[OS8_DH_ENTRIES]));
  extra = -os8SXT(le16toh(data->buf[OS8_DH_EXTRA]));
  entrysz = OS8_DI_SIZE + extra;

  space = size == 0 ? os8Neg(le16toh(data->buf[off + OS8_ED_LENGTH])) : size;
  left = os8Neg(le16toh(data->buf[off + OS8_ED_LENGTH])) - space;

  /*
   * Remove the requested space from the empty directory entry.
   */
  data->buf[off + OS8_ED_LENGTH] = htole16(os8Value(-left));

  os8SlideDown(mount, off, entries, entrysz, &file->remain);

  /*
   * Build the directory entry
   */
  data->buf[off + OS8_DI_FNAME1] = htole16(os8Value(spec->name[0]));
  data->buf[off + OS8_DI_FNAME2] = htole16(os8Value(spec->name[1]));
  data->buf[off + OS8_DI_FNAME3] = htole16(os8Value(spec->name[2]));
  data->buf[off + OS8_DI_EXT] = htole16(os8Value(spec->ext));

  if (extra != 0)
    data->buf[off + OS8_DI_DATE] = htole16(os8Value(data->date));

  data->buf[off + extra + OS8_DI_LENGTH] = htole16(os8Value(-space));

  if (os8WriteBlock(mount, unit, segment, NULL) == 0)
    return 0;

  /*
   * Fill in the open file descriptor
   */
  file->name[0] = spec->name[0];
  file->name[1] = spec->name[1];
  file->name[2] = spec->name[2];
  file->ext = spec->ext;
  file->creation = data->date;
  file->length = space;

  file->segment = segment;
  file->offset = off;
  file->entrysz = entrysz;
  file->extra = extra;

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

  file->current = 0;
  file->start = start;
  file->last = start + space - 1;

  file->wordpos = OS8_BLOCKSIZE;
  file->bytepos = OS8_CHECK;
  file->written = 0;

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x668, %rsp            # imm = 0x668
movq %rcx, 0x48(%rsp)
movq %rdx, 0x38(%rsp)
movl %esi, %ecx
movq %rdi, %r12
xorl %edx, %edx
movl %r8d, %eax
negl %eax
sbbl %edx, %edx
movq %rdx, 0x50(%rsp)
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x30(%rsp)
leaq 0x5c(%rdi), %r13
movl %esi, 0x28(%rsp)
movzbl %cl, %r14d
movq %rax, 0x40(%rsp)
movq %r8, 0x20(%rsp)
movl %r14d, 0x14(%rsp)
movq 0x50(%r12), %rax
movb $0x1, %dl
testq %rax, %rax
je 0xdeb1
movl 0x28(%rsp), %ecx
cmpb %cl, 0x8(%rax)
jbe 0xdeb1
movzbl 0x59(%r12), %eax
testl %eax, 0x30(%rsp)
je 0xdeb1
movw %r15w, 0xa(%rsp)
xorl %r15d, %r15d
movw $0x1, %r14w
movq 0x50(%rsp), %rax
movl %eax, %ebp
movzwl %r14w, %edx
movq %r12, %rdi
movl 0x14(%rsp), %esi
xorl %ecx, %ecx
callq 0xd979
testl %eax, %eax
je 0xde4b
movzwl 0x5c(%r12), %edx
xorl %ecx, %ecx
subw %dx, %cx
andw $0xfff, %cx             # imm = 0xFFF
movq 0x20(%rsp), %r8
je 0xde3c
movzwl 0x5e(%r12), %esi
movl $0xfff, %r10d           # imm = 0xFFF
andl %r10d, %esi
movzwl 0x64(%r12), %r9d
movl %r9d, %edx
negl %edx
andl %r10d, %edx
movl %r9d, %edi
andl $0x800, %edi            # imm = 0x800
addl %edi, %edi
andl %r10d, %r9d
subl %r9d, %edi
addl $0x4, %edx
addl $0x5, %edi
movw $0x5, %r11w
movzwl %r11w, %r10d
movzwl 0x5c(%r12,%r10,2), %r9d
testl $0xfff, %r9d            # imm = 0xFFF
je 0xdddb
leal (%r10,%rdx), %r9d
movzwl 0x5c(%r12,%r9,2), %r11d
xorl %r9d, %r9d
subw %r11w, %r9w
andl $0xfff, %r9d            # imm = 0xFFF
addl %edi, %r10d
movl %r10d, %r11d
jmp 0xde1f
movl %r10d, %r9d
movzwl 0x5e(%r12,%r9,2), %r10d
xorl %r9d, %r9d
subw %r10w, %r9w
andl $0xfff, %r9d            # imm = 0xFFF
testw %r8w, %r8w
je 0xde34
cmpw %r8w, %r9w
jb 0xde1b
cmpw %bp, %r9w
jae 0xde1b
movl $0x1, %r15d
movw %r11w, 0x18(%rsp)
movw %si, 0xc(%rsp)
movl %r9d, %ebp
movl %r14d, %ebx
addl $0x2, %r11d
movzwl %r9w, %r9d
addl %esi, %r9d
movl %r9d, %esi
decw %cx
jne 0xdda8
jmp 0xde3c
cmpw %bp, %r9w
ja 0xde04
jmp 0xde1b
movzwl 0x60(%r12), %r14d
andl $0xfff, %r14d           # imm = 0xFFF
jmp 0xde50
movq 0x20(%rsp), %r8
testl %eax, %eax
je 0xdea4
testw %r14w, %r14w
jne 0xdd3a
testl %r15d, %r15d
je 0xdea4
movzwl %bx, %edx
movq %r12, %rdi
movl 0x14(%rsp), %r14d
movl %r14d, %esi
xorl %ecx, %ecx
callq 0xd979
movq 0x20(%rsp), %r8
testl %eax, %eax
movzwl 0xa(%rsp), %r15d
movb $0x1, %dl
je 0xdeb1
xorl %edx, %edx
movl %ebx, %r15d
movzwl 0x18(%rsp), %eax
movw %ax, 0x12(%rsp)
movzwl 0xc(%rsp), %eax
movq %rax, 0x40(%rsp)
jmp 0xdeb1
movzwl 0xa(%rsp), %r15d
movl 0x14(%rsp), %r14d
movb $0x1, %dl
testb %dl, %dl
jne 0xe4c1
movw %r15w, 0xa(%rsp)
movzwl 0x64(%r12), %eax
movl %eax, %ecx
andl $0xfff, %ecx            # imm = 0xFFF
shll $0x4, %eax
movswl %ax, %ebx
sarl $0xf, %ebx
andl $0xfffff000, %ebx       # imm = 0xFFFFF000
orl %ecx, %ebx
movl $0x5, %ecx
movl $0x5, %ebp
subl %ebx, %ebp
movzwl 0x5c(%r12), %edx
xorl %eax, %eax
subw %dx, %ax
andw $0xfff, %ax             # imm = 0xFFF
leaq 0x60(%rsp), %r15
je 0xdf28
movw $0x5, %cx
movl %eax, %edx
movzwl %cx, %esi
movzwl (%r13,%rsi,2), %edi
movl $0x2, %esi
testl $0xfff, %edi            # imm = 0xFFF
je 0xdf1e
movl %ebp, %esi
addl %esi, %ecx
decw %dx
jne 0xdf06
movzwl %cx, %ecx
movzwl %bp, %edx
addl %ecx, %edx
cmpl $0x100, %edx            # imm = 0x100
jb 0xe276
xorl %ebx, %ebx
movw $0x1, %ax
movzwl %ax, %ebp
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
movq %r15, %rcx
callq 0xd979
testl %eax, %eax
je 0xe24d
movzwl %bx, %ebx
btsl %ebp, %ebx
movzwl 0x64(%rsp), %eax
movw $0xfff, %cx             # imm = 0xFFF
andw %cx, %ax
jne 0xdf3f
movl $0x1, %ebp
btl %ebp, %ebx
jae 0xdf84
incl %ebp
cmpl $0x7, %ebp
jne 0xdf73
jmp 0xe24d
xorl %esi, %esi
movw %si, 0x60(%rsp)
movzwl 0x60(%r12), %eax
movw %ax, 0x64(%rsp)
movw %si, 0x66(%rsp)
movl 0x64(%r12), %ecx
movw %cx, 0x68(%rsp)
xorl %eax, %eax
subw 0x5c(%r12), %ax
andw $0xfff, %ax             # imm = 0xFFF
movl %eax, 0x2c(%rsp)
je 0xe046
movzwl %ax, %edx
movl %ecx, %edi
andl $0x800, %edi            # imm = 0x800
addl %edi, %edi
movl %ecx, %eax
andl $0xfff, %eax            # imm = 0xFFF
subl %eax, %edi
addl $0x5, %edi
movw $0x5, %r9w
xorl %r8d, %r8d
xorl %r10d, %r10d
xorl %esi, %esi
movzwl %r9w, %eax
movzwl (%r13,%rax,2), %r11d
testl $0xfff, %r11d           # imm = 0xFFF
je 0xdff9
addl %edi, %r9d
jmp 0xe039
movzwl 0x2(%r13,%rax,2), %eax
xorl %r10d, %r10d
subw %ax, %r10w
andl $0xfff, %r10d           # imm = 0xFFF
movzwl %r8w, %eax
addl %eax, %eax
movw %r10w, 0x260(%rsp,%rax)
addl %r10d, %esi
movw %r9w, 0x460(%rsp,%rax)
incl %r8d
xorl %r10d, %r10d
cmpl $0x1, %edx
sete %r10b
addl $0x2, %r9d
decl %edx
jne 0xdfe1
testw %r10w, %r10w
sete %al
jmp 0xe04b
movb $0x1, %al
xorl %r8d, %r8d
movzwl %r8w, %edi
movzwl 0x45e(%rsp,%rdi,2), %edx
movw %dx, 0xc(%rsp)
movzwl %si, %esi
testb %al, %al
je 0xe070
testq %rdi, %rdi
movl $0xffff, %r8d           # imm = 0xFFFF
jne 0xe089
jmp 0xe0c9
testq %rdi, %rdi
movl $0xffff, %r8d           # imm = 0xFFFF
je 0xe0c9
movl %esi, %eax
shrl %eax
cmpw %ax, 0x25e(%rsp,%rdi,2)
jae 0xe0c9
cmpw $0x2, %di
movl $0x1, %eax
cmovbl %eax, %edi
xorl %eax, %eax
xorl %edx, %edx
movzwl %dx, %r8d
movzwl 0x260(%rsp,%rax,2), %edx
addl %r8d, %edx
cmpl %esi, %edx
jae 0xe0b6
incq %rax
cmpq %rax, %rdi
jne 0xe099
jmp 0xe0c3
movzwl 0x460(%rsp,%rax,2), %eax
movw %ax, 0xc(%rsp)
movl $0xffff, %r8d           # imm = 0xFFFF
movl 0x2c(%rsp), %edi
testw %di, %di
je 0xe1f0
negl %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movzwl 0x5e(%r12), %esi
movzwl %cx, %eax
leal 0x4(%rax), %ecx
movl %ecx, 0x34(%rsp)
addl $0x5, %eax
movq %rax, 0x58(%rsp)
movl $0x5, %r10d
movw $0x5, %r9w
movl $0x0, 0x18(%rsp)
xorl %r11d, %r11d
cmpw %r10w, 0xc(%rsp)
jne 0xe128
leal 0x1(%r11), %eax
movl %eax, 0x18(%rsp)
movl %esi, %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x62(%rsp)
movzwl %r10w, %ebx
movl %ebx, %eax
movzwl (%r13,%rax,2), %edx
testl $0xfff, %edx            # imm = 0xFFF
movl 0x34(%rsp), %ecx
movl %ecx, %r14d
movl $0x1, %ecx
cmovel %ecx, %r14d
movq 0x58(%rsp), %rcx
movl %ecx, %r10d
movl $0x2, %ecx
cmovel %ecx, %r10d
andl %r8d, %r10d
cmpw $0x0, 0x18(%rsp)
je 0xe1bc
movq %r12, %rcx
movq %r13, %r8
leaq (,%rax,2), %rax
addq %r13, %rax
movl %r10d, %edx
movzwl %r9w, %r12d
xorl %r13d, %r13d
leal (%r12,%r13), %r15d
movzwl (%rax,%r13,2), %edi
movzwl %r15w, %r15d
movw %di, 0x60(%rsp,%r15,2)
incq %r13
cmpq %r13, %rdx
jne 0xe181
incw 0x60(%rsp)
addl %r9d, %r13d
movl %r13d, %r9d
movq %rcx, %r12
movq %r8, %r13
leaq 0x60(%rsp), %r15
movl $0xffff, %r8d           # imm = 0xFFFF
movl 0x2c(%rsp), %edi
andl %r8d, %r14d
addl %ebx, %r14d
movzwl (%r13,%r14,2), %eax
addl %ebx, %r10d
subl %eax, %esi
incl %r11d
cmpw %di, %r11w
jne 0xe10c
movl $0x1, %ebx
subl 0x18(%rsp), %ebx
andl $0xfff, %ebx            # imm = 0xFFF
movl 0x14(%rsp), %r14d
jmp 0xe1f4
movw $0x1, %bx
movzwl 0x60(%rsp), %eax
xorl %ecx, %ecx
subw %ax, %cx
andl $0xfff, %ecx            # imm = 0xFFF
movw %cx, 0x60(%rsp)
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
movq %r15, %rcx
callq 0xda0d
testl %eax, %eax
je 0xe24d
movw %bx, 0x5c(%r12)
movw %bp, 0x60(%r12)
movzwl 0xa(%rsp), %r15d
movzwl %r15w, %edx
movq %r12, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0xda0d
testl %eax, %eax
movq 0x20(%rsp), %r8
jne 0xdcf8
leaq 0x60(%rsp), %rbx
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0x47e8
movl (%rbx), %ebx
movl %ebx, %eax
addq $0x668, %rsp            # imm = 0x668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movswl %bx, %ecx
negl %ecx
movzwl 0x12(%rsp), %edi
testw %r8w, %r8w
jne 0xe296
xorl %r8d, %r8d
subw 0x2(%r13,%rdi,2), %r8w
andl $0xfff, %r8d            # imm = 0xFFF
movl %ecx, 0xc(%rsp)
movzwl %cx, %ecx
movq %rcx, 0x18(%rsp)
movzwl %ax, %ecx
movzwl 0x5e(%r12,%rdi,2), %edx
movq %r8, 0x20(%rsp)
addw %r8w, %dx
movl $0xfff, %r8d            # imm = 0xFFF
andl %r8d, %edx
movl %ecx, %esi
xorl %r8d, %esi
movq %rdi, %r15
movw %dx, 0x5e(%r12,%rdi,2)
movw %si, 0x60(%rsp)
movzwl 0x5e(%r12), %edx
movw %dx, 0x62(%rsp)
movzwl 0x60(%r12), %edx
movw %dx, 0x64(%rsp)
movw $0x0, 0x66(%rsp)
movzwl 0x64(%r12), %edx
movw %dx, 0x68(%rsp)
testw %cx, %cx
je 0xe38e
movw $0x5, %r8w
xorl %edx, %edx
movswl %bp, %esi
movl $0x2, %edi
movw $0x5, %r9w
cmpw 0x12(%rsp), %r9w
jne 0xe333
addl %esi, %r8d
movl %edx, %r10d
notl %r10d
addl %ecx, %r10d
movq 0x48(%rsp), %r11
movw %r10w, 0x14(%r11)
movzwl %r9w, %r10d
movzwl (%r13,%r10,2), %r10d
testl $0xfff, %r10d           # imm = 0xFFF
movl %ebp, %r10d
cmovel %edi, %r10d
testw %r10w, %r10w
je 0xe37b
leal 0x1(%r9), %r11d
movzwl %r9w, %r9d
movzwl (%r13,%r9,2), %r9d
leal 0x1(%r8), %r14d
movzwl %r8w, %r8d
movw %r9w, 0x60(%rsp,%r8,2)
movl %r14d, %r8d
movl %r11d, %r9d
decw %r10w
jne 0xe351
jmp 0xe381
movl %r9d, %r11d
movl %r8d, %r14d
incl %edx
movl %r14d, %r8d
movl %r11d, %r9d
cmpw %ax, %dx
jne 0xe315
leaq 0x60(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
callq 0x31e0
movq 0x38(%rsp), %rcx
movzwl 0x2(%rcx), %eax
movl $0xfff, %esi            # imm = 0xFFF
andl %esi, %eax
movq %r15, %rdx
movw %ax, (%r13,%r15,2)
movzwl 0x4(%rcx), %eax
andl %esi, %eax
movw %ax, 0x2(%r13,%r15,2)
movzwl 0x6(%rcx), %eax
andl %esi, %eax
movw %ax, 0x4(%r13,%r15,2)
movzwl 0x8(%rcx), %eax
andl %esi, %eax
movw %ax, 0x6(%r13,%r15,2)
testw %bx, %bx
je 0xe3f3
movzwl 0x5a(%r12), %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x64(%r12,%rdx,2)
movq 0x20(%rsp), %r15
movl %r15d, %eax
negl %eax
andl $0xfff, %eax            # imm = 0xFFF
movq 0x18(%rsp), %rcx
addl %edx, %ecx
movw %ax, 0x64(%r12,%rcx,2)
movzwl 0xa(%rsp), %r14d
movzwl %r14w, %edx
xorl %ebx, %ebx
movq %r12, %rdi
movl 0x14(%rsp), %esi
xorl %ecx, %ecx
callq 0xda0d
testl %eax, %eax
je 0xe262
movq 0x38(%rsp), %rdx
movzwl 0x2(%rdx), %eax
movq 0x48(%rsp), %rcx
movw %ax, (%rcx)
movzwl 0x4(%rdx), %eax
movw %ax, 0x2(%rcx)
movzwl 0x6(%rdx), %eax
movw %ax, 0x4(%rcx)
movzwl 0x8(%rdx), %eax
movw %ax, 0x6(%rcx)
movzwl 0x5a(%r12), %eax
movw %ax, 0x8(%rcx)
movw %r15w, 0xa(%rcx)
movb %r14b, 0xc(%rcx)
movzwl 0x12(%rsp), %eax
movw %ax, 0xe(%rcx)
movw %bp, 0x10(%rcx)
movl 0xc(%rsp), %eax
movw %ax, 0x12(%rcx)
movq %r12, 0x20(%rcx)
movl 0x28(%rsp), %eax
movb %al, 0x28(%rcx)
movw $0x0, 0x3a(%rcx)
movq 0x40(%rsp), %rax
movw %ax, 0x38(%rcx)
leal -0x1(%rax,%r15), %eax
movw %ax, 0x3c(%rcx)
movw $0x100, 0x3e(%rcx)      # imm = 0x100
movb $0x3, 0x40(%rcx)
movq $0x0, 0x48(%rcx)
movl $0x1, %ebx
jmp 0xe262
leaq 0x60(%rsp), %rbx
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0x484d
jmp 0xe260