// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10150, %rsp          # imm = 0x10150
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x1080
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10038(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x125b
movq 0x2dd4(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10038(%rbp), %rdx
leaq 0xdcf(%rip), %rsi       # 0x2004
movb $0x0, %al
callq 0x1090
movq 0x2db5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdd4(%rip), %rsi       # 0x2021
movb $0x0, %al
callq 0x1090
xorl %edi, %edi
callq 0x10c0
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x1504
leaq -0x10140(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1030
leaq -0x10140(%rbp), %rdi
movl $0x2e, %esi
callq 0x1070
movq %rax, -0x10040(%rbp)
cmpq $0x0, %rax
je 0x12b9
movq -0x10040(%rbp), %rdi
leaq 0xdc5(%rip), %rsi       # 0x2077
callq 0x1030
jmp 0x12cc
leaq -0x10140(%rbp), %rdi
leaq 0xdb0(%rip), %rsi       # 0x2077
callq 0x10b0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xd9d(%rip), %rsi       # 0x207c
callq 0x10a0
movq %rax, -0x10148(%rbp)
cmpq $0x0, -0x10148(%rbp)
jne 0x1316
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0xd77(%rip), %rdi       # 0x207f
movb $0x0, %al
callq 0x1060
xorl %edi, %edi
callq 0x10c0
leaq -0x10140(%rbp), %rdi
leaq 0xd73(%rip), %rsi       # 0x2097
callq 0x10a0
movq %rax, -0x10150(%rbp)
cmpq $0x0, -0x10150(%rbp)
jne 0x1356
leaq -0x10140(%rbp), %rsi
leaq 0xd37(%rip), %rdi       # 0x207f
movb $0x0, %al
callq 0x1060
xorl %edi, %edi
callq 0x10c0
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0xd31(%rip), %rdi       # 0x209a
movb $0x0, %al
callq 0x1060
movl $0x1, -0x1c(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0x28(%rbp), %rdi
movq -0x10148(%rbp), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x1040
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x13a6
jmp 0x14de
movzbl -0x26(%rbp), %eax
movzbl -0x25(%rbp), %ecx
orl %ecx, %eax
cmpl $0x0, %eax
je 0x13dd
movl -0x24(%rbp), %esi
movzbl -0x25(%rbp), %edx
movzbl -0x26(%rbp), %ecx
movzbl -0x27(%rbp), %r8d
movzbl -0x28(%rbp), %r9d
leaq 0xcdd(%rip), %rdi       # 0x20ae
movb $0x0, %al
callq 0x1060
jmp 0x14de
movzbl -0x27(%rbp), %eax
shll $0x8, %eax
movzbl -0x28(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
andl $-0x2, %eax
movl %eax, -0x20(%rbp)
leaq -0x28(%rbp), %rdi
movq -0x10150(%rbp), %rcx
movl $0x1, %esi
movl $0x2, %edx
callq 0x10d0
cmpl $0x0, -0x20(%rbp)
je 0x149f
leaq -0x10030(%rbp), %rdi
movslq -0x20(%rbp), %rdx
movq -0x10148(%rbp), %rcx
movl $0x1, %esi
callq 0x1040
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x145b
movslq -0x18(%rbp), %rax
movb $0x0, -0x10030(%rbp,%rax)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x143c
leaq -0x10030(%rbp), %rdi
movslq -0x20(%rbp), %rdx
movq -0x10150(%rbp), %rcx
movl $0x1, %esi
callq 0x10d0
leaq -0x28(%rbp), %rdi
movq -0x10148(%rbp), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x1040
movl %eax, -0x18(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x14d9
cmpl $0x0, -0x24(%rbp)
je 0x14bb
movl -0x1c(%rbp), %esi
movl -0x24(%rbp), %edx
leaq 0xc37(%rip), %rdi       # 0x20e9
movb $0x0, %al
callq 0x1060
jmp 0x14c9
leaq 0xc4a(%rip), %rdi       # 0x210c
movb $0x0, %al
callq 0x1060
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x24(%rbp)
jmp 0x137e
movq -0x10148(%rbp), %rdi
callq 0x1050
movq -0x10150(%rbp), %rdi
callq 0x1050
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1262
xorl %eax, %eax
addq $0x10150, %rsp          # imm = 0x10150
popq %rbp
retq