// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movq %rdi, %r12
leaq 0x10(%rsi), %rbx
movq 0x10(%rsi), %rdx
leaq 0x61be9(%rip), %rsi     # 0x11e56f
xorl %eax, %eax
callq 0xe9226
movq %r12, %rdi
movl $0x1, %esi
callq 0xe9585
movl $0x50, %esi
movq %r12, %rdi
callq 0xe969b
movq %rax, %r14
movq %r15, (%rax)
movq 0x18(%r12), %rax
movq %rax, 0x8(%r14)
movq %rbx, 0x10(%r14)
cmpb $0x0, 0x19(%r15)
movb 0x1a(%r15), %al
je 0xbca05
xorl %ecx, %ecx
testb %al, %al
sete %cl
leal 0x11(%rcx,%rcx,4), %eax
movl %eax, 0x14(%rsp)
leal 0x12(%rcx,%rcx,4), %eax
movl %eax, 0x2c(%rsp)
movl $0xffff, %eax           # imm = 0xFFFF
movl $0x13, %edx
cmovel %eax, %edx
movl %edx, 0x24(%rsp)
movl $0x14, %edx
cmovel %eax, %edx
movl %edx, 0x28(%rsp)
leal (%rcx,%rcx,4), %eax
addl $0x14, %eax
movl %eax, 0x20(%rsp)
jmp 0xbca3c
testb %al, %al
movl $0xffff, %ecx           # imm = 0xFFFF
movl $0x9, %eax
cmovel %ecx, %eax
movl %eax, 0x24(%rsp)
movl $0xa, %eax
movl $0xa, 0x20(%rsp)
cmovnel %eax, %ecx
movl %ecx, 0x28(%rsp)
movl $0x8, 0x2c(%rsp)
movl $0x7, 0x14(%rsp)
movl 0xe0(%r15), %eax
movl %eax, 0x4c(%rsp)
leaq 0x40(%r14), %rsi
cmpb $0x0, 0x19(%r15)
movb 0x1a(%r15), %al
je 0xbca72
testb %al, %al
je 0xbca8e
leaq 0x7b91f(%rip), %rdx     # 0x138380
leaq 0x7bfb9(%rip), %r8      # 0x138a21
movq %r12, %rdi
movl $0x15, %ecx
jmp 0xbcabc
testb %al, %al
je 0xbcaa6
leaq 0x7b933(%rip), %rdx     # 0x1383b0
leaq 0x7bf9d(%rip), %r8      # 0x138a21
movq %r12, %rdi
movl $0xb, %ecx
jmp 0xbcabc
leaq 0x7b89b(%rip), %rdx     # 0x138330
leaq 0x7bf85(%rip), %r8      # 0x138a21
movq %r12, %rdi
movl $0x18, %ecx
jmp 0xbcabc
leaq 0x7b8b3(%rip), %rdx     # 0x138360
leaq 0x7bf6d(%rip), %r8      # 0x138a21
movq %r12, %rdi
movl $0x9, %ecx
callq 0xbdaf3
movq 0x10(%r14), %rax
leaq 0x48(%r14), %rsi
cmpb $0x0, 0xa(%rax)
leaq 0x7b8fc(%rip), %rax     # 0x1383d0
leaq 0x7b935(%rip), %rdx     # 0x138410
cmoveq %rax, %rdx
leaq 0x6c0ed(%rip), %r8      # 0x128bd3
movq %r12, %rdi
movl $0x20, %ecx
callq 0xbdaf3
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0xd8(%r15)
movl $0x100000, %esi         # imm = 0x100000
movq %rax, %rdi
callq 0xf1066
movq 0xd8(%r15), %rdi
callq 0xf0e3f
movq %r12, %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0xfb2c3
movq %r14, (%rax)
leaq 0xe90(%rip), %rcx       # 0xbd9c6
movq %rax, 0x30(%rsp)
movq %rcx, 0x8(%rax)
movq 0x10(%r15), %rax
leaq 0x28(%r14), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x28(%r14)
movq %r14, %rdi
addq $0x30, %rdi
movb $0x1, 0x30(%r14)
callq 0xf2261
movq %r12, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0xbd9df
movl $0x2, %eax
movl %eax, %eax
movq %rax, 0x40(%rsp)
leaq 0x7b69c(%rip), %rbx     # 0x138220
movl $0x4, %eax
movl %eax, %eax
movq %rax, 0x38(%rsp)
movl $0x5, %eax
movl %eax, %eax
movq %rax, 0x50(%rsp)
cmpb $0x0, (%r15)
movl $0x4, %r12d
je 0xbcbb4
movl %r12d, %eax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0xbda4e
testb %al, %al
je 0xbcbf5
movq 0x40(%r14), %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0xbda90
cmpb $0x0, (%r15)
movl $0x4, %r12d
jne 0xbcba8
cmpl 0x14(%rsp), %eax
jae 0xbcc51
addl $0x3, %eax
movb $0x1, %cl
xorl %r13d, %r13d
movl %eax, %ebp
jmp 0xbcc65
movq 0x8(%r14), %rdi
movq 0x18(%rsp), %rsi
callq 0xef14d
movzbl 0x18(%r15), %ecx
cmpl $0x2, %ecx
je 0xbcc18
cmpl $0x1, %ecx
jne 0xbcc1a
xorb 0x34(%r14), %al
jmp 0xbcc1a
notb %al
cmpl $0x3, (%r12)
movzbl %al, %ebp
jl 0xbcc37
movq %r12, %rdi
leaq 0x61960(%rip), %rsi     # 0x11e58e
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9461
movq 0x30(%rsp), %rdi
movl %ebp, %esi
callq 0xfb382
movq 0x40(%rsp), %rax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
xorl %r13d, %r13d
movl $0x2, %ebp
cmpl 0x2c(%rsp), %eax
jne 0xbcd27
xorl %ecx, %ecx
testb %cl, %cl
je 0xbcc7d
movq 0x48(%r14), %rdx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0xbda90
movl %eax, %r13d
movq 0x8(%r14), %rdi
movq 0x18(%rsp), %rsi
callq 0xef14d
cmpb $0x0, (%r15)
movl $0x4, %r12d
jne 0xbcba8
xorb 0x1b(%r15), %al
shll $0x8, %r13d
movzbl %al, %r12d
orl %r13d, %r12d
movq 0x8(%rsp), %r13
cmpl $0x3, (%r13)
jl 0xbcccc
movq %r13, %rdi
leaq 0x61907(%rip), %rsi     # 0x11e5c7
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9461
cmpl $0x100, 0x4c(%rsp)      # imm = 0x100
jne 0xbcce9
movl %r12d, %eax
movq 0x20(%r14), %rcx
incq %rcx
cmpq %rax, %rcx
je 0xbcdfb
testl %r12d, %r12d
je 0xbccfb
movl %r12d, %eax
cmpq %rax, 0x20(%r14)
jge 0xbcdfb
movq 0x20(%r14), %rcx
movq %r13, %rdi
leaq 0x7bc89(%rip), %rsi     # 0x138992
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9df7
movw $0x101, (%r15)          # imm = 0x101
movq 0x38(%rsp), %rax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
cmpl 0x14(%rsp), %eax
jne 0xbce15
movq 0x8(%r14), %rdi
movq 0x18(%rsp), %rsi
callq 0xef14d
movzbl %al, %ebp
cmpb $-0x3, %bpl
jb 0xbce4d
cmpl $0xff, %ebp
movq 0x8(%rsp), %r12
je 0xbcee0
cmpl $0xfe, %ebp
je 0xbce8b
cmpl $0xfd, %ebp
jne 0xbceb9
cmpb $0x0, 0x19(%r15)
je 0xbce7b
movb $0x1, 0x18(%r14)
movq 0x8(%r14), %rdi
movq 0x18(%rsp), %rsi
callq 0xef14d
movzbl %al, %r13d
movq 0x28(%r14), %rdx
movq %r12, %rdi
leaq 0x7bcc4(%rip), %rsi     # 0x138a69
movq %r13, %rcx
xorl %eax, %eax
callq 0xe9461
movl $0x9, %r12d
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
callq 0xfb382
decq %r12
jne 0xbcdba
movl $0x2, %r12d
testq %r13, %r13
je 0xbcba8
movq 0x8(%r14), %rdi
movq 0x18(%rsp), %rsi
callq 0xef14d
movzbl %al, %esi
movq %rbp, %rdi
callq 0xfb382
decq %r13
jne 0xbcdd8
jmp 0xbcba8
movq 0x30(%rsp), %rdi
movl 0x10(%rdi), %esi
subl %r12d, %esi
movl %ebp, %edx
callq 0xfb3af
xorl %r12d, %r12d
jmp 0xbcba8
cmpl 0x28(%rsp), %eax
jne 0xbce58
movq 0x8(%rsp), %rdi
cmpl $0x3, (%rdi)
jl 0xbce33
leaq 0x7bb53(%rip), %rsi     # 0x13897f
xorl %eax, %eax
callq 0xe9461
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0xfb382
movq 0x40(%rsp), %rax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
addl 0x20(%rsp), %ebp
movb $0x1, %cl
jmp 0xbcc65
cmpl 0x24(%rsp), %eax
jne 0xbce69
movl $0x1, %r13d
jmp 0xbcc63
movb $0x1, (%r15)
movq 0x50(%rsp), %rax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
cmpl $0xff, %ebp
je 0xbcee0
cmpl $0xfe, %ebp
jne 0xbceb9
cmpb $0x0, 0x19(%r15)
je 0xbceb1
movq %r12, %rdi
leaq 0x7baa9(%rip), %rsi     # 0x138945
xorl %eax, %eax
callq 0xe9461
movq 0x40(%rsp), %rax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
cmpl $0xff, %ebp
je 0xbcee0
movq %r12, %rdi
leaq 0x7ba8c(%rip), %rsi     # 0x13894f
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9df7
movw $0x101, (%r15)          # imm = 0x101
movq 0x38(%rsp), %rax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
movq %r12, %rdi
leaq 0x616af(%rip), %rsi     # 0x11e599
xorl %eax, %eax
callq 0xe9461
movq 0x38(%rsp), %rax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
movq 0xd8(%r15), %rdi
testq %rdi, %rdi
je 0xbcf78
callq 0xf0ea4
cmpb $0x0, (%r15)
jne 0xbcf78
movq 0x28(%r14), %rdx
movq %rdx, 0x88(%r15)
leaq 0x61792(%rip), %rsi     # 0x11e6ba
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x88(%r15), %rdx
movq 0xd8(%r15), %rax
subq 0x10(%r15), %rdx
movq 0x18(%rax), %rcx
leaq 0x55d34(%rip), %rsi     # 0x112c88
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpb $0x0, 0x18(%r14)
je 0xbcf78
leaq 0x7ba66(%rip), %rsi     # 0x1389d2
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x40(%r14), %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x10521e
movq 0x48(%r14), %rsi
movq %rbx, %rdi
callq 0x10521e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9585