// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/rsc.c
// Function: de_run_rsc
static void de_run_rsc(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	const char *tmps;

	d = de_malloc(c, sizeof(lctx));
	d->c = c;
	d->avail_file_size = c->infile->len; // Starting value. Will be adjusted later.

	d->fmt = RSCFMT_UNKNOWN;
	tmps = de_get_ext_option(c, "rsc:fmt");
	if(tmps) {
		if(!de_strcmp(tmps, "pc")) {
			d->fmt = RSCFMT_PC;
		}
		else if(!de_strcmp(tmps, "atari")) {
			d->fmt = RSCFMT_ATARI;
		}
	}

	if(d->fmt==RSCFMT_UNKNOWN) {
		detect_rsc_format(c, d);
	}

	if(d->fmt==RSCFMT_UNKNOWN) {
		d->fmt = RSCFMT_ATARI;
	}

	if(d->fmt==RSCFMT_PC) {
		de_declare_fmt(c, "GEM RSC, PC");
		d->is_le = 1;
	}
	else {
		de_declare_fmt(c, "GEM RSC, Atari");
	}

	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_UNKNOWN);
	if(d->input_encoding==DE_ENCODING_UNKNOWN) {
		if(d->fmt==RSCFMT_ATARI) {
			d->input_encoding = DE_ENCODING_ATARIST;
		}
		else {
			// TODO?: This should probably be the "GEM character set", but we don't
			// support that.
			d->input_encoding = DE_ENCODING_ASCII;
		}
	}

	d->decode_objects = 1;
	// TODO: For Atari format, maybe we can disallow unaligned offsets.
	d->allow_unaligned_offsets = 1;

	de_dbg(c, "header at %d", 0);
	de_dbg_indent(c, 1);
	d->version = gem_getu16(d, 0);
	de_dbg(c, "version: 0x%04x", (int)d->version);

	d->object_offs = gem_getu16(d, 2);
	d->iconblk_offs = gem_getu16(d, 6);
	d->bitblk_offs = gem_getu16(d, 8);
	d->imagedata_offs = gem_getu16(d, 14);
	d->imagepointertable_offs = gem_getu16(d, 16);
	d->object_num = gem_getu16(d, 20);
	d->objecttree_num = gem_getu16(d, 22);
	d->iconblk_num = gem_getu16(d, 26);
	d->bitblk_num = gem_getu16(d, 28);
	d->rssize = gem_getu16(d, 34);

	de_dbg(c, "OBJECT: %d at %d", (int)d->object_num, (int)d->object_offs);
	de_dbg(c, "num object trees: %d", (int)d->objecttree_num);
	de_dbg(c, "ICONBLK: %d at %d", (int)d->iconblk_num, (int)d->iconblk_offs);
	de_dbg(c, "BITBLK: %d at %d", (int)d->bitblk_num, (int)d->bitblk_offs);
	de_dbg(c, "imagedata: at %d", (int)d->imagedata_offs);
	de_dbg(c, "imagepointertable: at %d", (int)d->imagepointertable_offs);
	if(d->version & 0x0004) {
		de_dbg(c, "extension array offset: %"I64_FMT, d->rssize);
	}
	else {
		de_dbg(c, "reported file size: %"I64_FMT, d->rssize);
		d->reported_file_size = d->rssize;
		d->avail_file_size = de_min_int(d->reported_file_size, c->infile->len);
	}
	de_dbg_indent(c, -1);

	if(d->version & 0x0004) {
		do_extension_array(c, d);
	}
	else if(d->version==0 || d->version==1) {
		;
	}
	else {
		de_err(c, "Unknown or unsupported version of RSC");
		goto done;
	}

	do_OBJECTs(c, d);
	do_BITBLKs(c, d);
	do_ICONBLKs(c, d);
	if(d->version & 0x0004) {
		do_cicon(c, d);
	}

done:
	de_free(c, d);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x4d8, %esi            # imm = 0x4D8
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x8(%rbp), %rdi
leaq 0xa3a6c(%rip), %rsi     # 0x1c661e
callq 0x15cb30
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x122c08
movq -0x20(%rbp), %rdi
leaq 0xa43e0(%rip), %rsi     # 0x1c6fad
callq 0x191a0
cmpl $0x0, %eax
jne 0x122be4
movq -0x18(%rbp), %rax
movl $0x2, 0xc(%rax)
jmp 0x122c06
movq -0x20(%rbp), %rdi
leaq 0xa3a37(%rip), %rsi     # 0x1c6626
callq 0x191a0
cmpl $0x0, %eax
jne 0x122c04
movq -0x18(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x122c06
jmp 0x122c08
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x122c1f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1230b0
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x122c34
movq -0x18(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x18(%rbp), %rax
cmpl $0x2, 0xc(%rax)
jne 0x122c5b
movq -0x8(%rbp), %rdi
leaq 0xa39e3(%rip), %rsi     # 0x1c662c
callq 0x15e380
movq -0x18(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0x122c6b
movq -0x8(%rbp), %rdi
leaq 0xa39d2(%rip), %rsi     # 0x1c6638
callq 0x15e380
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x122cb1
movq -0x18(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x122ca4
movq -0x18(%rbp), %rax
movl $0x12, 0x8(%rax)
jmp 0x122caf
movq -0x18(%rbp), %rax
movl $0x1, 0x8(%rax)
jmp 0x122cb1
movq -0x18(%rbp), %rax
movb $0x1, 0x14(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x15(%rax)
movq -0x8(%rbp), %rdi
leaq 0xa22c2(%rip), %rsi     # 0x1c4f8e
xorl %edx, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x9e133(%rip), %rsi     # 0x1c0e43
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rdi
movl $0x2, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rdi
movl $0x6, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x18(%rbp), %rdi
movl $0x8, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x18(%rbp), %rdi
movl $0xe, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rdi
movl $0x10, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rdi
movl $0x14, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rdi
movl $0x16, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rdi
movl $0x1a, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rdi
movl $0x1c, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x18(%rbp), %rdi
movl $0x22, %esi
callq 0x123250
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %ecx
leaq 0xa3817(%rip), %rsi     # 0x1c6647
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
leaq 0xa380c(%rip), %rsi     # 0x1c6658
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %ecx
leaq 0xa37fb(%rip), %rsi     # 0x1c666d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %ecx
leaq 0xa37e7(%rip), %rsi     # 0x1c667f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, %edx
leaq 0xa37dc(%rip), %rsi     # 0x1c6690
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %edx
leaq 0xa37d1(%rip), %rsi     # 0x1c66a1
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
andq $0x4, %rax
cmpq $0x0, %rax
je 0x122f05
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rdx
leaq 0xa37be(%rip), %rsi     # 0x1c66ba
movb $0x0, %al
callq 0x15b0d0
jmp 0x122f56
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rdx
leaq 0x8452d(%rip), %rsi     # 0x1a7445
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rsi
callq 0x15ccf0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
andq $0x4, %rax
cmpq $0x0, %rax
je 0x122f85
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x123290
jmp 0x122fb3
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x122f9b
movq -0x18(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jne 0x122f9d
jmp 0x122fb1
movq -0x8(%rbp), %rdi
leaq 0xa372e(%rip), %rsi     # 0x1c66d6
movb $0x0, %al
callq 0x15bfb0
jmp 0x122ffb
jmp 0x122fb3
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1233b0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1234c0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1235c0
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
andq $0x4, %rax
cmpq $0x0, %rax
je 0x122ff9
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1236c0
jmp 0x122ffb
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x20, %rsp
popq %rbp
retq
nop