// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/storyboard.c
// Function: decompress_oldfmt
static int decompress_oldfmt(deark *c, struct storyboard_ctx *d, i64 pos1,
	dbuf *outf)
{
	i64 pos = pos1;
	i64 nbytes_written = 0;
	i64 img_seg_size;
	int found_attribs = 0;
	int element_count = 0;
	int retval = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "compressed data segment at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	img_seg_size = de_getu16le_p(&pos);
	d->img_endpos = pos + img_seg_size;
	de_dbg(c, "segment size: %"I64_FMT" (ends at %"I64_FMT")", img_seg_size, d->img_endpos);
	if(img_seg_size<2) {
		d->need_errmsg = 1;
		goto done;
	}

	while(1) {
		UI n;
		i64 count;

		if(nbytes_written >= d->max_unc_size) break;
		if(pos >= c->infile->len) break;

		n = (UI)de_getu16le_p(&pos);
		if(n == 0x0000) { // Seems to be a special stop/separator code
			element_count++;
			if(element_count==1 && d->is_text) {
				// End of foreground, start of attributes.
				// Kind of a hack, but it's easiest just to decompress everything
				// in one go.
				dbuf_flush(outf);
				d->attribs_pos = outf->len;
				found_attribs = 1;
			}
			else {
				break;
			}
		}
		else if(n < 0x8000) {
			count = (i64)n;
			dbuf_copy(c->infile, pos, count, outf);
			pos += count;
			nbytes_written += count;
		}
		else {
			u8 v;

			count = (i64)(n-0x8000);
			v = de_getbyte_p(&pos);
			dbuf_write_run(outf, v, count);
			nbytes_written += count;
		}
	}

	if(d->is_text && !found_attribs) {
		d->need_errmsg = 1;
	}
	else {
		retval = 1;
	}
	de_dbg(c, "decompressed to %"I64_FMT" bytes", nbytes_written);

done:
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rsp), %r13
movq %rdx, (%r13)
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
leaq 0x5ec06(%rip), %rsi     # 0x145edd
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeebeb
movq %rax, %r12
movq (%r13), %rcx
addq %rax, %rcx
movq %rcx, 0x48(%r14)
leaq 0x5ebe8(%rip), %rsi     # 0x145efc
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
cmpq $0x2, %r12
jge 0xe7331
movb $0x1, 0x6(%r14)
jmp 0xe741d
xorl %r13d, %r13d
cmpq $0x0, 0x38(%r14)
jle 0xe73eb
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xe73e2
leaq 0x8(%rsp), %rsi
callq 0xeebeb
movq %rax, %rbp
testl %ebp, %ebp
je 0xe738f
cmpl $0x7fff, %ebp           # imm = 0x7FFF
ja 0xe73b3
andl $0x7fff, %ebp           # imm = 0x7FFF
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0xef481
addq %rbp, 0x8(%rsp)
jmp 0xe73d5
testl %r13d, %r13d
jne 0xe73e2
cmpb $0x0, 0x5(%r14)
je 0xe73e2
movq %r15, %rdi
callq 0xf08ba
movq 0x18(%r15), %rax
movq %rax, 0x40(%r14)
movl $0x1, %r13d
jmp 0xe73d8
addl $0xffff8000, %ebp       # imm = 0xFFFF8000
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %esi
movq %r15, %rdi
movq %rbp, %rdx
callq 0xf0eba
addq %rbp, %r12
cmpq 0x38(%r14), %r12
jl 0xe7342
testl %r13d, %r13d
setne %r13b
jmp 0xe73ee
xorl %r12d, %r12d
cmpb $0x0, 0x5(%r14)
sete %al
orb %r13b, %al
movl $0x1, %ebp
testb $0x1, %al
jne 0xe7409
movb $0x1, 0x6(%r14)
xorl %ebp, %ebp
leaq 0x2c4b6(%rip), %rsi     # 0x1138c6
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop