// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/sfmtcvt/sfmtcvt.c
// Function: main
int main (int argc, char *argv[])
{
int i, d, d1, j, k, astrt, dstrt, addr, maxaddr[MAXR];
int numr, numf;
unsigned char data[MAXR][MAXA];
char *s, *ppos, *cptr, line[256], oname[256];
FILE *ifile, *ofile;

if ((argc < 2) || (argv[0] == NULL)) {
	printf ("Usage is: verb file [file...]\n");
	exit (0);  }

s = argv[1];
if ((s != NULL) && (*s++ == '-')) {
	++argv; --argc;
	switch (*s) {
 	case '1':
	    numr = 1; break;
	case '2':
	    numr = 2; break;
	case '4':
	    numr = 4; break;
	default:
	    fprintf (stderr, "Bad option %c\n", *s);
	return 0;  }
	}
else numr = 1;

for (i = 1, numf = 0; i < argc; i++) {
	ifile = fopen (argv[i], "r");
	if (ifile == NULL) {
		printf ("Error opening file: %s\n", argv[i]);
		exit (0);  }

	printf ("Processing file %s\n", argv[i]);
	astrt = 4;
	maxaddr[numf] = 0;
	for (;;) {
		cptr = fgets (line, 256, ifile);
		if (cptr == NULL) break;
		if (line[0] != 'S') continue;
		if (line[1] == '1') dstrt = 8;
		else if (line[1] == '2') dstrt = 10;
		else continue;
		for (k = astrt, addr = 0; k < dstrt; k++) {
			d = HEX (line[k]);
			addr = (addr << 4) + d;  }
		if (addr >= MAXA) {
			printf ("Address %o out of range\n", addr);
			break;  }
		for (k = dstrt; k < (dstrt + 32); k = k + 2, addr++) {
			d = HEX (line[k]);
			d1 = HEX (line[k+1]);
			data[numf][addr] = (d << 4) + d1;  }
		if (addr > maxaddr[numf]) maxaddr[numf] = addr;
		}
	fclose (ifile);
	numf++;
	if (numf >= numr) {
	    for (k = 0; k < numr; k++) {
		if (maxaddr[k] != maxaddr[0]) {
		    printf ("Rom lengths don't match, file 1 = %d, file %d = %d\n",
			maxaddr[0], k, maxaddr[k]);
		    return 0;  }  }
	    strcpy (oname, argv[i]);
            if (ppos = strrchr (oname, '.')) strcpy (ppos, ".bin");
            else strcat (oname, ".bin");
	    ofile = fopen (oname, "wb");
	    if (ofile == NULL) {
		printf ("Error opening file: %s\n", oname);
		exit (0);  }
	    printf ("Output file: %s, ROM size is %d\n", oname, maxaddr[0]);
	    for (k = 0; k < maxaddr[0]; k++) {
		for (j = numr - 1; j >= 0; j--) {
		    fwrite (&data[j][k], 1, 1, ofile);  }  }
	    fclose (ofile);
	    numf = 0;
	    }
	}
if (numf) printf ("Unprocessed files\n");
return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20248, %rsp          # imm = 0x20248
cmpl $0x2, %edi
jl 0x1523
movq %rsi, %rbx
cmpq $0x0, (%rsi)
je 0x1523
movq 0x8(%rbx), %rcx
pushq $0x1
popq %rax
testq %rcx, %rcx
je 0x1238
cmpb $0x2d, (%rcx)
jne 0x1238
addq $0x8, %rbx
decl %edi
movzbl 0x1(%rcx), %ecx
cmpl $0x31, %ecx
je 0x1238
cmpl $0x34, %ecx
je 0x1235
cmpl $0x32, %ecx
jne 0x14fd
pushq $0x2
jmp 0x1237
pushq $0x4
popq %rax
movl %eax, %r15d
movl %edi, %ecx
movq %rcx, 0x38(%rsp)
shll $0xf, %eax
leaq 0x240(%rsp), %rcx
addq %rcx, %rax
addq $-0x8000, %rax          # imm = 0x8000
movq %rax, 0x30(%rsp)
pushq $0x1
popq %r14
xorl %r12d, %r12d
cmpq 0x38(%rsp), %r14
jae 0x14ea
movq (%rbx,%r14,8), %rdi
leaq 0xd9b(%rip), %rsi       # 0x2013
callq 0x10c0
movq %r14, 0x8(%rsp)
movq (%rbx,%r14,8), %rsi
testq %rax, %rax
je 0x151a
movq %rax, %rbp
xorl %r14d, %r14d
leaq 0xd91(%rip), %rdi       # 0x202d
xorl %eax, %eax
callq 0x1070
movslq %r12d, %rax
movq %rax, %r13
shlq $0xf, %rax
addq %rsp, %rax
addq $0x240, %rax            # imm = 0x240
movq %rax, (%rsp)
leaq 0x40(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdx
callq 0x10a0
testq %rax, %rax
je 0x1387
cmpb $0x53, 0x40(%rsp)
jne 0x12ba
movzbl 0x41(%rsp), %eax
cmpl $0x32, %eax
je 0x12ef
cmpl $0x31, %eax
jne 0x12ba
pushq $0x8
jmp 0x12f1
pushq $0xa
popq %rax
movl %eax, %eax
xorl %esi, %esi
pushq $0x4
popq %rcx
pushq $0x9
popq %r11
jmp 0x1318
movsbl 0x40(%rsp,%rcx), %edx
shll $0x4, %esi
cmpl $0x41, %edx
leal -0x37(%rsi,%rdx), %edi
leal -0x30(%rsi,%rdx), %esi
cmovgel %edi, %esi
incq %rcx
cmpq %rcx, %rax
jne 0x12ff
cmpl $0x7fff, %esi           # imm = 0x7FFF
jg 0x14ae
leal 0x20(%rax), %ecx
movslq %esi, %rdx
addq (%rsp), %rdx
cmpq %rcx, %rax
jae 0x137b
movb 0x40(%rsp,%rax), %r8b
movl %r11d, %edi
cmpb $0x41, %r8b
jge 0x1349
pushq $0x40
popq %rdi
movb 0x41(%rsp,%rax), %r10b
movl $0xc9, %r9d
cmpb $0x41, %r10b
jge 0x1360
movl $0xd0, %r9d
addb %r8b, %dil
addb %r10b, %r9b
shlb $0x4, %dil
addb %r9b, %dil
movb %dil, (%rdx)
addq $0x2, %rax
incl %esi
incq %rdx
jmp 0x1333
cmpl %r14d, %esi
cmovgl %esi, %r14d
jmp 0x12ba
movl %r14d, 0x10(%rsp,%r13,4)
movq %rbp, %rdi
callq 0x1050
movl %r12d, %eax
incl %eax
movl %eax, %r12d
cmpl %r15d, %eax
movq 0x8(%rsp), %r14
jl 0x14a6
movl 0x10(%rsp), %ebp
xorl %edx, %edx
leaq 0x140(%rsp), %r13
cmpq %rdx, %r15
je 0x13ce
movl 0x10(%rsp,%rdx,4), %ecx
cmpl %ebp, %ecx
jne 0x14c6
incq %rdx
jmp 0x13b8
movq (%rbx,%r14,8), %rsi
movq %r13, %rdi
callq 0x1030
movq %r13, %rdi
pushq $0x2e
popq %rsi
callq 0x1080
testq %rax, %rax
je 0x13f6
movb $0x0, 0x4(%rax)
movl $0x6e69622e, (%rax)     # imm = 0x6E69622E
jmp 0x1411
movq %r13, %rdi
callq 0x1060
movl $0x6e69622e, 0x140(%rsp,%rax) # imm = 0x6E69622E
movb $0x0, 0x144(%rsp,%rax)
movq %rbx, 0x28(%rsp)
movq %r13, %rdi
leaq 0xc73(%rip), %rsi       # 0x2093
callq 0x10c0
testq %rax, %rax
je 0x1531
movq %rax, %r14
leaq 0xc5e(%rip), %rdi       # 0x2096
movq %r13, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1070
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
movq 0x30(%rsp), %r12
xorl %ebx, %ebx
movq %rbp, (%rsp)
cmpq %rbp, %rbx
je 0x1491
movq %r12, %r13
movq %r15, %rbp
testq %r15, %r15
jle 0x1482
decq %r15
movsbl (%r13), %edi
movq %r14, %rsi
callq 0x1090
addq $-0x8000, %r13          # imm = 0x8000
jmp 0x1464
incq %rbx
incq %r12
movq %rbp, %r15
movq (%rsp), %rbp
jmp 0x1459
movq %r14, %rdi
callq 0x1050
xorl %r12d, %r12d
movq 0x28(%rsp), %rbx
movq 0x8(%rsp), %r14
incq %r14
jmp 0x1262
movl %r14d, 0x10(%rsp,%r13,4)
leaq 0xb87(%rip), %rdi       # 0x2041
xorl %eax, %eax
callq 0x1070
jmp 0x138c
leaq 0xb8d(%rip), %rdi       # 0x205a
movl %ebp, %esi
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0x20248, %rsp          # imm = 0x20248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
je 0x14d6
leaq 0xbc1(%rip), %rdi       # 0x20b7
callq 0x1040
jmp 0x14d6
movsbl %cl, %edx
movq 0x2af1(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaf3(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x10b0
jmp 0x14d6
leaq 0xaf4(%rip), %rdi       # 0x2015
jmp 0x1540
leaq 0xb9f(%rip), %rdi       # 0x20c9
callq 0x1040
jmp 0x1547
leaq 0xadd(%rip), %rdi       # 0x2015
leaq 0x140(%rsp), %rsi
xorl %eax, %eax
callq 0x1070
xorl %edi, %edi
callq 0x10d0