// File: /workspace/github_c_makefile_star2_O1/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
movl $0x0, 0x8(%rsp)
cmpl $0x2, %edi
jl 0x155a
movq %rsi, %rbx
cmpq $0x0, (%rsi)
je 0x155a
movq 0x8(%rbx), %rax
movl $0x4, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
movb $0x1, %cl
movl %ecx, 0x24(%rsp)
testq %rax, %rax
je 0x1285
cmpb $0x2d, (%rax)
jne 0x1285
movzbl 0x1(%rax), %edx
leal -0x43(%rdx), %eax
cmpl $0x30, %eax
ja 0x1246
addq $0x8, %rbx
decl %edi
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
jb 0x126d
movabsq $0x400000004, %rcx      # imm = 0x400000004
btq %rax, %rcx
jb 0x1263
movabsq $0x1000000010000, %rcx  # imm = 0x1000000010000
btq %rax, %rcx
jb 0x1285
movq 0x2dab(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdad(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x1070
jmp 0x1534
movl $0x1, 0x14(%rsp)
jmp 0x1285
movl $0x2, 0x14(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x2, 0x10(%rsp)
cmpl $0x2, %edi
jl 0x1534
movl 0x10(%rsp), %eax
movq %rax, 0x40(%rsp)
movl %edi, %eax
movq %rax, 0x28(%rsp)
movl $0x1, %r14d
movl $0x0, 0x20(%rsp)
movq %rbx, 0x30(%rsp)
movq (%rbx,%r14,8), %rdi
leaq 0xd57(%rip), %rsi       # 0x2013
callq 0x1090
movq %r14, 0x38(%rsp)
movq (%rbx,%r14,8), %rsi
testq %rax, %rax
je 0x1545
movq %rax, %r13
xorl %r12d, %r12d
leaq 0xd4e(%rip), %rdi       # 0x202e
xorl %eax, %eax
callq 0x1060
movl $0x1, 0x1c(%rsp)
leaq 0xd52(%rip), %rdi       # 0x2048
movl $0x1, %esi
xorl %eax, %eax
callq 0x1060
movl $0x1, 0x18(%rsp)
movl $0x1, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
movslq %r12d, %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x1080
movl $0x1, %esi
leaq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq %r13, %rcx
callq 0x1040
movq %rax, %r14
cmpb $0x0, 0x24(%rsp)
je 0x1368
movzbl 0xb(%rsp), %eax
shll $0x18, %eax
movzbl 0xa(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0x9(%rsp), %ebx
shll $0x8, %ebx
orl %ecx, %ebx
jmp 0x1370
movzbl 0x9(%rsp), %ebx
shll $0x8, %ebx
movzbl 0x8(%rsp), %r15d
orl %ebx, %r15d
testl %r14d, %r14d
je 0x13b5
cmpl $-0x2, %r15d
je 0x138d
cmpl $0xfffeffff, %r15d      # imm = 0xFFFEFFFF
jne 0x13b5
xorl %eax, %eax
cmpl $-0x2, %r15d
sete %al
testl %ebp, %ebp
movl 0x20(%rsp), %ecx
cmovel %r12d, %ecx
movl %ecx, 0x20(%rsp)
leal 0x2(%rbp,%rax,2), %ebp
leal (%r12,%rax,2), %r12d
addl $0x2, %r12d
jmp 0x131c
testl %ebp, %ebp
je 0x13da
leaq 0xca1(%rip), %rdi       # 0x2061
movl 0x1c(%rsp), %esi
movl 0x20(%rsp), %edx
movl %ebp, %ecx
movl %ebp, %r8d
movl %esi, %ebp
xorl %eax, %eax
callq 0x1060
incl %ebp
jmp 0x13de
movl 0x1c(%rsp), %ebp
testl %r14d, %r14d
je 0x14e0
cmpl $-0x1, %r15d
je 0x14e0
testl %ebx, %ebx
jns 0x140e
leaq 0xc99(%rip), %rdi       # 0x2095
movl 0xc(%rsp), %esi
xorl %eax, %eax
callq 0x1060
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
testl %r15d, %r15d
je 0x147f
cmpl $0x10001, %r15d         # imm = 0x10001
jae 0x1503
cmpl $0x0, 0x4(%rsp)
je 0x1439
leaq 0xc1a(%rip), %rdi       # 0x2048
movl 0x18(%rsp), %esi
xorl %eax, %eax
callq 0x1060
leaq 0xcf0(%rip), %rdi       # 0x2130
movl %ebp, %esi
movl %r12d, %edx
movl 0xc(%rsp), %ebx
movl %ebx, %ecx
movl %r15d, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0x1060
incl %ebx
movl %ebx, 0xc(%rsp)
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
je 0x14bc
cmpl $0x1, %eax
je 0x1476
incl %r15d
andl $0x3fffe, %r15d         # imm = 0x3FFFE
addl %r15d, %r12d
addl $0x8, %r12d
jmp 0x14cd
cmpl $0x0, 0x4(%rsp)
jne 0x14ee
leaq 0xc4d(%rip), %rdi       # 0x20da
movl %ebp, %esi
movl %r12d, %edx
movl 0x18(%rsp), %ebx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1060
incl %ebx
movl %ebx, 0x18(%rsp)
addl 0x10(%rsp), %r12d
movl $0x1, 0xc(%rsp)
movl $0x1, 0x4(%rsp)
jmp 0x14d5
incl %r15d
andl $0x3fffe, %r15d         # imm = 0x3FFFE
addl %r15d, %r12d
addl $0x2, %r12d
movl $0x0, 0x4(%rsp)
incl %ebp
movl %ebp, 0x1c(%rsp)
jmp 0x131a
leaq 0xc7d(%rip), %rdi       # 0x2164
callq 0x1030
jmp 0x1514
leaq 0xbbb(%rip), %rdi       # 0x20b0
movl %ebp, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1060
jmp 0x1514
leaq 0xbfa(%rip), %rdi       # 0x2104
movl %r15d, %esi
xorl %eax, %eax
callq 0x1060
movq %r13, %rdi
callq 0x1050
movq 0x38(%rsp), %r14
incq %r14
cmpq 0x28(%rsp), %r14
movq 0x30(%rsp), %rbx
jne 0x12b1
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaca(%rip), %rdi       # 0x2016
xorl %eax, %eax
callq 0x1060
xorl %edi, %edi
callq 0x10a0
leaq 0xc18(%rip), %rdi       # 0x2179
callq 0x1030
jmp 0x1553