// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/tpc2mt/tpc2mt.c
// Function: main
int main (int argc, char *argv[])
{
int i, k, fc, wc, rc;
unsigned char bc[4] = { 0 };
unsigned char buf[FLPSIZ];
char *progname = argv[0];
char *ppos, oname[256];
FILE *ifile, *ofile;

if (argc < 2) {
    fprintf (stderr, "Usage:  %s file {file2 ...}\n", progname);
    fprintf (stderr, "This will create a simh tap file named file.tap from the input tpc image file\n");
    exit (0);
    }

for (i = 1; i < argc; i++) {
	strcpy (oname, argv[i]);
        if (ppos = strrchr (oname, '.')) strcpy (ppos, ".tap");
                else strcat (oname, ".tap");
	ifile = fopen (argv[i], "rb");
	if (ifile == NULL) {
		printf ("Error opening file: %s\n", argv[i]);
		exit (0);  }
	ofile = fopen (oname, "wb");
	if (ofile == NULL) {
		printf ("Error opening file: %s\n", oname);
		exit (0);  }

	printf ("Processing file %s\n", argv[i]);
	fc = 1; rc = 0;
	for (;;) {
		k = fread (bc, sizeof (char), 2, ifile);
		if (k == 0) break;
		wc = ((unsigned int) bc[1] << 8) | (unsigned int) bc[0];
		wc = (wc + 1) & ~1;
		fwrite (bc, sizeof (char), 4, ofile);
		if (wc) {
			k = fread (buf, sizeof (char), ((unsigned int) bc[1] << 8) | (unsigned int) bc[0], ifile);
			for ( ; k < wc; k++) buf[k] =0;
			fwrite (buf, sizeof (char), wc, ofile);
			k = fwrite (bc, sizeof (char), 4, ofile);
			rc++;  }
		else {	if (rc) printf ("End of file %d, record count = %d\n", fc, rc);
			else printf ("End of tape\n");
			fc++;
			rc = 0;  }
		}
	fclose (ifile);
	fclose (ofile);
	}

return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10128, %rsp          # imm = 0x10128
movq %rsi, %r12
movl $0x0, 0x4(%rsp)
cmpl $0x2, %edi
jl 0x1477
movl %edi, %eax
movq %rax, 0x10(%rsp)
movl $0x1, %r14d
leaq 0x20(%rsp), %r15
leaq 0x4(%rsp), %rbp
movq %r12, 0x18(%rsp)
movq (%r12,%r14,8), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1030
movq %r15, %rdi
movl $0x2e, %esi
callq 0x1090
testq %rax, %rax
je 0x129e
movb $0x0, 0x4(%rax)
movl $0x7061742e, (%rax)     # imm = 0x7061742E
movq (%r12,%r14,8), %rbx
jmp 0x12b3
movq %r15, %rdi
callq 0x1070
movl $0x7061742e, 0x20(%rsp,%rax) # imm = 0x7061742E
movb $0x0, 0x24(%rsp,%rax)
movq %r14, 0x8(%rsp)
movq %rbx, %rdi
leaq 0xdb3(%rip), %rsi       # 0x2075
callq 0x10c0
testq %rax, %rax
je 0x144b
movq %rax, %r15
leaq 0x20(%rsp), %rdi
leaq 0xdb1(%rip), %rsi       # 0x2090
callq 0x10c0
testq %rax, %rax
je 0x145d
movq %rax, %r14
movq 0x8(%rsp), %rax
movq (%r12,%rax,8), %rsi
leaq 0xd93(%rip), %rdi       # 0x2093
xorl %eax, %eax
callq 0x1080
movl $0x1, %esi
movl $0x2, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x1050
testl %eax, %eax
je 0x140a
xorl %ebx, %ebx
movl $0x1, %r12d
movzwl 0x4(%rsp), %r13d
incl %r13d
andl $-0x2, %r13d
movl $0x1, %esi
movl $0x4, %edx
movq %rbp, %rdi
movq %r14, %rcx
callq 0x10e0
testl %r13d, %r13d
je 0x13c3
movzwl 0x4(%rsp), %edx
movl $0x1, %esi
leaq 0x120(%rsp), %rdi
movq %r15, %rcx
callq 0x1050
cmpl %eax, %r13d
jle 0x1392
movslq %eax, %rcx
leaq (%rsp,%rcx), %rdi
addq $0x120, %rdi            # imm = 0x120
notl %eax
addl %r13d, %eax
incq %rax
xorl %esi, %esi
movq %rax, %rdx
callq 0x10a0
movl $0x1, %esi
leaq 0x120(%rsp), %rdi
movq %r13, %rdx
movq %r14, %rcx
callq 0x10e0
movl $0x1, %esi
movl $0x4, %edx
movq %rbp, %rdi
movq %r14, %rcx
callq 0x10e0
incl %ebx
jmp 0x13ed
testl %ebx, %ebx
je 0x13dc
leaq 0xcd9(%rip), %rdi       # 0x20a7
movl %r12d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1080
jmp 0x13e8
leaq 0xce7(%rip), %rdi       # 0x20ca
callq 0x1040
incl %r12d
xorl %ebx, %ebx
movl $0x1, %esi
movl $0x2, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x1050
testl %eax, %eax
jne 0x132c
movq %r15, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x1060
movq 0x8(%rsp), %r14
incq %r14
cmpq 0x10(%rsp), %r14
movq 0x18(%rsp), %r12
leaq 0x20(%rsp), %r15
jne 0x126d
xorl %eax, %eax
addq $0x10128, %rsp          # imm = 0x10128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq (%r12,%rax,8), %rsi
leaq 0xc1d(%rip), %rdi       # 0x2078
jmp 0x1469
leaq 0xc14(%rip), %rdi       # 0x2078
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1080
xorl %edi, %edi
callq 0x10d0
movq (%r12), %rdi
callq 0x1100