// File: /workspace/github_c_makefile_star2_O2/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 $0x10128, %rsp          # imm = 0x10128
movq %rsi, %r15
andl $0x0, (%rsp)
cmpl $0x2, %edi
jl 0x143f
movl %edi, %eax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r14
leaq 0x20(%rsp), %r13
movq %rsp, %r12
movq %r14, %rbp
movq %r15, 0x10(%rsp)
cmpq 0x18(%rsp), %rbp
je 0x13ff
movq (%r15,%rbp,8), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1030
movq %r13, %rdi
pushq $0x2e
popq %rsi
callq 0x1090
testq %rax, %rax
je 0x1252
movb $0x0, 0x4(%rax)
movl $0x6370742e, (%rax)     # imm = 0x6370742E
movq (%r15,%rbp,8), %rbx
jmp 0x1267
movq %r13, %rdi
callq 0x1070
movl $0x6370742e, 0x20(%rsp,%rax) # imm = 0x6370742E
movb $0x0, 0x24(%rsp,%rax)
movq %rbp, 0x8(%rsp)
movq %rbx, %rdi
leaq 0xe06(%rip), %rsi       # 0x207c
callq 0x10b0
testq %rax, %rax
je 0x1413
movq %rax, %rbx
movq %r13, %rdi
leaq 0xe06(%rip), %rsi       # 0x2097
callq 0x10b0
testq %rax, %rax
je 0x1425
movq %rax, %rbp
movq 0x8(%rsp), %rax
movq (%r15,%rax,8), %rsi
leaq 0xde8(%rip), %rdi       # 0x209a
xorl %eax, %eax
callq 0x1080
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
pushq $0x4
popq %r13
xorl %r15d, %r15d
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1050
testl %eax, %eax
je 0x13d8
movzbl 0x2(%rsp), %ecx
movzbl 0x3(%rsp), %edx
movl %edx, %eax
orl %ecx, %eax
jne 0x13bc
movzwl (%rsp), %r13d
incl %r13d
andl $-0x2, %r13d
movq %r12, %rdi
movq %r14, %rsi
pushq $0x2
popq %rdx
movq %rbp, %rcx
callq 0x10d0
testl %r13d, %r13d
je 0x1376
leaq 0x120(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1050
cltq
movl %r13d, %ecx
cmpq %rcx, %rax
jge 0x1343
movb $0x0, 0x120(%rsp,%rax)
incq %rax
jmp 0x1331
leaq 0x120(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x10d0
movq %r12, %rdi
movq %r14, %rsi
pushq $0x4
popq %r13
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1050
incl %r15d
jmp 0x12c7
testl %r15d, %r15d
je 0x139d
leaq 0xd67(%rip), %rdi       # 0x20e9
movl 0x4(%rsp), %r13d
movl %r13d, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1080
movl %r13d, %eax
pushq $0x4
popq %r13
jmp 0x13b1
leaq 0xd68(%rip), %rdi       # 0x210c
callq 0x1040
pushq $0x4
popq %r13
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
jmp 0x12c4
movzbl 0x1(%rsp), %r8d
movzbl (%rsp), %r9d
leaq 0xce0(%rip), %rdi       # 0x20ae
movl %r15d, %esi
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
callq 0x1060
movq %rbp, %rdi
callq 0x1060
movq 0x8(%rsp), %rbp
incq %rbp
movq 0x10(%rsp), %r15
leaq 0x20(%rsp), %r13
jmp 0x1218
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 (%r15,%rax,8), %rsi
leaq 0xc5c(%rip), %rdi       # 0x207f
jmp 0x1431
leaq 0xc53(%rip), %rdi       # 0x207f
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1080
xorl %edi, %edi
callq 0x10c0
movq (%r15), %rdx
movq 0x2baf(%rip), %rbx      # 0x3ff8
movq (%rbx), %rdi
leaq 0xbb1(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x10a0
movq (%rbx), %rcx
leaq 0xbbd(%rip), %rdi       # 0x2021
pushq $0x55
popq %rsi
pushq $0x1
popq %rdx
callq 0x10d0
jmp 0x1438