// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pklite.c
// Function: do_decompress
static void do_decompress(deark *c, lctx *d)
{
	struct decompr_internal_state *dctx = NULL;
	struct de_lz77buffer *ringbuf = NULL;
	u8 b;
	u8 allow_prehistory = 0;
	UI value_of_long_ml_code;
	UI value_of_ml2_0_code;
	UI value_of_ml2_1_code = 0xffff;
	UI value_of_lit0_code = 0xffff;
	UI long_matchlen_bias;

	de_dbg(c, "decompressing cmpr code at %"I64_FMT, d->dparams.cmpr_data_pos);
	de_dbg_indent(c, 1);

	dctx = de_malloc(c, sizeof(struct decompr_internal_state));
	dctx->d = d;
	dctx->inf = c->infile;
	dctx->dparams = &d->dparams;

	if(d->ver_reported.ver_num == 0x100) {
		// Need this for v1.00beta/1990-07-17. It makes use of an implied
		// 0-valued byte before the actual data (or it has a bug that does
		// that).
		allow_prehistory = 1;
	}

	if(d->dparams.large_cmpr) {
		if(d->dparams.v120_cmpr) {
			// There are 17 normal codes, and 4 special
			value_of_long_ml_code = 17;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			value_of_ml2_1_code = value_of_long_ml_code+2;
			value_of_lit0_code = value_of_long_ml_code+3;
			long_matchlen_bias = 20;
		}
		else {
			// There are 22 normal codes, and 2 special
			value_of_long_ml_code = 22;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			long_matchlen_bias = 25;
		}
	}
	else {
		if(d->dparams.v120_cmpr) {
			// There are 7 normal codes, and 4 special
			value_of_long_ml_code = 7;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			value_of_ml2_1_code = value_of_long_ml_code+2;
			value_of_lit0_code = value_of_long_ml_code+3;
			long_matchlen_bias = 10;
		}
		else {
			// There are 7 normal codes, and 2 special
			value_of_long_ml_code = 7;
			value_of_ml2_0_code = value_of_long_ml_code+1;
			long_matchlen_bias = 10;
		}
	}

	make_matchlengths_tree(c, dctx);
	make_offsets_tree(c, dctx);

	d->dcmpr_code = dbuf_create_membuf(c, 0, 0);
	dbuf_set_length_limit(d->dcmpr_code, 1048576);
	dbuf_enable_wbuffer(d->dcmpr_code);

	ringbuf = de_lz77buffer_create(c, 8192);
	ringbuf->userdata = (void*)dctx;
	ringbuf->writebyte_cb = my_lz77buf_writebytecb;

	dctx->dcmpr_cur_ipos = d->dparams.cmpr_data_pos;
	dctx->bbll.is_lsb = 1;
	de_bitbuf_lowlevel_empty(&dctx->bbll);

	fill_bitbuf(c, dctx);

	while(1) {
		u8 x;
		UI len_raw;
		UI matchlen;
		UI offs_hi_bits = 0;
		u8 offs_lo_byte;
		u8 offs_have_hi_bits = 0;
		UI matchpos;

		if(d->errflag) goto after_dcmpr;

		x = pklite_getbit(c, dctx);
		if(x==0) {
			b = dbuf_getbyte_p(dctx->inf, &dctx->dcmpr_cur_ipos);
			if(d->dparams.extra_cmpr==1) {
				b ^= (u8)(dctx->bbll.nbits_in_bitbuf);
			}
			else if(d->dparams.extra_cmpr==2) {
				b ^= 0xff;
			}
			if(c->debug_level>=3) {
				de_dbg3(c, "lit 0x%02x", (UI)b);
			}
			de_lz77buffer_add_literal_byte(ringbuf, b);
			continue;
		}

		len_raw = read_pklite_code_using_tree(c, dctx, dctx->lengths_tree);
		if(d->errflag) goto after_dcmpr;

		if(len_raw<value_of_long_ml_code) {
			matchlen = len_raw+3;
		}
		else if(len_raw==value_of_ml2_0_code) {
			matchlen = 2;
			// Leave offs_hi_bits at 0.
			offs_have_hi_bits = 1;
		}
		else if(len_raw==value_of_long_ml_code) {
			b = dbuf_getbyte_p(dctx->inf, &dctx->dcmpr_cur_ipos);

			if(b >= 0xfd) {
				if(b==0xfd && d->dparams.large_cmpr) {
					do_uncompressed_area(c, dctx, ringbuf);
					continue;
				}
				if(b==0xfe && d->dparams.large_cmpr) {
					// (segment separator) Just a no-op?
					de_dbg3(c, "code 0xfe");
					continue;
				}
				if(b==0xff) {
					de_dbg3(c, "stop code");
					goto after_dcmpr; // Normal completion
				}
				de_err(c, "Unexpected code (0x%02x) or unsupported feature", (UI)b);
				d->errflag = 1;
				d->errmsg_handled = 1;
				goto after_dcmpr;
			}
			matchlen = (UI)b+long_matchlen_bias;
		}
		else if(len_raw==value_of_lit0_code) {
			if(c->debug_level>=3) {
				de_dbg3(c, "lit 0x00 (special)");
			}
			de_lz77buffer_add_literal_byte(ringbuf, 0x00);
			continue;
		}
		else if(len_raw==value_of_ml2_1_code) {
			matchlen = 2;
			offs_hi_bits = 1;
			offs_have_hi_bits = 1;
		}
		else {
			d->errflag = 1;
			goto done;
		}

		if(!offs_have_hi_bits) {
			offs_hi_bits = read_pklite_code_using_tree(c, dctx, dctx->offsets_tree);
		}

		offs_lo_byte = dbuf_getbyte_p(dctx->inf, &dctx->dcmpr_cur_ipos);
		offs_lo_byte ^= d->dparams.offset_xor_key;
		if(d->errflag) goto after_dcmpr;

		matchpos = (offs_hi_bits<<8) | (UI)offs_lo_byte;

		if(c->debug_level>=3) {
			de_dbg3(c, "match pos=%u len=%u", matchpos, matchlen);
		}

		// PKLITE confirmed to use distances 1 to 8191. Have not observed matchpos=0.
		// Have not observed it to use distances larger than the number of bytes
		// decompressed so far [with one small exception].
		if(allow_prehistory && (matchpos == dctx->dcmpr_code_nbytes_written+1)) {
			;
		}
		else if(matchpos==0 || (i64)matchpos>dctx->dcmpr_code_nbytes_written) {
			de_err(c, "Bad or unsupported compressed data (dist=%u, expected 1 to %"I64_FMT")",
				matchpos, dctx->dcmpr_code_nbytes_written);
			d->errflag = 1;
			d->errmsg_handled = 1;
			goto after_dcmpr;
		}
		de_lz77buffer_copy_from_hist(ringbuf,
				(UI)(ringbuf->curpos-matchpos), matchlen);
	}

after_dcmpr:
	if(!d->dcmpr_code) goto done;
	dbuf_flush(d->dcmpr_code);

	if(!d->errflag) {
		d->cmpr_data_endpos = dctx->dcmpr_cur_ipos;
		de_dbg(c, "cmpr data end: %"I64_FMT, d->cmpr_data_endpos);
		de_dbg(c, "decompressed %"I64_FMT" bytes to %"I64_FMT,
			d->cmpr_data_endpos-d->dparams.cmpr_data_pos, d->dcmpr_code->len);

		if(dctx->has_uncompressed_area) {
			de_warn(c, "This file has an \"uncompressed area\", and might not be "
				"decompressed correctly.");
		}
	}

done:
	if(dctx) {
		fmtutil_huffman_destroy_decoder(c, dctx->lengths_tree);
		fmtutil_huffman_destroy_decoder(c, dctx->offsets_tree);
		de_free(c, dctx);
	}
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movb $0x0, -0x22(%rbp)
movl $0xffff, -0x30(%rbp)    # imm = 0xFFFF
movl $0xffff, -0x34(%rbp)    # imm = 0xFFFF
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x966ee(%rip), %rsi     # 0x1aa213
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x50, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rcx
addq $0x10, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
cmpl $0x100, 0xe0(%rax)      # imm = 0x100
jne 0x113b8b
movb $0x1, -0x22(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x19(%rax)
je 0x113be3
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1a(%rax)
je 0x113bca
movl $0x11, -0x28(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x30(%rbp)
movl -0x28(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x34(%rbp)
movl $0x14, -0x38(%rbp)
jmp 0x113be1
movl $0x16, -0x28(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl $0x19, -0x38(%rbp)
jmp 0x113c31
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1a(%rax)
je 0x113c18
movl $0x7, -0x28(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x30(%rbp)
movl -0x28(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x34(%rbp)
movl $0xa, -0x38(%rbp)
jmp 0x113c2f
movl $0x7, -0x28(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl $0xa, -0x38(%rbp)
jmp 0x113c31
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x115ed0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x115fb0
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd8(%rax)
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rdi
movl $0x100000, %esi         # imm = 0x100000
callq 0x168120
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0x1785d0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
leaq 0x236b(%rip), %rcx      # 0x116020
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x30(%rax)
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
callq 0x16a360
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x116070
movl $0x0, -0x48(%rbp)
movb $0x0, -0x4a(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x113d04
jmp 0x114048
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x116120
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x113da7
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x28, %rsi
callq 0x165170
movb %al, -0x21(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x18(%rax), %eax
cmpl $0x1, %eax
jne 0x113d5b
movq -0x18(%rbp), %rax
movl 0x34(%rax), %eax
movzbl %al, %ecx
movzbl -0x21(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x21(%rbp)
jmp 0x113d76
movq -0x10(%rbp), %rax
movzbl 0x18(%rax), %eax
cmpl $0x2, %eax
jne 0x113d74
movzbl -0x21(%rbp), %eax
xorl $0xff, %eax
movb %al, -0x21(%rbp)
jmp 0x113d76
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x113d95
movq -0x8(%rbp), %rdi
movzbl -0x21(%rbp), %edx
leaq 0x964a4(%rip), %rsi     # 0x1aa232
movb $0x0, %al
callq 0x15b400
movq -0x20(%rbp), %rdi
movzbl -0x21(%rbp), %esi
callq 0x178880
jmp 0x113ceb
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
callq 0x116180
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x113dcd
jmp 0x114048
movl -0x40(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x113de3
movl -0x40(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x44(%rbp)
jmp 0x113f46
movl -0x40(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x113dfb
movl $0x2, -0x44(%rbp)
movb $0x1, -0x4a(%rbp)
jmp 0x113f44
movl -0x40(%rbp), %eax
cmpl -0x28(%rbp), %eax
jne 0x113ee3
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x28, %rsi
callq 0x165170
movb %al, -0x21(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0xfd, %eax
jl 0x113ed7
movzbl -0x21(%rbp), %eax
cmpl $0xfd, %eax
jne 0x113e5c
movq -0x10(%rbp), %rax
movzbl 0x19(%rax), %eax
cmpl $0x0, %eax
je 0x113e5c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x116200
jmp 0x113ceb
movzbl -0x21(%rbp), %eax
cmpl $0xfe, %eax
jne 0x113e8b
movq -0x10(%rbp), %rax
movzbl 0x19(%rax), %eax
cmpl $0x0, %eax
je 0x113e8b
movq -0x8(%rbp), %rdi
leaq 0xb04af(%rip), %rsi     # 0x1c432e
movb $0x0, %al
callq 0x15b400
jmp 0x113ceb
movzbl -0x21(%rbp), %eax
cmpl $0xff, %eax
jne 0x113ead
movq -0x8(%rbp), %rdi
leaq 0x9639c(%rip), %rsi     # 0x1aa23d
movb $0x0, %al
callq 0x15b400
jmp 0x114048
movq -0x8(%rbp), %rdi
movzbl -0x21(%rbp), %edx
leaq 0xb047c(%rip), %rsi     # 0x1c4338
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x114048
movzbl -0x21(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x113f42
movl -0x40(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0x113f16
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x113f06
movq -0x8(%rbp), %rdi
leaq 0xb0469(%rip), %rsi     # 0x1c4368
movb $0x0, %al
callq 0x15b400
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x178880
jmp 0x113ceb
movl -0x40(%rbp), %eax
cmpl -0x30(%rbp), %eax
jne 0x113f32
movl $0x2, -0x44(%rbp)
movl $0x1, -0x48(%rbp)
movb $0x1, -0x4a(%rbp)
jmp 0x113f3e
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x1140fc
jmp 0x113f40
jmp 0x113f42
jmp 0x113f44
jmp 0x113f46
cmpb $0x0, -0x4a(%rbp)
jne 0x113f64
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdx
callq 0x116180
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x28, %rsi
callq 0x165170
movb %al, -0x49(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x1b(%rax), %ecx
movzbl -0x49(%rbp), %eax
xorl %ecx, %eax
movb %al, -0x49(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x113f9b
jmp 0x114048
movl -0x48(%rbp), %eax
shll $0x8, %eax
movzbl -0x49(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x50(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x113fcb
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %edx
movl -0x44(%rbp), %ecx
leaq 0x962a7(%rip), %rsi     # 0x1aa26b
movb $0x0, %al
callq 0x15b400
movzbl -0x22(%rbp), %eax
cmpl $0x0, %eax
je 0x113fea
movl -0x50(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x20(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x113fea
jmp 0x11402d
cmpl $0x0, -0x50(%rbp)
je 0x113ffd
movl -0x50(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jle 0x11402b
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
leaq 0xb0368(%rip), %rsi     # 0x1c437b
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x114048
jmp 0x11402d
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %esi
subl -0x50(%rbp), %esi
movl -0x44(%rbp), %edx
callq 0x1788e0
jmp 0x113ceb
movq -0x10(%rbp), %rax
cmpq $0x0, 0xd8(%rax)
jne 0x11405b
jmp 0x1140fc
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rdi
callq 0x167c30
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x1140fa
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdx
leaq 0x962bd(%rip), %rsi     # 0x1aa35e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0x10(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x18(%rax), %rcx
leaq 0x8ac6e(%rip), %rsi     # 0x19ed43
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpb $0x0, 0x18(%rax)
je 0x1140f8
movq -0x8(%rbp), %rdi
leaq 0xb02ca(%rip), %rsi     # 0x1c43bb
movb $0x0, %al
callq 0x15bc10
jmp 0x1140fa
jmp 0x1140fc
cmpq $0x0, -0x18(%rbp)
je 0x114132
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
callq 0x1886d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x1886d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)