// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
leaq 0x9f6b(%rip), %rax      # 0x1f1f8
movl (%rax), %edx
andl $0x1, %edx
movl $0xff, %eax
movl $0x7f, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, -0x31(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x154ef
movq -0x10(%rbp), %rax
movzbl 0x40(%rax), %eax
movq %rax, -0x40(%rbp)
subq $0x3, %rax
ja 0x154d8
movq -0x40(%rbp), %rax
leaq 0x54e0(%rip), %rcx      # 0x1a7b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movzwl 0x3e(%rax), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x15391
movq -0x10(%rbp), %rax
movzwl 0x3a(%rax), %eax
cmpl $0x0, %eax
jne 0x15324
movq -0x10(%rbp), %rax
movzwl 0x38(%rax), %eax
cmpl $0x0, %eax
jne 0x15312
movl $0x0, -0x4(%rbp)
jmp 0x154f5
movq -0x10(%rbp), %rax
movw 0x38(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x3a(%rax)
jmp 0x15353
movq -0x10(%rbp), %rax
movzwl 0x3a(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x3c(%rcx), %ecx
cmpl %ecx, %eax
jne 0x15343
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x154f5
movq -0x10(%rbp), %rax
movw 0x3a(%rax), %cx
addw $0x1, %cx
movw %cx, 0x3a(%rax)
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movb 0x28(%rax), %al
movq -0x10(%rbp), %rcx
movzwl 0x3a(%rcx), %edx
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movzbl %al, %esi
callq 0x132f0
cmpl $0x0, %eax
jne 0x15387
movl $0x0, -0x4(%rbp)
jmp 0x154f5
movq -0x10(%rbp), %rax
movw $0x0, 0x3e(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
movzwl (%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movw %ax, -0x2e(%rbp)
movzwl -0x2e(%rbp), %eax
movsbl -0x31(%rbp), %ecx
andl %ecx, %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x40(%rax)
jmp 0x154d8
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movw %ax, -0x2e(%rbp)
movzwl -0x2e(%rbp), %eax
movsbl -0x31(%rbp), %ecx
andl %ecx, %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movb $0x2, 0x40(%rax)
jmp 0x154d8
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
movzwl (%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movw %ax, -0x2e(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movw %ax, -0x30(%rbp)
movzwl -0x2e(%rbp), %eax
andl $0xf00, %eax            # imm = 0xF00
sarl $0x4, %eax
movzwl -0x30(%rbp), %ecx
andl $0xf00, %ecx            # imm = 0xF00
sarl $0x8, %ecx
orl %ecx, %eax
movsbl -0x31(%rbp), %ecx
andl %ecx, %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
movq -0x10(%rbp), %rax
movzwl 0x3e(%rax), %ecx
addl $0x2, %ecx
movw %cx, 0x3e(%rax)
movq -0x10(%rbp), %rax
movb $0x3, 0x40(%rax)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x152a5
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop