// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/extracters/cpytap/tapeio.c
// Function: verifyFormat
static int verifyFormat(
  FILE *handle
)
{
  int errorCount = 0, tmSeen = 0;
  uint8 meta[4];
  uint32 header, bc;
  off_t position;
  struct stat stat;

  /*
   * Determine the size of the file.
   */
  fstat(fileno(handle), &stat);

  for (;;) {
    position = ftello(handle);

    /*
     * If we are position at the end of file, there is a tape mark missing.
     * Treat it as though there is one present.
     */
    if (position == stat.st_size)
      return TIO_SUCCESS;

    if (fread(meta, sizeof(meta), 1, handle) != 1)
      return TIO_CORRUPT;

    bc = (((unsigned int)meta[3]) << 24) |
         (((unsigned int)meta[2]) << 16) |
         (((unsigned int)meta[1]) << 8) |
         (unsigned int)meta[0];

    switch (bc) {
      case ST_TM:
        if (++tmSeen <= 1)
          break;
        /* Treat second TM in a row as end of medium */
        /* FALLTHROUGH */

      case ST_EOM:
        if (fseek(handle, -sizeof(meta), SEEK_CUR) != 0)
          return TIO_IOERROR;
        return errorCount ? TIO_ERROR : TIO_SUCCESS;

      case ST_GAP:
        break;

      default:
        /*
         * Record descriptor
         */
        tmSeen = 0;

        header = bc;
        if ((bc & ST_ERROR) != 0)
          errorCount++;
        if ((bc & ST_MBZ) != 0)
          return TIO_CORRUPT;

        bc = RECLEN(bc & ST_LENGTH);

        /*
         * Check if we are seeking outside of the file. If so, this is not
         * a .tap container file.
         */
        if ((position + bc + (2 * sizeof(meta))) > (unsigned long long)stat.st_size)
          return TIO_CORRUPT;

        if (fseek(handle, bc, SEEK_CUR) != 0)
          return TIO_CORRUPT;
        if (fread(meta, sizeof(meta), 1, handle) != 1)
          return TIO_CORRUPT;

        bc = (((unsigned int)meta[3]) << 24) |
             (((unsigned int)meta[2]) << 16) |
             (((unsigned int)meta[1]) << 8) |
             (unsigned int)meta[0];
        if (header != bc)
          return TIO_CORRUPT;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
callq 0x10c0
leaq 0x18(%rsp), %r14
movl %eax, %edi
movq %r14, %rsi
callq 0x1140
movq %rbx, %rdi
callq 0x1040
movq %rax, %r15
movq 0x30(%r14), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r15
jne 0x1b86
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0xc(%rsp)
xorl %r14d, %r14d
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
movq %rbx, %rcx
callq 0x1060
cmpq $0x1, %rax
jne 0x1b72
movzbl 0xb(%rsp), %r12d
movl %r12d, %eax
shll $0x18, %eax
movzbl 0xa(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzwl 0x8(%rsp), %r13d
orl %ecx, %r13d
cmpl $-0x2, %r13d
je 0x1c74
testl %r13d, %r13d
je 0x1c69
cmpl $-0x1, %r13d
je 0x1c8f
testb $0x7f, %r12b
jne 0x1b72
movl %r13d, %esi
andl $0xffffff, %esi         # imm = 0xFFFFFF
incl %esi
andl $-0x2, %esi
leaq (%r15,%rsi), %rax
addq $0x8, %rax
cmpq 0x10(%rsp), %rax
ja 0x1b72
movq %rbx, %rdi
movl $0x1, %edx
callq 0x10d0
testl %eax, %eax
jne 0x1b72
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
movq %rbx, %rcx
callq 0x1060
cmpq $0x1, %rax
jne 0x1b72
cmpl 0x8(%rsp), %r13d
jne 0x1b72
shrl $0x7, %r12d
addl %r12d, 0xc(%rsp)
xorl %r14d, %r14d
jmp 0x1c74
testl %r14d, %r14d
jg 0x1c8f
movl $0x1, %r14d
movq %rbx, %rdi
callq 0x1040
movq %rax, %r15
cmpq 0x10(%rsp), %rax
jne 0x1b96
jmp 0x1b70
movq %rbx, %rdi
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x10d0
movl $0xfffffffb, %ebp       # imm = 0xFFFFFFFB
testl %eax, %eax
jne 0x1b72
xorl %ebp, %ebp
negl 0xc(%rsp)
sbbl %ebp, %ebp
jmp 0x1b72