// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/colorix.c
// Function: rixdecomp_codectype1
static void rixdecomp_codectype1(deark *c, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
	void *codec_private_params)
{
	struct rixdecomp_ctx *rhctx = NULL;
	i64 pos;
	i64 endpos;
	i64 seg_count;
	int ok = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	rhctx = de_malloc(c, sizeof(struct rixdecomp_ctx));
	rhctx->c = c;
	rhctx->private_params = (struct de_rixdecomp_params*)codec_private_params;
	rhctx->modname = "rixdecomp";
	rhctx->dcmpri = dcmpri;
	rhctx->dcmpro = dcmpro;
	rhctx->dres = dres;
	endpos = dcmpri->pos + rhctx->dcmpri->len;
	rhctx->use_xor_filter = (rhctx->private_params->imgtype==0);

	// Currently, we have some restrictions on the output dbuf.
	if(dcmpro->f->btype!=DBUF_TYPE_MEMBUF || dcmpro->f->len!=0 ||
		!dcmpro->len_known)
	{
		goto done;
	}

	if(rhctx->private_params->rowspan<1) goto done;

	rhctx->nodetable_pos = dcmpri->pos;
	if(!rixdecomp_read_nodetable(c, rhctx)) goto done;

	seg_count = 0;
	pos = rhctx->first_cmpr_segment_pos;
	while(1) {
		i64 saved_len;
		i64 seg_dcmpr_len;
		i64 seg_pos;
		i64 seg_dpos;
		i64 seg_dlen;
		i64 num_extra_bytes;

		if(pos+2 >= endpos) break;
		rhctx->nbytes_written = rhctx->dcmpro->f->len;
		if(rhctx->nbytes_written >= rhctx->dcmpro->expected_len) {
			break;
		}

		seg_pos = pos;
		seg_dlen = dbuf_getu16le_p(dcmpri->f, &pos);
		if(seg_dlen==0) break;
		seg_dpos = pos;

		de_dbg(c, "compressed segment at %"I64_FMT", dpos=%"I64_FMT", dlen=%"I64_FMT,
			seg_pos, seg_dpos, seg_dlen);
		de_dbg_indent(c, 1);

		saved_len = rhctx->dcmpro->f->len;
		dbuf_enable_wbuffer(rhctx->dcmpro->f);
		rixdecomp_process_codes_segment(c, rhctx, seg_dpos, seg_dlen);
		dbuf_disable_wbuffer(rhctx->dcmpro->f);

		seg_dcmpr_len = rhctx->dcmpro->f->len - saved_len;
		de_dbg(c, "decompressed size: %"I64_FMT, seg_dcmpr_len);

		if(seg_dcmpr_len < rhctx->private_params->rowspan) {
			de_dfilter_set_generic_error(c, dres, rhctx->modname);
			goto done;
		}

		de_dbg(c, "number of rows: %"I64_FMT, (i64)(seg_dcmpr_len/rhctx->private_params->rowspan));

		if(rhctx->private_params->known_segment_size) {
			rhctx->nbytes_written = (seg_count+1) * rhctx->private_params->known_segment_size;
			dbuf_truncate(rhctx->dcmpro->f, rhctx->nbytes_written);
		}
		else if(rhctx->dcmpro->f->len < rhctx->dcmpro->expected_len) {
			// For non-final segments, there is a potential problem.
			// For a compressed segment, we know neither the (bit-exact) size of the
			// compressed data, nor the size of the decompressed data. The padding
			// bits in the final byte can be misinterpreted as compressed data, so
			// we may have mistakenly decompressed them into garbage pixels that
			// will mess up the rest of the image.
			// It's possible that there is a formula that would tell us the size
			// of the decompressed data. But I don't know what it is.
			// Anyway, this is a quick and dirty attempt to work around the problem
			// by detecting and deleting such garbage pixels. It's not foolproof, and
			// better heuristics are possible.
			num_extra_bytes = rhctx->dcmpro->f->len % rhctx->private_params->rowspan;
			if(num_extra_bytes>0) {
				de_dbg(c, "[ignoring %"I64_FMT" bytes -- assuming garbage caused by padding bits]",
					num_extra_bytes);
				rhctx->nbytes_written = rhctx->dcmpro->f->len - num_extra_bytes;
				dbuf_truncate(rhctx->dcmpro->f, rhctx->nbytes_written);
			}
		}

		pos += seg_dlen;
		seg_count++;
		de_dbg_indent(c, -1);
	}

	ok = 1;

done:
	if(rhctx) {
		if(!ok || dres->errcode) {
			de_dfilter_set_generic_error(c, dres, rhctx->modname);
		}

		fmtutil_huffman_destroy_decoder(c, rhctx->ht);
		de_free(c, rhctx);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0xe8, %esi
callq 0x15b780
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
leaq 0x1414e2(%rip), %rcx    # 0x1a9f11
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
addq 0x10(%rcx), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movzbl 0x10(%rax), %eax
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x31(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpl $0x3, (%rax)
jne 0x68ab3
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x18(%rax)
jne 0x68ab3
movq -0x18(%rbp), %rax
cmpb $0x0, 0x8(%rax)
jne 0x68ab8
jmp 0x68d7b
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
cmpq $0x1, 0x8(%rax)
jge 0x68acc
jmp 0x68d7b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x68de0
cmpl $0x0, %eax
jne 0x68af3
jmp 0x68d7b
movq $0x0, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
cmpq -0x40(%rbp), %rax
jl 0x68b1a
jmp 0x68d74
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x10(%rcx), %rax
jl 0x68b4c
jmp 0x68d74
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x68b74
jmp 0x68d74
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
leaq 0x141388(%rip), %rsi    # 0x1a9f1b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x78(%rbp), %rcx
callq 0x68f80
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
callq 0x167be0
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0x141333(%rip), %rsi    # 0x1a9f49
movb $0x0, %al
callq 0x15b0d0
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpq 0x8(%rcx), %rax
jge 0x68c49
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x177560
jmp 0x68d7b
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
cqto
idivq 0x8(%rcx)
movq %rax, %rdx
leaq 0x1412f7(%rip), %rsi    # 0x1a9f60
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
cmpq $0x0, (%rax)
je 0x68cb7
movq -0x48(%rbp), %rcx
addq $0x1, %rcx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
imulq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0x168900
jmp 0x68d49
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x10(%rcx), %rax
jge 0x68d47
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
cqto
idivq 0x8(%rcx)
movq %rdx, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jle 0x68d45
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rdx
leaq 0x141269(%rip), %rsi    # 0x1a9f74
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rcx
subq -0x80(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0x168900
jmp 0x68d47
jmp 0x68d49
movq -0x78(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0x68b07
movl $0x1, -0x4c(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x68dc4
cmpl $0x0, -0x4c(%rbp)
je 0x68d91
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x68da6
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x177560
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rsi
callq 0x1886d0
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x15b5a0
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)