// File: /workspace/github_c_makefile_star2_O3/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 0x120a
movq (%rbx), %rsi
leaq 0xe08(%rip), %rdi       # 0x2004
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1060
jmp 0x1364
movl %edi, %ebp
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %rdi
movq $0x0, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, %r13
cmpl $0x3, %ebp
je 0x1240
movq 0x18(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, 0x8(%rsp)
leaq 0xde9(%rip), %rsi       # 0x2030
movq %r12, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1375
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10a0
movq %rbp, %rdi
callq 0x1080
movq %rax, %r15
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
movq %r15, %rdi
callq 0x1090
testq %rax, %rax
je 0x1392
movq %rax, %r14
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x1040
cmpq %r15, %rax
jne 0x13af
movq %r12, 0x10(%rsp)
movq %rbp, %rdi
callq 0x1050
testq %r15, %r15
je 0x1313
movq %r13, %r12
decq %r12
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %rbx, %rax
xorl %edx, %edx
divq %r13
testl %edx, %edx
movzbl %bpl, %ebp
cmovel %edx, %ebp
movslq %edx, %rax
cmpq %r12, %rax
jne 0x1307
movq 0x8(%rsp), %rax
subb %bpl, %al
movb %al, (%r14,%rbx)
movzbl %al, %edx
leaq 0xd83(%rip), %rdi       # 0x207f
movl %ebx, %esi
xorl %eax, %eax
callq 0x1060
jmp 0x130b
addb (%r14,%rbx), %bpl
incq %rbx
cmpq %rbx, %r15
jne 0x12cd
leaq 0xd73(%rip), %rsi       # 0x208d
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13e3
movq %rax, %rbx
movl $0x1, %esi
movq %r14, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x10d0
movq %rax, %r12
movq %rbx, %rdi
callq 0x1050
xorl %ebx, %ebx
cmpq %r15, %r12
jne 0x1400
movq %r14, %rdi
callq 0x1030
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c7c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcad(%rip), %rsi       # 0x2033
movq %r12, %rdx
xorl %eax, %eax
callq 0x1070
jmp 0x13dc
movq 0x2c5f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xca3(%rip), %rsi       # 0x2046
movl %r15d, %edx
xorl %eax, %eax
callq 0x1070
jmp 0x13d4
movq 0x2c42(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xca0(%rip), %rsi       # 0x2060
movl %eax, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1070
movq %r14, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x1364
movq 0x2c0e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc9c(%rip), %rsi       # 0x2090
movq %r12, %rdx
xorl %eax, %eax
callq 0x1070
jmp 0x141e
movq 0x2bf1(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc9e(%rip), %rsi       # 0x20af
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1070
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x135c