// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11UpdateFile
void rt11UpdateFile(
  struct mountedFS *mount,
  uint8_t unit,
  struct rt11OpenFile *file
)
{
  struct RT11data *data = &mount->rt11data;
  uint16_t offset, length, left, entrysz;

  if (rt11ReadDirSegment(mount, unit, file->segment) == 0)
    return;

  entrysz = RT11_DI_SIZE + (le16toh(data->buf[RT11_DH_EXTRA]) >> 1);
  offset = file->offset;

  length = file->current - file->start + 1;
  left = file->length - length;

  /*
   * Fix up the directory entry and the following empty entry
   */
  data->buf[offset + RT11_DI_STATUS] = htole16(RT11_E_PERM);
  data->buf[offset + RT11_DI_LENGTH] = htole16(length);

  data->buf[offset + entrysz + RT11_DI_LENGTH] =
    htole16(le16toh(data->buf[offset + entrysz + RT11_DI_LENGTH]) + left);

  if (le16toh(data->buf[offset + entrysz + RT11_DI_LENGTH]) == 0) {
    /*
     * Remove the now empty directory entry
     */
    uint16_t dest = offset + entrysz;
    uint16_t src = dest + entrysz;

    for (;src < RT11_DS_SIZE;) {
      uint16_t i;

      data->buf[dest + RT11_DI_STATUS] = data->buf[src + RT11_DI_STATUS];

      if (RT11EOS(le16toh(data->buf[dest + RT11_DI_STATUS])) ||
          ((RT11_DS_SIZE - src) < entrysz))
        break;

      for (i = RT11_DI_FNAME1;i < entrysz; i++)
        data->buf[dest + i] = data->buf[src + i];

      dest += entrysz;
      src += entrysz;
    }

    /*
     * Make sure the last entry contains the end-of-segment marker.
     */
    data->buf[dest + RT11_DI_STATUS] = htole16(RT11_E_EOS);
  }

  rt11WriteDirSegment(mount, file->unit, file->segment);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
movb %al, -0x9(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movb -0x9(%rbp), %cl
movq -0x18(%rbp), %rax
movzbl 0xc(%rax), %eax
movzbl %cl, %esi
movzwl %ax, %edx
callq 0xc3e0
cmpl $0x0, %eax
jne 0xd8e6
jmp 0xdb44
movq -0x20(%rbp), %rax
movzwl 0x430(%rax), %edi
callq 0xc6b0
movzwl %ax, %eax
sarl %eax
addl $0x7, %eax
movw %ax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movw 0xe(%rax), %ax
movw %ax, -0x22(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x30(%rax), %eax
movq -0x18(%rbp), %rcx
movzwl 0x36(%rcx), %ecx
subl %ecx, %eax
addl $0x1, %eax
movw %ax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x8(%rax), %eax
movzwl -0x24(%rbp), %ecx
subl %ecx, %eax
movw %ax, -0x26(%rbp)
movl $0x400, %edi            # imm = 0x400
callq 0xc6b0
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x22(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movzwl -0x24(%rbp), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x22(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl -0x22(%rbp), %ecx
movzwl -0x28(%rbp), %edx
addl %edx, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
movzwl -0x26(%rbp), %ecx
addl %ecx, %eax
movzwl %ax, %edi
callq 0xc6b0
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x22(%rbp), %ecx
movzwl -0x28(%rbp), %esi
addl %esi, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl -0x22(%rbp), %ecx
movzwl -0x28(%rbp), %edx
addl %edx, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0xdb26
movzwl -0x22(%rbp), %eax
movzwl -0x28(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x2a(%rbp)
movzwl -0x2a(%rbp), %eax
movzwl -0x28(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x2c(%rbp)
movzwl -0x2c(%rbp), %eax
cmpl $0x200, %eax            # imm = 0x200
jge 0xdb03
movq -0x20(%rbp), %rax
movzwl -0x2c(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %dx
movq -0x20(%rbp), %rax
movzwl -0x2a(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl -0x2a(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
jne 0xda8e
movzwl -0x2c(%rbp), %ecx
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movzwl -0x28(%rbp), %ecx
cmpl %ecx, %eax
jge 0xda90
jmp 0xdb03
movw $0x1, -0x2e(%rbp)
movzwl -0x2e(%rbp), %eax
movzwl -0x28(%rbp), %ecx
cmpl %ecx, %eax
jge 0xdae2
movq -0x20(%rbp), %rax
movzwl -0x2c(%rbp), %ecx
movzwl -0x2e(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %dx
movq -0x20(%rbp), %rax
movzwl -0x2a(%rbp), %ecx
movzwl -0x2e(%rbp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movw -0x2e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x2e(%rbp)
jmp 0xda96
movzwl -0x28(%rbp), %ecx
movzwl -0x2a(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x2a(%rbp)
movzwl -0x28(%rbp), %ecx
movzwl -0x2c(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x2c(%rbp)
jmp 0xda18
movl $0x800, %edi            # imm = 0x800
callq 0xc6b0
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x2a(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movb 0x20(%rax), %cl
movq -0x18(%rbp), %rax
movzbl 0xc(%rax), %eax
movzbl %cl, %esi
movzwl %ax, %edx
callq 0xc490
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)