// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10c0
movl %eax, %edi
leaq -0xc0(%rbp), %rsi
callq 0x1140
movq -0x10(%rbp), %rdi
callq 0x1040
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x90(%rbp), %rax
jne 0x1dda
movl $0x0, -0x4(%rbp)
jmp 0x1f7b
leaq -0x1c(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1060
cmpq $0x1, %rax
je 0x1e03
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1f7b
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0xc4(%rbp)
subl $-0x2, %eax
je 0x1e85
jmp 0x1e19
movl -0xc4(%rbp), %eax
subl $-0x1, %eax
je 0x1e47
jmp 0x1e26
movl -0xc4(%rbp), %eax
testl %eax, %eax
jne 0x1e8a
jmp 0x1e32
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x1, %eax
jg 0x1e45
jmp 0x1f76
jmp 0x1e47
movq -0x10(%rbp), %rdi
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x10d0
cmpl $0x0, %eax
je 0x1e6d
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x1f7b
movl -0x14(%rbp), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x1f7b
jmp 0x1f76
movl $0x0, -0x18(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x24(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x1ead
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x24(%rbp), %eax
andl $0x7f000000, %eax       # imm = 0x7F000000
cmpl $0x0, %eax
je 0x1ec6
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1f7b
movl -0x24(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
addl $0x1, %eax
andl $-0x2, %eax
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rax
movl -0x24(%rbp), %ecx
addq %rcx, %rax
addq $0x8, %rax
cmpq -0x90(%rbp), %rax
jbe 0x1efa
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1f7b
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x10d0
cmpl $0x0, %eax
je 0x1f1b
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1f7b
leaq -0x1c(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1060
cmpq $0x1, %rax
je 0x1f41
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1f7b
movzbl -0x19(%rbp), %eax
shll $0x18, %eax
movzbl -0x1a(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x1b(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x1c(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x1f74
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x1f7b
jmp 0x1f76
jmp 0x1db4
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)