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

	if(cctx->dcmpri->len==0) {
		// This special case is possibly a standard part of DCL Implode format:
		// A zero-length input file, with no header or stop code, decompresses to
		// a zero-length output file.
		goto done;
	}

	b = (u8)lzh_getbits(cctx, 8);
	if(b==1) {
		cctx->implode_3_trees = 1;
	}
	else if(b!=0) {
		cctx->err_flag = 1;
		goto done;
	}
	de_dbg2(c, "has literals tree: %u", (UI)cctx->implode_3_trees);

	cctx->dist_code_extra_bits = (UI)lzh_getbits(cctx, 8);
	de_dbg2(c, "dist code extra bits: %u", cctx->dist_code_extra_bits);
	if(cctx->dist_code_extra_bits<4 || cctx->dist_code_extra_bits>6) {
		cctx->err_flag = 1;
		goto done;
	}

	if(cctx->implode_3_trees) {
		make_dclimplode_tree(c, cctx, &cctx->literals_tree, 256, dclimpl_litlengths);
	}
	make_dclimplode_tree(c, cctx, &cctx->matchlengths_tree, 16, dclimpl_lenlengths);
	make_dclimplode_tree(c, cctx, &cctx->offsets_tree, 64, dclimpl_distlengths);

	// Need at least:
	//  1024 if dist_code_extra_bits==4
	//  2048 if dist_code_extra_bits==5
	//  4096 if dist_code_extra_bits==6
	cctx->ringbuf = de_lz77buffer_create(cctx->c, 4096);
	cctx->ringbuf->userdata = (void*)cctx;
	cctx->ringbuf->writebyte_cb = lzh_lz77buf_writebytecb_flagerrors;

	while(1) {
		UI n;

		if(cctx->bitrd.eof_flag || cctx->err_flag) {
			if(!cctx->err_flag) {
				de_dfilter_set_errorf(c, cctx->dres, cctx->modname,
					"Decoding error (end-of-data code not found)");
				cctx->err_flag = 1;
			}
			goto done;
		}

		n = (UI)lzh_getbits(cctx, 1);
		if(n==0) { // 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 {
			UI matchlen_code, matchlen;
			UI more_bits_count;
			UI more_bits;
			UI offset_code, offset;

			matchlen_code = (UI)read_next_code_using_tree(cctx, &cctx->matchlengths_tree);
			if(matchlen_code>=16) goto done;

			if(matchlen_code==0) {
				matchlen = 3;
			}
			else if(matchlen_code==1) {
				matchlen = 2;
			}
			else if(matchlen_code<=7) {
				matchlen = matchlen_code + 2;
			}
			else { // 8..15
				more_bits_count = matchlen_code - 7;
				more_bits = (UI)lzh_getbits(cctx, more_bits_count);
				matchlen = (1U << more_bits_count) + more_bits + 8;
			}
			if(matchlen==519) goto done;

			offset_code = (UI)read_next_code_using_tree(cctx, &cctx->offsets_tree);
			if(matchlen==2) {
				more_bits_count = 2;
			}
			else {
				more_bits_count = cctx->dist_code_extra_bits;
			}
			more_bits = (UI)lzh_getbits(cctx, more_bits_count);
			offset = (offset_code << more_bits_count) + more_bits;

			if(cctx->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, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x10(%rax)
jne 0x17fc1b
jmp 0x17ff96
movq -0x8(%rbp), %rdi
movl $0x8, %esi
callq 0x181a10
movb %al, -0x11(%rbp)
movzbl -0x11(%rbp), %eax
cmpl $0x1, %eax
jne 0x17fc42
movq -0x8(%rbp), %rax
movb $0x1, 0xb1(%rax)
jmp 0x17fc5d
movzbl -0x11(%rbp), %eax
cmpl $0x0, %eax
je 0x17fc5b
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x17ff96
jmp 0x17fc5d
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movzbl 0xb1(%rax), %edx
leaq 0x57629(%rip), %rsi     # 0x1d729c
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movl $0x8, %esi
callq 0x181a10
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xb8(%rax)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0xb8(%rax), %edx
leaq 0x57609(%rip), %rsi     # 0x1d72b2
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rax
cmpl $0x4, 0xb8(%rax)
jb 0x17fcca
movq -0x8(%rbp), %rax
cmpl $0x6, 0xb8(%rax)
jbe 0x17fcda
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x17ff96
movq -0x8(%rbp), %rax
cmpb $0x0, 0xb1(%rax)
je 0x17fd0b
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x8(%rbp), %rdx
addq $0x98, %rdx
movl $0x100, %ecx            # imm = 0x100
leaq 0x5773a(%rip), %r8      # 0x1d7440
callq 0x1838d0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x8(%rbp), %rdx
addq $0xa8, %rdx
movl $0x10, %ecx
leaq 0x57796(%rip), %r8      # 0x1d74c0
callq 0x1838d0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x8(%rbp), %rdx
addq $0xa0, %rdx
movl $0x40, %ecx
leaq 0x57782(%rip), %r8      # 0x1d74d0
callq 0x1838d0
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl $0x1000, %esi           # imm = 0x1000
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 0xcc3(%rip), %rcx       # 0x180a50
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
jne 0x17fda8
movq -0x8(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x17fde4
movq -0x8(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x17fddf
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x574fe(%rip), %rcx     # 0x1d72cb
movb $0x0, %al
callq 0x177450
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x17ff96
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x181a10
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x17fe6b
movq -0x8(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x17fe22
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x98, %rsi
callq 0x181ae0
movb %al, -0x19(%rbp)
jmp 0x17fe33
movq -0x8(%rbp), %rdi
movl $0x8, %esi
callq 0x181a10
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
cmpl $0x4, (%rax)
jl 0x17fe55
movq -0x10(%rbp), %rdi
movzbl -0x19(%rbp), %edx
leaq 0x5672a(%rip), %rsi     # 0x1d6578
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rdi
movzbl -0x19(%rbp), %esi
callq 0x178880
jmp 0x17ff91
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xa8, %rsi
callq 0x181ae0
movl %eax, -0x20(%rbp)
cmpl $0x10, -0x20(%rbp)
jb 0x17fe8d
jmp 0x17ff96
cmpl $0x0, -0x20(%rbp)
jne 0x17fe9c
movl $0x3, -0x24(%rbp)
jmp 0x17feeb
cmpl $0x1, -0x20(%rbp)
jne 0x17feab
movl $0x2, -0x24(%rbp)
jmp 0x17fee9
cmpl $0x7, -0x20(%rbp)
ja 0x17febc
movl -0x20(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x24(%rbp)
jmp 0x17fee7
movl -0x20(%rbp), %eax
subl $0x7, %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x181a10
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
addl -0x2c(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x24(%rbp)
jmp 0x17fee9
jmp 0x17feeb
cmpl $0x207, -0x24(%rbp)     # imm = 0x207
jne 0x17fef9
jmp 0x17ff96
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xa0, %rsi
callq 0x181ae0
movl %eax, -0x30(%rbp)
cmpl $0x2, -0x24(%rbp)
jne 0x17ff1f
movl $0x2, -0x28(%rbp)
jmp 0x17ff2c
movq -0x8(%rbp), %rax
movl 0xb8(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x181a10
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movl -0x28(%rbp), %ecx
shll %cl, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
cmpl $0x4, (%rax)
jl 0x17ff70
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %edx
addl $0x1, %edx
movl -0x24(%rbp), %ecx
leaq 0x57467(%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 -0x34(%rbp), %esi
movl -0x24(%rbp), %edx
callq 0x1788e0
jmp 0x17fd91
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)