// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %rbx
movl %ebp, (%rax)
testl %ebp, %ebp
leaq 0x73af2(%rip), %rax     # 0x13b2c1
leaq 0x73b10(%rip), %rdx     # 0x13b2e6
cmoveq %rax, %rdx
leaq 0x73b39(%rip), %rsi     # 0x13b31a
movq %r12, %rdi
xorl %eax, %eax
callq 0xeb703
leaq 0x73b38(%rip), %rsi     # 0x13b32a
movq %r12, %rdi
callq 0xea6cb
movq %rax, %r14
leaq 0x68(%rsp), %r15
movq $0x4, (%r15)
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xef34e
movq %rax, 0x10(%rbx)
leaq 0x73b1b(%rip), %rsi     # 0x13b33c
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xef34e
movq %rax, 0x20(%rbx)
leaq 0x73b11(%rip), %rsi     # 0x13b357
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%r15), %rdx
movq %rdx, 0x8(%rbx)
addq 0x10(%rbx), %rdx
movq %rdx, 0x18(%rbx)
addq 0x20(%rbx), %rdx
movq 0x18(%r12), %rax
cmpq 0x18(%rax), %rdx
jle 0xc788d
leaq 0x4c27c(%rip), %rsi     # 0x113af4
xorl %r14d, %r14d
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9df7
xorl %r13d, %r13d
jmp 0xc7e97
movq %r14, 0x38(%rsp)
cmpl $0x0, (%rbx)
je 0xc78dd
decq %rdx
leaq 0x73ad2(%rip), %rsi     # 0x13b373
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq (%rax,%rcx), %rsi
decq %rsi
callq 0xef0d5
andb $0x1, %al
movb %al, 0x4(%rbx)
movzbl %al, %edx
leaq 0x73ab3(%rip), %rsi     # 0x13b386
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rbx), %rdx
leaq 0x79def(%rip), %rsi     # 0x1416d7
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movl $0x7d00, %esi           # imm = 0x7D00
movq %r12, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %r13
movq %rax, %rdi
callq 0xf0e3f
leaq 0x48(%rsp), %r14
leaq 0x20(%rsp), %r15
leaq 0x70(%rsp), %rcx
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xfa6e0
movq %r12, 0x18(%rsp)
movq 0x18(%r12), %rax
movq %rax, (%r14)
movq 0x8(%rbx), %r12
movq %r12, 0x8(%r14)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r14)
movq %r13, 0x10(%rsp)
movq %r13, (%r15)
movq %rax, %r13
movb $0x1, 0x8(%r15)
movq $0x7d00, 0x10(%r15)     # imm = 0x7D00
addq %r12, %r13
movq %rbx, (%rsp)
cmpl $0x0, (%rbx)
movq %r13, 0x8(%rsp)
je 0xc7a15
xorl %ebx, %ebx
cmpb $0x0, 0x28(%rsp)
je 0xc798a
cmpq 0x30(%rsp), %rbx
jge 0xc7ab2
leaq 0x2(%r12), %rax
cmpq %r13, %rax
jg 0xc7ab2
movq 0x48(%rsp), %rdi
leaq 0x1(%r12), %rbp
movq %r12, %rsi
callq 0xef0d5
testb %al, %al
js 0xc79d7
addb $0x3, %al
movzbl %al, %r15d
movq 0x48(%rsp), %rdi
addq $0x2, %r12
movq %rbp, %rsi
callq 0xef0d5
movq 0x20(%rsp), %rdi
movzbl %al, %esi
movq %r15, %rdx
callq 0xf14bd
jmp 0xc7a0d
movzbl %al, %r15d
leaq (%r15,%rbp), %r14
addq $-0x7f, %r14
cmpq %r13, %r14
jg 0xc7ab2
addq $-0x7f, %r15
movq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %rbp, %rsi
movq %r15, %rdx
callq 0xefabc
movq %r14, %r12
movq 0x8(%rsp), %r13
addq %r15, %rbx
jmp 0xc7978
xorl %ebx, %ebx
cmpb $0x0, 0x28(%rsp)
je 0xc7a29
cmpq 0x30(%rsp), %rbx
jge 0xc7ab2
leaq 0x2(%r12), %rax
cmpq %r13, %rax
jg 0xc7ab2
movq 0x48(%rsp), %rdi
leaq 0x1(%r12), %r15
movq %r12, %rsi
callq 0xef0d5
movzbl %al, %ebp
testb %al, %al
js 0xc7a80
movq %r13, %rax
leaq (%r15,%rbp), %r13
incq %r13
cmpq %rax, %r13
jg 0xc7ab2
incq %rbp
movq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %r15, %rsi
movq %rbp, %rdx
callq 0xefabc
movq %rbp, %r14
movq %r13, %r12
movq 0x8(%rsp), %r13
jmp 0xc7aaa
movl $0x102, %r14d           # imm = 0x102
subq %rbp, %r14
movq 0x48(%rsp), %rdi
addq $0x2, %r12
movq %r15, %rsi
callq 0xef0d5
movq 0x20(%rsp), %rdi
movzbl %al, %esi
movq %r14, %rdx
callq 0xf14bd
addq %r14, %rbx
jmp 0xc7a17
movq 0x20(%rsp), %rdi
callq 0xf0ea4
subq 0x50(%rsp), %r12
movq %r12, 0x78(%rsp)
movb $0x1, 0x74(%rsp)
movq 0x10(%rsp), %rbp
movq 0x18(%rbp), %rcx
leaq 0x4b1ad(%rip), %rsi     # 0x112c88
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq $-0x7ce3, %rax          # imm = 0x831D
addq 0x18(%rbp), %rax
cmpq $-0x84, %rax
ja 0xc7b11
leaq 0x7388f(%rip), %rsi     # 0x13b396
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
cmpq $0x7c61, 0x18(%rbp)     # imm = 0x7C61
jl 0xc7b28
movl $0x7c60, %esi           # imm = 0x7C60
movq %rbp, %rdi
callq 0xf152a
movq %rbp, %rdi
callq 0xf0e67
movl $0xc7a0, %esi           # imm = 0xC7A0
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %r13
movq (%rsp), %rax
cmpl $0x0, (%rax)
je 0xc7b58
cmpb $0x0, 0x4(%rax)
je 0xc7bec
cmpq $0x0, 0x18(%rbp)
jle 0xc7cc8
movq %rbp, %r12
movl $0x7c5d, %ebx           # imm = 0x7C5D
xorl %r14d, %r14d
movabsq $-0x7c42d597e3916c99, %rbp # imm = 0x83BD2A681C6E9367
xorl %r15d, %r15d
movq %r12, %rdi
movq %r15, %rsi
callq 0xef0d5
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 0xf148e
incq %r15
addq $0x4, %r14
addq $0x4, %rbx
cmpq 0x18(%r12), %r15
jl 0xc7b7b
jmp 0xc7cc8
cmpq $0x0, 0x18(%rbp)
jle 0xc7cc8
movl $0xa0, %ebx
xorl %r15d, %r15d
movabsq $0x49539e3b2d066ea3, %r12 # imm = 0x49539E3B2D066EA3
movq %r15, %rax
shrq %rax
movabsq $0x5254e78ecb419ba9, %rcx # imm = 0x5254E78ECB419BA9
mulq %rcx
shrq $0x6, %rdx
imulq $0x18e, %rdx, %rcx      # imm = 0x18E
movq %r15, %rax
subq %rcx, %rax
movabsq $0x149539e3b2d066f, %rcx # imm = 0x149539E3B2D066F
mulq %rcx
movq %rdx, %rcx
movq %r15, %rax
movabsq $-0x7c44f3e85463d3bf, %rdx # imm = 0x83BB0C17AB9C2C41
mulq %rdx
movq %rdx, %rsi
shrq $0xc, %rsi
imulq $0x1f18, %rsi, %rdx     # imm = 0x1F18
movq %r15, %rax
subq %rdx, %rax
movabsq $0xa4a9cf1d968338, %rdx # imm = 0xA4A9CF1D968338
mulq %rdx
addq %rbx, %rcx
movl %edx, %eax
leaq (%rcx,%rax,8), %rax
leaq (%rax,%rsi,2), %r14
movq %r15, %rax
mulq %r12
movq %r15, %rax
subq %rdx, %rax
shrq %rax
addq %rdx, %rax
shrq $0x7, %rax
imulq $0x7c60, %rax, %rax     # imm = 0x7C60
subq %rax, %r14
movq %rbp, %rdi
movq %r15, %rsi
callq 0xef0d5
movzbl %al, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0xf148e
incq %r15
addq $0xa0, %rbx
cmpq 0x18(%rbp), %r15
jl 0xc7c09
movl $0x7d00, %esi           # imm = 0x7D00
movq %r13, %rdi
callq 0xf152a
movq (%rsp), %rbx
movq 0x18(%rbx), %rdx
leaq 0x6e4c4(%rip), %rsi     # 0x1361a8
movq 0x18(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
cmpl $0x0, (%rbx)
movq 0x18(%rbx), %r15
je 0xc7de4
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rcx
movaps %xmm0, 0x20(%rcx)
movaps %xmm0, 0x10(%rcx)
movaps %xmm0, (%rcx)
movq 0x18(%r12), %rax
movq %rax, (%rcx)
movq %r15, 0x8(%rcx)
movq 0x18(%rax), %rax
movq %rax, 0x10(%rcx)
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movl $0xe, %ebx
leaq 0x70(%rsp), %rdi
movl $0xe, %esi
callq 0xf2271
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
testl %ebp, %ebp
je 0xc7db4
cmpl $0xf, %ebp
je 0xc7db4
movq 0x8(%rsp), %rax
btl %ebx, %eax
jae 0xc7db4
movq %r13, %r15
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movl $0x3, %esi
callq 0xf2271
movq %rax, %r14
movq %r13, %rdi
movl $0x3, %esi
callq 0xf2271
movq %rax, %r12
movq %r13, %rdi
movq %r15, %r13
movl $0x3, %esi
callq 0xf2271
shll $0x8, %r14d
shll $0x4, %r12d
orl %r14d, %r12d
orl %eax, %r12d
movq %r15, %rdi
movq %r12, %rsi
movq 0x18(%rsp), %r12
callq 0xf15b0
jmp 0xc7dc1
movl $0x2, %esi
movq %r13, %rdi
callq 0xf1346
incl %ebp
decl %ebx
cmpl $-0x2, %ebx
jne 0xc7d49
movq 0x40(%rsp), %rax
incq %rax
cmpq $0x255, %rax            # imm = 0x255
jne 0xc7d29
jmp 0xc7e48
xorl %ebx, %ebx
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xef169
movq %rax, %rbp
addq $0x2, %r15
movl $0x10, %r14d
cmpq $0x1, %r14
je 0xc7e28
movl %ebp, %eax
andl $0x1, %eax
je 0xc7e28
movq 0x18(%r12), %rdi
movl $0x2, %edx
movq %r15, %rsi
movq %r13, %rcx
callq 0xefabc
addq $0x2, %r15
jmp 0xc7e35
movl $0x2, %esi
movq %r13, %rdi
callq 0xf1346
shrl %ebp
decq %r14
jne 0xc7e00
incq %rbx
cmpq $0x255, %rbx            # imm = 0x255
jne 0xc7de6
cmpq $0x0, 0x38(%rsp)
je 0xc7e81
leaq 0x734e1(%rip), %rsi     # 0x13b338
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r14
movq 0x18(%r13), %rdx
movq %r13, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xefabc
movq %r14, %rdi
callq 0xf0b13
jmp 0xc7e8e
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xc767a
movq (%rsp), %rbx
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xf0b13
movq %r13, %rdi
callq 0xf0b13
movq %r12, %rdi
movq %rbx, %rsi
callq 0xe9752
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq