// File: /workspace/github_c_makefile_star2_O2/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, %r14
callq 0x10c0
leaq 0x18(%rsp), %rbx
movl %eax, %edi
movq %rbx, %rsi
callq 0x1140
xorl %eax, %eax
movq 0x30(%rbx), %rbx
pushq $0x1
popq %r13
movl %eax, 0x10(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
movq %r14, %rdi
callq 0x1040
cmpq %rbx, %rax
je 0x1b5a
movq %rax, %rbp
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x1060
cmpq $0x1, %rax
jne 0x1b56
movzbl 0xf(%rsp), %r15d
movl %r15d, %eax
shll $0x18, %eax
movzbl 0xe(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzwl 0xc(%rsp), %r12d
orl %ecx, %r12d
cmpl $-0x2, %r12d
je 0x1a85
cmpl $-0x1, %r12d
je 0x1b5e
testl %r12d, %r12d
jne 0x1af7
testb $0x1, 0x14(%rsp)
movl $0x0, 0x14(%rsp)
jne 0x1a85
jmp 0x1b5e
testb $0x7f, %r15b
jne 0x1b56
movl %r12d, %esi
andl $0xffffff, %esi         # imm = 0xFFFFFF
incl %esi
andl $-0x2, %esi
leaq (%rsi,%rbp), %rax
addq $0x8, %rax
cmpq %rbx, %rax
ja 0x1b56
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x10d0
testl %eax, %eax
jne 0x1b56
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x1060
cmpq $0x1, %rax
jne 0x1b56
shrl $0x7, %r15d
movl 0x10(%rsp), %eax
addl %r15d, %eax
cmpl 0xc(%rsp), %r12d
je 0x1a7b
pushq $-0x2
jmp 0x1b72
xorl %eax, %eax
jmp 0x1b73
pushq $-0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x10d0
testl %eax, %eax
je 0x1b85
pushq $-0x5
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
negl 0x10(%rsp)
sbbl %eax, %eax
jmp 0x1b73