// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movl $0x28, %esi
callq 0x15b780
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %ecx
leaq 0xa034b(%rip), %rdx     # 0x1c6eb9
leaq 0xa0369(%rip), %rax     # 0x1c6ede
cmpl $0x0, %ecx
cmovneq %rax, %rdx
leaq 0xa038f(%rip), %rsi     # 0x1c6f12
movb $0x0, %al
callq 0x15e3d0
movq -0x8(%rbp), %rdi
leaq 0xa038d(%rip), %rsi     # 0x1c6f22
callq 0x15cb30
cmpq $0x0, %rax
je 0x126ba7
movl $0x1, -0x3c(%rbp)
movq $0x4, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0xa0356(%rip), %rsi     # 0x1c6f34
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0xa033b(%rip), %rsi     # 0x1c6f4f
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
addq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
addq 0x20(%rcx), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x126c74
movq -0x8(%rbp), %rdi
leaq 0x78e6c(%rip), %rsi     # 0x19fad4
movb $0x0, %al
callq 0x15bfb0
jmp 0x126e62
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x126cf5
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x20(%rbp), %rax
addq 0x20(%rax), %rdx
subq $0x1, %rdx
leaq 0xa02cf(%rip), %rsi     # 0x1c6f6b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rax
addq 0x20(%rax), %rsi
subq $0x1, %rsi
callq 0x165060
movb %al, %cl
movq -0x20(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x20(%rbp), %rax
movzbl 0x4(%rax), %ecx
andl $0x1, %ecx
movb %cl, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movzbl 0x4(%rax), %edx
leaq 0xa0290(%rip), %rsi     # 0x1c6f7e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xa6a5c(%rip), %rsi     # 0x1cd764
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x7d00, %esi           # imm = 0x7D00
movl $0x1, %edx
callq 0x168020
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x126e90
movq -0x30(%rbp), %rdi
callq 0x167be0
movq -0x8(%rbp), %rdi
movl $0xc7a0, %esi           # imm = 0xC7A0
movl $0x1, %edx
callq 0x168020
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x126d90
movq -0x20(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
jne 0x126d90
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq 0x2a7(%rip), %rcx       # 0x127030
callq 0x126fb0
jmp 0x126da8
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq 0x33d(%rip), %rcx       # 0x1270e0
callq 0x126fb0
movq -0x38(%rbp), %rdi
movl $0x7d00, %esi           # imm = 0x7D00
callq 0x168900
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x9b124(%rip), %rsi     # 0x1c1eed
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x126df0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
callq 0x127150
jmp 0x126e05
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
callq 0x127290
cmpl $0x0, -0x3c(%rbp)
je 0x126e51
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xa0112(%rip), %rsi     # 0x1c6f30
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x48(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x48(%rbp), %rdi
callq 0x1676a0
jmp 0x126e60
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0x126960
jmp 0x126e62
movq -0x30(%rbp), %rdi
callq 0x1676a0
movq -0x38(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)