// File: /workspace/github_c_makefile_star2_O1/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 0x3200
leaq 0x18(%rsp), %r15
movl %eax, %edi
movq %r15, %rsi
callq 0x32e0
movq %rbx, %rdi
callq 0x30d0
movq 0x30(%r15), %r14
cmpq %r14, %rax
jne 0x689b
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x6883
movq %rbx, %rdi
callq 0x30d0
movq %rax, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3260
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
jmp 0x6982
movq %rax, %r12
movq 0x48(%rsp), %r13
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %rbx, %rcx
callq 0x30f0
cmpq $0x1, %rax
jne 0x6980
movl 0xc(%rsp), %r15d
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %r15
je 0x6964
testl %r15d, %r15d
je 0x695b
cmpl $-0x1, %r15d
je 0x6996
testl $0x7f000000, %r15d      # imm = 0x7F000000
jne 0x6980
leal 0x1(%r15), %esi
andl $0x1fffffe, %esi        # imm = 0x1FFFFFE
leaq (%r12,%rsi), %rax
addq $0x8, %rax
cmpq %r14, %rax
ja 0x6980
movq %rbx, %rdi
movl $0x1, %edx
callq 0x3260
testl %eax, %eax
jne 0x6980
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %rbx, %rcx
callq 0x30f0
xorl %ebp, %ebp
cmpq $0x1, %rax
jne 0x6980
cmpl 0xc(%rsp), %r15d
jne 0x6980
shrl $0x1f, %r15d
addl 0x8(%rsp), %r15d
movl %r15d, 0x8(%rsp)
jmp 0x6964
testl %ebp, %ebp
jg 0x6996
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x30d0
movq %rax, %r12
movq %r13, %r14
cmpq %r13, %rax
jne 0x68ad
jmp 0x686e
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 0x3260
testl %eax, %eax
movl $0x0, %ecx
jne 0x6982
cmpl $0x0, 0x8(%rsp)
je 0x686e
leaq 0xae67(%rip), %rdi      # 0x1182c
callq 0x30e0
jmp 0x686e