// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/os2bootlogo.c
// Function: decompress_exepack2
static void decompress_exepack2(deark *c, lctx *d,
	struct de_dfilter_in_params *dcmpri, struct de_dfilter_out_params *dcmpro,
	struct de_dfilter_results *dres)
{
	struct exepack2_ctx *ectx = NULL;

	ectx = de_malloc(c, sizeof(struct exepack2_ctx));
	ectx->dcmpri = dcmpri;
	ectx->dcmpro = dcmpro;
	ectx->cur_ipos = dcmpri->pos;
	ectx->endpos = dcmpri->pos + dcmpri->len;

	ectx->ringbuf = de_lz77buffer_create(c, 4096);
	ectx->ringbuf->writebyte_cb = exepack2_lz77buf_writebytecb;
	ectx->ringbuf->userdata = (void*)ectx;

	while(1) {
		i64 code_startpos;
		UI i;
		UI matchpos;
		UI matchlen;
		UI unc_count;
		u8 b0, b1, b2;
		u8 opcode0;
		const char *optype = "";

		if(ectx->cur_ipos >= ectx->endpos) goto after_dcmpr;
		if(ectx->nbytes_written >= dcmpro->expected_len) goto after_dcmpr;
		code_startpos = ectx->cur_ipos;
		b0 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);

		if(b0==0x00) { // stop code or RLE compressed data
			b1 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);
			if(b1==0) {
				de_dbg2(c, "stop code at %"I64_FMT, code_startpos);
				goto after_dcmpr;
			}
			else {
				UI run_len;

				run_len = (UI)b1;
				b2 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);
				if(c->debug_level>=4) {
					de_dbg(c, "op=R val=%u count=%u", (UI)b2, run_len);
				}
				for(i=0; i<run_len; i++) {
					de_lz77buffer_add_literal_byte(ectx->ringbuf, b2);
				}
			}
			continue;
		}

		opcode0 = b0 & 0x3;
		if(opcode0!=0) d->uses_lzss = 1;

		switch(opcode0) {
		case 0: // nRoots token (uncompressed bytes only)
			optype = "U";
			unc_count = (UI)(b0>>2);
			matchlen = 0;
			matchpos = 0;
			break;
		case 1: // Short String token
			optype = "S";
			b1 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);
			unc_count = (UI)((b0>>2)&0x3);
			matchlen = 3+(UI)((b0>>4)&0x7);
			matchpos = ((UI)b1<<1) | (b0>>7);
			break;
		case 2: // Mid String token
			optype = "M";
			b1 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);
			unc_count = 0;
			matchlen = 3+(UI)((b0>>2)&0x3);
			matchpos = ((UI)b1<<4) | (b0>>4);
			break;
		default: // (3) Long String token
			optype = "L";
			b1 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);
			b2 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);
			unc_count = (UI)((b0>>2)&0xf);
			matchlen = ((UI)(b1&0xf)<<2) | (b0>>6);
			matchpos = ((UI)b2<<4) | (b1>>4);
			break;
		}

		if(c->debug_level>=4) {
			de_dbg(c, "op=%s u=%u d=%u l=%u", optype, unc_count, matchpos, matchlen);
			de_dbg_indent(c, 1);
		}

		for(i=0; i<unc_count; i++) {
			b2 = dbuf_getbyte_p(dcmpri->f, &ectx->cur_ipos);
			if(c->debug_level>=4) {
				de_dbg(c, "lit %u", (UI)b2);
			}
			de_lz77buffer_add_literal_byte(ectx->ringbuf, b2);
		}

		if(c->debug_level>=4) {
			de_dbg_indent(c, -1);
		}

		if(matchlen!=0) {
			de_lz77buffer_copy_from_hist(ectx->ringbuf,
				(UI)(ectx->ringbuf->curpos-matchpos), matchlen);
		}
	}

after_dcmpr:
	dbuf_flush(dcmpro->f);
	dres->bytes_consumed_valid = 1;
	dres->bytes_consumed = ectx->cur_ipos - dcmpri->pos;
	de_lz77buffer_destroy(c, ectx->ringbuf);
	de_free(c, ectx);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl $0x30, %esi
