// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x31f0
movl %eax, %edi
leaq -0xc8(%rbp), %rsi
callq 0x32d0
movq -0x10(%rbp), %rdi
callq 0x30c0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x98(%rbp), %rax
jne 0x6017
jmp 0x61aa
movq -0x10(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x30f0
cmpq $0x1, %rax
je 0x6040
movl $0x0, -0x4(%rbp)
jmp 0x61ea
movl -0x24(%rbp), %edi
callq 0x6200
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0xcc(%rbp)
subl $-0x2, %eax
je 0x60c8
jmp 0x605b
movl -0xcc(%rbp), %eax
subl $-0x1, %eax
je 0x6089
jmp 0x6068
movl -0xcc(%rbp), %eax
testl %eax, %eax
jne 0x60cd
jmp 0x6074
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
cmpl $0x1, %eax
jg 0x6087
jmp 0x61a5
jmp 0x6089
movq -0x10(%rbp), %rdi
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x3250
cmpl $0x0, %eax
je 0x60af
movl $0x0, -0x4(%rbp)
jmp 0x61ea
cmpl $0x0, -0x1c(%rbp)
je 0x60c3
leaq 0x137cd(%rip), %rdi     # 0x19889
movb $0x0, %al
callq 0x3160
jmp 0x61ac
jmp 0x61a5
movl $0x0, -0x20(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x60f0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x2c(%rbp), %eax
andl $0x7f000000, %eax       # imm = 0x7F000000
cmpl $0x0, %eax
je 0x6109
movl $0x0, -0x4(%rbp)
jmp 0x61ea
movl -0x2c(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
addl $0x1, %eax
andl $-0x2, %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movl -0x2c(%rbp), %ecx
addq %rcx, %rax
addq $0x8, %rax
cmpq -0x98(%rbp), %rax
jbe 0x613d
movl $0x0, -0x4(%rbp)
jmp 0x61ea
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x3250
cmpl $0x0, %eax
je 0x6161
movl $0x0, -0x4(%rbp)
jmp 0x61ea
movq -0x10(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x30f0
cmpq $0x1, %rax
je 0x6187
movl $0x0, -0x4(%rbp)
jmp 0x61ea
movl -0x24(%rbp), %edi
callq 0x6200
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
je 0x61a3
movl $0x0, -0x4(%rbp)
jmp 0x61ea
jmp 0x61a5
jmp 0x5ff8
jmp 0x61ac
cmpq $0x0, -0x18(%rbp)
je 0x61c6
movq -0x10(%rbp), %rdi
callq 0x30c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x3250
cmpl $0x0, %eax
jne 0x61e3
movl $0x1, -0x4(%rbp)
jmp 0x61ea
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)