// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %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)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
leaq -0x3a(%rbp), %rcx
leaq -0x3c(%rbp), %r8
leaq -0x3e(%rbp), %r9
movzbl %al, %esi
movzwl -0x2a(%rbp), %edx
callq 0x13dd0
cmpl $0x0, %eax
jne 0x139e7
movq 0xa681(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x673e(%rip), %rsi      # 0x1a0cb
movb $0x0, %al
callq 0x31c0
leaq 0xb84d(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x139db
leaq 0xb840(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0xa60e(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x139db
leaq 0xb82a(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x66f7(%rip), %rsi      # 0x1a0cb
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x13dc0
movq -0x10(%rbp), %rdi
callq 0x140b0
cmpl $0x0, %eax
jne 0x13a8d
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x3a(%rbp), %edx
callq 0x14190
cmpl $0x0, %eax
jne 0x13a88
movq 0xa5e0(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x6df4(%rip), %rsi      # 0x1a822
movb $0x0, %al
callq 0x31c0
leaq 0xb7ac(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x13a7c
leaq 0xb79f(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0xa56d(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x13a7c
leaq 0xb789(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x6dad(%rip), %rsi      # 0x1a822
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x13dc0
jmp 0x1394c
movq -0x38(%rbp), %rax
movzwl 0x14(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x1c(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x148b0
movzwl %ax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movw %ax, -0x42(%rbp)
movzwl -0x42(%rbp), %eax
addl $0x5, %eax
movw %ax, -0x44(%rbp)
movzwl -0x2a(%rbp), %eax
cmpl $0x0, %eax
jne 0x13b02
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movzwl %ax, %eax
movl %eax, -0x4c(%rbp)
jmp 0x13b09
movzwl -0x2a(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movw %ax, -0x46(%rbp)
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movzwl %ax, %eax
movzwl -0x46(%rbp), %ecx
subl %ecx, %eax
movw %ax, -0x48(%rbp)
movzwl -0x48(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movzwl %ax, %edi
callq 0x148e0
movzwl %ax, %edi
callq 0x14890
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw %dx, 0x14(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movw -0x3c(%rbp), %dx
movw -0x40(%rbp), %cx
movw -0x44(%rbp), %ax
movq -0x28(%rbp), %r8
addq $0x14, %r8
movzwl %dx, %esi
movzwl %cx, %edx
movzwl %ax, %ecx
callq 0x14900
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x148e0
movzwl %ax, %edi
callq 0x14890
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x14(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %edi
callq 0x148e0
movzwl %ax, %edi
callq 0x14890
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw %dx, 0x14(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x148e0
movzwl %ax, %edi
callq 0x14890
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movw %dx, 0x14(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0x148e0
movzwl %ax, %edi
callq 0x14890
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movw %dx, 0x14(%rax,%rcx,2)
movzwl -0x42(%rbp), %eax
cmpl $0x0, %eax
je 0x13c71
movq -0x38(%rbp), %rax
movzwl 0x12(%rax), %edi
callq 0x148e0
movzwl %ax, %edi
callq 0x14890
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x14(%rax,%rcx,2)
movzwl -0x46(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movzwl %ax, %edi
callq 0x148e0
movzwl %ax, %edi
callq 0x14890
movw %ax, %dx
movq -0x38(%rbp), %rax
movzwl -0x3c(%rbp), %ecx
movzwl -0x42(%rbp), %esi
addl %esi, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x14(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzwl -0x3a(%rbp), %edx
xorl %ecx, %ecx
movzbl %al, %esi
callq 0x13450
cmpl $0x0, %eax
jne 0x13ccb
movl $0x0, -0x4(%rbp)
jmp 0x13dc0
movq -0x20(%rbp), %rax
movw 0x2(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
movq -0x20(%rbp), %rax
movw 0x4(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x20(%rbp), %rax
movw 0x6(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x20(%rbp), %rax
movw 0x8(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x6(%rax)
movq -0x38(%rbp), %rax
movw 0x12(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x8(%rax)
movw -0x46(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0xa(%rax)
movw -0x3a(%rbp), %ax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0xc(%rax)
movw -0x3c(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0xe(%rax)
movw -0x44(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x10(%rax)
movw -0x42(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x12(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movb -0x11(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x28(%rax)
movq -0x28(%rbp), %rax
movw $0x0, 0x3a(%rax)
movw -0x3e(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x38(%rax)
movzwl -0x3e(%rbp), %eax
movzwl -0x46(%rbp), %ecx
addl %ecx, %eax
subl $0x1, %eax
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0x3c(%rax)
movq -0x28(%rbp), %rax
movw $0x100, 0x3e(%rax)      # imm = 0x100
movq -0x28(%rbp), %rax
movb $0x3, 0x40(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x48(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)