// File: /workspace/github_c_makefile_star2_O1/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 %ebp, %ebp
xorl %eax, %eax
callq 0x1050
jmp 0x13a7
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %r15
movq 0x18(%rsi), %rdi
movq 0x20(%rsi), %r12
xorl %esi, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, %r13
leaq 0xe22(%rip), %rsi       # 0x2039
movq %r14, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13b8
movq %rax, %rbx
leaq 0xe07(%rip), %rsi       # 0x2039
movq %r15, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13d5
movq %rax, %r14
leaq 0xe0e(%rip), %rsi       # 0x205b
movq %r12, %rdi
callq 0x10b0
testq %rax, %rax
je 0x13f5
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10a0
movq %rbx, %rdi
callq 0x1090
movq %rax, %r12
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 %r12, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1050
leaq 0xdd3(%rip), %rdi       # 0x209b
movq %rbp, %rsi
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x1050
cmpq %r12, %r13
jae 0x1375
movq %rbp, %rax
addq %r13, %rax
cmpq %rax, %r12
jbe 0x137e
movq %rax, (%rsp)
testq %r13, %r13
je 0x1310
movq %rbx, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
decq %r13
jne 0x12f9
testq %rbp, %rbp
je 0x132c
movq %r14, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
decq %rbp
jne 0x1315
xorl %ebp, %ebp
movq %rbx, %rdi
movq (%rsp), %r13
movq %r13, %rsi
xorl %edx, %edx
callq 0x10a0
subq %r13, %r12
je 0x135b
movq %rbx, %rdi
callq 0x1060
movl %eax, %edi
movq %r15, %rsi
callq 0x1070
decq %r12
jne 0x1344
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
jmp 0x13a7
leaq 0xd70(%rip), %rdi       # 0x20ec
jmp 0x1385
leaq 0xd39(%rip), %rdi       # 0x20be
callq 0x1030
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
movl $0x1, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c39(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc73(%rip), %rsi       # 0x203c
movq %r14, %rdx
xorl %eax, %eax
callq 0x1080
jmp 0x13a2
movq 0x2c1c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc56(%rip), %rsi       # 0x203c
movq %r15, %rdx
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
jmp 0x139d
movq 0x2bfc(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc58(%rip), %rsi       # 0x205e
movq %r12, %rdx
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
jmp 0x139d