// File: /workspace/github_c_makefile_star2_O2/Mikejmoffitt[P]romtools/namcosum/namcosum.c
// Function: main
int main(int argc, char **argv)
{
	int ret = -1;
	uint8_t *data = NULL;
	FILE *f = NULL;
	if (argc < 3)
	{
		printf("Usage: %s prg.bin rom_size <desired value>\n", argv[0]);
		ret = 0;
		goto done;
	}
	const char *fname = argv[1];
	const size_t rom_size = strtoul(argv[2], NULL, 0);
	uint8_t desired_value = 0;
	if (argc >= 4)
	{
		desired_value = strtoul(argv[3], NULL, 0);
	}
	// set up buffer
	f = fopen(fname, "rb");
	if (!f)
	{
		fprintf(stderr, "Couldn't open %s.\n", fname);
		goto done;
	}
	fseek(f, 0, SEEK_END);
	const size_t len = ftell(f);
	fseek(f, 0, SEEK_SET);
	data = malloc(len);
	if (!data)
	{
		fprintf(stderr, "Couldn't malloc %d bytes\n", (int)len);
		goto done;
	}
	// read the data
	const size_t read_bytes = fread(data, 1, len, f);
	if (read_bytes != len)
	{
		fprintf(stderr, "Read %d bytes but expected %d\n",
		        (int)read_bytes, (int)len);
		goto done;
	}
	fclose(f);
	f = NULL;
	// mark checksums at 1K boundaries
	uint8_t sum = 0;
	for (size_t i = 0; i < len; i++)
	{
		const int rom_idx = (i % rom_size);
		if (rom_idx == 0) sum = 0;
		if (rom_idx == rom_size - 1)
		{
			data[i] = (0x100 + desired_value - sum) & 0xFF;
			printf("$%X := $%02X\n", (int)i, data[i]);
		}
		else
		{
			sum += data[i];
		}
	}
	// write the file back
	f = fopen(fname, "wb");
	if (!f)
	{
		fprintf(stderr, "Couldn't open %s for writing.\n", fname);
		goto done;
	}
	const size_t written_bytes = fwrite(data, 1, len, f);
	fclose(f);
	f = NULL;
	if (written_bytes != len)
	{
		fprintf(stderr, "Wrote %d bytes but expected %d\n",
		        (int)written_bytes, (int)len);
		goto done;
	}
	ret = 0;
done:
	if (data) free(data);
	if (f) fclose(f);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
cmpl $0x2, %edi
jg 0x120d
movq (%rbx), %rsi
leaq 0xe08(%rip), %rdi       # 0x2004
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x1060
xorl %ebx, %ebx
jmp 0x135b
movl %edi, %r14d
movq 0x8(%rbx), %rbp
movq 0x10(%rbx), %rdi
movq $0x0, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, %r15
cmpl $0x3, %r14d
je 0x1245
movq 0x18(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, 0x8(%rsp)
leaq 0xde4(%rip), %rsi       # 0x2030
movq %rbp, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1383
movq %rax, %r13
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r13, %rdi
callq 0x1080
movq %rax, %r12
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
movq %r12, %rdi
callq 0x1090
testq %rax, %rax
je 0x13a8
movq %rax, %rbx
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x1040
cmpq %r12, %rax
jne 0x13c7
movq %rbp, 0x10(%rsp)
movq %r13, %rdi
callq 0x1050
movq %r15, %r13
decq %r13
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq %r14, %r12
je 0x1313
movq %r14, %rax
xorl %edx, %edx
divq %r15
testl %edx, %edx
movzbl %bpl, %ebp
cmovel %edx, %ebp
movslq %edx, %rax
cmpq %r13, %rax
jne 0x130a
movq 0x8(%rsp), %rax
subb %bpl, %al
movb %al, (%rbx,%r14)
movzbl %al, %edx
leaq 0xd81(%rip), %rdi       # 0x207f
movl %r14d, %esi
xorl %eax, %eax
callq 0x1060
jmp 0x130e
addb (%rbx,%r14), %bpl
incq %r14
jmp 0x12ca
leaq 0xd73(%rip), %rsi       # 0x208d
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13dc
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %r12, %rdx
movq %rax, %rcx
callq 0x10d0
movq %rax, %r14
movq %r15, %rdi
callq 0x1050
cmpq %r12, %r14
jne 0x13fc
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x1030
testq %r13, %r13
je 0x1372
movq %r13, %rdi
callq 0x1050
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c6e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc9f(%rip), %rsi       # 0x2033
xorl %r13d, %r13d
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1070
pushq $-0x1
popq %rbp
xorl %ebx, %ebx
jmp 0x135d
movq 0x2c49(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc8d(%rip), %rsi       # 0x2046
xorl %ebx, %ebx
movl %r12d, %edx
xorl %eax, %eax
callq 0x1070
jmp 0x141d
movq 0x2c2a(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xc88(%rip), %rsi       # 0x2060
movl %eax, %edx
jmp 0x1413
movq 0x2c15(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xca3(%rip), %rsi       # 0x2090
xorl %r13d, %r13d
movq %r14, %rdx
xorl %eax, %eax
callq 0x1070
jmp 0x141d
movq 0x2bf5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xca2(%rip), %rsi       # 0x20af
xorl %r13d, %r13d
movl %r14d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1070
pushq $-0x1
popq %rbp
jmp 0x135d