// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/extracters/mtdump/mtdump.c
// Function: main
int main (int argc, char *argv[])
{
int obj, i, k, fc, rc, tpos;
unsigned int bc = 0, fmt, rlntsiz;
 unsigned char *s;
 unsigned char bca[4] = { 0,0,0,0 };
int preveof, gapcount, gpos = 0, gincr;
FILE *ifile;
#define MAXRLNT 65536

if ((argc < 2) || (argv[0] == NULL)) {
	printf ("Usage is: mtdump {-secf} file [file...]\n");
	exit (0);  }

s = argv[1];
if ((s != NULL) && (*s++ == '-')) {
	++argv; --argc;
	switch (*s) {
 	case 'S': case 's':
	    fmt = F_STD; rlntsiz = 4; break;
	case 'E': case 'e':
	    fmt = F_E11; rlntsiz = 4; break;
        case 'C': case 'c':
	    fmt = F_TPC; rlntsiz = 2; break;
            /*	case 'F': case 'f': */
/*	    fmt = F_TPF; break; */
	default:
	    fprintf (stderr, "Bad option %c\n", *s);
	    return 0;
	    }
	}
else {	fmt = F_STD;
	rlntsiz = 4;  }

for (i = 1; i < argc; i++) {
	ifile = fopen (argv[i], "rb");
	if (ifile == NULL) {
	    printf ("Error opening file: %s\n", argv[i]);
	    exit (0);  }
	printf ("Processing input file %s\n", argv[i]);
	tpos = 0; rc = 1; fc = 1; preveof = 0; gapcount = 0;
	printf ("Processing tape file %d\n", fc);
	for (obj = 1;;) {
	    fseek (ifile, tpos, SEEK_SET);
	    k = fread (bca, 1, rlntsiz, ifile);
	    switch (fmt) {
	    case F_STD: case F_E11:
		bc = (((unsigned int) bca[3]) << 24) | (((unsigned int) bca[2]) << 16) |
		    (((unsigned int) bca[1]) << 8) | ((unsigned int) bca[0]);
		break;
	    case F_TPC:
		bc = (((unsigned int) bca[1]) << 8) | ((unsigned int) bca[0]);
	        break;
		}

        if (k && ((bc == 0xFFFFFFFE) | (bc == 0xFFFEFFFF))) {
            if (gapcount == 0)
                gpos = tpos;
            gincr = (bc == 0xFFFFFFFE ? 4 : 2);
            gapcount = gapcount + gincr;
            tpos = tpos + gincr;
            continue;  }
        else if (gapcount) {
            printf ("Obj %d, position %d, erase gap, length = %d (0x%X)\n",
                obj, gpos, gapcount, gapcount);
            obj++;
            gapcount = 0;  }

	    if ((k == 0) || (bc == 0xFFFFFFFF)) {
		printf ("End of physical tape\n");
		break;  }
	    if (bc & 0x80000000) {
		printf ("Error marker at record %d\n", rc);
		bc = bc & ~0x80000000;  }
	    if (bc == 0) {
		if (preveof) {
		    printf ("Obj %d, position %d, end of logical tape\n", obj, tpos);
		    break;  }
		preveof = 1;
		printf ("Obj %d, position %d, end of tape file %d\n", obj, tpos, fc);
		fc++; obj++;
		rc = 1;
		tpos = tpos + rlntsiz;  }
	    else if (bc > MAXRLNT) {
		printf ("Invalid record length %d, terminating dump\n", bc);
		break;  }
	    else {
		if (preveof) printf ("Processing tape file %d\n", fc);
		preveof = 0;
		printf ("Obj %d, position %d, record %d, length = %d (0x%X)\n",
		    obj, tpos, rc, bc, bc);
		rc++; obj++;
		switch (fmt) {
		case F_STD:
		    tpos = tpos + 8 + ((bc + 1) & ~1); break;
		case F_E11:
		    tpos = tpos + 8 + bc; break;
		case F_TPC:
		    tpos = tpos + 2 + ((bc + 1) & ~1); break;
		    }
		}
	    }
	fclose (ifile);
	}

return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
andl $0x0, 0x20(%rsp)
cmpl $0x2, %edi
jl 0x1515
movq %rsi, %r14
cmpq $0x0, (%rsi)
je 0x1515
movq 0x8(%r14), %rcx
pushq $0x4
popq %rax
movb $0x1, %dl
movl %edx, 0x24(%rsp)
testq %rcx, %rcx
je 0x126b
cmpb $0x2d, (%rcx)
jne 0x126b
movzbl 0x1(%rcx), %edx
leal -0x43(%rdx), %ecx
cmpl $0x30, %ecx
ja 0x1242
addq $0x8, %r14
decl %edi
movabsq $0x100000001, %rsi      # imm = 0x100000001
btq %rcx, %rsi
jb 0x14ec
movabsq $0x400000004, %rsi      # imm = 0x400000004
btq %rcx, %rsi
jb 0x14e0
movabsq $0x1000000010000, %rsi  # imm = 0x1000000010000
btq %rcx, %rsi
movl $0x0, 0x10(%rsp)
jb 0x1273
movq 0x2daf(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdb1(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x10(%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
movl %edi, %eax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %r15
xorl %ebx, %ebx
movq %r14, 0x38(%rsp)
cmpq 0x40(%rsp), %r15
jae 0x125a
movq (%r14,%r15,8), %rdi
leaq 0xd75(%rip), %rsi       # 0x2013
callq 0x1090
movq %r15, 0x28(%rsp)
movq (%r14,%r15,8), %rsi
testq %rax, %rax
je 0x1500
movq %rax, %r12
xorl %ebp, %ebp
leaq 0xd6d(%rip), %rdi       # 0x202e
xorl %eax, %eax
callq 0x1060
leaq 0xd79(%rip), %rdi       # 0x2048
pushq $0x1
popq %r14
movl %r14d, %esi
xorl %eax, %eax
callq 0x1060
movl %r14d, 0x1c(%rsp)
movl %r14d, 0x18(%rsp)
movl %r14d, 0x14(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
movslq %ebp, %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1080
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
callq 0x1040
movq %rax, %r14
cmpb $0x0, 0x24(%rsp)
je 0x132a
movl 0x20(%rsp), %r15d
jmp 0x1330
movzwl 0x20(%rsp), %r15d
testl %r14d, %r14d
je 0x135e
cmpl $-0x2, %r15d
je 0x1344
cmpl $0xfffeffff, %r15d      # imm = 0xFFFEFFFF
jne 0x135e
xorl %eax, %eax
cmpl $-0x2, %r15d
sete %al
testl %r13d, %r13d
cmovel %ebp, %ebx
leal 0x2(%r13,%rax,2), %r13d
leal 0x2(%rbp,%rax,2), %ebp
jmp 0x12f7
testl %r13d, %r13d
je 0x1385
leaq 0xcf7(%rip), %rdi       # 0x2061
movl 0x1c(%rsp), %esi
movl %ebx, %edx
movl %r13d, %ecx
movl %r13d, %r8d
movl %esi, %r13d
xorl %eax, %eax
callq 0x1060
incl %r13d
jmp 0x138a
movl 0x1c(%rsp), %r13d
testl %r14d, %r14d
je 0x1492
cmpl $-0x1, %r15d
je 0x1492
testl %r15d, %r15d
jns 0x13bb
leaq 0xcec(%rip), %rdi       # 0x2095
movl 0x14(%rsp), %esi
xorl %eax, %eax
callq 0x1060
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
testl %r15d, %r15d
je 0x142f
cmpl $0x10001, %r15d         # imm = 0x10001
jae 0x14b5
cmpl $0x0, 0xc(%rsp)
je 0x13e6
leaq 0xc6d(%rip), %rdi       # 0x2048
movl 0x18(%rsp), %esi
xorl %eax, %eax
callq 0x1060
leaq 0xd43(%rip), %rdi       # 0x2130
movl %r13d, %esi
movl %ebp, %edx
movl 0x14(%rsp), %r14d
movl %r14d, %ecx
movl %r15d, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0x1060
incl %r14d
movl %r14d, 0x14(%rsp)
movl 0x10(%rsp), %eax
cmpl $0x2, %eax
je 0x146d
cmpl $0x1, %eax
je 0x1427
incl %r15d
andl $0x3fffe, %r15d         # imm = 0x3FFFE
addl %r15d, %ebp
addl $0x8, %ebp
jmp 0x147d
cmpl $0x0, 0xc(%rsp)
jne 0x14a0
leaq 0xc9d(%rip), %rdi       # 0x20da
movl %r13d, %esi
movl %ebp, %edx
movl 0x18(%rsp), %r14d
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1060
incl %r14d
movl %r14d, 0x18(%rsp)
movq 0x30(%rsp), %rax
addl %eax, %ebp
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movl %eax, 0xc(%rsp)
jmp 0x1485
incl %r15d
andl $0x3fffe, %r15d         # imm = 0x3FFFE
addl %r15d, %ebp
addl $0x2, %ebp
movl $0x0, 0xc(%rsp)
incl %r13d
movl %r13d, 0x1c(%rsp)
jmp 0x12f4
leaq 0xccb(%rip), %rdi       # 0x2164
callq 0x1030
jmp 0x14c6
leaq 0xc09(%rip), %rdi       # 0x20b0
movl %r13d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1060
jmp 0x14c6
leaq 0xc48(%rip), %rdi       # 0x2104
movl %r15d, %esi
xorl %eax, %eax
callq 0x1060
movq 0x28(%rsp), %r15
movq %r12, %rdi
callq 0x1050
incq %r15
movq 0x38(%rsp), %r14
jmp 0x128c
pushq $0x1
popq %rcx
movl %ecx, 0x10(%rsp)
jmp 0x1273
pushq $0x2
popq %rax
movl $0x0, 0x24(%rsp)
movl %eax, 0x10(%rsp)
jmp 0x1273
leaq 0xb0f(%rip), %rdi       # 0x2016
xorl %eax, %eax
callq 0x1060
xorl %edi, %edi
callq 0x10a0
leaq 0xc5d(%rip), %rdi       # 0x2179
callq 0x1030
jmp 0x150e