// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11SplitDirSegment
static int rt11SplitDirSegment(
  struct mountedFS *mount,
  uint8_t unit,
  uint16_t entrysz,
  uint16_t segment,
  uint16_t newsegment
)
{
  struct RT11data *data = &mount->rt11data;
  uint16_t entries = RT11_DS_DISPACE / entrysz;
  uint16_t nextsegment = data->buf[RT11_DH_NEXT];
  uint16_t entry, count, status, startblk = le16toh(data->buf[RT11_DH_START]);
  uint16_t dest;

  /*
   * Find first permanent or tentative file entry in the middle of the
   * directory segment.
   */
  for (count = 0, entry = RT11_DH_SIZE;;count++, entry += entrysz) {
    status = le16toh(data->buf[entry + RT11_DI_STATUS]);
    if ((count > (entries / 2)) &&
        ((status & (RT11_E_PERM | RT11_E_TENT)) != 0))
      break;
    startblk += le16toh(data->buf[entry + RT11_DI_LENGTH]);
  }

  /*
   * Terminate the current directory segment at this point.
   */
  data->buf[entry + RT11_DI_STATUS] = htole16(RT11_E_EOS);

  /*
   * Link to the new segment and update the directory segment on disk.
   */
  data->buf[RT11_DH_NEXT] = htole16(newsegment);
  if (rt11WriteDirSegment(mount, unit, segment) == 0)
    return 0;

  /*
   * Update the directory header to represent the new directory segment.
   */
  data->buf[RT11_DH_NEXT] = nextsegment;
  data->buf[RT11_DH_START] = htole16(startblk);

  /*
   * Restore the directory entry status word, slide down the directory entries
   * to the beginning of the directory segment and update the new directory
   * segment on disk.
   */
  data->buf[entry + RT11_DI_STATUS] = htole16(status);

  for (dest = RT11_DH_SIZE;entry < RT11_DS_SIZE;) {
    uint16_t i;

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

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

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

  if (rt11WriteDirSegment(mount, unit, newsegment) == 0)
    return 0;

  /*
   * Update the highest directory segment in use in the first directory
   * segment.
   */
  if (rt11ReadDirSegment(mount, unit, 1) == 0)
    return 0;

  data->buf[RT11_DH_HIGHEST] = htole16(newsegment);

  if (rt11WriteDirSegment(mount, unit, 1) == 0)
    return 0;

  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movw %r8w, %ax
movq %rdi, -0x10(%rbp)
movb %sil, -0x11(%rbp)
movw %dx, -0x14(%rbp)
movw %cx, -0x16(%rbp)
movw %ax, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x20(%rbp)
movzwl -0x14(%rbp), %ecx
movl $0x1fb, %eax            # imm = 0x1FB
cltd
idivl %ecx
movw %ax, -0x22(%rbp)
movq -0x20(%rbp), %rax
movw 0x42c(%rax), %ax
movw %ax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movzwl 0x432(%rax), %edi
callq 0xc6b0
movw %ax, -0x2c(%rbp)
movw $0x0, -0x28(%rbp)
movw $0x5, -0x26(%rbp)
movq -0x20(%rbp), %rax
movzwl -0x26(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movw %ax, -0x2a(%rbp)
movzwl -0x28(%rbp), %eax
movl %eax, -0x34(%rbp)
movzwl -0x22(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x34(%rbp), %eax
cmpl %ecx, %eax
jle 0xd2c6
movzwl -0x2a(%rbp), %eax
andl $0x500, %eax            # imm = 0x500
cmpl $0x0, %eax
je 0xd2c6
jmp 0xd30d
movq -0x20(%rbp), %rax
movzwl -0x26(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
movzwl -0x2c(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x2c(%rbp)
movw -0x28(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x28(%rbp)
movzwl -0x14(%rbp), %ecx
movzwl -0x26(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x26(%rbp)
jmp 0xd27b
movl $0x800, %edi            # imm = 0x800
callq 0xc6b0
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x26(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movzwl -0x18(%rbp), %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x42c(%rax)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x16(%rbp), %edx
callq 0xc490
cmpl $0x0, %eax
jne 0xd36b
movl $0x0, -0x4(%rbp)
jmp 0xd546
movw -0x24(%rbp), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x42c(%rax)
movzwl -0x2c(%rbp), %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x432(%rax)
movzwl -0x2a(%rbp), %edi
callq 0xc6b0
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x26(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movw $0x5, -0x2e(%rbp)
movzwl -0x26(%rbp), %eax
cmpl $0x200, %eax            # imm = 0x200
jge 0xd4a4
movq -0x20(%rbp), %rax
movzwl -0x26(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %dx
movq -0x20(%rbp), %rax
movzwl -0x2e(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x20(%rbp), %rax
movzwl -0x2e(%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 0xd42f
movzwl -0x26(%rbp), %ecx
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movzwl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jge 0xd431
jmp 0xd4a4
movw $0x1, -0x30(%rbp)
movzwl -0x30(%rbp), %eax
movzwl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jge 0xd483
movq -0x20(%rbp), %rax
movzwl -0x26(%rbp), %ecx
movzwl -0x30(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %dx
movq -0x20(%rbp), %rax
movzwl -0x2e(%rbp), %ecx
movzwl -0x30(%rbp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movw -0x30(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x30(%rbp)
jmp 0xd437
movzwl -0x14(%rbp), %ecx
movzwl -0x2e(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x2e(%rbp)
movzwl -0x14(%rbp), %ecx
movzwl -0x26(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x26(%rbp)
jmp 0xd3b9
movl $0x800, %edi            # imm = 0x800
callq 0xc6b0
movw %ax, %dx
movq -0x20(%rbp), %rax
movzwl -0x2e(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw %dx, 0x42a(%rax,%rcx,2)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x18(%rbp), %edx
callq 0xc490
cmpl $0x0, %eax
jne 0xd4e8
movl $0x0, -0x4(%rbp)
jmp 0xd546
movq -0x10(%rbp), %rdi
movl $0x1, %edx
movzbl -0x11(%rbp), %esi
callq 0xc3e0
cmpl $0x0, %eax
jne 0xd508
movl $0x0, -0x4(%rbp)
jmp 0xd546
movzwl -0x18(%rbp), %edi
callq 0xc6b0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x42e(%rax)
movq -0x10(%rbp), %rdi
movl $0x1, %edx
movzbl -0x11(%rbp), %esi
callq 0xc490
cmpl $0x0, %eax
jne 0xd53f
movl $0x0, -0x4(%rbp)
jmp 0xd546
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop