// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: mash_main
static void mash_main(deark *c, struct lzh_ctx *cctx)
{
	while(1) {
		UI count_of_1s;
		UI offset = 0;
		UI matchlen = 0;
		UI unc_run_len = 0;
		u8 hflag1, hflag2;

		if(cctx->err_flag) goto done;
		if(lzh_have_enough_output(cctx)) goto done;

		// Read/decode the length of an uncompressed run (may be 0)

		count_of_1s = mash_read_run_of_1_bits(cctx, 6);
		if(count_of_1s<=5) {
			unc_run_len = count_of_1s;
		}
		else {
			count_of_1s = mash_read_run_of_1_bits(cctx, 16);
			if(count_of_1s>=16) {
				// TODO: Verify this limit
				cctx->err_flag = 1;
				goto done;
			}

			unc_run_len = mash_getbits(cctx, count_of_1s+1);
			unc_run_len += (2<<count_of_1s) + 4;
		}

		// Copy the literal bytes

		if(unc_run_len>0) {
			UI ii;
			u8 ib;

			for(ii=0; ii<unc_run_len; ii++) {
				if(cctx->bitrd.curpos >= cctx->bitrd.endpos) {
					cctx->err_flag = 1;
					return;
				}
				ib = dbuf_getbyte_p(cctx->dcmpri->f, &cctx->bitrd.curpos);
				if(c->debug_level>=4) {
					de_dbg(c, "lit %u", (UI)ib);
				}
				de_lz77buffer_add_literal_byte(cctx->ringbuf, ib);
				if(cctx->err_flag) goto done;
			}
		}

		if(lzh_have_enough_output(cctx)) goto done;

		// Read/decode a matchlen and offset

		hflag1 = mash_getbit(cctx);
		if(hflag1==0) {
			hflag2 = mash_getbit(cctx);
			if(hflag2==0) {
				matchlen = 2;
				offset = mash_getbits(cctx, 9);
			}
			else {
				matchlen = 3;
				offset = mash_read_offset(cctx);
			}
		}
		else {
			count_of_1s = mash_read_run_of_1_bits(cctx, 15);
			if(count_of_1s>=15) {
				// TODO: Verify this limit
				cctx->err_flag = 1;
				goto done;
			}

			matchlen = mash_getbits(cctx, count_of_1s+1);
			matchlen += (2<<count_of_1s) + 2;
			offset = mash_read_offset(cctx);
		}

		if(c->debug_level>=4) {
			de_dbg(c, "match d=%u l=%u", offset, matchlen);
		}

		if(offset==0) {
			// TODO: Figure out what offset 0 means
			// (seen at end of final chunk).
			goto done;
		}

		if((i64)offset > cctx->nbytes_written) {
			de_dfilter_set_errorf(c, cctx->dres, cctx->modname, "Bad offset");
			cctx->err_flag = 1;
			goto done;
		}

		de_lz77buffer_copy_from_hist(cctx->ringbuf,
			(UI)(cctx->ringbuf->curpos-offset), matchlen);
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x180ec4
jmp 0x18113d
movq -0x10(%rbp), %rdi
callq 0x181570
cmpl $0x0, %eax
je 0x180ed7
jmp 0x18113d
movq -0x10(%rbp), %rdi
movl $0x6, %esi
callq 0x183d00
movl %eax, -0x14(%rbp)
cmpl $0x5, -0x14(%rbp)
ja 0x180ef6
movl -0x14(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x180f42
movq -0x10(%rbp), %rdi
movl $0x10, %esi
callq 0x183d00
movl %eax, -0x14(%rbp)
cmpl $0x10, -0x14(%rbp)
jb 0x180f1d
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x18113d
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
callq 0x183d50
movl %eax, -0x20(%rbp)
movl -0x14(%rbp), %ecx
movl $0x2, %eax
shll %cl, %eax
addl $0x4, %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jbe 0x180ff1
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0x180fef
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jl 0x180f81
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x18113d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x38, %rsi
addq $0x8, %rsi
callq 0x165170
movb %al, -0x29(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
jl 0x180fbf
movq -0x8(%rbp), %rdi
movzbl -0x29(%rbp), %edx
leaq 0x555c0(%rip), %rsi     # 0x1d6578
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
movzbl -0x29(%rbp), %esi
callq 0x178880
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x180fdf
jmp 0x18113d
jmp 0x180fe1
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x180f53
jmp 0x180ff1
movq -0x10(%rbp), %rdi
callq 0x181570
cmpl $0x0, %eax
je 0x181004
jmp 0x18113d
movq -0x10(%rbp), %rdi
callq 0x183db0
movb %al, -0x21(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x0, %eax
jne 0x18105d
movq -0x10(%rbp), %rdi
callq 0x183db0
movb %al, -0x22(%rbp)
movzbl -0x22(%rbp), %eax
cmpl $0x0, %eax
jne 0x181048
movl $0x2, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
movl $0x9, %esi
callq 0x183d50
movl %eax, -0x18(%rbp)
jmp 0x18105b
movl $0x3, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x183e10
movl %eax, -0x18(%rbp)
jmp 0x1810b5
movq -0x10(%rbp), %rdi
movl $0xf, %esi
callq 0x183d00
movl %eax, -0x14(%rbp)
cmpl $0xf, -0x14(%rbp)
jb 0x181084
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x18113d
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
addl $0x1, %esi
callq 0x183d50
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %ecx
movl $0x2, %eax
shll %cl, %eax
addl $0x2, %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x183e10
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
jl 0x1810d6
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq 0x56301(%rip), %rsi     # 0x1d73d0
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x18(%rbp)
jne 0x1810de
jmp 0x18113d
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jle 0x18111a
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x5627d(%rip), %rcx     # 0x1d7383
movb $0x0, %al
callq 0x177450
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x18113d
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl 0x10(%rax), %esi
subl -0x18(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x1788e0
jmp 0x180ea0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)