// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x10(%rsp), %r13
movq %rdx, (%r13)
leaq 0x24(%rsp), %rsi
callq 0xe958c
leaq 0x5db68(%rip), %rsi     # 0x1459cd
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ed
movq %rax, %r12
movq (%r13), %rcx
addq %rax, %rcx
movq %rcx, 0x48(%r14)
leaq 0x5db49(%rip), %rsi     # 0x1459ec
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
cmpq $0x2, %r12
jge 0xe7ec2
movb $0x1, 0x6(%r14)
xorl %ebp, %ebp
jmp 0xe7fc4
leaq 0x10(%rsp), %r13
movl $0x0, 0xc(%rsp)
xorl %r12d, %r12d
cmpq 0x38(%r14), %r12
jge 0xe7f21
movq 0x10(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xe7f21
movq %r13, %rsi
callq 0xef1ed
movq %rax, %rbp
testl %ebp, %ebp
je 0xe7f29
cmpl $0x7fff, %ebp           # imm = 0x7FFF
ja 0xe7f39
andl $0x7fff, %ebp           # imm = 0x7FFF
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
callq 0xefabc
addq %rbp, 0x10(%rsp)
jmp 0xe7f5b
xorl %eax, %eax
testb %al, %al
jne 0xe7ed2
jmp 0xe7f90
leal 0x1(%r15), %ecx
testl %r15d, %r15d
je 0xe7f62
xorl %eax, %eax
movl %ecx, %r15d
jmp 0xe7f23
addl $0xffff8000, %ebp       # imm = 0xFFFF8000
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
movzbl %al, %esi
movq 0x18(%rsp), %rdi
movq %rbp, %rdx
callq 0xf14bd
addq %rbp, %r12
movb $0x1, %al
jmp 0xe7f23
cmpb $0x0, 0x5(%r14)
je 0xe7f32
movq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0xf0ea4
movq 0x18(%r15), %rax
movq %rax, 0x40(%r14)
movb $0x1, %al
movl $0x1, %r15d
movl $0x1, 0xc(%rsp)
jmp 0xe7f23
cmpb $0x0, 0x5(%r14)
sete %al
cmpl $0x0, 0xc(%rsp)
setne %cl
orb %al, %cl
movl $0x1, %ebp
jne 0xe7fb0
movb $0x1, 0x6(%r14)
xorl %ebp, %ebp
leaq 0x2b85f(%rip), %rsi     # 0x113816
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movl 0x24(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)