// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x10(%rsp), %r12
movq %rdx, (%r12)
leaq 0x1c(%rsp), %rsi
callq 0xdebae
leaq 0x5b4b0(%rip), %rsi     # 0x138a8d
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, %r13
movq (%r12), %rcx
addq %rax, %rcx
movq %rcx, 0x48(%r14)
leaq 0x5b494(%rip), %rsi     # 0x138aac
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpq $0x2, %r13
jge 0xdd635
movb $0x1, 0x6(%r14)
jmp 0xdd726
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x10(%rsp), %r13
xorl %r12d, %r12d
cmpq 0x38(%r14), %r12
jge 0xdd6f8
movq 0x10(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xdd6f8
movq %r13, %rsi
callq 0xe3fc4
movq %rax, %rbp
testl %ebp, %ebp
je 0xdd6c6
cmpl $0x7fff, %ebp           # imm = 0x7FFF
ja 0xdd6a1
andl $0x7fff, %ebp           # imm = 0x7FFF
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0xe45f4
addq %rbp, 0x10(%rsp)
jmp 0xdd6c1
addl $0xffff8000, %ebp       # imm = 0xFFFF8000
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
movzbl %al, %esi
movq %r15, %rdi
movq %rbp, %rdx
callq 0xe5e40
addq %rbp, %r12
jmp 0xdd64c
testb $0x1, 0xc(%rsp)
je 0xdd6f8
cmpb $0x0, 0x5(%r14)
je 0xdd6f8
movq %r15, %rdi
callq 0xe58e8
movq 0x18(%r15), %rax
movq %rax, 0x40(%r14)
movb $0x1, %al
movq %rax, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0xdd64c
cmpb $0x0, 0x5(%r14)
sete %al
orb 0x20(%rsp), %al
testb $0x1, %al
pushq $0x1
popq %rbp
jne 0xdd712
movb $0x1, 0x6(%r14)
xorl %ebp, %ebp
leaq 0x2914d(%rip), %rsi     # 0x106866
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop