// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
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 0x37f65
movb $0x1, 0xc(%r12)
movq %rbx, %rdi
movl $0x4000, %esi           # imm = 0x4000
callq 0xfb2c3
movq %rax, %r13
leaq 0x1b6(%rip), %rax       # 0x38132
movq %rax, 0x8(%r13)
movq %r12, (%r13)
cmpb $0x0, 0x30(%r12)
je 0x37fcb
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 0xfb408
movq %rbx, %rdi
movq %r12, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9752
movq %r12, %rbp
addq $0x18, %rbp
cmpl $0x0, 0x8(%r12)
jne 0x37f8c
movq 0x10(%r12), %rax
cmpb $0x0, 0x8(%rax)
je 0x37fef
movq (%r12), %rcx
cmpq 0x10(%rax), %rcx
jge 0x37f8c
xorl %r15d, %r15d
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf2271
testl %eax, %eax
je 0x38027
incl %r15d
cmpl $0x7, %r15d
jne 0x37ff2
movl $0x7, %r15d
cmpb $0x0, 0xc(%r12)
je 0x38027
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf2271
testl %r15d, %r15d
je 0x3804b
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf2271
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r15d, %ecx
shll %cl, %r14d
notl %r14d
addl %eax, %r14d
jmp 0x3804e
xorl %r14d, %r14d
testl %r14d, %r14d
je 0x380eb
movl $0x9, %r15d
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf2271
testl %eax, %eax
je 0x38092
incl %r15d
cmpl $0xd, %r15d
jne 0x3805d
movl $0xd, %r15d
cmpb $0x0, 0xc(%r12)
je 0x38092
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf2271
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf2271
movl $0x1, %edx
movl %r15d, %ecx
shll %cl, %edx
leal (%rdx,%rax), %r15d
addl $0xfffffe00, %r15d      # imm = 0xFFFFFE00
cmpl $0x4, (%rbx)
jl 0x380d0
leal 0x1(%r15), %edx
leal 0x2(%r14), %ecx
movq %rbx, %rdi
leaq 0x1132e4(%rip), %rsi    # 0x14b3ad
xorl %eax, %eax
callq 0xe9226
notl %r15d
addl 0x10(%r13), %r15d
addl $0x2, %r14d
movq %r13, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0xfb3af
jmp 0x38121
movq %rbp, %rdi
movl $0x8, %esi
callq 0xf2271
movq %rax, %r14
cmpl $0x4, (%rbx)
jl 0x38115
movzbl %r14b, %edx
movq %rbx, %rdi
leaq 0x11243e(%rip), %rsi    # 0x14a54c
xorl %eax, %eax
callq 0xe9226
movzbl %r14b, %esi
movq %r13, %rdi
callq 0xfb382
cmpb $0x0, 0x30(%r12)
je 0x37fd2
jmp 0x37f8c