// File: /workspace/github_c_makefile_star2_O2/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, %r15d
movq %rcx, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
leaq 0x48(%rdi), %rax
movq %rax, 0x28(%rsp)
xorl %ecx, %ecx
movl %r8d, %eax
negl %eax
sbbl %ecx, %ecx
movq %rcx, 0x58(%rsp)
movq %rdi, 0x8(%rsp)
leaq 0x5c(%rdi), %r13
movl %esi, 0x44(%rsp)
movzbl %sil, %ebp
movl %ebp, 0x4(%rsp)
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0xd345
testl %eax, %eax
movq 0x8(%rsp), %r11
je 0xce92
xorl %r12d, %r12d
movw $0x1, %bp
movq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rax
movl %eax, %r14d
movzwl %bp, %edx
movq %r11, %rdi
movl 0x4(%rsp), %esi
xorl %ecx, %ecx
callq 0xc5d7
movq 0x8(%rsp), %r11
testl %eax, %eax
je 0xce92
movzwl 0x64(%r11), %eax
movl %eax, %edx
movl $0xfff, %edi            # imm = 0xFFF
andl %edi, %edx
movl %eax, %ecx
andl $0x800, %ecx            # imm = 0x800
addl %ecx, %ecx
subl %edx, %ecx
movzwl 0x5c(%r11), %esi
xorl %edx, %edx
subw %si, %dx
andl %edi, %edx
negl %eax
andl %edi, %eax
movzwl 0x5e(%r11), %esi
andl %edi, %esi
addl $0x4, %eax
addl $0x5, %ecx
movw $0x5, %di
subw $0x1, %dx
jb 0xcac5
movzwl %di, %r9d
movzwl (%r13,%r9,2), %r8d
testw $0xfff, %r8w            # imm = 0xFFF
je 0xca5f
leal (%r9,%rax), %edi
movzwl (%r13,%rdi,2), %edi
xorl %r8d, %r8d
subw %di, %r8w
andl $0xfff, %r8d            # imm = 0xFFF
addl %ecx, %r9d
movl %r9d, %edi
jmp 0xcaae
movl %r9d, %r8d
movzwl 0x2(%r13,%r8,2), %r9d
xorl %r8d, %r8d
subw %r9w, %r8w
andl $0xfff, %r8d            # imm = 0xFFF
testw %r15w, %r15w
je 0xcabd
cmpw %r15w, %r8w
setae %r9b
cmpw %r14w, %r8w
setb %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0xcaab
movw %di, 0x2(%rsp)
movl %esi, %r9d
movq %r9, 0x10(%rsp)
movl %r8d, %r14d
movl %ebp, %ebx
pushq $0x1
popq %r12
addl $0x2, %edi
movzwl %r8w, %r8d
addl %esi, %r8d
movl %r8d, %esi
jmp 0xca23
cmpw %r14w, %r8w
ja 0xca95
jmp 0xcaab
movzwl 0x60(%r11), %ebp
movw $0xfff, %ax             # imm = 0xFFF
andw %ax, %bp
jne 0xc9ca
testl %r12d, %r12d
je 0xce92
movzwl %bx, %ebp
movq %r11, %rdi
movl 0x4(%rsp), %esi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0xc5d7
movq 0x8(%rsp), %r11
testl %eax, %eax
je 0xce92
movzwl 0x64(%r11), %eax
movl %eax, %ecx
movl $0xfff, %edx            # imm = 0xFFF
andl %edx, %ecx
shll $0x4, %eax
movswl %ax, %r9d
sarl $0xf, %r9d
andl $0xfffff000, %r9d       # imm = 0xFFFFF000
orl %ecx, %r9d
pushq $0x5
popq %r12
subl %r9d, %r12d
movzwl 0x5c(%r11), %ecx
xorl %eax, %eax
subw %cx, %ax
andl %edx, %eax
movw $0x5, %cx
movl %eax, %edx
pushq $0x2
popq %r8
movzwl %cx, %esi
subw $0x1, %dx
jb 0xcb60
movzwl (%r13,%rsi,2), %edi
movl %r8d, %esi
testw $0xfff, %di             # imm = 0xFFF
je 0xcb5c
movl %r12d, %esi
addl %esi, %ecx
jmp 0xcb40
movl %ebp, 0x1c(%rsp)
movzwl %r12w, %ecx
addl %ecx, %esi
cmpl $0x100, %esi            # imm = 0x100
jb 0xcf03
xorl %ebx, %ebx
movw $0x1, %ax
movzwl %ax, %r12d
movq %r11, %rdi
movl 0x4(%rsp), %esi
movl %r12d, %edx
leaq 0x60(%rsp), %rcx
callq 0xc5d7
movq 0x8(%rsp), %r11
testl %eax, %eax
je 0xce41
movzwl %bx, %ebx
btsl %r12d, %ebx
movzwl 0x64(%rsp), %eax
movw $0xfff, %cx             # imm = 0xFFF
andw %cx, %ax
jne 0xcb7c
pushq $0x1
popq %r12
cmpl $0x7, %r12d
je 0xce41
btl %r12d, %ebx
jae 0xcbcf
incl %r12d
jmp 0xcbba
movl %r12d, 0x30(%rsp)
andw $0x0, 0x60(%rsp)
movzwl 0x60(%r11), %eax
movw %ax, 0x64(%rsp)
andw $0x0, 0x66(%rsp)
movzwl 0x64(%r11), %esi
movw %si, 0x68(%rsp)
movl %esi, %eax
movl $0xfff, %ecx            # imm = 0xFFF
andl %ecx, %eax
movl %esi, %edx
andl $0x800, %edx            # imm = 0x800
addl %edx, %edx
subl %eax, %edx
xorl %r10d, %r10d
subw 0x5c(%r11), %r10w
xorl %edi, %edi
andl %ecx, %r10d
movzwl 0x5e(%r11), %ecx
addl $0x5, %edx
movw $0x5, %r8w
movl %r10d, 0x10(%rsp)
xorl %r11d, %r11d
xorl %ebp, %ebp
movl %r10d, %ebx
movzwl %bp, %r9d
subl $0x1, %ebx
jb 0xcc99
movzwl %r8w, %eax
movzwl (%r13,%rax,2), %r14d
testw $0xfff, %r14w           # imm = 0xFFF
je 0xcc53
addl %edx, %r8d
movl %ebx, %r10d
jmp 0xcc2d
movzwl 0x2(%r13,%rax,2), %eax
xorl %r11d, %r11d
subw %ax, %r11w
andl $0xfff, %r11d           # imm = 0xFFF
movl %r9d, %eax
addl %eax, %eax
movw %r11w, 0x260(%rsp,%rax)
addl %r11d, %edi
movw %r8w, 0x460(%rsp,%rax)
leal 0x1(%r9), %ebp
xorl %r11d, %r11d
cmpl $0x1, %r10d
sete %r11b
addl $0x2, %r8d
movl %ebx, %r10d
jmp 0xcc2d
movzwl 0x45e(%rsp,%r9,2), %eax
movw %ax, 0x2(%rsp)
movzwl %di, %edi
testw %r11w, %r11w
je 0xccbf
movl %edi, %eax
shrl %eax
cmpw %ax, 0x25e(%rsp,%r9,2)
jae 0xcceb
xorl %eax, %eax
xorl %edx, %edx
cmpq %rax, %r9
je 0xcceb
movzwl %dx, %r8d
movzwl 0x260(%rsp,%rax,2), %edx
addl %r8d, %edx
incq %rax
cmpl %edi, %edx
jb 0xccc3
movzwl 0x45e(%rsp,%rax,2), %eax
movw %ax, 0x2(%rsp)
negl %esi
andl $0xfff, %esi            # imm = 0xFFF
movzwl %si, %eax
leal 0x4(%rax), %edx
movl %edx, 0x20(%rsp)
addl $0x5, %eax
movq %rax, 0x38(%rsp)
movw $0x5, %r8w
xorl %r11d, %r11d
xorl %r9d, %r9d
xorl %r12d, %r12d
pushq $0x5
popq %r10
movl $0xffff, %esi           # imm = 0xFFFF
movl %ecx, %edi
cmpw 0x10(%rsp), %r9w
je 0xcdde
cmpw %r10w, 0x2(%rsp)
jne 0xcd42
leal 0x1(%r9), %r12d
movl %edi, %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x62(%rsp)
movzwl %r10w, %ebp
movl %ebp, %eax
movzwl (%r13,%rax,2), %edx
testw $0xfff, %dx             # imm = 0xFFF
movl 0x20(%rsp), %ecx
movl %ecx, %r14d
pushq $0x1
popq %rcx
cmovel %ecx, %r14d
movq 0x38(%rsp), %rcx
movl %ecx, %r10d
pushq $0x2
popq %rcx
cmovel %ecx, %r10d
andl %esi, %r10d
testw %r12w, %r12w
je 0xcdc5
movl %edi, %ebx
leaq (,%rax,2), %r11
addq %r13, %r11
movl %r10d, %edx
movzwl %r8w, %edi
xorl %eax, %eax
cmpq %rax, %rdx
je 0xcdaa
leaq (%rdi,%rax), %rsi
movzwl (%r11,%rax,2), %ecx
movzwl %si, %esi
movw %cx, 0x60(%rsp,%rsi,2)
incq %rax
jmp 0xcd8f
movl 0x60(%rsp), %r11d
incl %r11d
movw %r11w, 0x60(%rsp)
addl %r8d, %eax
movl %eax, %r8d
movl $0xffff, %esi           # imm = 0xFFFF
movl %ebx, %edi
andl %esi, %r14d
addl %ebp, %r14d
movzwl (%r13,%r14,2), %eax
addl %ebp, %r10d
subl %eax, %edi
incl %r9d
jmp 0xcd1e
negl %r11d
andl $0xfff, %r11d           # imm = 0xFFF
movw %r11w, 0x60(%rsp)
movq 0x8(%rsp), %r14
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl 0x30(%rsp), %ebp
movl %ebp, %edx
leaq 0x60(%rsp), %rcx
callq 0xc6a8
testl %eax, %eax
je 0xce41
pushq $0x1
popq %rax
subl %r12d, %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x5c(%r14)
movw %bp, 0x60(%r14)
movq %r14, %rdi
movl 0x4(%rsp), %ebp
movl %ebp, %esi
movl 0x1c(%rsp), %edx
xorl %ecx, %ecx
callq 0xc6a8
testl %eax, %eax
jne 0xc99d
jmp 0xce45
movl 0x4(%rsp), %ebp
movq 0xa1ac(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
movq 0x8(%rsp), %rdx
addq $0x8, %rdx
leaq 0x4aa3(%rip), %rsi      # 0x11902
xorl %r13d, %r13d
movq %rdx, %rbx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xb153(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0xceee
movq 0xa13c(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0xceee
leaq 0x4a72(%rip), %rsi      # 0x11902
jmp 0xcedf
movq 0xa15f(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
addq $0x8, %r11
leaq 0x41d6(%rip), %rsi      # 0x1107d
xorl %r13d, %r13d
movq %r11, %rbx
movq %r11, %rdx
movl 0x4(%rsp), %ebp
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xb104(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0xceee
movq 0xa0ed(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0xceee
leaq 0x419e(%rip), %rsi      # 0x1107d
xorl %r13d, %r13d
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x31e0
movl %r13d, %eax
addq $0x668, %rsp            # imm = 0x668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r9d, 0x38(%rsp)
movswl %r9w, %ecx
negl %ecx
movl %ecx, 0x28(%rsp)
movzwl %cx, %ecx
movq %rcx, 0x20(%rsp)
movzwl 0x2(%rsp), %esi
testw %r15w, %r15w
jne 0xcf35
xorl %r15d, %r15d
subw 0x2(%r13,%rsi,2), %r15w
andl $0xfff, %r15d           # imm = 0xFFF
movzwl 0x5e(%r11,%rsi,2), %ecx
addw %r15w, %cx
movl $0xfff, %edx            # imm = 0xFFF
andl %edx, %ecx
movq %rsi, 0x30(%rsp)
movw %cx, 0x5e(%r11,%rsi,2)
movzwl %ax, %ecx
xorl %ecx, %edx
movw %dx, 0x60(%rsp)
movl 0x5e(%r11), %edx
movl %edx, 0x62(%rsp)
andw $0x0, 0x66(%rsp)
movzwl 0x64(%r11), %edx
movw %dx, 0x68(%rsp)
movw $0x5, %r9w
xorl %edx, %edx
movswl %r12w, %esi
pushq $0x2
popq %rdi
movw $0x5, %r8w
cmpw %ax, %dx
je 0xcff0
cmpw 0x2(%rsp), %r9w
jne 0xcfa9
addl %esi, %r8d
movl %edx, %r10d
notl %r10d
addl %ecx, %r10d
movq 0x50(%rsp), %r11
movw %r10w, 0x14(%r11)
movzwl %r9w, %r10d
movzwl (%r13,%r10,2), %r10d
testw $0xfff, %r10w           # imm = 0xFFF
movl %r12d, %r11d
cmovel %edi, %r11d
movl %r11d, %r10d
addl %r9d, %r10d
subw $0x1, %r11w
jb 0xcfe9
movzwl %r9w, %ebp
incl %r9d
movzwl (%r13,%rbp,2), %ebp
movzwl %r8w, %r14d
incl %r8d
movw %bp, 0x60(%rsp,%r14,2)
jmp 0xcfc6
incl %edx
movl %r10d, %r9d
jmp 0xcf86
leaq 0x60(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
callq 0x31f0
movq 0x48(%rsp), %rdx
movzwl 0x2(%rdx), %eax
movl $0xfff, %ecx            # imm = 0xFFF
andl %ecx, %eax
movq 0x30(%rsp), %rsi
movw %ax, (%r13,%rsi,2)
movzwl 0x4(%rdx), %eax
andl %ecx, %eax
movw %ax, 0x2(%r13,%rsi,2)
movzwl 0x6(%rdx), %eax
andl %ecx, %eax
movw %ax, 0x4(%r13,%rsi,2)
movzwl 0x8(%rdx), %eax
andl %ecx, %eax
movw %ax, 0x6(%r13,%rsi,2)
cmpw $0x0, 0x38(%rsp)
je 0xd062
movq 0x8(%rsp), %rdi
movzwl 0x5a(%rdi), %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x64(%rdi,%rsi,2)
addq $0x4, %rsi
jmp 0xd06b
addq $0x4, %rsi
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rcx
movl %r15d, %eax
negl %eax
andl $0xfff, %eax            # imm = 0xFFF
addl %ecx, %esi
movw %ax, 0x5c(%rdi,%rsi,2)
xorl %r13d, %r13d
movl 0x4(%rsp), %esi
movl 0x1c(%rsp), %edx
xorl %ecx, %ecx
callq 0xc6a8
testl %eax, %eax
je 0xceee
movq 0x48(%rsp), %rcx
movzwl 0x2(%rcx), %eax
movq 0x50(%rsp), %rdx
movw %ax, (%rdx)
movzwl 0x4(%rcx), %eax
movw %ax, 0x2(%rdx)
movzwl 0x6(%rcx), %eax
movw %ax, 0x4(%rdx)
movzwl 0x8(%rcx), %eax
movw %ax, 0x6(%rdx)
movq 0x8(%rsp), %rcx
movzwl 0x5a(%rcx), %eax
movw %ax, 0x8(%rdx)
movw %r15w, 0xa(%rdx)
movb %bl, 0xc(%rdx)
movzwl 0x2(%rsp), %eax
movw %ax, 0xe(%rdx)
movw %r12w, 0x10(%rdx)
movl 0x28(%rsp), %eax
movw %ax, 0x12(%rdx)
movq %rcx, 0x20(%rdx)
movl 0x44(%rsp), %eax
movb %al, 0x28(%rdx)
andw $0x0, 0x3a(%rdx)
movq 0x10(%rsp), %rax
movw %ax, 0x38(%rdx)
addl %r15d, %eax
decl %eax
movw %ax, 0x3c(%rdx)
movw $0x100, 0x3e(%rdx)      # imm = 0x100
movb $0x3, 0x40(%rdx)
andq $0x0, 0x48(%rdx)
pushq $0x1
popq %r13
jmp 0xceee