// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11BestFit
static int rt11BestFit(
  struct mountedFS *mount,
  uint8_t unit,
  uint16_t rqsize,
  uint16_t *start,
  uint16_t *segment,
  uint16_t *offset,
  uint16_t *next
)
{
  struct RT11data *data = &mount->rt11data;
  uint16_t bestsize, beststart, bestsegment, bestoffset, dsseg = 1;

  bestsize = rqsize == 0 ? 0 : 0177777;
  bestsegment = 0;

  do {
    uint16_t entrysz, startblk, off = RT11_DH_SIZE;

    if (rt11ReadDirSegment(mount, unit, dsseg) == 0)
      return 0;

    /*
     * Return the next available directory segment or 0 if no more are
     * available.
     */
    if ((next != NULL) && (dsseg == 1)) {
      if (data->buf[RT11_DH_COUNT] != data->buf[RT11_DH_HIGHEST])
        *next = le16toh(data->buf[RT11_DH_HIGHEST]) + 1;
      else *next = 0;
    }

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

    /*
     * Loop until we see and end-of-segment marker or there is no room for
     * another directory entry.
     */
    while (!RT11EOS(le16toh(data->buf[off + RT11_DI_STATUS])) &&
           ((RT11_DS_SIZE - off) >= entrysz)) {
      uint16_t status = le16toh(data->buf[off + RT11_DI_STATUS]);
      uint16_t length = le16toh(data->buf[off + RT11_DI_LENGTH]);

      if ((status & RT11_E_MPTY) != 0) {
        /*
         * Check for largest or best fit
         */
        if (((rqsize == 0) && (length > bestsize)) ||
            ((rqsize != 0) &&
             ((length >= rqsize) && (length < bestsize)))) {
          bestsize = length;
          beststart = startblk;
          bestsegment = dsseg;
          bestoffset = off;
        }
      }
      startblk += length;
      off += entrysz;
    }
    dsseg = le16toh(data->buf[RT11_DH_NEXT]);
  } while (dsseg != 0);

  if (bestsegment != 0) {
    *start = beststart;
    *segment = bestsegment;
    *offset = bestoffset;
    return 1;
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movw %dx, %ax
movb %sil, %dl
movq 0x10(%rbp), %rsi
movq %rdi, -0x10(%rbp)
movb %dl, -0x11(%rbp)
movw %ax, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x38(%rbp)
movw $0x1, -0x42(%rbp)
movzwl -0x14(%rbp), %edx
movl $0xffff, %eax           # imm = 0xFFFF
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movw %ax, -0x3a(%rbp)
movw $0x0, -0x3e(%rbp)
movw $0x5, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x42(%rbp), %edx
callq 0xc3e0
cmpl $0x0, %eax
jne 0xd000
movl $0x0, -0x4(%rbp)
jmp 0xd1f8
cmpq $0x0, 0x10(%rbp)
je 0xd057
movzwl -0x42(%rbp), %eax
cmpl $0x1, %eax
jne 0xd057
movq -0x38(%rbp), %rax
movzwl 0x42a(%rax), %eax
movq -0x38(%rbp), %rcx
movzwl 0x42e(%rcx), %ecx
cmpl %ecx, %eax
je 0xd04c
movq -0x38(%rbp), %rax
movzwl 0x42e(%rax), %edi
callq 0xc6b0
movzwl %ax, %eax
addl $0x1, %eax
movw %ax, %cx
movq 0x10(%rbp), %rax
movw %cx, (%rax)
jmp 0xd055
movq 0x10(%rbp), %rax
movw $0x0, (%rax)
jmp 0xd057
movq -0x38(%rbp), %rax
movzwl 0x430(%rax), %edi
callq 0xc6b0
movzwl %ax, %eax
sarl %eax
addl $0x7, %eax
movw %ax, -0x44(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x432(%rax), %edi
callq 0xc6b0
movw %ax, -0x46(%rbp)
movq -0x38(%rbp), %rax
movzwl -0x48(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
andl $0x800, %ecx            # imm = 0x800
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x4d(%rbp)
jne 0xd0cc
movzwl -0x48(%rbp), %ecx
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movzwl -0x44(%rbp), %ecx
cmpl %ecx, %eax
setge %al
movb %al, -0x4d(%rbp)
movb -0x4d(%rbp), %al
testb $0x1, %al
jne 0xd0d8
jmp 0xd19d
movq -0x38(%rbp), %rax
movzwl -0x48(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movw %ax, -0x4a(%rbp)
movq -0x38(%rbp), %rax
movzwl -0x48(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movw %ax, -0x4c(%rbp)
movzwl -0x4a(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xd17c
movzwl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0xd139
movzwl -0x4c(%rbp), %eax
movzwl -0x3a(%rbp), %ecx
cmpl %ecx, %eax
jg 0xd15a
movzwl -0x14(%rbp), %eax
cmpl $0x0, %eax
je 0xd17a
movzwl -0x4c(%rbp), %eax
movzwl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jl 0xd17a
movzwl -0x4c(%rbp), %eax
movzwl -0x3a(%rbp), %ecx
cmpl %ecx, %eax
jge 0xd17a
movw -0x4c(%rbp), %ax
movw %ax, -0x3a(%rbp)
movw -0x46(%rbp), %ax
movw %ax, -0x3c(%rbp)
movw -0x42(%rbp), %ax
movw %ax, -0x3e(%rbp)
movw -0x48(%rbp), %ax
movw %ax, -0x40(%rbp)
jmp 0xd17c
movzwl -0x4c(%rbp), %ecx
movzwl -0x46(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x46(%rbp)
movzwl -0x44(%rbp), %ecx
movzwl -0x48(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x48(%rbp)
jmp 0xd087
movq -0x38(%rbp), %rax
movzwl 0x42c(%rax), %edi
callq 0xc6b0
movw %ax, -0x42(%rbp)
movzwl -0x42(%rbp), %eax
cmpl $0x0, %eax
jne 0xcfd6
movzwl -0x3e(%rbp), %eax
cmpl $0x0, %eax
je 0xd1f1
movw -0x3c(%rbp), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movw -0x3e(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
movw -0x40(%rbp), %cx
movq -0x30(%rbp), %rax
movw %cx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0xd1f8
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)