// File: /workspace/github_c_makefile_star2_O1/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 0xa724(%rip), %rax      # 0x18fc8
testb $0x1, (%rax)
sete %r13b
testl %edx, %edx
je 0xe9b1
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq %rax, (%rsp)
shlb $0x7, %r13b
orb $0x7f, %r13b
xorl %ebp, %ebp
leaq 0x3e8c(%rip), %r12      # 0x12760
movzbl 0x40(%r15), %eax
cmpq $0x3, %rax
ja 0xe9a3
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0x30(%r15), %rax
movzwl 0x3e(%r15), %ecx
movb 0x2(%rax,%rcx,2), %al
andb %r13b, %al
movb %al, (%r14)
movb $0x2, %al
jmp 0xe99c
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 0xe99c
cmpw $0x100, 0x3e(%r15)      # imm = 0x100
jne 0xe983
movzwl 0x3a(%r15), %eax
testw %ax, %ax
je 0xe954
cmpw 0x3c(%r15), %ax
je 0xe9b3
incl %eax
jmp 0xe95e
movzwl 0x38(%r15), %eax
testw %ax, %ax
je 0xe9b1
movw %ax, 0x3a(%r15)
movzbl 0x28(%r15), %esi
movzwl %ax, %edx
movq 0x30(%r15), %rcx
movq (%rsp), %rdi
callq 0xd979
testl %eax, %eax
je 0xe9b1
movw $0x0, 0x3e(%r15)
movb $0x0, 0x40(%r15)
movq 0x30(%r15), %rax
movzwl 0x3e(%r15), %ecx
movb (%rax,%rcx,2), %al
andb %r13b, %al
movb %al, (%r14)
movb $0x1, %al
incq %r14
movb %al, 0x40(%r15)
incl %ebp
cmpl %ebp, %ebx
jne 0xe8d4
movl %ebx, %ebp
jmp 0xe9b3
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq