// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl $0x0, (%rsp)
cmpl $0x2, %edi
jl 0x15ba
movq %rsi, %r14
cmpq $0x0, (%rsi)
je 0x15ba
movq 0x8(%r14), %rcx
movb $0x1, %al
movl %eax, 0x8(%rsp)
movl $0x4, %eax
testq %rcx, %rcx
je 0x1264
cmpb $0x2d, (%rcx)
jne 0x1264
movzbl 0x1(%rcx), %edx
leal -0x43(%rdx), %ecx
cmpl $0x30, %ecx
ja 0x1247
movabsq $0x100000001, %rax      # imm = 0x100000001
btq %rcx, %rax
jb 0x156c
movl $0x4, %eax
movb $0x1, %sil
movl %esi, 0x8(%rsp)
movabsq $0x400000004, %rsi      # imm = 0x400000004
btq %rcx, %rsi
jb 0x1564
xorl %r8d, %r8d
movabsq $0x1000000010000, %rsi  # imm = 0x1000000010000
btq %rcx, %rsi
jb 0x157f
movq 0x2daa(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdac(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x1070
jmp 0x1594
movl $0x0, 0x14(%rsp)
movl %eax, %r15d
movl %edi, %eax
movq %rax, 0x20(%rsp)
movl $0x1, %r12d
movl $0x0, 0x1c(%rsp)
movq %r14, 0x28(%rsp)
movq (%r14,%r12,8), %rdi
leaq 0xd7f(%rip), %rsi       # 0x2013
callq 0x1090
movq (%r14,%r12,8), %rsi
testq %rax, %rax
je 0x15a5
movq %rax, %rbx
leaq 0xd7e(%rip), %rdi       # 0x202e
xorl %eax, %eax
callq 0x1060
leaq 0xd8a(%rip), %rdi       # 0x2048
movl $0x1, %esi
xorl %eax, %eax
callq 0x1060
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1080
movl $0x1, %esi
movq %rsp, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1040
testl %eax, %eax
movq %r12, 0x30(%rsp)
je 0x150e
cmpb $0x0, 0x8(%rsp)
je 0x1302
movl (%rsp), %ebp
jmp 0x1306
movzwl (%rsp), %ebp
movl $0x1, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
xorl %r12d, %r12d
movl $0x1, 0x18(%rsp)
movl $0x1, %r14d
movl %r14d, 0x10(%rsp)
movq %r15, %r13
xorl %r15d, %r15d
cmpl $-0x2, %ebp
je 0x133f
cmpl $0xfffeffff, %ebp       # imm = 0xFFFEFFFF
jne 0x13a0
xorl %r14d, %r14d
cmpl $-0x2, %ebp
sete %r14b
testl %r15d, %r15d
movl 0x1c(%rsp), %eax
cmovel %r12d, %eax
movl %eax, 0x1c(%rsp)
leal (%r12,%r14,2), %r12d
addl $0x2, %r12d
movslq %r12d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1080
movl $0x1, %esi
movq %rsp, %rdi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1040
cmpb $0x0, 0x8(%rsp)
je 0x138c
movl (%rsp), %ebp
jmp 0x1390
movzwl (%rsp), %ebp
leal (%r15,%r14,2), %r15d
addl $0x2, %r15d
testl %eax, %eax
jne 0x1332
movb $0x1, %al
jmp 0x13a7
xorl %eax, %eax
testl %r15d, %r15d
je 0x13d5
leaq 0xcb3(%rip), %rdi       # 0x2061
movl 0x10(%rsp), %r14d
movl %r14d, %esi
movl 0x1c(%rsp), %edx
movl %r15d, %ecx
movl %r15d, %r8d
movl %eax, %r15d
xorl %eax, %eax
callq 0x1060
movl %r15d, %eax
incl %r14d
movl %r14d, 0x10(%rsp)
movq %r13, %r15
testb %al, %al
jne 0x150e
cmpl $-0x1, %ebp
je 0x150e
testl %ebp, %ebp
jns 0x1405
leaq 0xca1(%rip), %rdi       # 0x2095
movl 0xc(%rsp), %esi
xorl %eax, %eax
callq 0x1060
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x10(%rsp), %r14d
testl %ebp, %ebp
je 0x147c
cmpl $0x10001, %ebp          # imm = 0x10001
jae 0x1552
cmpl $0x0, 0x4(%rsp)
je 0x1433
leaq 0xc20(%rip), %rdi       # 0x2048
movl 0x18(%rsp), %esi
xorl %eax, %eax
callq 0x1060
leaq 0xcf6(%rip), %rdi       # 0x2130
movl %r14d, %esi
movl %r12d, %edx
movl 0xc(%rsp), %r13d
movl %r13d, %ecx
movl %ebp, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1060
incl %r13d
movl %r13d, 0xc(%rsp)
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
je 0x14bc
cmpl $0x1, %eax
je 0x1473
incl %ebp
andl $0x3fffe, %ebp          # imm = 0x3FFFE
addl %ebp, %r12d
addl $0x8, %r12d
jmp 0x14cb
cmpl $0x0, 0x4(%rsp)
jne 0x153c
leaq 0xc4c(%rip), %rdi       # 0x20da
movl %r14d, %esi
movl %r12d, %edx
movl 0x18(%rsp), %ebp
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1060
incl %ebp
movl %ebp, 0x18(%rsp)
addl %r15d, %r12d
movl $0x1, 0xc(%rsp)
movl $0x1, 0x4(%rsp)
jmp 0x14d3
incl %ebp
andl $0x3fffe, %ebp          # imm = 0x3FFFE
addl %ebp, %r12d
addl $0x2, %r12d
movl $0x0, 0x4(%rsp)
movslq %r12d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1080
movl $0x1, %esi
movq %rsp, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1040
cmpb $0x0, 0x8(%rsp)
je 0x14ff
movl (%rsp), %ebp
jmp 0x1503
movzwl (%rsp), %ebp
incl %r14d
testl %eax, %eax
jne 0x1327
leaq 0xc4f(%rip), %rdi       # 0x2164
callq 0x1030
movq %rbx, %rdi
callq 0x1050
movq 0x30(%rsp), %r12
incq %r12
cmpq 0x20(%rsp), %r12
movq 0x28(%rsp), %r14
jne 0x1289
jmp 0x1594
leaq 0xb6d(%rip), %rdi       # 0x20b0
movl %r14d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1060
jmp 0x151a
leaq 0xbab(%rip), %rdi       # 0x2104
movl %ebp, %esi
xorl %eax, %eax
callq 0x1060
jmp 0x151a
movl $0x1, %r8d
jmp 0x157f
movl $0x2, %r8d
movl $0x0, 0x8(%rsp)
movl $0x2, %eax
movl %r8d, 0x14(%rsp)
decl %edi
cmpl $0x2, %edi
jb 0x1594
addq $0x8, %r14
jmp 0x126c
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa6a(%rip), %rdi       # 0x2016
xorl %eax, %eax
callq 0x1060
xorl %edi, %edi
callq 0x10a0
leaq 0xbb8(%rip), %rdi       # 0x2179
callq 0x1030
jmp 0x15b3