// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: decompress_implode_internal
static void decompress_implode_internal(struct lzh_ctx *cctx)
{
	UI rb_size;
	UI offset_num_low_bits;
	deark *c = cctx->c;

	rb_size = (cctx->implode_8k_buffer) ? 8192 : 4096;

	cctx->ringbuf = de_lz77buffer_create(c, rb_size);
	cctx->ringbuf->userdata = (void*)cctx;
	cctx->ringbuf->writebyte_cb = lzh_lz77buf_writebytecb;

	if(!implode_read_trees(cctx)) {
		cctx->err_flag = 1;
		goto done;
	}

	de_dbg2(c, "compressed data codes at %"I64_FMT, cctx->bitrd.curpos);
	offset_num_low_bits = (cctx->implode_8k_buffer) ? 7 : 6;

	while(1) {
		UI n;

		if(cctx->bitrd.eof_flag || cctx->err_flag) break;
		if(lzh_have_enough_output(cctx)) break;

		n = (UI)lzh_getbits(cctx, 1);
		if(n) { // literal
			u8 b;

			if(cctx->literals_tree.ht) {
				b = (u8)read_next_code_using_tree(cctx, &cctx->literals_tree);
			}
			else {
				b = (u8)lzh_getbits(cctx, 8);
			}
			if(cctx->c->debug_level>=4) {
				de_dbg(c, "lit %u", (UI)b);
			}
			de_lz77buffer_add_literal_byte(cctx->ringbuf, b);
		}
		else { // match
			UI offset_low_bits, offset_high_bits, offset;
			UI matchlen_code, matchlen;

			offset_low_bits = (UI)lzh_getbits(cctx, offset_num_low_bits);
			offset_high_bits = (UI)read_next_code_using_tree(cctx, &cctx->offsets_tree);
			offset = (offset_high_bits << offset_num_low_bits) | offset_low_bits;

			matchlen_code = (UI)read_next_code_using_tree(cctx, &cctx->matchlengths_tree);
			if(matchlen_code == 63) {
				n = (UI)lzh_getbits(cctx, 8);
				matchlen = cctx->implode_min_match_len + 63 + n;
			}
			else {
				matchlen = cctx->implode_min_match_len + matchlen_code;
			}

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

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movzbl 0xb0(%rax), %edx
movl $0x1000, %eax           # imm = 0x1000
movl $0x2000, %ecx           # imm = 0x2000
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0xc(%rbp)
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x1785d0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x8(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
leaq 0x906(%rip), %rcx       # 0x1801b0
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
callq 0x1835c0
cmpl $0x0, %eax
jne 0x17f8cc
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x17fa8d
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdx
leaq 0x57922(%rip), %rsi     # 0x1d7201
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rax
movzbl 0xb0(%rax), %edx
movl $0x6, %eax
movl $0x7, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
jne 0x17f91b
movq -0x8(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x17f920
jmp 0x17fa8b
movq -0x8(%rbp), %rdi
callq 0x181570
cmpl $0x0, %eax
je 0x17f933
jmp 0x17fa8b
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x181a10
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x17f9ba
movq -0x8(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x17f971
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x98, %rsi
callq 0x181ae0
movb %al, -0x1d(%rbp)
jmp 0x17f982
movq -0x8(%rbp), %rdi
movl $0x8, %esi
callq 0x181a10
movb %al, -0x1d(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
cmpl $0x4, (%rax)
jl 0x17f9a4
movq -0x18(%rbp), %rdi
movzbl -0x1d(%rbp), %edx
leaq 0x56bdb(%rip), %rsi     # 0x1d6578
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rdi
movzbl -0x1d(%rbp), %esi
callq 0x178880
jmp 0x17fa86
movq -0x8(%rbp), %rdi
movl -0x10(%rbp), %esi
callq 0x181a10
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xa0, %rsi
callq 0x181ae0
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl -0x10(%rbp), %ecx
shll %cl, %eax
orl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xa8, %rsi
callq 0x181ae0
movl %eax, -0x30(%rbp)
cmpl $0x3f, -0x30(%rbp)
jne 0x17fa31
movq -0x8(%rbp), %rdi
movl $0x8, %esi
callq 0x181a10
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movl 0xb4(%rax), %eax
addl $0x3f, %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x17fa41
movq -0x8(%rbp), %rax
movl 0xb4(%rax), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x4, (%rax)
jl 0x17fa65
movq -0x18(%rbp), %rdi
movl -0x2c(%rbp), %edx
addl $0x1, %edx
movl -0x34(%rbp), %ecx
leaq 0x57972(%rip), %rsi     # 0x1d73d0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movl 0x10(%rax), %esi
subl $0x1, %esi
subl -0x2c(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0x1788e0
jmp 0x17f904
jmp 0x17fa8d
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)