// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/spectrum512.c
// Function: do_run_spectrum512c_s_internal
static void do_run_spectrum512c_s_internal(deark *c, de_module_params *mparams, int is_sps1)
{
	struct spc_sps_ctx *d = NULL;
	i64 pos;
	dbuf *unc_pixels_planar = NULL;
	dbuf *spufile = NULL;
	int to_spu = 0;

	d = de_malloc(c, sizeof(struct spc_sps_ctx));
	d->is_sps = is_sps1;
	de_declare_fmtf(c, "Spectrum 512 %s", (d->is_sps?"Smooshed":"Compressed"));

	if(de_get_ext_option(c, "spectrum512:tospu")) {
		to_spu = 1;
	}

	pos = 4;
	d->pixels_cmpr_len = de_getu32be_p(&pos);
	de_dbg(c, "pixels compressed len: %"I64_FMT, d->pixels_cmpr_len);
	d->pal_cmpr_len = de_getu32be_p(&pos);
	de_dbg(c, "palette compressed len: %"I64_FMT, d->pal_cmpr_len);

	d->pixels_pos = pos;
	d->pal_pos = d->pixels_pos + d->pixels_cmpr_len;

	if(d->pal_pos + d->pal_cmpr_len > c->infile->len) {
		de_err(c, "Invalid or truncated file");
		goto done;
	}

	if(d->is_sps) {
		de_dbg(c, "format code at %"I64_FMT, d->pal_pos + d->pal_cmpr_len-1);
		d->sps_format_code = de_getbyte(d->pal_pos + d->pal_cmpr_len-1);
		d->sps_format_code &= 0x1;
		de_dbg(c, "format code: %u", (UI)d->sps_format_code);
	}

	de_dbg(c, "pixels at %"I64_FMT, d->pixels_pos);
	// Decompress the pixel data into an in-memory buffer.
	unc_pixels_planar = dbuf_create_membuf(c, 32000, 1);
	dbuf_enable_wbuffer(unc_pixels_planar);
	decompress_spc_or_sps(c, d, unc_pixels_planar);
	dbuf_disable_wbuffer(unc_pixels_planar);

	// We'll construct an in-memory SPU file, then (usually) use our
	// SPU module's decoder to process it.
	spufile = dbuf_create_membuf(c, 51104, 0x1);

	// Rearrange the bytes in the image data, as we write them to our
	// in-memory image of an SPU file.
	// (This could be done more efficiently during decompression,
	// but the code would be messier.)
	if(d->is_sps && d->sps_format_code==0) {
		reorder_img_bytes(c, unc_pixels_planar, spufile, reorderfn_sps0);
	}
	else {
		reorder_img_bytes(c, unc_pixels_planar, spufile, reorderfn_spc);
	}

	// Make sure we write the uncompressed palette at exactly offset 32000.
	dbuf_truncate(spufile, 32000);

	de_dbg(c, "palette at %"I64_FMT, d->pal_pos);
	if(d->is_sps) {
		sps_decompress_pal(c, d->pal_pos, spufile);
	}
	else {
		spc_decompress_pal(c, d->pal_pos, spufile);
	}

	if(to_spu) {
		// Instead of decoding the image, write it in .SPU format
		dbuf *outf = NULL;
		outf = dbuf_create_output_file(c, "spu", NULL, 0);
		dbuf_copy(spufile, 0, spufile->len, outf);
		dbuf_close(outf);
	}
	else {
		do_spu_internal(c, spufile, 0);
	}

done:
	dbuf_close(unc_pixels_planar);
	dbuf_close(spufile);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %esi, %ebp
movq %rdi, %r12
movl $0x28, %esi
callq 0xe8b2b
movq %rax, %rbx
movl %ebp, (%rax)
testl %ebp, %ebp
leaq 0x74a0e(%rip), %rax     # 0x13b679
leaq 0x74a2c(%rip), %rdx     # 0x13b69e
cmoveq %rax, %rdx
leaq 0x74a55(%rip), %rsi     # 0x13b6d2
movq %r12, %rdi
xorl %eax, %eax
callq 0xeb152
leaq 0x74a54(%rip), %rsi     # 0x13b6e2
movq %r12, %rdi
callq 0xe9b74
movq %rax, %r14
leaq 0x70(%rsp), %r15
movq $0x4, (%r15)
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xeed31
movq %rax, 0x10(%rbx)
leaq 0x74a37(%rip), %rsi     # 0x13b6f4
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xeed31
movq %rax, 0x20(%rbx)
leaq 0x74a2d(%rip), %rsi     # 0x13b70f
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%r15), %rdx
movq %rdx, 0x8(%rbx)
movq 0x10(%rbx), %rax
addq %rdx, %rax
movq %rax, 0x18(%rbx)
addq 0x20(%rbx), %rax
movq 0x18(%r12), %rcx
cmpq 0x18(%rcx), %rax
movq %rbx, 0x18(%rsp)
jle 0xc6d30
leaq 0x4ce88(%rip), %rsi     # 0x113ba4
xorl %ebx, %ebx
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9295
xorl %r13d, %r13d
jmp 0xc7264
movq %r14, 0x40(%rsp)
cmpl $0x0, (%rbx)
je 0xc6d87
decq %rax
leaq 0x749e7(%rip), %rsi     # 0x13b72b
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r12), %rdi
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq (%rax,%rcx), %rsi
decq %rsi
callq 0xeead3
andb $0x1, %al
movb %al, 0x4(%rbx)
movzbl %al, %edx
leaq 0x749c5(%rip), %rsi     # 0x13b73e
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rbx), %rdx
leaq 0x7ae29(%rip), %rsi     # 0x141bb7
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8617
movl $0x7d00, %esi           # imm = 0x7D00
movq %r12, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %rbp
movq %rax, %rdi
callq 0xf0855
leaq 0x50(%rsp), %r14
leaq 0x28(%rsp), %r15
leaq 0x78(%rsp), %rcx
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xfa1dc
movq %r12, 0x20(%rsp)
movq 0x18(%r12), %rax
movq %rax, (%r14)
movq 0x8(%rbx), %r13
movq %r13, 0x8(%r14)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r14)
movq %rbp, 0x10(%rsp)
movq %rbp, (%r15)
movb $0x1, 0x8(%r15)
movq $0x7d00, 0x10(%r15)     # imm = 0x7D00
addq %r13, %rax
movq %rax, 0x8(%rsp)
cmpl $0x0, (%rbx)
je 0xc6eb5
movb $0x1, %al
xorl %r14d, %r14d
testb %al, %al
je 0xc6e29
cmpq 0x38(%rsp), %r14
jge 0xc6f50
leaq 0x2(%r13), %rax
cmpq 0x8(%rsp), %rax
jg 0xc6f50
movq 0x50(%rsp), %rdi
leaq 0x1(%r13), %rbp
movq %r13, %rsi
callq 0xeead3
testb %al, %al
js 0xc6e76
addb $0x3, %al
movzbl %al, %r15d
movq 0x50(%rsp), %rdi
addq $0x2, %r13
movq %rbp, %rsi
callq 0xeead3
movq 0x28(%rsp), %rdi
movzbl %al, %esi
movq %r15, %rdx
callq 0xf0eba
jmp 0xc6ea9
movzbl %al, %r15d
leaq (%r15,%rbp), %rbx
addq $-0x7f, %rbx
cmpq 0x8(%rsp), %rbx
jg 0xc6f50
addq $-0x7f, %r15
movq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq %rbp, %rsi
movq %r15, %rdx
callq 0xef481
movq %rbx, %r13
addq %r15, %r14
movb 0x30(%rsp), %al
jmp 0xc6e1a
movb $0x1, %al
xorl %r12d, %r12d
testb %al, %al
je 0xc6ec9
cmpq 0x38(%rsp), %r12
jge 0xc6f50
leaq 0x2(%r13), %rax
cmpq 0x8(%rsp), %rax
jg 0xc6f50
movq 0x50(%rsp), %rdi
leaq 0x1(%r13), %r15
movq %r13, %rsi
callq 0xeead3
movzbl %al, %ebp
testb %al, %al
js 0xc6f1a
leaq (%r15,%rbp), %rbx
incq %rbx
cmpq 0x8(%rsp), %rbx
jg 0xc6f50
incq %rbp
movq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq %r15, %rsi
movq %rbp, %rdx
callq 0xef481
movq %rbp, %r14
movq %rbx, %r13
jmp 0xc6f44
movl $0x102, %r14d           # imm = 0x102
subq %rbp, %r14
movq 0x50(%rsp), %rdi
addq $0x2, %r13
movq %r15, %rsi
callq 0xeead3
movq 0x28(%rsp), %rdi
movzbl %al, %esi
movq %r14, %rdx
callq 0xf0eba
addq %r14, %r12
movb 0x30(%rsp), %al
jmp 0xc6eba
movq 0x28(%rsp), %rdi
callq 0xf08ba
subq 0x58(%rsp), %r13
movq %r13, 0x80(%rsp)
movb $0x1, 0x7c(%rsp)
movq 0x10(%rsp), %r12
movq 0x18(%r12), %rcx
leaq 0x4bd1b(%rip), %rsi     # 0x112c98
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r12), %rax
leaq -0x7ce3(%rax), %rcx
cmpq $-0x84, %rcx
ja 0xc6fba
leaq 0x747a3(%rip), %rsi     # 0x13b74e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%r12), %rax
cmpq $0x7c61, %rax           # imm = 0x7C61
jl 0xc6fcf
movl $0x7c60, %esi           # imm = 0x7C60
movq %r12, %rdi
callq 0xf0f27
movq %r12, %rdi
callq 0xf087d
movl $0xc7a0, %esi           # imm = 0xC7A0
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %r13
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
je 0xc7000
cmpb $0x0, 0x4(%rax)
je 0xc708f
cmpq $0x0, 0x18(%r12)
jle 0xc70a1
movl $0x7c5d, %ebx           # imm = 0x7C5D
xorl %r14d, %r14d
movabsq $-0x7c42d597e3916c99, %rbp # imm = 0x83BD2A681C6E9367
xorl %r15d, %r15d
movq %r12, %rdi
movq %r15, %rsi
callq 0xeead3
movl %eax, %ecx
xorl %eax, %eax
testb $0x1, %r15b
sete %al
leaq (%rax,%rax,2), %rdx
leaq (%rbx,%rdx), %rax
addq $0x9d, %rax
leaq (%r14,%rdx), %rdi
addq $0x9d, %rdi
movl $0x7cff, %edx           # imm = 0x7CFF
cmpq %rdx, %rdi
cmovlq %rdi, %rdx
subq %rdx, %rax
mulq %rbp
shrq $0xe, %rdx
imulq $-0x7c5e, %rdx, %rsi    # imm = 0x83A2
addq %rdi, %rsi
movzbl %cl, %edx
movq %r13, %rdi
callq 0xf0e8b
incq %r15
addq $0x4, %r14
addq $0x4, %rbx
cmpq 0x18(%r12), %r15
jl 0xc7021
jmp 0xc70a1
leaq 0x24b(%rip), %rdx       # 0xc72e1
movq %r12, %rdi
movq %r13, %rsi
callq 0xc7293
movl $0x7d00, %esi           # imm = 0x7D00
movq %r13, %rdi
callq 0xf0f27
movq 0x18(%rsp), %rbx
movq 0x18(%rbx), %rdx
leaq 0x6f52d(%rip), %rsi     # 0x1365eb
movq 0x20(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8617
cmpl $0x0, (%rbx)
movq 0x18(%rbx), %r15
je 0xc71b5
movq 0x18(%r12), %rax
xorps %xmm0, %xmm0
leaq 0x78(%rsp), %rcx
movups %xmm0, 0x18(%rcx)
movq $0x0, 0x28(%rcx)
movq %rax, (%rcx)
movq %r15, 0x8(%rcx)
movq 0x18(%rax), %rax
movq %rax, 0x10(%rcx)
xorl %eax, %eax
movq %rax, 0x48(%rsp)
movl $0xe, %ebx
leaq 0x78(%rsp), %rdi
movl $0xe, %esi
callq 0xf1bed
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
testl %ebp, %ebp
je 0xc7189
cmpl $0xf, %ebp
je 0xc7189
movq 0x8(%rsp), %rax
btl %ebx, %eax
jae 0xc7189
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movl $0x3, %esi
callq 0xf1bed
movq %rax, %r14
movq %r15, %rdi
movl $0x3, %esi
callq 0xf1bed
movq %rax, %r12
movq %r15, %rdi
movl $0x3, %esi
callq 0xf1bed
shll $0x8, %r14d
shll $0x4, %r12d
orl %r14d, %r12d
orl %eax, %r12d
movq %r13, %rdi
movq %r12, %rsi
movq 0x20(%rsp), %r12
callq 0xf0fad
jmp 0xc7196
movl $0x2, %esi
movq %r13, %rdi
callq 0xf0d35
incl %ebp
decl %ebx
cmpl $-0x2, %ebx
jne 0xc7124
movq 0x48(%rsp), %rax
incq %rax
cmpq $0x255, %rax            # imm = 0x255
jne 0xc7104
jmp 0xc7219
xorl %ebx, %ebx
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xeeb67
movq %rax, %rbp
addq $0x2, %r15
movl $0x10, %r14d
cmpq $0x1, %r14
je 0xc71f9
movl %ebp, %eax
andl $0x1, %eax
je 0xc71f9
movq 0x18(%r12), %rdi
movl $0x2, %edx
movq %r15, %rsi
movq %r13, %rcx
callq 0xef481
addq $0x2, %r15
jmp 0xc7206
movl $0x2, %esi
movq %r13, %rdi
callq 0xf0d35
shrl %ebp
decq %r14
jne 0xc71d1
incq %rbx
cmpq $0x255, %rbx            # imm = 0x255
jne 0xc71b7
cmpq $0x0, 0x40(%rsp)
je 0xc7252
leaq 0x744c8(%rip), %rsi     # 0x13b6f0
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %r14
movq 0x18(%r13), %rdx
movq %r13, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xef481
movq %r14, %rdi
callq 0xf052b
jmp 0xc725f
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xc6b16
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0xf052b
movq %r13, %rdi
callq 0xf052b
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0xe8be2
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq