// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/os8.c
// Function: os8ReadBytes
int os8ReadBytes(
  struct os8OpenFile *file,
  char *buf,
  int len
)
{
  struct mountedFS *mount = file->mount;
  int count = 0;
  uint16_t temp1, temp2;
  char mask = SWISSET('a') ? 0x7F : 0xFF;

  while (len) {
    switch (file->bytepos) {
      case OS8_CHECK:
        if (file->wordpos == OS8_BLOCKSIZE) {
          if (file->current == 0) {
            /*
             * For an empty file (does OS/8 support this?)
             * start == current == last == 0
             */
            if (file->start == 0)
              return 0;

            file->current = file->start;
          } else {
            if (file->current == file->last)
              return count;
            file->current++;
          }
          if (os8ReadBlock(mount, file->unit, file->current, file->buffer) == 0)
            return 0;
          file->wordpos = 0;
        }
        file->bytepos = OS8_BYTE0;
        /* FALLTHROUGH */

      case OS8_BYTE0:
        temp1 = os8Word(le16toh(file->buffer[file->wordpos]));
        *buf++ = temp1 & mask;
        file->bytepos = OS8_BYTE1;
        break;

      case OS8_BYTE1:
        temp1 = os8Word(le16toh(file->buffer[file->wordpos + 1]));
        *buf++ = temp1 & mask;
        file->bytepos = OS8_BYTE2;
        break;

      case OS8_BYTE2:
        temp1 = os8Word(le16toh(file->buffer[file->wordpos]));
        temp2 = os8Word(le16toh(file->buffer[file->wordpos + 1]));
        *buf++ = (((temp1 & 0xF00) >> 4) | ((temp2 & 0xF00) >> 8)) & mask;
        file->wordpos += 2;
        file->bytepos = OS8_CHECK;
        break;
    }
    len--;
    count++;
  }
  return count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq %rax, (%rsp)
leaq 0xab22(%rip), %rax      # 0x17fd8
testb $0x1, (%rax)
sete %bpl
shlb $0x7, %bpl
orb $0x7f, %bpl
xorl %r13d, %r13d
leaq 0x4291(%rip), %r12      # 0x11760
cmpl %r13d, %ebx
je 0xd5b9
movzbl 0x40(%r15), %eax
cmpq $0x3, %rax
ja 0xd5aa
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movzwl 0x3e(%r15), %eax
jmp 0xd591
movq 0x30(%r15), %rax
movzwl 0x3e(%r15), %ecx
movzwl (%rax,%rcx,2), %edx
movzbl 0x3(%rax,%rcx,2), %eax
shrl $0x4, %edx
andl $0xf0, %edx
andl $0xf, %eax
orl %edx, %eax
andb %bpl, %al
movb %al, (%r14)
addw $0x2, 0x3e(%r15)
movb $0x3, %al
jmp 0xd5a3
movzwl 0x3e(%r15), %eax
cmpw $0x100, %ax             # imm = 0x100
jne 0xd591
movzwl 0x3a(%r15), %eax
testw %ax, %ax
je 0xd561
cmpw 0x3c(%r15), %ax
je 0xd5b6
incl %eax
jmp 0xd56b
movq 0x30(%r15), %rax
movzwl 0x3e(%r15), %ecx
movb 0x2(%rax,%rcx,2), %al
andb %bpl, %al
movb %al, (%r14)
movb $0x2, %al
jmp 0xd5a3
movzwl 0x38(%r15), %eax
testw %ax, %ax
je 0xd5b2
movw %ax, 0x3a(%r15)
movzbl 0x28(%r15), %esi
movzwl %ax, %edx
movq 0x30(%r15), %rcx
movq (%rsp), %rdi
callq 0xc5d7
testl %eax, %eax
je 0xd5b2
andw $0x0, 0x3e(%r15)
xorl %eax, %eax
movq 0x30(%r15), %rcx
movzwl %ax, %eax
movb (%rcx,%rax,2), %al
andb %bpl, %al
movb %al, (%r14)
movb $0x1, %al
incq %r14
movb %al, 0x40(%r15)
incl %r13d
jmp 0xd4cf
xorl %ebx, %ebx
jmp 0xd5b9
movl %r13d, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq