// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: lh5x_do_lzh_block
static void lh5x_do_lzh_block(struct lzh_ctx *cctx, int blk_idx)
{
	deark *c = cctx->c;
	UI ncodes_in_this_block;
	UI ncodes_remaining_this_block;
	int saved_indent_level;
	char pos_descr[32];

	de_dbg_indent_save(c, &saved_indent_level);

	de_bitreader_describe_curpos(&cctx->bitrd, pos_descr, sizeof(pos_descr));
	ncodes_in_this_block = (UI)lzh_getbits(cctx, 16);
	if(cctx->bitrd.eof_flag) {
		de_dbg2(c, "stopping, not enough room for a block at %s", pos_descr);
		goto done;
	}
	de_dbg2(c, "block#%d at %s", blk_idx, pos_descr);
	de_dbg_indent(c, 1);
	de_dbg2(cctx->c, "num codes in block: %u", (UI)ncodes_in_this_block);

	if(ncodes_in_this_block==0) {
		if(cctx->warn_about_zero_codes_block && !cctx->zero_codes_block_warned) {
			de_warn(c, "Block with \"0\" codes found. This file might not be portable.");
			cctx->zero_codes_block_warned = 1;
		}

		if(cctx->zero_codes_block_behavior==DE_LH5X_ZCB_0) {
			;
		}
		else if(cctx->zero_codes_block_behavior==DE_LH5X_ZCB_65536) {
			ncodes_in_this_block = 65536;
		}
		else if(cctx->zero_codes_block_behavior==DE_LH5X_ZCB_STOP) {
			de_dbg2(c, "stopping, 'stop' code found");
			cctx->bitrd.eof_flag = 1;
			goto done;
		}
		else {
			de_dfilter_set_errorf(c, cctx->dres, cctx->modname, "Bad LZH 'blocksize'");
			cctx->bitrd.eof_flag = 1;
			goto done;
		}
	}

	if(!lh5x_do_read_trees(cctx)) {
		de_dfilter_set_errorf(c, cctx->dres, cctx->modname, "Bad Huffman tree definitions");
		goto done;
	}

	de_bitreader_describe_curpos(&cctx->bitrd, pos_descr, sizeof(pos_descr));
	de_dbg2(c, "cmpr data codes at %s", pos_descr);
	ncodes_remaining_this_block = ncodes_in_this_block;
	while(1) {
		UI code;

		if(ncodes_remaining_this_block==0) goto done;
		if(cctx->bitrd.eof_flag) goto done;

		code = read_next_code_using_tree(cctx, &cctx->literals_tree);
		if(cctx->bitrd.eof_flag) goto done;

		if(code < 256) { // literal
			if(c->debug_level>=4) {
				de_dbg(c, "lit %u", code);
			}
			de_lz77buffer_add_literal_byte(cctx->ringbuf, (u8)code);
		}
		else { // repeat previous bytes
			UI offset;
			UI length;
			UI ocode1;
			UI offs_low_nbits;
			UI ocode2;

			if(cctx->is_lhark_lh7 && code>=264) {
				if(code<288) {
					UI len_low;
					UI len_low_nbits;

					len_low_nbits = (code-260)/4;
					len_low = (UI)lzh_getbits(cctx, len_low_nbits);
					if(cctx->bitrd.eof_flag) goto done;
					length = ((4+(code%4)) << len_low_nbits) + len_low + 3;
				}
				else { // presumably, code==288
					length = 514;
				}
			}
			else {
				length = code-253;
			}

			ocode1 = read_next_code_using_tree(cctx, &cctx->offsets_tree);
			if(cctx->bitrd.eof_flag) goto done;

			if(cctx->is_lhark_lh7) {
				if(ocode1<=3) {
					offset = ocode1;
				}
				else {
					offs_low_nbits = (ocode1-2)/2;
					ocode2 = (UI)lzh_getbits(cctx, offs_low_nbits);
					if(cctx->bitrd.eof_flag) goto done;
					offset = ((2+(ocode1%2))<<offs_low_nbits) + ocode2;
				}
			}
			else {
				if(ocode1<=1) {
					offset = ocode1;
				}
				else {
					offs_low_nbits = ocode1-1;
					ocode2 = (UI)lzh_getbits(cctx, offs_low_nbits);
					if(cctx->bitrd.eof_flag) goto done;
					offset = (1U<<offs_low_nbits) + ocode2;
				}
			}

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

		ncodes_remaining_this_block--;
	}

done:
	de_bitreader_describe_curpos(&cctx->bitrd, pos_descr, sizeof(pos_descr));
	de_dbg3(c, "block ends at %s", pos_descr);

	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
leaq -0x50(%rbp), %rsi
movl $0x20, %edx
callq 0x16a540
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x181a10
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x181633
movq -0x18(%rbp), %rdi
leaq -0x50(%rbp), %rdx
leaq 0x55753(%rip), %rsi     # 0x1d6d7a
movb $0x0, %al
callq 0x15b350
jmp 0x1819d0
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %edx
leaq -0x50(%rbp), %rcx
leaq 0x55761(%rip), %rsi     # 0x1d6da6
movb $0x0, %al
callq 0x15b350
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0x5574a(%rip), %rsi     # 0x1d6db5
movb $0x0, %al
callq 0x15b350
cmpl $0x0, -0x1c(%rbp)
jne 0x181731
movq -0x8(%rbp), %rax
movzbl 0x69(%rax), %eax
cmpl $0x0, %eax
je 0x1816ad
movq -0x8(%rbp), %rax
cmpb $0x0, 0x6a(%rax)
jne 0x1816ad
movq -0x18(%rbp), %rdi
leaq 0x5572e(%rip), %rsi     # 0x1d6dcc
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movb $0x1, 0x6a(%rax)
movq -0x8(%rbp), %rax
movzbl 0x68(%rax), %eax
cmpl $0x2, %eax
jne 0x1816bc
jmp 0x18172f
movq -0x8(%rbp), %rax
movzbl 0x68(%rax), %eax
cmpl $0x3, %eax
jne 0x1816d2
movl $0x10000, -0x1c(%rbp)   # imm = 0x10000
jmp 0x18172d
movq -0x8(%rbp), %rax
movzbl 0x68(%rax), %eax
cmpl $0x1, %eax
jne 0x1816fe
movq -0x18(%rbp), %rdi
leaq 0x5571f(%rip), %rsi     # 0x1d6e09
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x1819d0
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x5570c(%rip), %rcx     # 0x1d6e25
movb $0x0, %al
callq 0x177450
movq -0x8(%rbp), %rax
movb $0x1, 0x50(%rax)
jmp 0x1819d0
jmp 0x18172f
jmp 0x181731
movq -0x8(%rbp), %rdi
callq 0x181a40
cmpl $0x0, %eax
jne 0x181766
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x556df(%rip), %rcx     # 0x1d6e39
movb $0x0, %al
callq 0x177450
jmp 0x1819d0
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
leaq -0x50(%rbp), %rsi
movl $0x20, %edx
callq 0x16a540
movq -0x18(%rbp), %rdi
leaq -0x50(%rbp), %rdx
leaq 0x28611(%rip), %rsi     # 0x1a9d9c
movb $0x0, %al
callq 0x15b350
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x1817a3
jmp 0x1819d0
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x1817b2
jmp 0x1819d0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x98, %rsi
callq 0x181ae0
movl %eax, -0x54(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x1817d8
jmp 0x1819d0
cmpl $0x100, -0x54(%rbp)     # imm = 0x100
jae 0x181817
movq -0x18(%rbp), %rax
cmpl $0x4, (%rax)
jl 0x1817ff
movq -0x18(%rbp), %rdi
movl -0x54(%rbp), %edx
leaq 0x54d80(%rip), %rsi     # 0x1d6578
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rdi
movl -0x54(%rbp), %eax
movzbl %al, %esi
callq 0x178880
jmp 0x1819c2
movq -0x8(%rbp), %rax
movzbl 0x7d(%rax), %eax
cmpl $0x0, %eax
je 0x181884
cmpl $0x108, -0x54(%rbp)     # imm = 0x108
jb 0x181884
cmpl $0x120, -0x54(%rbp)     # imm = 0x120
jae 0x18187b
movl -0x54(%rbp), %eax
subl $0x104, %eax            # imm = 0x104
shrl $0x2, %eax
movl %eax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movl -0x70(%rbp), %esi
callq 0x181a10
movl %eax, -0x6c(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x181862
jmp 0x1819d0
movl -0x54(%rbp), %eax
andl $0x3, %eax
addl $0x4, %eax
movl -0x70(%rbp), %ecx
shll %cl, %eax
addl -0x6c(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x5c(%rbp)
jmp 0x181882
movl $0x202, -0x5c(%rbp)     # imm = 0x202
jmp 0x18188f
movl -0x54(%rbp), %eax
subl $0xfd, %eax
movl %eax, -0x5c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xa0, %rsi
callq 0x181ae0
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x1818b5
jmp 0x1819d0
movq -0x8(%rbp), %rax
cmpb $0x0, 0x7d(%rax)
je 0x18190c
cmpl $0x3, -0x60(%rbp)
ja 0x1818cd
movl -0x60(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x18190a
movl -0x60(%rbp), %eax
subl $0x2, %eax
shrl %eax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x181a10
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x1818f6
jmp 0x1819d0
movl -0x60(%rbp), %eax
andl $0x1, %eax
addl $0x2, %eax
movl -0x64(%rbp), %ecx
shll %cl, %eax
addl -0x68(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x181953
cmpl $0x1, -0x60(%rbp)
ja 0x18191a
movl -0x60(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x181951
movl -0x60(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x181a10
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x181941
jmp 0x1819d0
movl -0x64(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
addl -0x68(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x181953
movq -0x18(%rbp), %rax
cmpl $0x4, (%rax)
jl 0x181977
movq -0x18(%rbp), %rdi
movl -0x58(%rbp), %edx
addl $0x1, %edx
movl -0x5c(%rbp), %ecx
leaq 0x55a60(%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 -0x58(%rbp), %esi
subl $0x1, %esi
movl -0x5c(%rbp), %edx
callq 0x1788e0
movl -0x58(%rbp), %eax
addq $0x1, %rax
movq -0x8(%rbp), %rcx
cmpq 0xc8(%rcx), %rax
jle 0x1819c0
movl -0x58(%rbp), %eax
movl %eax, %ecx
addq $0x1, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0xc8(%rax)
jmp 0x1819c2
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x181798
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
leaq -0x50(%rbp), %rsi
movl $0x20, %edx
callq 0x16a540
movq -0x18(%rbp), %rdi
leaq -0x50(%rbp), %rdx
leaq 0x55461(%rip), %rsi     # 0x1d6e56
movb $0x0, %al
callq 0x15b400
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x15b5a0
addq $0x70, %rsp
popq %rbp
retq
nop