// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/tape.c
// Function: tapeEOM
int tapeEOM(
  FILE *container,
  off_t *eot
)
{
  int whence = eot == NULL ? SEEK_END : SEEK_SET;
  off_t pos = eot == NULL ? 0 : *eot;
  uint32_t bc1, bc2;

  /*
   * Move to the end of the tape and then look backwards to see how it is
   * terminated.
   */
  if (fseeko(container, pos, whence) == 0) {
    if (ftello(container) == 0) {
      /*
       * Empty file, we are correctly positioned.
       */
      return 1;
    }

    if ((bc1 = tapeReadRecordLengthReverse(container)) == ST_FAIL)
      return 0;

    if ((bc1 == ST_EOM) || (bc1 == ST_TM)) {
      if (ftello(container) == 0) {
        /*
         * Only ST_EOM or ST_TM present, we are correctly positioned.
         */
        return 1;
      }

      if ((bc2 = tapeReadRecordLengthReverse(container)) == ST_FAIL)
        return 0;

      if (bc2 == ST_TM) {
        if (ftello(container) == 0) {
          /*
           * Only ST_TM followed by ST_TM or ST_EOM, we are correctly
           * positioned
           */
          return 1;
        }
      
        /*
         * ST_TM followed by ST_TM or ST_EOM with at least one data block
         * present, skip over the initial ST_TM.
         */
        if (fseeko(container, sizeof(uint32_t), SEEK_CUR) == 0)
          return 1;
      } else {
        /*
         * Only a single ST_TM at the end of the container file. This
         * indicates that there is a missing ST_TM or ST_EOM. Position
         * the tape at the logical end-of-tape so that any subsequent file
         * write will fix the problem.
         */
        if (fseeko(container, pos, whence) == 0)
          return 1;
      }
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdx
xorl %eax, %eax
movl $0x2, %ecx
cmpq $0x0, %rdx
cmovel %ecx, %eax
movl %eax, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x67a4
xorl %eax, %eax
movq %rax, -0x38(%rbp)
jmp 0x67af
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x3250
cmpl $0x0, %eax
jne 0x68b6
movq -0x10(%rbp), %rdi
callq 0x30c0
cmpq $0x0, %rax
jne 0x67eb
movl $0x1, -0x4(%rbp)
jmp 0x68bd
movq -0x10(%rbp), %rdi
callq 0x6370
movl %eax, -0x2c(%rbp)
cmpl $0xfefefefe, %eax       # imm = 0xFEFEFEFE
jne 0x680a
movl $0x0, -0x4(%rbp)
jmp 0x68bd
cmpl $-0x1, -0x2c(%rbp)
je 0x681a
cmpl $0x0, -0x2c(%rbp)
jne 0x68b4
movq -0x10(%rbp), %rdi
callq 0x30c0
cmpq $0x0, %rax
jne 0x6835
movl $0x1, -0x4(%rbp)
jmp 0x68bd
movq -0x10(%rbp), %rdi
callq 0x6370
movl %eax, -0x30(%rbp)
cmpl $0xfefefefe, %eax       # imm = 0xFEFEFEFE
jne 0x6851
movl $0x0, -0x4(%rbp)
jmp 0x68bd
cmpl $0x0, -0x30(%rbp)
jne 0x6892
movq -0x10(%rbp), %rdi
callq 0x30c0
cmpq $0x0, %rax
jne 0x686f
movl $0x1, -0x4(%rbp)
jmp 0x68bd
movq -0x10(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x3250
cmpl $0x0, %eax
jne 0x6890
movl $0x1, -0x4(%rbp)
jmp 0x68bd
jmp 0x68b2
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x3250
cmpl $0x0, %eax
jne 0x68b0
movl $0x1, -0x4(%rbp)
jmp 0x68bd
jmp 0x68b2
jmp 0x68b4
jmp 0x68b6
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)