// File: /workspace/github_c_makefile_star2_O2/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 0x388d(%rip), %rdi      # 0x5220
callq 0x1150
leaq 0x18(%rsp), %rbx
movl %eax, %edi
movq %rbx, %rsi
callq 0x11f0
xorl %eax, %eax
movq 0x30(%rbx), %rbx
pushq $0x4
popq %r15
pushq $0x1
popq %r12
movl %eax, 0x10(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
movq 0x385a(%rip), %rdi      # 0x5220
callq 0x1070
cmpq %rbx, %rax
je 0x1aa4
movq %rax, %r13
movq 0x3842(%rip), %rcx      # 0x5220
leaq 0xc(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1aa0
movzbl 0xf(%rsp), %ebp
movl %ebp, %eax
shll $0x18, %eax
movzbl 0xe(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzwl 0xc(%rsp), %r14d
orl %ecx, %r14d
cmpl $-0x2, %r14d
je 0x19bf
cmpl $-0x1, %r14d
je 0x1aa8
testl %r14d, %r14d
jne 0x1a3b
testb $0x1, 0x14(%rsp)
movl $0x0, 0x14(%rsp)
jne 0x19bf
jmp 0x1aa8
testb $0x7f, %bpl
jne 0x1aa0
movl %r14d, %esi
andl $0xffffff, %esi         # imm = 0xFFFFFF
incl %esi
andl $-0x2, %esi
leaq (%rsi,%r13), %rax
addq $0x8, %rax
cmpq %rbx, %rax
ja 0x1aa0
movq 0x37bd(%rip), %rdi      # 0x5220
pushq $0x1
popq %rdx
callq 0x1170
testl %eax, %eax
jne 0x1aa0
movq 0x37aa(%rip), %rcx      # 0x5220
leaq 0xc(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1aa0
shrl $0x7, %ebp
movl 0x10(%rsp), %eax
addl %ebp, %eax
cmpl 0xc(%rsp), %r14d
je 0x19b5
pushq $-0x2
jmp 0x1ac0
xorl %eax, %eax
jmp 0x1ac1
movq 0x3771(%rip), %rdi      # 0x5220
pushq $-0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x1170
testl %eax, %eax
je 0x1ad3
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 0x1ac1