// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movl $0x0, 0x74(%rsp)
movl %edi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
cmpl $0x5, 0x70(%rsp)
jge 0x11ff
movq 0x68(%rsp), %rax
movq (%rax), %rsi
leaq 0xe19(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1040
movl $0x0, 0x74(%rsp)
jmp 0x1563
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x68(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rdi
leaq 0xde1(%rip), %rsi       # 0x2039
callq 0x10a0
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x1294
movq 0x2d87(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x60(%rsp), %rdx
leaq 0xdbc(%rip), %rsi       # 0x203c
movb $0x0, %al
callq 0x1070
movl $0x1, 0x74(%rsp)
jmp 0x1563
movq 0x58(%rsp), %rdi
leaq 0xd99(%rip), %rsi       # 0x2039
callq 0x10a0
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x12e6
movq 0x2d3f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x58(%rsp), %rdx
leaq 0xd74(%rip), %rsi       # 0x203c
movb $0x0, %al
callq 0x1070
movq 0x38(%rsp), %rdi
callq 0x1030
movl $0x1, 0x74(%rsp)
jmp 0x1563
movq 0x48(%rsp), %rdi
leaq 0xd69(%rip), %rsi       # 0x205b
callq 0x10a0
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x1342
movq 0x2ced(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x48(%rsp), %rdx
leaq 0xd44(%rip), %rsi       # 0x205e
movb $0x0, %al
callq 0x1070
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
callq 0x1030
movl $0x1, 0x74(%rsp)
jmp 0x1563
movq 0x38(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1090
movq 0x38(%rsp), %rdi
callq 0x1080
movq %rax, 0x20(%rsp)
movq 0x38(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1090
movq 0x30(%rsp), %rdi
callq 0x1080
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
movq 0x20(%rsp), %rsi
movq 0x20(%rsp), %rdx
leaq 0xcc6(%rip), %rdi       # 0x207d
movb $0x0, %al
callq 0x1040
movq 0x18(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x40(%rsp), %rcx
leaq 0xcc7(%rip), %rdi       # 0x209b
movb $0x0, %al
callq 0x1040
movq 0x40(%rsp), %rax
cmpq 0x20(%rsp), %rax
jb 0x1420
leaq 0xcd0(%rip), %rdi       # 0x20be
movb $0x0, %al
callq 0x1040
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0x1, 0x74(%rsp)
jmp 0x1563
movq 0x40(%rsp), %rax
addq 0x18(%rsp), %rax
cmpq 0x20(%rsp), %rax
jb 0x146a
leaq 0xcbb(%rip), %rdi       # 0x20f3
movb $0x0, %al
callq 0x1040
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0x1, 0x74(%rsp)
jmp 0x1563
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq 0x40(%rsp), %rax
jae 0x14a5
movq 0x38(%rsp), %rdi
callq 0x1050
movl %eax, %edi
movq 0x28(%rsp), %rsi
callq 0x1060
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x1473
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpq 0x18(%rsp), %rax
jae 0x14e0
movq 0x30(%rsp), %rdi
callq 0x1050
movl %eax, %edi
movq 0x28(%rsp), %rsi
callq 0x1060
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x14ae
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
addq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x1090
movq $0x0, (%rsp)
movq (%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rdx
addq 0x18(%rsp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x153d
movq 0x38(%rsp), %rdi
callq 0x1050
movl %eax, %edi
movq 0x28(%rsp), %rsi
callq 0x1060
movq (%rsp), %rax
addq $0x1, %rax
movq %rax, (%rsp)
jmp 0x14fe
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq