// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/os8.c
// Function: os8FindSpace
static int os8FindSpace(
  struct mountedFS *mount,
  uint8_t unit,
  uint16_t space,
  uint16_t *segment,
  uint16_t *offset,
  uint16_t *start
)
{
  struct OS8data *data = &mount->os8data;
  int found = 0;
  uint16_t dirseg, diroff, dirstart, size = space == 0 ? 0 : 0xFFFF;
  uint16_t dirblk = OS8_DSSTART;

  if (os8UnitValid(data, unit)) {
    do {
      uint16_t i, entrysz, entries, extra, startblk, off = OS8_DH_SIZE;

      if (os8ReadBlock(mount, unit, dirblk, NULL) == 0)
        return 0;

      entrysz = OS8_DI_SIZE + (-os8SXT(le16toh(data->buf[OS8_DH_EXTRA])));
      entries = os8Neg(le16toh(data->buf[OS8_DH_ENTRIES]));
      extra = os8Neg(le16toh(data->buf[OS8_DH_EXTRA]));

      startblk = os8Word(le16toh(data->buf[OS8_DH_START]));

      for (i = 0; i < entries; i++) {
        uint16_t blks;

        if (os8Word(le16toh(data->buf[off + OS8_DI_FNAME1])) == 0) {
          blks = os8Neg(le16toh(data->buf[off + OS8_ED_LENGTH]));

          if (space == 0) {
            if (blks > size) {
              size = blks;
              dirseg = dirblk;
              diroff = off;
              dirstart = startblk;
              found = 1;
            }
          } else {
              if (blks >= space)
                if (blks < size) {
                  size = blks;
                  dirseg = dirblk;
                  diroff = off;
                  dirstart = startblk;
                  found = 1;
                }
          }
          off += OS8_ED_SIZE;
        } else {
          blks = os8Neg(le16toh(data->buf[off + extra + OS8_DI_LENGTH]));
          off += entrysz;
        }
        startblk += blks;
      }

      dirblk = os8Word(le16toh(data->buf[OS8_DH_NEXT]));
    } while (dirblk != 0);

    /*
     * If we found a suitable entry, load the directory segment and update
     * return information.
     */
    if (found) {
      if (os8ReadBlock(mount, unit, dirseg, NULL) == 0)
        return 0;

      *segment = dirseg;
      *offset = diroff;
      *start = dirstart;
    }
  }
  return found;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movw %dx, %ax
movb %sil, %dl
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)
movl $0x0, -0x3c(%rbp)
movzwl -0x14(%rbp), %edx
movl $0xffff, %eax           # imm = 0xFFFF
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movw %ax, -0x44(%rbp)
movw $0x1, -0x46(%rbp)
movq -0x38(%rbp), %rdi
movzbl -0x11(%rbp), %esi
callq 0x14f20
cmpl $0x0, %eax
je 0x14092
jmp 0x13e3b
movw $0x5, -0x52(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzwl -0x46(%rbp), %edx
xorl %ecx, %ecx
movzbl %al, %esi
callq 0x132f0
cmpl $0x0, %eax
jne 0x13e67
movl $0x0, -0x4(%rbp)
jmp 0x14098
movq -0x38(%rbp), %rax
movzwl 0x1c(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x148b0
movzwl %ax, %ecx
xorl %eax, %eax
subl %ecx, %eax
addl $0x5, %eax
movw %ax, -0x4a(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x14(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, -0x4c(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x1c(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, -0x4e(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x16(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movw %ax, -0x50(%rbp)
movw $0x0, -0x48(%rbp)
movzwl -0x48(%rbp), %eax
movzwl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x14020
movq -0x38(%rbp), %rax
movzwl -0x52(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x13fc9
movq -0x38(%rbp), %rax
movzwl -0x52(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, -0x54(%rbp)
movzwl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0x13f79
movzwl -0x54(%rbp), %eax
movzwl -0x44(%rbp), %ecx
cmpl %ecx, %eax
jle 0x13f77
movw -0x54(%rbp), %ax
movw %ax, -0x44(%rbp)
movw -0x46(%rbp), %ax
movw %ax, -0x3e(%rbp)
movw -0x52(%rbp), %ax
movw %ax, -0x40(%rbp)
movw -0x50(%rbp), %ax
movw %ax, -0x42(%rbp)
movl $0x1, -0x3c(%rbp)
jmp 0x13fbc
movzwl -0x54(%rbp), %eax
movzwl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jl 0x13fba
movzwl -0x54(%rbp), %eax
movzwl -0x44(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13fb8
movw -0x54(%rbp), %ax
movw %ax, -0x44(%rbp)
movw -0x46(%rbp), %ax
movw %ax, -0x3e(%rbp)
movw -0x52(%rbp), %ax
movw %ax, -0x40(%rbp)
movw -0x50(%rbp), %ax
movw %ax, -0x42(%rbp)
movl $0x1, -0x3c(%rbp)
jmp 0x13fba
jmp 0x13fbc
movzwl -0x52(%rbp), %eax
addl $0x2, %eax
movw %ax, -0x52(%rbp)
jmp 0x14001
movq -0x38(%rbp), %rax
movzwl -0x52(%rbp), %ecx
movzwl -0x4e(%rbp), %edx
addl %edx, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, -0x54(%rbp)
movzwl -0x4a(%rbp), %ecx
movzwl -0x52(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x52(%rbp)
movzwl -0x54(%rbp), %ecx
movzwl -0x50(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x50(%rbp)
movw -0x48(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x48(%rbp)
jmp 0x13edb
movq -0x38(%rbp), %rax
movzwl 0x18(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movw %ax, -0x46(%rbp)
movzwl -0x46(%rbp), %eax
cmpl $0x0, %eax
jne 0x13e3b
cmpl $0x0, -0x3c(%rbp)
je 0x14090
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzwl -0x3e(%rbp), %edx
xorl %ecx, %ecx
movzbl %al, %esi
callq 0x132f0
cmpl $0x0, %eax
jne 0x1406f
movl $0x0, -0x4(%rbp)
jmp 0x14098
movw -0x3e(%rbp), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movw -0x40(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
movw -0x42(%rbp), %cx
movq -0x30(%rbp), %rax
movw %cx, (%rax)
jmp 0x14092
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)