// File: /workspace/github_c_makefile_star2_O3/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
leaq 0xaa22(%rip), %rax      # 0x18fd8
testb $0x1, (%rax)
sete %r13b
testl %edx, %edx
je 0xe6c7
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq %rax, (%rsp)
shlb $0x7, %r13b
orb $0x7f, %r13b
movb 0x40(%rdi), %al
xorl %ebp, %ebp
leaq 0x4197(%rip), %r12      # 0x12780
cmpb $0x3, %al
ja 0xe6b9
movzbl %al, %eax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movzwl 0x3e(%r15), %eax
jmp 0xe6a0
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 %r13b, %al
movb %al, (%r14)
addw $0x2, 0x3e(%r15)
movb $0x3, %al
jmp 0xe6b2
movzwl 0x3e(%r15), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0xe6a0
movzwl 0x3a(%r15), %eax
testw %ax, %ax
je 0xe66f
cmpw 0x3c(%r15), %ax
je 0xe6c9
incl %eax
jmp 0xe679
movq 0x30(%r15), %rax
movzwl 0x3e(%r15), %ecx
movb 0x2(%rax,%rcx,2), %al
andb %r13b, %al
movb %al, (%r14)
movb $0x2, %al
jmp 0xe6b2
movzwl 0x38(%r15), %eax
testw %ax, %ax
je 0xe6c7
movw %ax, 0x3a(%r15)
movzbl 0x28(%r15), %esi
movzwl %ax, %edx
movq 0x30(%r15), %rcx
movq (%rsp), %rdi
callq 0xd707
testl %eax, %eax
je 0xe6c7
movw $0x0, 0x3e(%r15)
xorl %eax, %eax
movq 0x30(%r15), %rcx
movzwl %ax, %eax
movb (%rcx,%rax,2), %al
andb %r13b, %al
movb %al, (%r14)
movb $0x1, %al
incq %r14
movb %al, 0x40(%r15)
incl %ebp
cmpl %ebp, %ebx
jne 0xe5e9
movl %ebx, %ebp
jmp 0xe6c9
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq