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

	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) { // a literal
			u8 b;

			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 { // an offset+distance, or a stop code

			UI matchlen_code, matchlen;
			UI offset_code, offset;

			// Read the offset

			n = (UI)lzh_getbits(cctx, 1);
			if(n==0) { // an 11-bit offset...
				offset_code = (UI)lzh_getbits(cctx, 11);
				if(offset_code==0) { // Expecting at least 128
					cctx->err_flag = 1;
					goto done;
				}
				offset = offset_code - 1;
			}
			else { // 7-bit offset, or stop code
				offset_code = (UI)lzh_getbits(cctx, 7);
				if(offset_code==0) {
					if(cctx->c->debug_level>=4) {
						de_dbg(c, "stop code");
					}
					goto done;
				}
				offset = offset_code - 1;

			}

			// Read the match-length

			matchlen_code = (UI)read_next_code_using_tree(cctx, &cctx->matchlengths_tree);

			if(matchlen_code < 6) {
				matchlen = matchlen_code + 2;
			}
			else {
				matchlen = 8; // This will be increased as we read more bits

				while(1) {
					matchlen_code = (UI)lzh_getbits(cctx, 4);
					matchlen += matchlen_code;

					// Arbitrary limit. The matchlen can definitely be *much* larger
					// than the history buffer.
					if(matchlen > 0x80000000) {
						cctx->err_flag = 1;
						goto done;
					}

					if(matchlen_code < 15) {
						break;
					}
				}
			}

			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:
	de_bitreader_skip_to_byte_boundary(&cctx->bitrd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x50(%rax), %eax
cmpl $0x0, %eax
jne 0x180afe
movq -0x8(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x180b3a
movq -0x8(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x180b35
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x567a8(%rip), %rcx     # 0x1d72cb
movb $0x0, %al
callq 0x177450
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x180cde
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x181a10
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x180b9a
movq -0x8(%rbp), %rdi
movl $0x8, %esi
callq 0x181a10
movb %al, -0x15(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
cmpl $0x4, (%rax)
jl 0x180b84
movq -0x10(%rbp), %rdi
movzbl -0x15(%rbp), %edx
leaq 0x559fb(%rip), %rsi     # 0x1d6578
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rdi
movzbl -0x15(%rbp), %esi
callq 0x178880
jmp 0x180cd9
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x181a10
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x180be3
movq -0x8(%rbp), %rdi
movl $0xb, %esi
callq 0x181a10
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x180bd8
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x180cde
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x180c26
movq -0x8(%rbp), %rdi
movl $0x7, %esi
callq 0x181a10
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x180c1d
movq -0x8(%rbp), %rax
movq (%rax), %rax
cmpl $0x4, (%rax)
jl 0x180c18
movq -0x10(%rbp), %rdi
leaq 0x2962c(%rip), %rsi     # 0x1aa23d
movb $0x0, %al
callq 0x15b0d0
jmp 0x180cde
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xa8, %rsi
callq 0x181ae0
movl %eax, -0x1c(%rbp)
cmpl $0x6, -0x1c(%rbp)
jae 0x180c4e
movl -0x1c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x20(%rbp)
jmp 0x180c91
movl $0x8, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl $0x4, %esi
callq 0x181a10
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x80000000, -0x20(%rbp) # imm = 0x80000000
jbe 0x180c85
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x180cde
cmpl $0xf, -0x1c(%rbp)
jae 0x180c8d
jmp 0x180c8f
jmp 0x180c55
jmp 0x180c91
movq -0x8(%rbp), %rax
movq (%rax), %rax
cmpl $0x4, (%rax)
jl 0x180cb8
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %edx
addl $0x1, %edx
movl -0x20(%rbp), %ecx
leaq 0x5671f(%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 -0x28(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x1788e0
jmp 0x180ae7
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
callq 0x16a460
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)