// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/arj.c
// Function: decompress_method_4
static void decompress_method_4(deark *c, lctx *d, struct member_data *md,
	struct de_dfilter_in_params *dcmpri, struct de_dfilter_out_params *dcmpro,
	struct de_dfilter_results *dres)
{
	struct method4_ctx *cctx = NULL;
	struct de_lz77buffer *ringbuf = NULL;

	cctx = de_malloc(c, sizeof(struct method4_ctx));
	cctx->dcmpro = dcmpro;
	cctx->bitrd.f = dcmpri->f;
	cctx->bitrd.curpos = dcmpri->pos;
	cctx->bitrd.endpos = dcmpri->pos + dcmpri->len;

	if(md->archiver_ver_num==1) {
		cctx->old_format = 1;
	}

	// The maximum offset that can be encoded is 15871, so a 16K history is enough.
	ringbuf = de_lz77buffer_create(c, 16384);
	ringbuf->writebyte_cb = method4_lz77buf_writebytecb;
	ringbuf->userdata = (void*)cctx;

	while(1) {
		UI len_code;

		if(cctx->bitrd.eof_flag) goto done;
		if(cctx->stop_flag) goto done;
		if(cctx->dcmpro->len_known && (cctx->nbytes_written >= cctx->dcmpro->expected_len)) {
			goto done;
		}

		len_code = method4_read_a_length_code(cctx);
		if(len_code==0) {
			u8 b;

			b = (u8)de_bitreader_getbits(&cctx->bitrd, 8);
			if(c->debug_level>=4) {
				de_dbg(c, "lit %u", (UI)b);
			}
			de_lz77buffer_add_literal_byte(ringbuf, b);
		}
		else {
			UI offs;

			offs = method4_read_an_offset(cctx);
			if(c->debug_level>=4) {
				de_dbg(c, "match d=%u l=%u", (UI)(offs+1), (UI)(len_code+2));
			}
			de_lz77buffer_copy_from_hist(ringbuf, ringbuf->curpos-1-offs, len_code+2);
		}
	}

done:
	dres->bytes_consumed_valid = 1;
	dres->bytes_consumed = cctx->bitrd.curpos - dcmpri->pos;
	de_lz77buffer_destroy(c, ringbuf);
	de_free(c, cctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
movl $0x48, %esi
callq 0xe8b2b
movq %rax, %r12
movq %r14, 0x10(%rax)
movq (%r13), %rax
movq %rax, 0x18(%r12)
movq 0x8(%r13), %rax
movq %rax, 0x20(%r12)
movq %r13, 0x8(%rsp)
addq 0x10(%r13), %rax
movq %rax, 0x28(%r12)
cmpb $0x1, 0xd(%r15)
jne 0x37e50
movb $0x1, 0xc(%r12)
movq %rbx, %rdi
movl $0x4000, %esi           # imm = 0x4000
callq 0xfad8f
movq %rax, %r13
leaq 0x1b5(%rip), %rax       # 0x3801c
movq %rax, 0x8(%r13)
movq %r12, (%r13)
cmpb $0x0, 0x30(%r12)
je 0x37eb6
movq 0x10(%rsp), %rcx
movb $0x1, 0x4(%rcx)
movq 0x20(%r12), %rax
movq 0x8(%rsp), %rdx
subq 0x8(%rdx), %rax
movq %rax, 0x8(%rcx)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xfaed4
movq %rbx, %rdi
movq %r12, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2
movq %r12, %rbp
addq $0x18, %rbp
cmpl $0x0, 0x8(%r12)
jne 0x37e77
movq 0x10(%r12), %rax
cmpb $0x0, 0x8(%rax)
je 0x37eda
movq (%r12), %rcx
cmpq 0x10(%rax), %rcx
jge 0x37e77
xorl %r15d, %r15d
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf1bed
testl %eax, %eax
je 0x37f14
incl %r15d
cmpl $0x7, %r15d
jne 0x37edd
movl $0x7, %r15d
cmpb $0x0, 0xc(%r12)
je 0x37f1d
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf1bed
jmp 0x37f1d
testl %r15d, %r15d
je 0x37fba
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf1bed
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r15d, %ecx
shll %cl, %r14d
notl %r14d
addl %eax, %r14d
je 0x37fba
movl $0x9, %r15d
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf1bed
testl %eax, %eax
je 0x37f77
incl %r15d
cmpl $0xd, %r15d
jne 0x37f42
movl $0xd, %r15d
cmpb $0x0, 0xc(%r12)
je 0x37f77
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf1bed
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf1bed
movl $0x1, %edx
movl %r15d, %ecx
shll %cl, %edx
leal (%rdx,%rax), %r15d
addl $0xfffffe00, %r15d      # imm = 0xFFFFFE00
cmpl $0x3, (%rbx)
jle 0x37ff2
leal 0x1(%r15), %edx
addl $0x2, %r14d
movq %rbx, %rdi
leaq 0x1139f6(%rip), %rsi    # 0x14b9a4
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe8617
jmp 0x37ff6
movq %rbp, %rdi
movl $0x8, %esi
callq 0xf1bed
movq %rax, %r14
cmpl $0x4, (%rbx)
jl 0x37fe4
movzbl %r14b, %edx
movq %rbx, %rdi
leaq 0x112b1f(%rip), %rsi    # 0x14aafc
xorl %eax, %eax
callq 0xe8617
movzbl %r14b, %esi
movq %r13, %rdi
callq 0xfae4e
jmp 0x3800b
addl $0x2, %r14d
notl %r15d
addl 0x10(%r13), %r15d
movq %r13, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0xfae7b
cmpb $0x0, 0x30(%r12)
je 0x37ebd
jmp 0x37e77