callq 0x15b780
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
addq 0x10(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1785d0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
leaq 0x3d3(%rip), %rcx       # 0xf1c60
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, (%rax)
leaq 0xdf329(%rip), %rax     # 0x1d0bd0
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jl 0xf18c2
jmp 0xf1c0d
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0xf18d9
jmp 0xf1c0d
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x49(%rbp)
movzbl -0x49(%rbp), %eax
cmpl $0x0, %eax
jne 0xf19b6
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x4a(%rbp)
movzbl -0x4a(%rbp), %eax
cmpl $0x0, %eax
jne 0xf1944
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xcce63(%rip), %rsi     # 0x1be79b
movb $0x0, %al
callq 0x15b350
jmp 0xf1c0d
movzbl -0x4a(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x4b(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
jl 0xf1984
movq -0x8(%rbp), %rdi
movzbl -0x4b(%rbp), %edx
movl -0x5c(%rbp), %ecx
leaq 0xcce2f(%rip), %rsi     # 0x1be7ac
movb $0x0, %al
callq 0x15b0d0
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0xf19af
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movzbl -0x4b(%rbp), %esi
callq 0x178880
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xf198b
jmp 0xf19b1
jmp 0xf18a0
movzbl -0x49(%rbp), %eax
andl $0x3, %eax
movb %al, -0x4c(%rbp)
movzbl -0x4c(%rbp), %eax
cmpl $0x0, %eax
je 0xf19d1
movq -0x10(%rbp), %rax
movb $0x1, 0x68(%rax)
movzbl -0x4c(%rbp), %eax
movl %eax, -0x60(%rbp)
testl %eax, %eax
je 0xf19f9
jmp 0xf19de
movl -0x60(%rbp), %eax
subl $0x1, %eax
je 0xf1a21
jmp 0xf19e8
movl -0x60(%rbp), %eax
subl $0x2, %eax
je 0xf1a77
jmp 0xf1ac5
leaq 0xb74a0(%rip), %rax     # 0x1a8ea0
movq %rax, -0x58(%rbp)
movzbl -0x49(%rbp), %eax
sarl $0x2, %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
jmp 0xf1b34
leaq 0xdabbe(%rip), %rax     # 0x1cc5e6
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x4a(%rbp)
movzbl -0x49(%rbp), %eax
sarl $0x2, %eax
andl $0x3, %eax
movl %eax, -0x48(%rbp)
movzbl -0x49(%rbp), %eax
sarl $0x4, %eax
andl $0x7, %eax
addl $0x3, %eax
movl %eax, -0x44(%rbp)
movzbl -0x4a(%rbp), %eax
shll %eax
movzbl -0x49(%rbp), %ecx
sarl $0x7, %ecx
orl %ecx, %eax
movl %eax, -0x40(%rbp)
jmp 0xf1b34
leaq 0xba8fa(%rip), %rax     # 0x1ac378
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x4a(%rbp)
movl $0x0, -0x48(%rbp)
movzbl -0x49(%rbp), %eax
sarl $0x2, %eax
andl $0x3, %eax
addl $0x3, %eax
movl %eax, -0x44(%rbp)
movzbl -0x4a(%rbp), %eax
shll $0x4, %eax
movzbl -0x49(%rbp), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
movl %eax, -0x40(%rbp)
jmp 0xf1b34
leaq 0xd916e(%rip), %rax     # 0x1cac3a
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x4a(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x4b(%rbp)
movzbl -0x49(%rbp), %eax
sarl $0x2, %eax
andl $0xf, %eax
movl %eax, -0x48(%rbp)
movzbl -0x4a(%rbp), %eax
andl $0xf, %eax
shll $0x2, %eax
movzbl -0x49(%rbp), %ecx
sarl $0x6, %ecx
orl %ecx, %eax
movl %eax, -0x44(%rbp)
movzbl -0x4b(%rbp), %eax
shll $0x4, %eax
movzbl -0x4a(%rbp), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
jl 0xf1b6c
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rdx
movl -0x48(%rbp), %ecx
movl -0x40(%rbp), %r8d
movl -0x44(%rbp), %r9d
leaq 0xccc6a(%rip), %rsi     # 0x1be7c1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0xf1bcd
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
callq 0x165170
movb %al, -0x4b(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
jl 0xf1bb1
movq -0x8(%rbp), %rdi
movzbl -0x4b(%rbp), %edx
leaq 0xe49ce(%rip), %rsi     # 0x1d6578
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movzbl -0x4b(%rbp), %esi
callq 0x178880
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xf1b73
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
jl 0xf1be4
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpl $0x0, -0x44(%rbp)
je 0xf1c08
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax), %esi
subl -0x40(%rbp), %esi
movl -0x44(%rbp), %edx
callq 0x1788e0
jmp 0xf18a0
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x167c30
movq -0x28(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
subq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x178950
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15b820
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)