// File: /workspace/github_c_makefile_star2_O3/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 0x139d
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 0x13ae
movq %rax, %rbx
leaq 0xe07(%rip), %rsi       # 0x2039
movq %r15, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13cb
movq %rax, %r14
leaq 0xe0e(%rip), %rsi       # 0x205b
movq %r13, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13eb
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %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 $0x2, %edx
callq 0x10a0
movq %r14, %rdi
callq 0x1090
movq %rax, %rbp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
leaq 0xdc9(%rip), %rdi       # 0x207d
movq %r13, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1050
leaq 0xdd3(%rip), %rdi       # 0x209b
movq %rbp, %rsi
movq %rbp, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1050
cmpq %r13, %r12
jae 0x136b
leaq (%r12,%rbp), %rax
subq %rax, %r13
jbe 0x1374
movq %rax, (%rsp)
testq %r12, %r12
je 0x130e
movq %rbx, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
decq %r12
jne 0x12f7
testq %rbp, %rbp
je 0x132a
movq %r14, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
decq %rbp
jne 0x1313
movq %rbx, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0x10a0
movq %rbx, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
decq %r13
jne 0x1338
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
xorl %ebx, %ebx
jmp 0x139d
leaq 0xd7a(%rip), %rdi       # 0x20ec
jmp 0x137b
leaq 0xd43(%rip), %rdi       # 0x20be
callq 0x1030
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
movl $0x1, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c43(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc7d(%rip), %rsi       # 0x203c
movq %r14, %rdx
xorl %eax, %eax
callq 0x1080
jmp 0x1398
movq 0x2c26(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc60(%rip), %rsi       # 0x203c
movq %r15, %rdx
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
jmp 0x1393
movq 0x2c06(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc62(%rip), %rsi       # 0x205e
movq %r13, %rdx
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
jmp 0x1393