// File: /workspace/github_c_makefile_star2_O1/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 $0x20258, %rsp          # imm = 0x20258
cmpl $0x2, %edi
jl 0x1566
movq %rsi, %r12
cmpq $0x0, (%rsi)
je 0x1566
movq 0x8(%r12), %rax
movl $0x1, %r13d
testq %rax, %rax
je 0x1243
cmpb $0x2d, (%rax)
jne 0x1243
addq $0x8, %r12
decl %edi
movzbl 0x1(%rax), %eax
cmpl $0x31, %eax
je 0x1243
cmpl $0x34, %eax
je 0x123d
cmpl $0x32, %eax
jne 0x1549
movl $0x2, %r13d
jmp 0x1243
movl $0x4, %r13d
cmpl $0x2, %edi
jl 0x1535
movl %edi, %eax
movq %rax, 0x40(%rsp)
movl %r13d, %eax
movq %rax, 0x38(%rsp)
incq %rax
movq %rax, 0x48(%rsp)
movl %r13d, %eax
shll $0xf, %eax
leaq 0x250(%rsp), %rcx
addq %rcx, %rax
addq $-0x8000, %rax          # imm = 0x8000
movq %rax, 0x28(%rsp)
movl $0x1, %ebx
movl $0xffffffc9, %r15d      # imm = 0xFFFFFFC9
xorl %r14d, %r14d
movq (%r12,%rbx,8), %rdi
leaq 0xd7b(%rip), %rsi       # 0x2013
callq 0x10c0
movq %rbx, 0x20(%rsp)
movq (%r12,%rbx,8), %rsi
testq %rax, %rax
je 0x1574
movq %rax, %rbp
leaq 0xd74(%rip), %rdi       # 0x202d
xorl %eax, %eax
callq 0x1070
movl %r14d, (%rsp)
movslq %r14d, %r14
movl $0x0, 0x10(%rsp,%r14,4)
leaq 0x50(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdx
callq 0x10a0
testq %rax, %rax
je 0x13e5
movq %r14, %rax
shlq $0xf, %rax
leaq (%rsp,%rax), %rbx
addq $0x250, %rbx            # imm = 0x250
cmpb $0x53, 0x50(%rsp)
jne 0x13ba
movzbl 0x51(%rsp), %eax
cmpl $0x31, %eax
je 0x1322
cmpl $0x32, %eax
jne 0x13ba
movl $0xa, %eax
jmp 0x1327
movl $0x8, %eax
movl %eax, %eax
xorl %esi, %esi
movl $0x4, %ecx
movsbl 0x50(%rsp,%rcx), %edx
cmpl $0x41, %edx
movl $0xffffffd0, %edi       # imm = 0xFFFFFFD0
cmovgel %r15d, %edi
shll $0x4, %esi
addl %edx, %esi
addl %edi, %esi
incq %rcx
cmpq %rcx, %rax
jne 0x1330
cmpl $0x7fff, %esi           # imm = 0x7FFF
jg 0x13d7
leal 0x20(%rax), %ecx
movslq %esi, %rdx
addq %rbx, %rdx
movb 0x50(%rsp,%rax), %r8b
movl $0x9, %edi
cmpb $0x41, %r8b
jge 0x1376
movl $0x40, %edi
movb 0x51(%rsp,%rax), %r10b
movl $0xc9, %r9d
cmpb $0x41, %r10b
jge 0x138d
movl $0xd0, %r9d
addb %r8b, %dil
addb %r10b, %r9b
shlb $0x4, %dil
addb %r9b, %dil
movb %dil, (%rdx)
addq $0x2, %rax
incq %rdx
incl %esi
cmpq %rcx, %rax
jb 0x1361
movl 0x10(%rsp,%r14,4), %eax
cmpl %eax, %esi
cmovgl %esi, %eax
movl %eax, 0x10(%rsp,%r14,4)
leaq 0x50(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdx
callq 0x10a0
testq %rax, %rax
jne 0x12fd
jmp 0x13e5
leaq 0xc63(%rip), %rdi       # 0x2041
xorl %eax, %eax
callq 0x1070
movq %rbp, %rdi
callq 0x1050
movl (%rsp), %r14d
incl %r14d
cmpl %r13d, %r14d
jl 0x1501
movl 0x10(%rsp), %esi
xorl %edx, %edx
movq 0x38(%rsp), %rax
movq 0x20(%rsp), %rdi
leaq 0x150(%rsp), %rbx
movl 0x10(%rsp,%rdx,4), %ecx
cmpl %esi, %ecx
jne 0x1527
incq %rdx
cmpq %rdx, %rax
jne 0x1415
movq (%r12,%rdi,8), %rsi
movq %rbx, %rdi
callq 0x1030
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x1080
testq %rax, %rax
je 0x1453
movb $0x0, 0x4(%rax)
movl $0x6e69622e, (%rax)     # imm = 0x6E69622E
jmp 0x146e
movq %rbx, %rdi
callq 0x1060
movl $0x6e69622e, 0x150(%rsp,%rax) # imm = 0x6E69622E
movb $0x0, 0x154(%rsp,%rax)
movl %r13d, 0xc(%rsp)
movq %r12, 0x30(%rsp)
movq %rbx, %rdi
leaq 0xc11(%rip), %rsi       # 0x2093
callq 0x10c0
testq %rax, %rax
je 0x1589
movq %rax, %rbp
movslq 0x10(%rsp), %r14
leaq 0xbf7(%rip), %rdi       # 0x2096
movq %rbx, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1070
movq %r14, (%rsp)
testq %r14, %r14
jle 0x14ec
movq 0x28(%rsp), %r12
xorl %r13d, %r13d
movq %r12, %rbx
movq 0x48(%rsp), %r14
movsbl (%rbx), %edi
movq %rbp, %rsi
callq 0x1090
decq %r14
addq $-0x8000, %rbx          # imm = 0x8000
cmpq $0x1, %r14
jg 0x14c5
incq %r13
incq %r12
cmpq (%rsp), %r13
jne 0x14bd
movq %rbp, %rdi
callq 0x1050
xorl %r14d, %r14d
movq 0x30(%rsp), %r12
movl 0xc(%rsp), %r13d
movq 0x20(%rsp), %rbx
incq %rbx
cmpq 0x40(%rsp), %rbx
jne 0x128d
testl %r14d, %r14d
je 0x1535
leaq 0xb97(%rip), %rdi       # 0x20b7
callq 0x1040
jmp 0x1535
leaq 0xb2c(%rip), %rdi       # 0x205a
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0x20258, %rsp          # imm = 0x20258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbl %al, %edx
movq 0x2aa5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaa7(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x10b0
jmp 0x1535
leaq 0xb5c(%rip), %rdi       # 0x20c9
callq 0x1040
jmp 0x1582
leaq 0xa9a(%rip), %rdi       # 0x2015
xorl %eax, %eax
callq 0x1070
xorl %edi, %edi
callq 0x10d0
leaq 0xa85(%rip), %rdi       # 0x2015
leaq 0x150(%rsp), %rsi
jmp 0x157b