// File: /workspace/github_c_makefile_star2_O1/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 0x1364
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 0x1391
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
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 0x13b8
movq %rax, %rbx
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x1040
cmpq %r12, %rax
jne 0x13d7
movq %rbp, 0x10(%rsp)
movq %r13, %rdi
callq 0x1050
testq %r12, %r12
je 0x131a
movq %r15, %r13
decq %r13
xorl %ebp, %ebp
xorl %r14d, %r14d
movq %r14, %rax
xorl %edx, %edx
divq %r15
testl %edx, %edx
movzbl %bpl, %ebp
cmovel %edx, %ebp
movslq %edx, %rax
cmpq %r13, %rax
jne 0x130e
movq 0x8(%rsp), %rax
subb %bpl, %al
movb %al, (%rbx,%r14)
movzbl %al, %edx
leaq 0xd7d(%rip), %rdi       # 0x207f
movl %r14d, %esi
xorl %eax, %eax
callq 0x1060
jmp 0x1312
addb (%rbx,%r14), %bpl
incq %r14
cmpq %r14, %r12
jne 0x12d3
leaq 0xd6c(%rip), %rsi       # 0x208d
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13ec
movq %rax, %r15
movl $0x1, %esi
movq %rbx, %rdi
movq %r12, %rdx
movq %rax, %rcx
callq 0x10d0
movq %rax, %r14
movq %r15, %rdi
callq 0x1050
cmpq %r12, %r14
jne 0x140c
xorl %r13d, %r13d
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x1373
movq %rbx, %rdi
callq 0x1030
testq %r13, %r13
je 0x1380
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 0x2c60(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc91(%rip), %rsi       # 0x2033
xorl %r13d, %r13d
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1070
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %ebx, %ebx
jmp 0x1366
movq 0x2c39(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc7d(%rip), %rsi       # 0x2046
xorl %ebx, %ebx
movl %r12d, %edx
xorl %eax, %eax
callq 0x1070
jmp 0x142d
movq 0x2c1a(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xc78(%rip), %rsi       # 0x2060
movl %eax, %edx
jmp 0x1423
movq 0x2c05(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc93(%rip), %rsi       # 0x2090
xorl %r13d, %r13d
movq %r14, %rdx
xorl %eax, %eax
callq 0x1070
jmp 0x142d
movq 0x2be5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc92(%rip), %rsi       # 0x20af
xorl %r13d, %r13d
movl %r14d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1070
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1366