// File: /workspace/github_c_makefile_star2_O1/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
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x28(%rsi), %r14
movq 0x58(%rsi), %rax
shlq $0x4, %rax
subq %rax, %r14
leaq 0x8ca72(%rip), %rsi     # 0x11e56f
movq %r14, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0xfb2c3
movq %rax, %rbp
movq %r15, (%rax)
leaq 0x492(%rip), %rax       # 0x91fc0
movq %rax, 0x8(%rbp)
leaq 0x90(%r15), %r13
movq %r14, 0x18(%rsp)
movq %r14, 0x90(%r15)
leaq 0x98(%r15), %rdi
movb $0x1, 0x98(%r15)
callq 0xf2261
movl $0x2, %eax
movl %eax, %eax
movq %rax, 0x20(%rsp)
leaq 0x9d2a0(%rip), %r12     # 0x12ee0c
movq %rbp, 0x10(%rsp)
movl $0x4, %eax
cmpl $0x0, 0x4(%r15)
je 0x91b88
movl %eax, %eax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91fcf
testb %al, %al
je 0x91bbc
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
movzbl %al, %esi
movq %rbp, %rdi
callq 0xfb382
movq 0x20(%rsp), %rax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91fcf
testb %al, %al
je 0x91c2a
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
movb %al, 0xb(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
movzbl %al, %ebp
movl %ebp, %ecx
andl $0x7, %ecx
je 0x91cb5
addl $0x2, %ecx
xorl %eax, %eax
movb $0x1, %r14b
movl $0x2, %edx
movl %ecx, 0xc(%rsp)
testb %r14b, %r14b
je 0x91cab
shll $0x5, %ebp
andl $0x1f00, %ebp           # imm = 0x1F00
movzbl 0xb(%rsp), %r14d
orl %ebp, %r14d
xorl $0x1fff, %r14d          # imm = 0x1FFF
jmp 0x91c6a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91fcf
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91fcf
movzbl %bpl, %ecx
movzbl %al, %eax
leal (%rax,%rcx,2), %eax
addl $0x2, %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
notb %al
movzbl %al, %r14d
movl $0x1, %edx
cmpl $0x3, (%rbx)
movq 0x10(%rsp), %rbp
jl 0x91c8e
leal 0x1(%r14), %ecx
movq %rbx, %rdi
leaq 0x9d35f(%rip), %rsi     # 0x12efe1
movl 0xc(%rsp), %r8d
xorl %eax, %eax
callq 0xe9461
notl %r14d
addl 0x10(%rbp), %r14d
movq %rbp, %rdi
movl %r14d, %esi
movl 0xc(%rsp), %edx
callq 0xfb3af
xorl %eax, %eax
jmp 0x91b7d
movq 0x10(%rsp), %rbp
jmp 0x91b7d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
movl $0x3, %edx
cmpb $0x1, %al
je 0x91cf4
movzbl %al, %ecx
testl %ecx, %ecx
jne 0x91d01
xorl %r14d, %r14d
movq %rbx, %rdi
leaq 0x9d2fa(%rip), %rsi     # 0x12efd8
xorl %eax, %eax
callq 0xe9461
movl $0x3, %edx
movl $0x5, %eax
jmp 0x91c06
movl $0x2, %eax
xorl %r14d, %r14d
jmp 0x91c06
incl %ecx
xorl %eax, %eax
movb $0x1, %r14b
jmp 0x91c02
movq 0x88(%r15), %rdi
callq 0xf0ea4
movq 0x88(%r15), %rax
movq 0x90(%r15), %rdx
subq 0x18(%rsp), %rdx
movq 0x18(%rax), %rcx
leaq 0x80f51(%rip), %rsi     # 0x112c88
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfb408
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9585