// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movl $0x0, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movq 0x45f0(%rip), %rdi      # 0x6220
callq 0x1140
movl %eax, %edi
leaq -0xb0(%rbp), %rsi
callq 0x11e0
movq 0x45d6(%rip), %rdi      # 0x6220
callq 0x1060
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x80(%rbp), %rax
jne 0x1c69
movl $0x0, -0x4(%rbp)
jmp 0x1e13
leaq -0x10(%rbp), %rdi
movq 0x45ac(%rip), %rcx      # 0x6220
movl $0x4, %esi
movl $0x1, %edx
callq 0x1090
cmpq $0x1, %rax
je 0x1c95
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1e13
movl -0x10(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xb4(%rbp)
subl $-0x2, %eax
je 0x1d1a
jmp 0x1cab
movl -0xb4(%rbp), %eax
subl $-0x1, %eax
je 0x1cd9
jmp 0x1cb8
movl -0xb4(%rbp), %eax
testl %eax, %eax
jne 0x1d1f
jmp 0x1cc4
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
cmpl $0x1, %eax
jg 0x1cd7
jmp 0x1e0e
jmp 0x1cd9
movq 0x4540(%rip), %rdi      # 0x6220
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x1160
cmpl $0x0, %eax
je 0x1d02
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x1e13
movl -0x8(%rbp), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x1e13
jmp 0x1e0e
movl $0x0, -0xc(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x1d42
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
movl -0x18(%rbp), %eax
andl $0x7f000000, %eax       # imm = 0x7F000000
cmpl $0x0, %eax
je 0x1d5b
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1e13
movl -0x18(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
addl $0x1, %eax
andl $-0x2, %eax
movl %eax, -0x18(%rbp)
movq -0x20(%rbp), %rax
movl -0x18(%rbp), %ecx
addq %rcx, %rax
addq $0x8, %rax
cmpq -0x80(%rbp), %rax
jbe 0x1d8c
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1e13
movq 0x448d(%rip), %rdi      # 0x6220
movl -0x18(%rbp), %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x1160
cmpl $0x0, %eax
je 0x1db0
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1e13
leaq -0x10(%rbp), %rdi
movq 0x4465(%rip), %rcx      # 0x6220
movl $0x4, %esi
movl $0x1, %edx
callq 0x1090
cmpq $0x1, %rax
je 0x1dd9
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1e13
movzbl -0xd(%rbp), %eax
shll $0x18, %eax
movzbl -0xe(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0xf(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x10(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x18(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0x1e0c
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1e13
jmp 0x1e0e
jmp 0x1c43
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nop