// File: /workspace/github_c_makefile_star2_O3/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
movl %r8d, %r14d
movq %rcx, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movl %esi, %ecx
movq %rdi, %r15
xorl %edx, %edx
movl %r8d, %eax
negl %eax
sbbl %edx, %edx
movq %rdx, 0x58(%rsp)
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x24(%rsp)
leaq 0x5c(%rdi), %r12
movl %esi, 0x14(%rsp)
movzbl %cl, %eax
movl %eax, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq 0x50(%r15), %rax
testq %rax, %rax
je 0xdfb7
movl 0x14(%rsp), %ecx
cmpb %cl, 0x8(%rax)
jbe 0xdfb7
movq 0x8(%rsp), %rax
movzbl 0x59(%rax), %eax
testl %eax, 0x24(%rsp)
je 0xdfb7
xorl %r15d, %r15d
movw $0x1, %r13w
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movl %eax, %ebp
movq 0x8(%rsp), %rdi
movzwl %r13w, %edx
movl 0x10(%rsp), %esi
xorl %ecx, %ecx
callq 0xd707
testl %eax, %eax
je 0xdfb7
movzwl (%r12), %ecx
xorl %eax, %eax
subw %cx, %ax
andw $0xfff, %ax             # imm = 0xFFF
je 0xdbf7
movq 0x8(%rsp), %rdi
movzwl 0x5e(%rdi), %edx
movl $0xfff, %r9d            # imm = 0xFFF
andl %r9d, %edx
movzwl 0x64(%rdi), %r8d
movl %r8d, %ecx
negl %ecx
andl %r9d, %ecx
movl %r8d, %esi
andl $0x800, %esi            # imm = 0x800
addl %esi, %esi
andl %r9d, %r8d
subl %r8d, %esi
addl $0x4, %ecx
addl $0x5, %esi
movw $0x5, %r10w
movq 0x18(%rsp), %r11
movzwl %r10w, %r9d
movzwl (%r12,%r9,2), %r8d
testl $0xfff, %r8d            # imm = 0xFFF
je 0xdb98
leal (%r9,%rcx), %r8d
movzwl (%r12,%r8,2), %r10d
xorl %r8d, %r8d
subw %r10w, %r8w
andl $0xfff, %r8d            # imm = 0xFFF
addl %esi, %r9d
movl %r9d, %r10d
jmp 0xdbda
movl %r9d, %r8d
movzwl 0x2(%r12,%r8,2), %r9d
xorl %r8d, %r8d
subw %r9w, %r8w
andl $0xfff, %r8d            # imm = 0xFFF
testw %r14w, %r14w
je 0xdbef
cmpw %r14w, %r8w
jb 0xdbd6
cmpw %bp, %r8w
jae 0xdbd6
movl $0x1, %r15d
movl %r10d, %ebx
movl %edx, %r11d
movl %r8d, %ebp
movw %r13w, 0x4(%rsp)
addl $0x2, %r10d
movzwl %r8w, %r8d
addl %edx, %r8d
movl %r8d, %edx
decw %ax
jne 0xdb67
jmp 0xdc01
cmpw %bp, %r8w
ja 0xdbc1
jmp 0xdbd6
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r11
movq %r11, 0x18(%rsp)
movzwl 0x60(%rdi), %r13d
movw $0xfff, %ax             # imm = 0xFFF
andw %ax, %r13w
jne 0xdafc
testl %r15d, %r15d
je 0xdfb7
movzwl 0x4(%rsp), %r13d
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0xd707
testl %eax, %eax
je 0xdfb7
movq 0x8(%rsp), %rdi
movzwl 0x64(%rdi), %eax
movl %eax, %ecx
andl $0xfff, %ecx            # imm = 0xFFF
shll $0x4, %eax
movswl %ax, %r9d
sarl $0xf, %r9d
andl $0xfffff000, %r9d       # imm = 0xFFFFF000
orl %ecx, %r9d
movl $0x5, %ecx
movl $0x5, %ebp
subl %r9d, %ebp
movzwl 0x5c(%rdi), %edx
xorl %eax, %eax
subw %dx, %ax
andw $0xfff, %ax             # imm = 0xFFF
je 0xdcad
movw $0x5, %cx
movl %eax, %edx
movzwl %cx, %esi
movzwl (%r12,%rsi,2), %r8d
movl $0x2, %esi
testl $0xfff, %r8d            # imm = 0xFFF
je 0xdca3
movl %ebp, %esi
addl %esi, %ecx
decw %dx
jne 0xdc8b
movzwl %cx, %ecx
movzwl %bp, %edx
addl %ecx, %edx
cmpl $0x100, %edx            # imm = 0x100
leaq 0x60(%rsp), %r15
jb 0xdfe5
xorl %ebx, %ebx
movw $0x1, %ax
movzwl %ax, %ebp
movl 0x10(%rsp), %esi
movl %ebp, %edx
movq %r15, %rcx
callq 0xd707
testl %eax, %eax
je 0xdf9c
movzwl %bx, %ebx
btsl %ebp, %ebx
movzwl 0x64(%rsp), %eax
movw $0xfff, %cx             # imm = 0xFFF
andw %cx, %ax
movq 0x8(%rsp), %rdi
jne 0xdcc9
movl $0x1, %ebp
btl %ebp, %ebx
jae 0xdd11
incl %ebp
cmpl $0x7, %ebp
jne 0xdd00
jmp 0xdf9c
xorl %esi, %esi
movw %si, 0x60(%rsp)
movq 0x8(%rsp), %rdi
movzwl 0x60(%rdi), %eax
movw %ax, 0x64(%rsp)
movw %si, 0x66(%rsp)
movl 0x64(%rdi), %eax
movw %ax, 0x68(%rsp)
xorl %edx, %edx
subw 0x5c(%rdi), %dx
andw $0xfff, %dx             # imm = 0xFFF
je 0xde11
movl %eax, %esi
negl %esi
andl $0xfff, %esi            # imm = 0xFFF
movzwl 0x5e(%rdi), %ecx
movl %edx, 0x28(%rsp)
movzwl %dx, %edx
movl %eax, %edi
andl $0x800, %edi            # imm = 0x800
addl %edi, %edi
andl $0xfff, %eax            # imm = 0xFFF
subl %eax, %edi
addl $0x5, %edi
movw $0x5, %r9w
xorl %r8d, %r8d
xorl %r11d, %r11d
xorl %r10d, %r10d
movzwl %r9w, %eax
movzwl (%r12,%rax,2), %ebx
testl $0xfff, %ebx            # imm = 0xFFF
je 0xdd91
addl %edi, %r9d
jmp 0xddd1
movzwl 0x2(%r12,%rax,2), %eax
xorl %r11d, %r11d
subw %ax, %r11w
andl $0xfff, %r11d           # imm = 0xFFF
movzwl %r8w, %eax
addl %eax, %eax
movw %r11w, 0x260(%rsp,%rax)
addl %r11d, %r10d
movw %r9w, 0x460(%rsp,%rax)
incl %r8d
xorl %r11d, %r11d
cmpl $0x1, %edx
sete %r11b
addl $0x2, %r9d
decl %edx
jne 0xdd7b
movzwl %r8w, %edi
movzwl 0x45e(%rsp,%rdi,2), %eax
movw %ax, 0x18(%rsp)
movzwl %r10w, %r9d
testw %r11w, %r11w
movl %r13d, 0x44(%rsp)
je 0xde1a
testw %r8w, %r8w
movl 0x28(%rsp), %r11d
je 0xde54
movl %r9d, %eax
shrl %eax
cmpw %ax, 0x25e(%rsp,%rdi,2)
jb 0xde25
jmp 0xde54
movw $0x1, %bx
jmp 0xdf53
testw %r8w, %r8w
movl 0x28(%rsp), %r11d
je 0xde54
xorl %eax, %eax
xorl %edx, %edx
movzwl %dx, %r8d
movzwl 0x260(%rsp,%rax,2), %edx
addl %r8d, %edx
cmpl %r9d, %edx
jae 0xde47
incq %rax
cmpq %rax, %rdi
jne 0xde29
jmp 0xde54
movzwl 0x460(%rsp,%rax,2), %eax
movw %ax, 0x18(%rsp)
movzwl %si, %eax
leal 0x4(%rax), %edx
movl %edx, 0x30(%rsp)
addl $0x5, %eax
movq %rax, 0x38(%rsp)
movl $0x5, %r10d
movw $0x5, %ax
movl %eax, 0x4(%rsp)
xorl %esi, %esi
xorl %r9d, %r9d
xorl %ebx, %ebx
movl $0xffff, %edi           # imm = 0xFFFF
cmpw %r10w, 0x18(%rsp)
jne 0xde98
leal 0x1(%rbx), %r9d
movl %ecx, %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x62(%rsp)
movzwl %r10w, %r15d
movl %r15d, %eax
movzwl (%r12,%rax,2), %edx
testl $0xfff, %edx            # imm = 0xFFF
movl 0x30(%rsp), %edx
movl %edx, %r13d
movl $0x1, %edx
cmovel %edx, %r13d
movq 0x38(%rsp), %rdx
movl %edx, %r10d
movl $0x2, %edx
cmovel %edx, %r10d
andl %edi, %r10d
testw %r9w, %r9w
je 0xdf1a
leaq (%r12,%rax,2), %rsi
movl %r10d, %edx
movzwl 0x4(%rsp), %r8d
xorl %eax, %eax
leal (%r8,%rax), %edi
movzwl (%rsi,%rax,2), %r11d
movzwl %di, %edi
movw %r11w, 0x60(%rsp,%rdi,2)
incq %rax
cmpq %rax, %rdx
jne 0xdee3
movl 0x60(%rsp), %esi
incl %esi
movw %si, 0x60(%rsp)
addl 0x4(%rsp), %eax
movl %eax, 0x4(%rsp)
movl $0xffff, %edi           # imm = 0xFFFF
movl 0x28(%rsp), %r11d
andl %edi, %r13d
addl %r15d, %r13d
movzwl (%r12,%r13,2), %eax
addl %r15d, %r10d
subl %eax, %ecx
incl %ebx
cmpw %r11w, %bx
jne 0xde80
movl $0x1, %ebx
subl %r9d, %ebx
andl $0xfff, %ebx            # imm = 0xFFF
movq 0x8(%rsp), %rdi
leaq 0x60(%rsp), %r15
movl 0x44(%rsp), %r13d
negl %esi
andl $0xfff, %esi            # imm = 0xFFF
movw %si, 0x60(%rsp)
movl 0x10(%rsp), %esi
movl %ebp, %edx
movq %r15, %rcx
callq 0xd79b
testl %eax, %eax
je 0xdf9c
movq 0x8(%rsp), %r15
movw %bx, 0x5c(%r15)
movw %bp, 0x60(%r15)
movq %r15, %rdi
movl 0x10(%rsp), %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0xd79b
testl %eax, %eax
jne 0xdab6
jmp 0xdfa1
movq 0x8(%rsp), %r15
leaq 0x60(%rsp), %rbx
movq %r15, %rdi
movl 0x14(%rsp), %esi
movq %rbx, %rdx
callq 0x4815
jmp 0xdfcd
leaq 0x60(%rsp), %rbx
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
movq %rbx, %rdx
callq 0x4878
movl (%rbx), %r12d
movl %r12d, %eax
addq $0x668, %rsp            # imm = 0x668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movswl %r9w, %ecx
negl %ecx
movzwl %bx, %r8d
testw %r14w, %r14w
jne 0xe005
xorl %r14d, %r14d
subw 0x2(%r12,%r8,2), %r14w
andl $0xfff, %r14d           # imm = 0xFFF
movl %r9d, 0x38(%rsp)
movl %ecx, 0x24(%rsp)
movzwl %cx, %ecx
movq %rcx, 0x28(%rsp)
movzwl %ax, %ecx
movzwl 0x5e(%rdi,%r8,2), %edx
addw %r14w, %dx
movl $0xfff, %r9d            # imm = 0xFFF
andl %r9d, %edx
movl %ecx, %esi
xorl %r9d, %esi
movq %r8, 0x30(%rsp)
movw %dx, 0x5e(%rdi,%r8,2)
movw %si, 0x60(%rsp)
movl 0x5e(%rdi), %edx
movl %edx, 0x62(%rsp)
movw $0x0, 0x66(%rsp)
movzwl 0x64(%rdi), %edx
movw %dx, 0x68(%rsp)
testw %cx, %cx
je 0xe0e5
movw $0x5, %r8w
xorl %edx, %edx
movswl %bp, %esi
movw $0x5, %r9w
cmpw %bx, %r9w
jne 0xe08c
addl %esi, %r8d
movl %edx, %r10d
notl %r10d
addl %ecx, %r10d
movq 0x50(%rsp), %r11
movw %r10w, 0x14(%r11)
movzwl %r9w, %r10d
movzwl (%r12,%r10,2), %r10d
testl $0xfff, %r10d           # imm = 0xFFF
movl %ebp, %r10d
movl $0x2, %edi
cmovel %edi, %r10d
testw %r10w, %r10w
je 0xe0de
movl %ebp, %edi
movl %r13d, %r15d
leal (%r10,%r9), %r11d
movzwl %r9w, %r13d
incl %r9d
movzwl (%r12,%r13,2), %r13d
movzwl %r8w, %ebp
incl %r8d
movw %r13w, 0x60(%rsp,%rbp,2)
decw %r10w
jne 0xe0b7
movl %r11d, %r9d
movl %r15d, %r13d
movl %edi, %ebp
incl %edx
cmpw %ax, %dx
jne 0xe070
leaq 0x60(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
callq 0x31f0
movq 0x48(%rsp), %rcx
movzwl 0x2(%rcx), %eax
movl $0xfff, %esi            # imm = 0xFFF
andl %esi, %eax
movq 0x30(%rsp), %rdx
movw %ax, (%r12,%rdx,2)
movzwl 0x4(%rcx), %eax
andl %esi, %eax
movw %ax, 0x2(%r12,%rdx,2)
movzwl 0x6(%rcx), %eax
andl %esi, %eax
movw %ax, 0x4(%r12,%rdx,2)
movzwl 0x8(%rcx), %eax
andl %esi, %eax
movw %ax, 0x6(%r12,%rdx,2)
cmpw $0x0, 0x38(%rsp)
je 0xe156
movq 0x8(%rsp), %rdi
movzwl 0x5a(%rdi), %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x64(%rdi,%rdx,2)
addq $0x4, %rdx
jmp 0xe15f
addq $0x4, %rdx
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rcx
movl %r14d, %eax
negl %eax
andl $0xfff, %eax            # imm = 0xFFF
addl %ecx, %edx
movw %ax, 0x5c(%rdi,%rdx,2)
xorl %r12d, %r12d
movl 0x10(%rsp), %esi
movl %r13d, %edx
xorl %ecx, %ecx
movq %rdi, %r15
callq 0xd79b
testl %eax, %eax
je 0xdfd0
movq 0x48(%rsp), %rdx
movzwl 0x2(%rdx), %eax
movq 0x50(%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(%r15), %eax
movw %ax, 0x8(%rcx)
movw %r14w, 0xa(%rcx)
movzwl 0x4(%rsp), %eax
movb %al, 0xc(%rcx)
movw %bx, 0xe(%rcx)
movw %bp, 0x10(%rcx)
movl 0x24(%rsp), %eax
movw %ax, 0x12(%rcx)
movq %r15, 0x20(%rcx)
movl 0x14(%rsp), %eax
movb %al, 0x28(%rcx)
movw $0x0, 0x3a(%rcx)
movq 0x18(%rsp), %rbx
movw %bx, 0x38(%rcx)
leal -0x1(%rbx,%r14), %eax
movw %ax, 0x3c(%rcx)
movw $0x100, 0x3e(%rcx)      # imm = 0x100
movb $0x3, 0x40(%rcx)
movq $0x0, 0x48(%rcx)
movl $0x1, %r12d
jmp 0xdfd0