// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movq 0x3638(%rip), %rdi      # 0x5220
callq 0x1150
leaq 0x18(%rsp), %rbx
movl %eax, %edi
movq %rbx, %rsi
callq 0x11f0
movq 0x361d(%rip), %rdi      # 0x5220
callq 0x1070
movq 0x30(%rbx), %r13
cmpq %r13, %rax
jne 0x1c27
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x0, 0x14(%rsp)
movq 0x48(%rsp), %r12
movl $0x0, 0x10(%rsp)
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movq 0x35d5(%rip), %rcx      # 0x5220
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
callq 0x10a0
cmpq $0x1, %rax
jne 0x1c13
movzbl 0xf(%rsp), %r15d
movl %r15d, %eax
shll $0x18, %eax
movzbl 0xe(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0xd(%rsp), %eax
shll $0x8, %eax
movzbl 0xc(%rsp), %ebx
orl %ecx, %ebx
orl %eax, %ebx
cmpl $-0x2, %ebx
je 0x1d38
testl %ebx, %ebx
je 0x1d29
cmpl $-0x1, %ebx
je 0x1d58
testb $0x7f, %r15b
jne 0x1c13
movl %ebx, %esi
andl $0xffffff, %esi         # imm = 0xFFFFFF
incl %esi
andl $-0x2, %esi
leaq (%r14,%rsi), %rax
addq $0x8, %rax
cmpq %r13, %rax
ja 0x1c13
movq 0x354b(%rip), %rdi      # 0x5220
movl $0x1, %edx
callq 0x1170
testl %eax, %eax
jne 0x1c13
movq 0x3532(%rip), %rcx      # 0x5220
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
callq 0x10a0
cmpq $0x1, %rax
jne 0x1c13
cmpl 0xc(%rsp), %ebx
jne 0x1c13
shrl $0x7, %r15d
addl %r15d, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
jmp 0x1d38
cmpl $0x0, 0x10(%rsp)
jg 0x1d58
movl $0x1, 0x10(%rsp)
movq 0x34e1(%rip), %rdi      # 0x5220
callq 0x1070
movq %rax, %r14
movq %r12, %r13
cmpq %r12, %rax
jne 0x1c44
jmp 0x1c11
movq 0x34c1(%rip), %rdi      # 0x5220
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x1170
movl $0xfffffffb, %ebp       # imm = 0xFFFFFFFB
testl %eax, %eax
jne 0x1c13
xorl %ebp, %ebp
negl 0x14(%rsp)
sbbl %ebp, %ebp
jmp 0x1c13