// File: /workspace/github_c_makefile_star2_O2/Mikejmoffitt[P]romtools/bsplat/bsplat.c
// Function: main
int main(int argc, char **argv)
{
	if (argc < 5)
	{
		printf("usage: %s base_fname overlay_fname offset out_fname\n", argv[0]);
		return 0;
	}

	const char *base_fname_arg = argv[1];
	const char *overlay_fname_arg = argv[2];
	const char *offset_arg = argv[3];
	const char *out_fname_arg = argv[4];
	const size_t offset = strtoul(offset_arg, NULL, 0);

	FILE *fi = fopen(base_fname_arg, "rb");
	if (!fi)
	{
		fprintf(stderr, "Couldn't open %s for reading.\n", base_fname_arg);
		return 1;
	}

	FILE *fs = fopen(overlay_fname_arg, "rb");
	if (!fs)
	{
		fprintf(stderr, "Couldn't open %s for reading.\n", overlay_fname_arg);
		fclose(fi);
		return 1;
	}

	FILE *fo = fopen(out_fname_arg, "wb");
	if (!fo)
	{
		fprintf(stderr, "Couldn't open %s for writing.\n", out_fname_arg);
		fclose(fi);
		fclose(fs);
		return 1;
	}

	fseek(fi, 0, SEEK_END);
	const size_t fi_size = ftell(fi);
	fseek(fi, 0, SEEK_SET);
	fseek(fs, 0, SEEK_END);
	const size_t fs_size = ftell(fs);
	fseek(fs, 0, SEEK_SET);

	printf("Base file: %ld ($%lX) bytes.\n", fi_size, fi_size);
	printf("Overlay: %ld ($%lX) bytes @ $%lX.\n", fs_size, fs_size, offset);

	if (offset >= fi_size)
	{
		printf("Warning: Overlay data begins outside original size.\n");
		fclose(fi);
		fclose(fs);
		fclose(fo);
		return 1;
	}
	if (offset + fs_size >= fi_size)
	{
		printf("Warning: Overlay data overruns original size.\n");
		fclose(fi);
		fclose(fs);
		fclose(fo);
		return 1;
	}

	for (size_t i = 0; i < offset; i++)
	{
		fputc(fgetc(fi), fo);
	}
	for (size_t i = 0; i < fs_size; i++)
	{
		fputc(fgetc(fs), fo);
	}
	fseek(fi, offset + fs_size, SEEK_SET);
	for (size_t i = 0; i < fi_size - (offset + fs_size); i++)
	{
		fputc(fgetc(fi), fo);
	}

	fclose(fi);
	fclose(fs);
	fclose(fo);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x4, %edi
jg 0x11f4
movq (%rsi), %rsi
leaq 0xe1e(%rip), %rdi       # 0x2004
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1050
jmp 0x138f
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %r15
movq 0x18(%rsi), %rdi
movq 0x20(%rsi), %r13
xorl %esi, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, %r12
leaq 0xe22(%rip), %rsi       # 0x2039
movq %r14, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13a0
movq %rax, %rbx
leaq 0xe07(%rip), %rsi       # 0x2039
movq %r15, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13bd
movq %rax, %r14
leaq 0xe0e(%rip), %rsi       # 0x205b
movq %r13, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13dd
movq %rax, %r15
pushq $0x2
popq %rbp
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x10a0
movq %rbx, %rdi
callq 0x1090
movq %rax, %r13
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
movq %r14, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x10a0
movq %r14, %rdi
callq 0x1090
movq %rax, %rbp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
leaq 0xdcc(%rip), %rdi       # 0x207d
movq %r13, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1050
leaq 0xdd6(%rip), %rdi       # 0x209b
movq %rbp, %rsi
movq %rbp, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1050
cmpq %r13, %r12
jae 0x1343
movq %r12, %rax
addq %rbp, %rax
subq %rax, %r13
jbe 0x134c
movq %rax, (%rsp)
subq $0x1, %r12
jb 0x1315
movq %rbx, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
jmp 0x12e9
movq %r14, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
subq $0x1, %rbp
jae 0x1303
movq %rbx, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0x10a0
subq $0x1, %r13
jb 0x1375
movq %rbx, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
jmp 0x1329
leaq 0xda2(%rip), %rdi       # 0x20ec
jmp 0x1353
leaq 0xd6b(%rip), %rdi       # 0x20be
callq 0x1030
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
pushq $0x1
popq %rbx
jmp 0x138f
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c51(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc8b(%rip), %rsi       # 0x203c
movq %r14, %rdx
xorl %eax, %eax
callq 0x1080
jmp 0x1370
movq 0x2c34(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc6e(%rip), %rsi       # 0x203c
movq %r15, %rdx
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
jmp 0x136b
movq 0x2c14(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc70(%rip), %rsi       # 0x205e
movq %r13, %rdx
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
jmp 0x136b