// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/lzexe.c
// Function: do_decompress_code
static void do_decompress_code(deark *c, lctx *d)
{
	i64 ipos1;
	struct de_lz77buffer *ringbuf = NULL;

	// (I'd expect ipos1 to always equal d->host_ei->start_of_dos_code, but anyway...)
	ipos1 = d->ephdr_pos -  d->ephdr.cmpr_len_para*16;
	de_dbg(c, "decompressing cmpr code at %"I64_FMT, ipos1);
	de_dbg_indent(c, 1);

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

	d->dcmpr_cur_ipos = ipos1;
	d->bbll.is_lsb = 1;
	de_bitbuf_lowlevel_empty(&d->bbll);

	while(1) {
		u8 x, x2, x3;
		UI matchpos;
		UI matchlen;
		u8 matchtype = 1;

		if(d->errflag) goto done;

		x = lzexe_getbit(c, d);
		if(x) { // 1...
			u8 b;

			b = de_getbyte_p(&d->dcmpr_cur_ipos);
			de_lz77buffer_add_literal_byte(ringbuf, (u8)b);
			continue;
		}

		x = lzexe_getbit(c, d);
		if(x==0) { // 00...
			x2 = lzexe_getbit(c, d);
			x3 = lzexe_getbit(c, d);
			matchlen = ((UI)x2<<1) + (UI)x3 + 2;
			matchpos = 0xffU-(UI)de_getbyte_p(&d->dcmpr_cur_ipos);
		}
		else { // 01...
			u8 lb, hb;

			lb = de_getbyte_p(&d->dcmpr_cur_ipos);
			hb = de_getbyte_p(&d->dcmpr_cur_ipos);

			matchpos = 0x1fffU - ((((UI)(hb & 0xf8))<<5) | (UI)lb);

			if((hb & 0x07)==0) {
				u8 xb;

				matchtype = 3;
				xb = de_getbyte_p(&d->dcmpr_cur_ipos);

				if(xb==0) {
					de_dbg3(c, "eof code");
					goto after_decompress;
				}
				else if(xb==1) {
					continue; // something about segments...
				}
				else {
					matchlen = (UI)xb + 1;
				}
			}
			else {
				matchtype = 2;
				matchlen = (UI)(hb & 0x07) + 2;
			}
		}

		if(c->debug_level>=3) {
			de_dbg3(c, "match (%u) pos=%u len=%u", (UI)matchtype, matchpos+1, matchlen);
		}
		de_lz77buffer_copy_from_hist(ringbuf,
				(UI)(ringbuf->curpos-1-matchpos), matchlen);
	}

after_decompress:
	dbuf_flush(d->dcmpr_code);
	de_dbg(c, "decompressed %"I64_FMT" bytes to %"I64_FMT, (d->dcmpr_cur_ipos-ipos1),
		d->dcmpr_code->len);

done:
	de_lz77buffer_destroy(c, ringbuf);
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
movq 0x28(%rsi), %r14
movq 0x58(%rsi), %rax
shlq $0x4, %rax
subq %rax, %r14
leaq 0x8d468(%rip), %rsi     # 0x11e676
movq %r14, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0xfad8f
movq %rax, %r12
movq %r15, (%rax)
leaq 0x413(%rip), %rax       # 0x91652
movq %rax, 0x8(%r12)
movq %r14, (%rsp)
movq %r14, 0x90(%r15)
leaq 0x98(%r15), %rdi
movb $0x1, 0x98(%r15)
callq 0xf1bdd
cmpl $0x0, 0x4(%r15)
jne 0x9138d
leaq 0x90(%r15), %r13
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91661
testb %al, %al
je 0x912a0
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
movzbl %al, %esi
movq %r12, %rdi
callq 0xfae4e
jmp 0x91382
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91661
testb %al, %al
je 0x912f4
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
movl %eax, %r14d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
movzbl %al, %ebp
movl %ebp, %eax
shll $0x5, %eax
andl $0x1f00, %eax           # imm = 0x1F00
movzbl %r14b, %r14d
orl %eax, %r14d
xorl $0x1fff, %r14d          # imm = 0x1FFF
andl $0x7, %ebp
je 0x91332
addl $0x2, %ebp
movl $0x2, %edx
jmp 0x91350
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91661
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91661
movzbl %bpl, %ecx
movzbl %al, %eax
leal (%rax,%rcx,2), %ebp
addl $0x2, %ebp
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
notb %al
movzbl %al, %r14d
movl $0x1, %edx
jmp 0x91350
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
cmpb $0x1, %al
je 0x91382
movzbl %al, %ebp
testl %ebp, %ebp
je 0x913b3
incl %ebp
movl $0x3, %edx
cmpl $0x3, (%rbx)
jl 0x9136d
leal 0x1(%r14), %ecx
movq %rbx, %rdi
leaq 0x9df61(%rip), %rsi     # 0x12f2c4
movl %ebp, %r8d
xorl %eax, %eax
callq 0xe889f
notl %r14d
addl 0x10(%r12), %r14d
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0xfae7b
cmpl $0x0, 0x4(%r15)
je 0x91275
movq %rbx, %rdi
movq %r12, %rsi
callq 0xfaed4
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8a15
leaq 0x9df01(%rip), %rsi     # 0x12f2bb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe889f
movq 0x88(%r15), %rdi
callq 0xf08ba
movq 0x88(%r15), %rax
movq 0x90(%r15), %rdx
subq (%rsp), %rdx
movq 0x18(%rax), %rcx
leaq 0x818ab(%rip), %rsi     # 0x112c98
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0x9138d