// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/mtcvtfix/mtcvtfix.c
// Function: main
int main (int argc, char *argv[])
{
int i, fc, rc;
unsigned int k, tbc;
unsigned char bc[4] = { 0 };
unsigned char bceof[4] = { 0 };
unsigned char buf[FLPSIZ];
char *ppos, oname[256];
FILE *ifile, *ofile;

if ((argc < 2) || (argv[0] == NULL)) {
	printf ("Usage is: verb file [file...]\n");
	exit (0);  }

for (i = 1; i < argc; i++) {
	strcpy (oname, argv[i]);
        if (ppos = strrchr (oname, '.')) strcpy (ppos, ".new");
            else strcat (oname, ".new");
	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;
	    tbc = ((unsigned int) bc[3] << 24) | ((unsigned int) bc[2] << 16) |
		((unsigned int) bc[1] << 8) | (unsigned int) bc[0];
	    if (tbc) {
		printf ("Record size = %d\n", tbc);
		if (tbc > FLPSIZ) {
		    printf ("Record too big\n");
		    return 0;  }
		k = fread (buf, sizeof (char), tbc, ifile);
		for ( ; k < tbc; k++) buf[k] = 0;
		fread (bc, sizeof (char), 4, ifile);
		if (tbc > 1) {
		    fwrite (bc, sizeof (char), 4, ofile);
		    fwrite (buf, sizeof (char), tbc, ofile);
		    fwrite (bc, sizeof (char), 4, ofile);
		    rc++;  }
		else {
		    printf ("Record length = 1, ignored\n");
		    }  }
	    else {
		fwrite (bceof, sizeof (char), 4, ofile);
		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
andl $0x0, (%rsp)
andl $0x0, 0x24(%rsp)
cmpl $0x2, %edi
jl 0x143d
cmpq $0x0, (%rsi)
je 0x143d
movl %edi, %eax
pushq $0x1
popq %r14
leaq 0x30(%rsp), %rbp
pushq $0x4
popq %r13
movq %r14, %r15
movq %rsi, 0x8(%rsp)
movq %rax, 0x28(%rsp)
cmpq %rax, %r15
je 0x1429
movq (%rsi,%r15,8), %rbx
movq %rbp, %rdi
movq %rsi, %r12
movq %rbx, %rsi
callq 0x1030
movq %rbp, %rdi
pushq $0x2e
popq %rsi
callq 0x1090
testq %rax, %rax
movq %r15, 0x18(%rsp)
je 0x1255
movb $0x0, 0x4(%rax)
movl $0x77656e2e, (%rax)     # imm = 0x77656E2E
movq (%r12,%r15,8), %rbx
jmp 0x126a
movq %rbp, %rdi
callq 0x1070
movl $0x77656e2e, 0x30(%rsp,%rax) # imm = 0x77656E2E
movb $0x0, 0x34(%rsp,%rax)
movq %rbx, %rdi
leaq 0xd95(%rip), %rsi       # 0x2009
callq 0x10a0
testq %rax, %rax
je 0x144b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
leaq 0xd93(%rip), %rsi       # 0x2024
callq 0x10a0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1462
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0xd6e(%rip), %rdi       # 0x2027
xorl %eax, %eax
callq 0x1080
pushq $0x1
popq %r12
movl $0x0, 0x4(%rsp)
movq %rsp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1050
testl %eax, %eax
je 0x13ef
movl (%rsp), %r15d
testl %r15d, %r15d
je 0x13ab
leaq 0xd42(%rip), %rdi       # 0x203b
movl %r15d, %esi
xorl %eax, %eax
callq 0x1080
cmpl $0x10001, %r15d         # imm = 0x10001
jae 0x141d
movl %r15d, %ebp
leaq 0x130(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1050
movl %eax, %eax
cmpq %rbp, %rax
jae 0x133d
movb $0x0, 0x130(%rsp,%rax)
incq %rax
jmp 0x132b
movq %rsp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1050
cmpl $0x1, %r15d
jne 0x1365
leaq 0xd21(%rip), %rdi       # 0x207c
callq 0x1040
jmp 0x12cc
movq %rsp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x10(%rsp), %r15
movq %r15, %rcx
callq 0x10c0
leaq 0x130(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x10c0
movq %rsp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x10c0
incl 0x4(%rsp)
jmp 0x12cc
leaq 0x24(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x10(%rsp), %rcx
callq 0x10c0
movl 0x4(%rsp), %edx
testl %edx, %edx
je 0x13db
leaq 0xc7e(%rip), %rdi       # 0x204d
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
jmp 0x13e7
leaq 0xc8e(%rip), %rdi       # 0x2070
callq 0x1040
incl %r12d
jmp 0x12c4
movq %rbx, %rdi
callq 0x1060
movq 0x10(%rsp), %rdi
callq 0x1060
movq 0x18(%rsp), %r15
incq %r15
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rax
leaq 0x30(%rsp), %rbp
jmp 0x1215
leaq 0xc73(%rip), %rdi       # 0x2097
callq 0x1040
xorl %eax, %eax
addq $0x10138, %rsp          # imm = 0x10138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc62(%rip), %rdi       # 0x20a6
callq 0x1040
jmp 0x1475
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0xbac(%rip), %rdi       # 0x200c
jmp 0x146e
leaq 0xba3(%rip), %rdi       # 0x200c
leaq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x1080
xorl %edi, %edi
callq 0x10b0