// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/tape.c
// Function: tapeVerify
int tapeVerify(
  FILE *container,
  off_t *eot
)
{
  int errorCount = 0, tmSeen = 0;
  uint32_t meta, header, bc;
  off_t position;
  struct stat stat;

  /*
   * Determine the size of the file.
   */
  fstat(fileno(container), &stat);

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

    /*
     * If we are positioned at the end-of-file, there is a tape mark or
     * end-of-media marker missing. Treat it as though one is present.
     */
    if (position == stat.st_size)
      break;

    if (fread(&meta, sizeof(meta), 1, container) != 1)
      return 0;

    bc = le32toh(meta);

    switch (bc) {
      case ST_TM:
        if (++tmSeen <= 1)
          break;
        /* Treat second TM in a row as end of medium */
        /* FALLTHROUGH */

      case ST_EOM:
        if (fseeko(container, -sizeof(meta), SEEK_CUR) != 0)
          return 0;

        if (errorCount)
          printf("mount: Tape contains error records\n");
        goto done;
        
      case ST_GAP:
        break;

      default:
        /*
         * Record descriptor
         */
        tmSeen = 0;

        header = bc;
        if ((bc & ST_ERROR) != 0)
          errorCount++;
        if ((bc & ST_MBZ) != 0)
          return 0;

        bc = RECLEN(bc & ST_LENGTH);

        /*
         * Check if we are seeking ouside 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 0;

        if (fseeko(container, bc, SEEK_CUR) != 0)
          return 0;
        if (fread(&meta, sizeof(meta), 1, container) != 1)
          return 0;

        bc = le32toh(meta);
        
        if (header != bc)
          return 0;
    }
  }
 done:
  if (eot != NULL)
    *eot = ftello(container);

  /*
   * Position at beginning-of-tape.
   */
  if (fseeko(container, 0, SEEK_SET) == 0)
    return 1;
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
callq 0x3210
leaq 0x18(%rsp), %r15
movl %eax, %edi
movq %r15, %rsi
callq 0x32f0
movq %rbx, %rdi
callq 0x30d0
movq 0x30(%r15), %r13
cmpq %r13, %rax
jne 0x6843
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x682b
movq %rbx, %rdi
callq 0x30d0
movq %rax, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3270
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
jmp 0x691b
movq %rax, %r12
leaq 0xc(%rsp), %r15
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x30f0
cmpq $0x1, %rax
jne 0x6919
movl 0xc(%rsp), %r14d
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %r14
je 0x6900
testl %r14d, %r14d
je 0x68f7
cmpl $-0x1, %r14d
je 0x692f
testl $0x7f000000, %r14d      # imm = 0x7F000000
jne 0x6919
leal 0x1(%r14), %esi
andl $0x1fffffe, %esi        # imm = 0x1FFFFFE
leaq (%r12,%rsi), %rax
addq $0x8, %rax
cmpq %r13, %rax
ja 0x6919
movq %rbx, %rdi
movl $0x1, %edx
callq 0x3270
testl %eax, %eax
jne 0x6919
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x30f0
xorl %ebp, %ebp
cmpq $0x1, %rax
jne 0x6919
cmpl 0xc(%rsp), %r14d
jne 0x6919
shrl $0x1f, %r14d
addl 0x8(%rsp), %r14d
movl %r14d, 0x8(%rsp)
jmp 0x6900
testl %ebp, %ebp
jg 0x692f
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x30d0
movq %rax, %r12
cmpq %r13, %rax
jne 0x6855
jmp 0x6816
xorl %ecx, %ecx
movl %ecx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x3270
testl %eax, %eax
movl $0x0, %ecx
jne 0x691b
cmpl $0x0, 0x8(%rsp)
je 0x6816
leaq 0xaece(%rip), %rdi      # 0x1182c
callq 0x30e0
jmp 0x6816