// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x79654(%rip), %rsi     # 0x1d196d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rcx
addq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
leaq 0x79622(%rip), %rsi     # 0x1d198c
movb $0x0, %al
callq 0x15b0d0
cmpq $0x2, -0x38(%rbp)
jge 0x158385
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x1584d8
jmp 0x158387
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jl 0x15839a
jmp 0x15849e
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x1583b1
jmp 0x15849e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x158413
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
cmpl $0x1, -0x40(%rbp)
jne 0x158409
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x0, %eax
je 0x158409
movq -0x20(%rbp), %rdi
callq 0x167c30
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movl $0x1, -0x3c(%rbp)
jmp 0x15840e
jmp 0x15849e
jmp 0x158499
cmpl $0x8000, -0x4c(%rbp)    # imm = 0x8000
jae 0x158456
movl -0x4c(%rbp), %eax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x166070
movq -0x58(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x58(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x158497
movl -0x4c(%rbp), %eax
subl $0x8000, %eax           # imm = 0x8000
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, -0x59(%rbp)
movq -0x20(%rbp), %rdi
movb -0x59(%rbp), %al
movq -0x58(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
movq -0x58(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x158499
jmp 0x158387
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x0, %eax
je 0x1584bb
cmpl $0x0, -0x3c(%rbp)
jne 0x1584bb
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x1584c2
movl $0x1, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x47325(%rip), %rsi     # 0x19f7f6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x15b5a0
movl -0x44(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)