// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: ic1_read_distance
static UI ic1_read_distance(struct lzh_ctx *cctx)
{
	i64 bd;
	UI v = 0;
	UI n_extra_bits1 = 0;
	UI n_extra_bits2 = 0;
	UI bias1 = 0;
	UI code11_param = 0;
	u8 b0, b1;
	deark *c = cctx->c;

	bd = cctx->nbytes_written;

	if(bd<=10) {
		n_extra_bits1 = 2;
	}
	else if(bd<=20) {
		n_extra_bits1 = 3;
	}
	else if(bd<=40) {
		n_extra_bits1 = 4;
	}
	else if(bd<=80) {
		n_extra_bits1 = 5;
	}
	else if(bd<=160) {
		n_extra_bits1 = 6;
	}
	else if(bd<=672) {
		n_extra_bits1 = 7;
	}
	else if(bd<=1000) {
		// ??? Obviously this should be 1344, not 1000. But 1344 doesn't work.
		n_extra_bits1 = 8;
	}
	else if(bd<=2688) {
		n_extra_bits1 = 9;
	}
	else if(bd<=5376) {
		n_extra_bits1 = 10;
	}
	else if(bd<=10752) {
		n_extra_bits1 = 11;
	}
	else if(bd<=21504) {
		n_extra_bits1 = 12;
	}
	else {
		n_extra_bits1 = 13;
	}

	if(n_extra_bits1>=2) {
		n_extra_bits2 = n_extra_bits1 - 2;
		bias1 = 1U<<n_extra_bits2;
	}
	if(n_extra_bits1>=3) {
		code11_param = (1U<<(n_extra_bits1-3))*10;
	}

	b0 = (u8)lzh_getbits(cctx, 1);
	if(b0==1) {
		b1 = (u8)lzh_getbits(cctx, 1);
		if(b1==0) { // addressing mode "10"
			v = (UI)lzh_getbits(cctx, n_extra_bits1);
			v += bias1;
		}
		else { // addressing mode "11"
			if(code11_param) {
				UI n_extra_bits3;

				if(bd>=1644 && bd<=1664) {
					// FIXME: This hack shouldn't be here, and this range may be wrong.
					// 1642 needs to NOT be included
					// 1643 unknown
					// 1644 needs to be included
					n_extra_bits3 = 11;
				}
				else if(bd>43008) {
					n_extra_bits3 = 15;
				}
				else {
					n_extra_bits3 = (UI)de_log2_rounded_up((i64)bd-(i64)code11_param);
				}

				v = (UI)lzh_getbits(cctx, n_extra_bits3);
				v += code11_param;
			}
			else {
				cctx->err_flag = 1;
				de_dfilter_set_errorf(c, cctx->dres, cctx->modname,
					"TODO code11 bd=%"I64_FMT, bd);
				goto done;
			}
		}
	}
	else { // addressing mode "0"
		v = (UI)lzh_getbits(cctx, n_extra_bits2);
	}

done:
	return v;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x10(%rbp)
cmpq $0xa, -0x10(%rbp)
jg 0x1840d9
movl $0x2, -0x18(%rbp)
jmp 0x1841b5
cmpq $0x14, -0x10(%rbp)
jg 0x1840ec
movl $0x3, -0x18(%rbp)
jmp 0x1841b3
cmpq $0x28, -0x10(%rbp)
jg 0x1840ff
movl $0x4, -0x18(%rbp)
jmp 0x1841b1
cmpq $0x50, -0x10(%rbp)
jg 0x184112
movl $0x5, -0x18(%rbp)
jmp 0x1841af
cmpq $0xa0, -0x10(%rbp)
jg 0x184128
movl $0x6, -0x18(%rbp)
jmp 0x1841ad
cmpq $0x2a0, -0x10(%rbp)     # imm = 0x2A0
jg 0x18413b
movl $0x7, -0x18(%rbp)
jmp 0x1841ab
cmpq $0x3e8, -0x10(%rbp)     # imm = 0x3E8
jg 0x18414e
movl $0x8, -0x18(%rbp)
jmp 0x1841a9
cmpq $0xa80, -0x10(%rbp)     # imm = 0xA80
jg 0x184161
movl $0x9, -0x18(%rbp)
jmp 0x1841a7
cmpq $0x1500, -0x10(%rbp)    # imm = 0x1500
jg 0x184174
movl $0xa, -0x18(%rbp)
jmp 0x1841a5
cmpq $0x2a00, -0x10(%rbp)    # imm = 0x2A00
jg 0x184187
movl $0xb, -0x18(%rbp)
jmp 0x1841a3
cmpq $0x5400, -0x10(%rbp)    # imm = 0x5400
jg 0x18419a
movl $0xc, -0x18(%rbp)
jmp 0x1841a1
movl $0xd, -0x18(%rbp)
jmp 0x1841a3
jmp 0x1841a5
jmp 0x1841a7
jmp 0x1841a9
jmp 0x1841ab
jmp 0x1841ad
jmp 0x1841af
jmp 0x1841b1
jmp 0x1841b3
jmp 0x1841b5
cmpl $0x2, -0x18(%rbp)
jb 0x1841d1
movl -0x18(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x20(%rbp)
cmpl $0x3, -0x18(%rbp)
jb 0x1841ea
movl -0x18(%rbp), %ecx
subl $0x3, %ecx
movl $0x1, %eax
shll %cl, %eax
imull $0xa, %eax, %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x181a10
movb %al, -0x25(%rbp)
movzbl -0x25(%rbp), %eax
cmpl $0x1, %eax
jne 0x1842da
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x181a10
movb %al, -0x26(%rbp)
movzbl -0x26(%rbp), %eax
cmpl $0x0, %eax
jne 0x18423f
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x181a10
movl %eax, -0x14(%rbp)
movl -0x20(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x1842d8
cmpl $0x0, -0x24(%rbp)
je 0x1842a3
cmpq $0x66c, -0x10(%rbp)     # imm = 0x66C
jl 0x184262
cmpq $0x680, -0x10(%rbp)     # imm = 0x680
jg 0x184262
movl $0xb, -0x34(%rbp)
jmp 0x184289
cmpq $0xa800, -0x10(%rbp)    # imm = 0xA800
jle 0x184275
movl $0xf, -0x34(%rbp)
jmp 0x184287
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %eax
subq %rax, %rdi
callq 0x15cef0
movl %eax, -0x34(%rbp)
jmp 0x184289
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x181a10
movl %eax, -0x14(%rbp)
movl -0x24(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x1842d6
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x10(%rbp), %r8
leaq 0x53125(%rip), %rcx     # 0x1d73f2
movb $0x0, %al
callq 0x177450
jmp 0x1842eb
jmp 0x1842d8
jmp 0x1842e9
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x181a10
movl %eax, -0x14(%rbp)
jmp 0x1842eb
movl -0x14(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)