// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/mt2tpc/mt2tpc.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 TPC tape image file named file.tpc from the input simh image file\n");
    exit (0);
    }

for (i = 1; i < argc; i++) {
	strcpy (oname, argv[i]);
        if (ppos = strrchr (oname, '.')) strcpy (ppos, ".tpc");
                else strcat (oname, ".tpc");
	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), 4, ifile);
		if (k == 0) break;
		if (bc[2] | bc[3]) {
			printf ("Invalid record size, record %d, size = 0x%02X%02X%02X%02X\n", rc, bc[3], bc[2], bc[1], bc[0]);
			break;  }
		wc = ((unsigned int) bc[1] << 8) | (unsigned int) bc[0];
		wc = (wc + 1) & ~1;
		fwrite (bc, sizeof (char), 2, ofile);
		if (wc) {
			k = fread (buf, sizeof (char), wc, ifile);
			for ( ; k < wc; k++) buf[k] =0;
			fwrite (buf, sizeof (char), wc, ofile);
			k = fread (bc, sizeof (char), 4, ifile);
			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 $0x10138, %rsp          # imm = 0x10138
movq %rsi, %rbx
movl $0x0, (%rsp)
cmpl $0x2, %edi
jl 0x14d7
movl %edi, %eax
movq %rax, 0x20(%rsp)
movl $0x1, %r14d
leaq 0x30(%rsp), %r15
movq %rsp, %r13
movq %rbx, 0x28(%rsp)
movq (%rbx,%r14,8), %rsi
movq %r15, %rdi
callq 0x1030
movq %r15, %rdi
movl $0x2e, %esi
callq 0x1090
testq %rax, %rax
je 0x1294
movb $0x0, 0x4(%rax)
movl $0x6370742e, (%rax)     # imm = 0x6370742E
jmp 0x12a9
movq %r15, %rdi
callq 0x1070
movl $0x6370742e, 0x30(%rsp,%rax) # imm = 0x6370742E
movb $0x0, 0x34(%rsp,%rax)
movq %r14, 0x8(%rsp)
movq (%rbx,%r14,8), %rdi
leaq 0xdc3(%rip), %rsi       # 0x207c
callq 0x10c0
testq %rax, %rax
je 0x14ab
movq %rax, %r15
leaq 0x30(%rsp), %rdi
leaq 0xdc1(%rip), %rsi       # 0x2097
callq 0x10c0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x14bd
movq 0x8(%rsp), %rax
movq (%rbx,%rax,8), %rsi
leaq 0xda1(%rip), %rdi       # 0x209a
xorl %eax, %eax
callq 0x1080
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x1050
testl %eax, %eax
je 0x1468
xorl %ebp, %ebp
movl $0x1, %ebx
movzbl 0x2(%rsp), %ecx
movzbl 0x3(%rsp), %edx
movl %edx, %eax
orl %ecx, %eax
jne 0x144d
movl %ebp, 0x4(%rsp)
movzbl 0x1(%rsp), %ebp
shll $0x8, %ebp
movzbl (%rsp), %r14d
leal (%r14,%rbp), %r12d
incl %r12d
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq 0x10(%rsp), %rcx
callq 0x10e0
andl $0x1fffe, %r12d         # imm = 0x1FFFE
je 0x1404
movl %ebx, 0x1c(%rsp)
movq %r15, %rcx
movq %r13, %rbx
movl %r12d, %r13d
movl $0x1, %esi
leaq 0x130(%rsp), %rdi
movq %r13, %rdx
callq 0x1050
cmpl %eax, %r12d
jle 0x13c6
movslq %eax, %rcx
leaq (%rsp,%rcx), %rdi
addq $0x130, %rdi            # imm = 0x130
leal (%r14,%rbp), %ecx
incl %ecx
andl $0x1fffe, %ecx          # imm = 0x1FFFE
notl %eax
addl %ecx, %eax
incq %rax
xorl %esi, %esi
movq %rax, %rdx
callq 0x10a0
movl $0x1, %esi
leaq 0x130(%rsp), %rdi
movq %r13, %rdx
movq 0x10(%rsp), %rcx
callq 0x10e0
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %r13
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1050
movl 0x4(%rsp), %ebp
incl %ebp
movl 0x1c(%rsp), %ebx
jmp 0x142e
movl 0x4(%rsp), %edx
testl %edx, %edx
je 0x141e
leaq 0xcd6(%rip), %rdi       # 0x20e9
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
jmp 0x142a
leaq 0xce7(%rip), %rdi       # 0x210c
callq 0x1040
incl %ebx
xorl %ebp, %ebp
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x1050
testl %eax, %eax
jne 0x1324
jmp 0x1468
movzbl 0x1(%rsp), %r8d
movzbl (%rsp), %r9d
leaq 0xc4f(%rip), %rdi       # 0x20ae
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
movq %r15, %rdi
callq 0x1060
movq 0x10(%rsp), %rdi
callq 0x1060
movq 0x8(%rsp), %r14
incq %r14
cmpq 0x20(%rsp), %r14
movq 0x28(%rsp), %rbx
leaq 0x30(%rsp), %r15
jne 0x126a
xorl %eax, %eax
addq $0x10138, %rsp          # imm = 0x10138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq (%rbx,%rax,8), %rsi
leaq 0xbc4(%rip), %rdi       # 0x207f
jmp 0x14c9
leaq 0xbbb(%rip), %rdi       # 0x207f
leaq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x1080
xorl %edi, %edi
callq 0x10d0
movq (%rbx), %rdi
callq 0x1100