// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/extracters/dbtap/tapeio.c
// Function: verifyFormat
static int verifyFormat(void)
{
  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(tfile), &stat);

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

    /*
     * 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, tfile) != 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(tfile, -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(tfile, bc, SEEK_CUR) != 0)
          return TIO_CORRUPT;
        if (fread(meta, sizeof(meta), 1, tfile) != 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 $0x98, %rsp
movq 0x3644(%rip), %rdi      # 0x5220
callq 0x1150
leaq 0x8(%rsp), %rbx
movl %eax, %edi
movq %rbx, %rsi
callq 0x11f0
movq 0x3629(%rip), %rdi      # 0x5220
callq 0x1070
movq 0x30(%rbx), %r12
cmpq %r12, %rax
jne 0x1c1b
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x0, 0x4(%rsp)
xorl %r15d, %r15d
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movq 0x35eb(%rip), %rcx      # 0x5220
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
callq 0x10a0
cmpq $0x1, %rax
jne 0x1c07
movzbl 0x3(%rsp), %r13d
movl %r13d, %eax
shll $0x18, %eax
movzbl 0x2(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzwl (%rsp), %ebx
orl %ecx, %ebx
cmpl $-0x2, %ebx
je 0x1d09
testl %ebx, %ebx
je 0x1cfe
cmpl $-0x1, %ebx
je 0x1d26
testb $0x7f, %r13b
jne 0x1c07
movl %ebx, %esi
andl $0xffffff, %esi         # imm = 0xFFFFFF
incl %esi
andl $-0x2, %esi
leaq (%r14,%rsi), %rax
addq $0x8, %rax
cmpq %r12, %rax
ja 0x1c07
movq 0x356e(%rip), %rdi      # 0x5220
movl $0x1, %edx
callq 0x1170
testl %eax, %eax
jne 0x1c07
movq 0x3555(%rip), %rcx      # 0x5220
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
callq 0x10a0
cmpq $0x1, %rax
jne 0x1c07
cmpl (%rsp), %ebx
jne 0x1c07
shrl $0x7, %r13d
addl %r13d, 0x4(%rsp)
xorl %r15d, %r15d
jmp 0x1d09
testl %r15d, %r15d
jg 0x1d26
movl $0x1, %r15d
movq 0x3510(%rip), %rdi      # 0x5220
callq 0x1070
movq %rax, %r14
cmpq %r12, %rax
jne 0x1c2e
jmp 0x1c05
movq 0x34f3(%rip), %rdi      # 0x5220
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x1170
movl $0xfffffffb, %ebp       # imm = 0xFFFFFFFB
testl %eax, %eax
jne 0x1c07
xorl %ebp, %ebp
negl 0x4(%rsp)
sbbl %ebp, %ebp
jmp 0x1c07