// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11MergeEmptyRegions
static void rt11MergeEmptyRegions(
  struct mountedFS *mount,
  uint16_t offset
)
{
  struct RT11data *data = &mount->rt11data;
  uint16_t entrysz = RT11_DI_SIZE + (le16toh(data->buf[RT11_DH_EXTRA]) >> 1);
  uint16_t src = offset, dest = offset;
  uint16_t free = le16toh(data->buf[offset + RT11_DI_LENGTH]);

  /*
   * Check for previous empty entry.
   */
  if (offset != RT11_DH_SIZE) {
    uint16_t off = offset - entrysz;

    if ((le16toh(data->buf[off + RT11_DI_STATUS]) & RT11_E_MPTY) != 0) {
      free += le16toh(data->buf[off + RT11_DI_LENGTH]);
      dest = off;
    }
  }

  /*
   * Check for following empty entry
   */
  if ((RT11_DS_SIZE - (offset + entrysz)) >= entrysz) {
    uint16_t off = offset + entrysz;

    if ((le16toh(data->buf[off + RT11_DI_STATUS]) & RT11_E_MPTY) != 0) {
      free += le16toh(data->buf[off + RT11_DI_LENGTH]);
      src = off;
    }
  }

  if (src != dest) {
    /*
     * We need to adjust the size of the unused region and slide the rest
     * of the directory down 1 or 2 entries. Since we are collapsing at
     * least 2 directory entries together, we will zero out the file name
     * and type.
     */
    data->buf[dest + RT11_DI_LENGTH] = htole16(free);
    data->buf[dest + RT11_DI_FNAME1] = 0;
    data->buf[dest + RT11_DI_FNAME2] = 0;
    data->buf[dest + RT11_DI_FTYPE] = 0;

    dest += entrysz;
    src += entrysz;

    /*
     * Now slide the directory down
     */
    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);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movw %si, %ax
movq %rdi, -0x8(%rbp)
movw %ax, -0xa(%rbp)
movq -0x8(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x430(%rax), %edi
callq 0xc6b0
movzwl %ax, %eax
sarl %eax
addl $0x7, %eax
movw %ax, -0x1a(%rbp)
movw -0xa(%rbp), %ax
movw %ax, -0x1c(%rbp)
movw -0xa(%rbp), %ax
movw %ax, -0x1e(%rbp)
movq -0x18(%rbp), %rax
movzwl -0xa(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movw %ax, -0x20(%rbp)
movzwl -0xa(%rbp), %eax
cmpl $0x5, %eax
je 0x1014b
movzwl -0xa(%rbp), %eax
movzwl -0x1a(%rbp), %ecx
subl %ecx, %eax
movw %ax, -0x22(%rbp)
movq -0x18(%rbp), %rax
movzwl -0x22(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x10149
movq -0x18(%rbp), %rax
movzwl -0x22(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
movzwl -0x20(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x20(%rbp)
movw -0x22(%rbp), %ax
movw %ax, -0x1e(%rbp)
jmp 0x1014b
movzwl -0xa(%rbp), %ecx
movzwl -0x1a(%rbp), %eax
addl %eax, %ecx
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movzwl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jl 0x101cc
movzwl -0xa(%rbp), %eax
movzwl -0x1a(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movzwl -0x24(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x101ca
movq -0x18(%rbp), %rax
movzwl -0x24(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
movzwl -0x20(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x20(%rbp)
movw -0x24(%rbp), %ax
movw %ax, -0x1c(%rbp)
jmp 0x101cc
movzwl -0x1c(%rbp), %eax
movzwl -0x1e(%rbp), %ecx
cmpl %ecx, %eax
je 0x10370
movzwl -0x20(%rbp), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw $0x0, 0x42a(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movw $0x0, 0x42a(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movw $0x0, 0x42a(%rax,%rcx,2)
movzwl -0x1a(%rbp), %ecx
movzwl -0x1e(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x1e(%rbp)
movzwl -0x1a(%rbp), %ecx
movzwl -0x1c(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x1c(%rbp)
movzwl -0x1c(%rbp), %eax
cmpl $0x200, %eax            # imm = 0x200
jge 0x1034d
movq -0x18(%rbp), %rax
movzwl -0x1c(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %dx
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x18(%rbp), %rax
movzwl -0x1e(%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 0x102d8
movzwl -0x1c(%rbp), %ecx
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movzwl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x102da
jmp 0x1034d
movw $0x1, -0x26(%rbp)
movzwl -0x26(%rbp), %eax
movzwl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1032c
movq -0x18(%rbp), %rax
movzwl -0x1c(%rbp), %ecx
movzwl -0x26(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %dx
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
movzwl -0x26(%rbp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movw -0x26(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x26(%rbp)
jmp 0x102e0
movzwl -0x1a(%rbp), %ecx
movzwl -0x1e(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x1e(%rbp)
movzwl -0x1a(%rbp), %ecx
movzwl -0x1c(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x1c(%rbp)
jmp 0x10262
movl $0x800, %edi            # imm = 0x800
callq 0xc6b0
movw %ax, %dx
movq -0x18(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)