// File: /workspace/github_c_makefile_star2_O2/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, %r15
pushq $0x28
popq %rsi
callq 0xdecaf
movq %rax, %r12
movl %ebp, (%rax)
testl %ebp, %ebp
leaq 0x6f6a6(%rip), %rax     # 0x12e389
leaq 0x6f6c4(%rip), %rdx     # 0x12e3ae
cmoveq %rax, %rdx
leaq 0x6f6ed(%rip), %rsi     # 0x12e3e2
movq %r15, %rdi
xorl %eax, %eax
callq 0xe0824
leaq 0x6f6ec(%rip), %rsi     # 0x12e3f2
movq %r15, %rdi
callq 0xdfa87
movq %rax, %rbx
leaq 0x70(%rsp), %r14
movq $0x4, (%r14)
movq 0x18(%r15), %rdi
movq %r14, %rsi
callq 0xe4071
movq %rax, 0x10(%r12)
leaq 0x6f6cf(%rip), %rsi     # 0x12e404
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rdi
movq %r14, %rsi
callq 0xe4071
movq %rax, 0x20(%r12)
leaq 0x6f6c5(%rip), %rsi     # 0x12e41f
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq (%r14), %rdx
movq %rdx, 0x8(%r12)
movq 0x10(%r12), %rax
addq %rdx, %rax
movq %rax, 0x18(%r12)
addq 0x20(%r12), %rax
movq 0x18(%r15), %rcx
cmpq 0x18(%rcx), %rax
movq %r12, 0x18(%rsp)
jle 0xbedb0
leaq 0x47dad(%rip), %rsi     # 0x106b44
xorl %ebx, %ebx
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq $0x0, (%rsp)
jmp 0xbf277
movq %rbx, 0x38(%rsp)
cmpl $0x0, (%r12)
je 0xbee0d
decq %rax
leaq 0x6f675(%rip), %rsi     # 0x12e43b
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rdi
movq 0x18(%r12), %rax
movq 0x20(%r12), %rcx
leaq (%rax,%rcx), %rsi
decq %rsi
callq 0xe3eb4
andb $0x1, %al
movb %al, 0x4(%r12)
movzbl %al, %edx
leaq 0x6f650(%rip), %rsi     # 0x12e44e
movq %r15, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x8(%r12), %rdx
leaq 0x75953(%rip), %rsi     # 0x134767
movq %r15, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rdx
movl $0x7d00, %esi           # imm = 0x7D00
movq %r15, %rdi
callq 0xe59cd
movq %rax, %r13
movq %rax, %rdi
callq 0xe589c
leaq 0x50(%rsp), %r14
movq %r15, %rbx
leaq 0x20(%rsp), %r15
leaq 0x78(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xee7ee
movq %rbx, 0x8(%rsp)
movq 0x18(%rbx), %rax
movq %rax, (%r14)
movq 0x8(%r12), %rbx
movq %rbx, 0x8(%r14)
movq 0x10(%r12), %rax
movq %rax, 0x10(%r14)
movq %r13, 0x10(%rsp)
movq %r13, (%r15)
movb $0x1, 0x8(%r15)
movq $0x7d00, 0x10(%r15)     # imm = 0x7D00
addq %rbx, %rax
movq %rax, (%rsp)
cmpl $0x0, (%r12)
je 0xbef3c
movb $0x1, %al
xorl %r12d, %r12d
testb %al, %al
je 0xbeeb2
cmpq 0x30(%rsp), %r12
jge 0xbefd5
leaq 0x2(%rbx), %rax
cmpq (%rsp), %rax
jg 0xbefd5
movq 0x50(%rsp), %rdi
leaq 0x1(%rbx), %r15
movq %rbx, %rsi
callq 0xe3eb4
testb %al, %al
js 0xbeefe
addb $0x3, %al
movzbl %al, %r14d
movq 0x50(%rsp), %rdi
addq $0x2, %rbx
movq %r15, %rsi
callq 0xe3eb4
movq 0x20(%rsp), %rdi
movzbl %al, %esi
movq %r14, %rdx
callq 0xe5e40
jmp 0xbef30
movzbl %al, %r14d
leaq (%r14,%r15), %r13
addq $-0x7f, %r13
cmpq (%rsp), %r13
jg 0xbefd5
addq $-0x7f, %r14
movq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %r15, %rsi
movq %r14, %rdx
callq 0xe45f4
movq %r13, %rbx
addq %r14, %r12
movb 0x28(%rsp), %al
jmp 0xbeea3
movb $0x1, %al
xorl %ebp, %ebp
testb %al, %al
je 0xbef4f
cmpq 0x30(%rsp), %rbp
jge 0xbefd5
leaq 0x2(%rbx), %rax
cmpq (%rsp), %rax
jg 0xbefd5
movq 0x50(%rsp), %rdi
leaq 0x1(%rbx), %r14
movq %rbx, %rsi
callq 0xe3eb4
movzbl %al, %r12d
testb %al, %al
js 0xbef9f
leaq (%r14,%r12), %r13
incq %r13
cmpq (%rsp), %r13
jg 0xbefd5
incq %r12
movq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %r14, %rsi
movq %r12, %rdx
callq 0xe45f4
movq %r12, %r15
movq %r13, %rbx
jmp 0xbefc9
movl $0x102, %r15d           # imm = 0x102
subq %r12, %r15
movq 0x50(%rsp), %rdi
addq $0x2, %rbx
movq %r14, %rsi
callq 0xe3eb4
movq 0x20(%rsp), %rdi
movzbl %al, %esi
movq %r15, %rdx
callq 0xe5e40
addq %r15, %rbp
movb 0x28(%rsp), %al
jmp 0xbef40
movq 0x20(%rsp), %rdi
callq 0xe58e8
subq 0x58(%rsp), %rbx
movq %rbx, 0x80(%rsp)
movb $0x1, 0x7c(%rsp)
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rcx
leaq 0x46c77(%rip), %rsi     # 0x105c78
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rdx
movq %rax, %rbx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rax
leaq -0x7ce3(%rax), %rcx
cmpq $-0x84, %rcx
ja 0xbf03f
leaq 0x6f42d(%rip), %rsi     # 0x12e45e
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rax
cmpq $0x7c61, %rax           # imm = 0x7C61
jl 0xbf054
movl $0x7c60, %esi           # imm = 0x7C60
movq %rbx, %rdi
callq 0xe5eb2
movq %rbx, %rdi
callq 0xe58c4
pushq $0x1
popq %rdx
movl $0xc7a0, %esi           # imm = 0xC7A0
movq %r15, %rdi
callq 0xe59cd
movq %rax, (%rsp)
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
je 0xbf080
cmpb $0x0, 0x4(%rax)
je 0xbf089
leaq 0x2cc(%rip), %rdx       # 0xbf353
jmp 0xbf090
leaq 0x260(%rip), %rdx       # 0xbf2f0
movq %rbx, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
callq 0xbf2a7
movl $0x7d00, %esi           # imm = 0x7D00
movq %rbx, %rdi
callq 0xe5eb2
movq 0x18(%rsp), %rbx
movq 0x18(%rbx), %rdx
leaq 0x6a22f(%rip), %rsi     # 0x1292eb
movq %r15, %rdi
xorl %eax, %eax
callq 0xde896
cmpl $0x0, (%rbx)
movq 0x18(%rbx), %r14
je 0xbf1ae
xorps %xmm0, %xmm0
leaq 0x78(%rsp), %rcx
movups %xmm0, 0x18(%rcx)
andq $0x0, 0x28(%rcx)
movq 0x18(%r15), %rax
movq %rax, (%rcx)
movq %r14, 0x8(%rcx)
movq 0x18(%rax), %rax
movq %rax, 0x10(%rcx)
xorl %eax, %eax
pushq $0x2
popq %rbx
pushq $0x3
popq %r13
cmpq $0x255, %rax            # imm = 0x255
je 0xbf220
movq %rax, 0x40(%rsp)
leaq 0x78(%rsp), %rdi
pushq $0xe
popq %r12
movl %r12d, %esi
callq 0xe6a4a
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
testl %r14d, %r14d
je 0xbf18d
cmpl $0xf, %r14d
je 0xbf18d
cmpl $0x10, %r14d
je 0xbf1a1
movq 0x48(%rsp), %rax
btl %r12d, %eax
jae 0xbf18d
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movl %r13d, %esi
callq 0xe6a4a
movq %rax, %rbx
movq %r15, %rdi
movl %r13d, %esi
callq 0xe6a4a
movq %rax, %rbp
movq %r15, %rdi
movl %r13d, %esi
callq 0xe6a4a
shll $0x8, %ebx
shll $0x4, %ebp
orl %ebx, %ebp
pushq $0x2
popq %rbx
orl %eax, %ebp
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0xe5f1e
jmp 0xbf199
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0xe5d20
incl %r14d
decl %r12d
jmp 0xbf12a
movq 0x40(%rsp), %rax
incq %rax
jmp 0xbf100
xorl %ebx, %ebx
pushq $0x10
popq %r13
pushq $0x2
popq %r15
cmpq $0x255, %rbx            # imm = 0x255
je 0xbf220
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r14, %rsi
callq 0xe3f46
movq %rax, %r12
addq $0x2, %r14
movq %r13, %rbp
subq $0x1, %rbp
jb 0xbf21b
je 0xbf20a
movl %r12d, %eax
andl $0x1, %eax
je 0xbf20a
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq (%rsp), %rcx
callq 0xe45f4
addq $0x2, %r14
jmp 0xbf216
movq (%rsp), %rdi
movq %r15, %rsi
callq 0xe5d20
shrl %r12d
jmp 0xbf1dc
incq %rbx
jmp 0xbf1b8
cmpq $0x0, 0x38(%rsp)
je 0xbf25f
leaq 0x6f1d1(%rip), %rsi     # 0x12e400
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, %rbx
movq (%rsp), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rax, %rcx
callq 0xe45f4
movq %rbx, %rdi
callq 0xe5591
jmp 0xbf272
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0xbeb96
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0xe5591
movq (%rsp), %rdi
callq 0xe5591
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0xded32
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq