// File: /workspace/github_c_makefile_star2_O2/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), %r14
movl %eax, %edi
movq %r14, %rsi
callq 0x32f0
movl $0x0, 0x8(%rsp)
movq 0x30(%r14), %r15
pushq $0x1
popq %r13
xorl %ebp, %ebp
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
callq 0x30d0
cmpq %r15, %rax
je 0x53a2
movq %rax, %r14
leaq 0x4(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x30f0
cmpq $0x1, %rax
jne 0x53cb
movl 0x4(%rsp), %r12d
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %r12
je 0x52b9
cmpl $-0x1, %r12d
je 0x537d
testl %r12d, %r12d
jne 0x5315
testb $0x1, 0xc(%rsp)
movl $0x0, 0xc(%rsp)
jne 0x52b9
jmp 0x537d
testl $0x7f000000, %r12d      # imm = 0x7F000000
jne 0x53cb
leal 0x1(%r12), %esi
andl $0x1fffffe, %esi        # imm = 0x1FFFFFE
leaq (%r14,%rsi), %rax
addq $0x8, %rax
cmpq %r15, %rax
ja 0x53cb
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0x3270
testl %eax, %eax
jne 0x53cb
leaq 0x4(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x30f0
cmpq $0x1, %rax
jne 0x53cb
movl %r12d, %eax
shrl $0x1f, %eax
addl %eax, 0x8(%rsp)
cmpl 0x4(%rsp), %r12d
je 0x52b3
jmp 0x53cb
pushq $-0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x3270
testl %eax, %eax
jne 0x53cb
cmpl $0x0, 0x8(%rsp)
je 0x53a2
leaq 0xb48f(%rip), %rdi      # 0x1082c
callq 0x30e0
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x53b7
movq %rbx, %rdi
callq 0x30d0
movq %rax, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3270
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq