// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/storyboard.c
// Function: do_decompress_newfmt
static int do_decompress_newfmt(deark *c, struct storyboard_ctx *d,
	dbuf *outf)
{
	i64 pos = 0;
	i64 ntotal = 0;
	int failure_flag = 0;
	u8 max_ucode, max_rcode;
	i64 endpos = c->infile->len;

	// There seem to be two compression schemes, one of which supports
	// "long" codes and other things.
	// I don't know how to decide which one to use -- maybe long codes
	// are only used by 640x480x256 files?

	if(d->cmpr_meth==1) {
		max_ucode = 0x7e;
		max_rcode = 0xfc;
	}
	else {
		max_ucode = 0x7f;
		max_rcode = 0xff;
	}

	if(d->pixels_start>0) {
		pos = d->pixels_start;
	}
	else {
		pos = 2048;
	}

	while(1) {
		i64 count;
		u8 b, b2;
		u8 uflag = 0;
		u8 rflag = 0;
		u8 cflag = 0;

		if(ntotal >= d->max_unc_size) break;
		if(pos>=endpos) break;

		b = de_getbyte_p(&pos);

		if(b==0x00) {
			// I don't know if 0x00 is legal, but it seems to (?) disable compression
			// for the rest of the file.
			uflag = 1;
			count = endpos - pos;
			if(count<0) count = 0;
		}
		else if(b>=0x01 && b<=max_ucode) {
			uflag = 1;
			count = (i64)(b & 0x7f);
		}
		else if(b==0x7f) {
			uflag = 1;
			count = de_getu16le_p(&pos);
		}
		else if(b>=0x81 && b<=max_rcode) {
			rflag = 1;
			count = (i64)(b & 0x7f);
		}
		else if(b==0xfd) {
			cflag = 1;
			count = de_getu16le_p(&pos);
		}
		else if(b==0xfe) {
			cflag = 1;
			count = de_getbyte_p(&pos);
		}
		else if(b==0xff) {
			rflag = 1;
			count = de_getu16le_p(&pos);
		}
		else {
			d->need_errmsg = 1;
			failure_flag = 1;
			goto done;
		}

		if(rflag) { // compressed run
			b2 = de_getbyte_p(&pos);
			dbuf_write_run(outf, b2, count);
			ntotal += count;
		}
		else if(uflag) { // uncompressed run
			dbuf_copy(c->infile, pos, count, outf);
			pos += count;
			ntotal += count;
		}
		else if(cflag) {
			if(!newfmt_copy_from_prev_row(c, d, outf, count)) {
				failure_flag = 1;
				d->need_errmsg = 1;
				goto done;
			}
		}
	}

done:
	if(d->need_errmsg) {
		de_err(c, "Decompression failed (near %"I64_FMT")", pos);
		d->need_errmsg = 0;
	}
	return !failure_flag;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x78(%rax), %eax
cmpl $0x1, %eax
jne 0x158e42
movb $0x7e, -0x2d(%rbp)
movb $-0x4, -0x2e(%rbp)
jmp 0x158e4a
movb $0x7f, -0x2d(%rbp)
movb $-0x1, -0x2e(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
jle 0x158e63
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x158e6b
movq $0x800, -0x20(%rbp)     # imm = 0x800
jmp 0x158e6d
movb $0x0, -0x43(%rbp)
movb $0x0, -0x44(%rbp)
movb $0x0, -0x45(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jl 0x158e8c
jmp 0x1590a0
movq -0x20(%rbp), %rax
cmpq -0x38(%rbp), %rax
jl 0x158e9b
jmp 0x1590a0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, -0x41(%rbp)
movzbl -0x41(%rbp), %eax
cmpl $0x0, %eax
jne 0x158edc
movb $0x1, -0x43(%rbp)
movq -0x38(%rbp), %rax
subq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jge 0x158ed7
movq $0x0, -0x40(%rbp)
jmp 0x158ff0
movzbl -0x41(%rbp), %eax
cmpl $0x1, %eax
jl 0x158f07
movzbl -0x41(%rbp), %eax
movzbl -0x2d(%rbp), %ecx
cmpl %ecx, %eax
jg 0x158f07
movb $0x1, -0x43(%rbp)
movzbl -0x41(%rbp), %eax
andl $0x7f, %eax
cltq
movq %rax, -0x40(%rbp)
jmp 0x158fee
movzbl -0x41(%rbp), %eax
cmpl $0x7f, %eax
jne 0x158f2e
movb $0x1, -0x43(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x40(%rbp)
jmp 0x158fec
movzbl -0x41(%rbp), %eax
cmpl $0x81, %eax
jl 0x158f5b
movzbl -0x41(%rbp), %eax
movzbl -0x2e(%rbp), %ecx
cmpl %ecx, %eax
jg 0x158f5b
movb $0x1, -0x44(%rbp)
movzbl -0x41(%rbp), %eax
andl $0x7f, %eax
cltq
movq %rax, -0x40(%rbp)
jmp 0x158fea
movzbl -0x41(%rbp), %eax
cmpl $0xfd, %eax
jne 0x158f81
movb $0x1, -0x45(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x40(%rbp)
jmp 0x158fe8
movzbl -0x41(%rbp), %eax
cmpl $0xfe, %eax
jne 0x158faa
movb $0x1, -0x45(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x40(%rbp)
jmp 0x158fe6
movzbl -0x41(%rbp), %eax
cmpl $0xff, %eax
jne 0x158fd0
movb $0x1, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x40(%rbp)
jmp 0x158fe4
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
movl $0x1, -0x2c(%rbp)
jmp 0x1590a2
jmp 0x158fe6
jmp 0x158fe8
jmp 0x158fea
jmp 0x158fec
jmp 0x158fee
jmp 0x158ff0
cmpb $0x0, -0x44(%rbp)
je 0x15902b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, -0x42(%rbp)
movq -0x18(%rbp), %rdi
movb -0x42(%rbp), %al
movq -0x40(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
movq -0x40(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x15909b
cmpb $0x0, -0x43(%rbp)
je 0x159064
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x166070
movq -0x40(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x40(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x159099
cmpb $0x0, -0x45(%rbp)
je 0x159097
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x159250
cmpl $0x0, %eax
jne 0x159095
movl $0x1, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x1590a2
jmp 0x159097
jmp 0x159099
jmp 0x15909b
jmp 0x158e6d
jmp 0x1590a2
movq -0x10(%rbp), %rax
cmpb $0x0, 0x6(%rax)
je 0x1590ca
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x78930(%rip), %rsi     # 0x1d19eb
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x0, 0x6(%rax)
cmpl $0x0, -0x2c(%rbp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
addq $0x50, %rsp
popq %rbp
retq
nop