// File: /workspace/github_c_makefile_star2_O1/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 0x30(%r14), %r13
cmpq %r13, %rax
jne 0x1b86
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movl $0x0, 0xc(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %rbx, %rcx
callq 0x1060
cmpq $0x1, %rax
jne 0x1b72
movzbl 0x7(%rsp), %r14d
movl %r14d, %eax
shll $0x18, %eax
movzbl 0x6(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0x5(%rsp), %eax
shll $0x8, %eax
movzbl 0x4(%rsp), %r12d
orl %ecx, %r12d
orl %eax, %r12d
cmpl $-0x2, %r12d
je 0x1c98
testl %r12d, %r12d
je 0x1c89
cmpl $-0x1, %r12d
je 0x1cb9
testb $0x7f, %r14b
jne 0x1b72
movl %r12d, %esi
andl $0xffffff, %esi         # imm = 0xFFFFFF
incl %esi
andl $-0x2, %esi
leaq (%r15,%rsi), %rax
addq $0x8, %rax
cmpq %r13, %rax
ja 0x1b72
movq %rbx, %rdi
movl $0x1, %edx
callq 0x10d0
testl %eax, %eax
jne 0x1b72
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %rbx, %rcx
callq 0x1060
cmpq $0x1, %rax
jne 0x1b72
cmpl 0x4(%rsp), %r12d
jne 0x1b72
shrl $0x7, %r14d
addl %r14d, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
jmp 0x1c98
cmpl $0x0, 0x8(%rsp)
jg 0x1cb9
movl $0x1, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1040
movq %rax, %r15
movq 0x10(%rsp), %rax
movq %rax, %r13
cmpq %rax, %r15
jne 0x1ba8
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