// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: do_xpk_chunk
static void do_xpk_chunk(deark *c, struct xpkc_ctx *xc)
{
	struct xpkc_chunk *chk;
	u8 hck;
	UI ck;
	i64 pos1;
	i64 hdrlen;
	int saved_indent_level;
	struct de_dfilter_in_params d2i;
	struct de_dfilter_out_params d2o;
	struct de_dfilter_results d2r;

	de_dfilter_init_objects(c, &d2i, &d2o, &d2r);
	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "XPK chunk at %"I64_FMT, xc->pos);
	de_dbg_indent(c, 1);
	chk = &xc->curchunk;
	de_zeromem(chk, sizeof(struct xpkc_chunk));

	pos1 = xc->pos;
	chk->chk_type = dbuf_getbyte_p(xc->dcmpri->f, &xc->pos);
	de_dbg(c, "type: 0x%02x", (UI)chk->chk_type);
	chk->hchk = dbuf_getbyte_p(xc->dcmpri->f, &xc->pos);
	de_dbg(c, "chunk hdr checksum (reported): 0x%02x", (UI)chk->hchk);
	chk->cchk = (UI)dbuf_getu16be_p(xc->dcmpri->f, &xc->pos);
	de_dbg(c, "chunk data checksum (reported): 0x%04x", (UI)chk->cchk);

	if(xc->flags & XPKSTREAMF_LONGHEADERS) {
		hdrlen = 12;
		chk->clen = dbuf_getu32be_p(xc->dcmpri->f, &xc->pos);
		chk->ulen = dbuf_getu32be_p(xc->dcmpri->f, &xc->pos);
	}
	else {
		hdrlen = 8;
		chk->clen = dbuf_getu16be_p(xc->dcmpri->f, &xc->pos);
		chk->ulen = dbuf_getu16be_p(xc->dcmpri->f, &xc->pos);
	}
	de_dbg(c, "clen: %"I64_FMT, chk->clen);
	de_dbg(c, "ulen: %"I64_FMT, chk->ulen);

	hck = xpk_checksumh(xc->dcmpri->f, pos1, hdrlen, chk->hchk);
	de_dbg(c, "chunk hdr checksum (calculated): 0x%02x", hck);
	if(hck != chk->hchk) {
		xpk_on_checksum_error(c, xc);
		goto done;
	}

	if(chk->chk_type==XPKCHUNK_END) {
		goto done; // EOF marker
	}
	if(chk->chk_type!=XPKCHUNK_RAW && chk->chk_type!=XPKCHUNK_PACKED) {
		xc->errflag = 1;
		goto done;
	}

	chk->dpos = xc->pos;
	de_dbg(c, "chunk dpos: %"I64_FMT, chk->dpos);

	if(chk->dpos + chk->clen > xc->inf_endpos) {
		xc->errflag = 1;
		goto done;
	}

	ck = xpk_checksumc(xc->dcmpri->f, chk->dpos, chk->clen);
	de_dbg(c, "chunk data checksum (calculated): 0x%04x", ck);
	if(ck != chk->cchk) {
		xpk_on_checksum_error(c, xc);
		goto done;
	}

	if(chk->chk_type==XPKCHUNK_RAW || xc->method4cc.id==CODE_NONE) {
		de_dbg(c, "[uncompressed chunk]");
		dbuf_copy(xc->dcmpri->f, chk->dpos, chk->clen, xc->dcmpro->f);
	}
	else if(xc->codec) {
		i64 old_olen, new_olen;

		d2i.f = xc->dcmpri->f;
		d2i.pos = chk->dpos;
		d2i.len = chk->clen;
		d2o.f = xc->dcmpro->f;
		d2o.expected_len = chk->ulen;
		d2o.len_known = 1;

		old_olen = xc->dcmpro->f->len;

		de_dbg(c, "[decompressing chunk]");
		de_dbg_indent(c, 1);
		xc->codec(c, &d2i, &d2o, &d2r, NULL);
		de_dbg_indent(c, -1);
		dbuf_flush(xc->dcmpro->f);

		new_olen = xc->dcmpro->f->len;
		if(d2r.errcode) {
			de_dfilter_transfer_error2(c, &d2r, xc->dres, xc->modname);
			xc->errflag = 1;
			goto done;
		}
		if(new_olen != old_olen+chk->ulen) {
			xc->errflag = 1;
			goto done;
		}
	}
	else {
		xc->errflag = 1;
		goto done;
	}

	xc->pos += chk->clen;

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x70(%rbp), %rdx
leaq -0xd0(%rbp), %rcx
callq 0x1773f0
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x5c824(%rip), %rsi     # 0x1d679e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x19140
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl (%rax), %edx
leaq 0x37f45(%rip), %rsi     # 0x1b1f2b
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %edx
leaq 0x5c792(%rip), %rsi     # 0x1d67af
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x165210
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x5c782(%rip), %rsi     # 0x1d67d5
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x39(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x17a0b2
movq $0xc, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x17a0f8
movq $0x8, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x5c6f1(%rip), %rsi     # 0x1d67fc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x5c6e1(%rip), %rsi     # 0x1d6806
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %ecx
callq 0x179ea0
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x19(%rbp), %edx
leaq 0x5c6b2(%rip), %rsi     # 0x1d6810
movb $0x0, %al
callq 0x15b0d0
movzbl -0x19(%rbp), %eax
movq -0x18(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
cmpl %ecx, %eax
je 0x17a187
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x179ef0
jmp 0x17a415
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xf, %eax
jne 0x17a198
jmp 0x17a415
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
je 0x17a1bd
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x17a1bd
movq -0x10(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x17a415
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x5c659(%rip), %rsi     # 0x1d6838
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
addq 0x8(%rcx), %rax
movq -0x10(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jle 0x17a20d
movq -0x10(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x17a415
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x17a930
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %edx
leaq 0x5c60a(%rip), %rsi     # 0x1d6848
movb $0x0, %al
callq 0x15b0d0
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
je 0x17a263
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x179ef0
jmp 0x17a415
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
je 0x17a27c
movq -0x10(%rbp), %rax
cmpl $0x4e4f4e45, 0x4c(%rax) # imm = 0x4E4F4E45
jne 0x17a2be
movq -0x8(%rbp), %rdi
leaq 0x5c5ea(%rip), %rsi     # 0x1d6871
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rcx
callq 0x166070
jmp 0x17a403
movq -0x10(%rbp), %rax
cmpq $0x0, 0x78(%rax)
je 0x17a3f7
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
movb $0x1, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0xd8(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x5c552(%rip), %rsi     # 0x1d6886
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x70(%rbp), %rdx
leaq -0xd0(%rbp), %rcx
xorl %r8d, %r8d
callq *%rax
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
callq 0x167c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0xe0(%rbp)
cmpl $0x0, -0xd0(%rbp)
je 0x17a3d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rcx
leaq -0xd0(%rbp), %rsi
callq 0x178c80
movq -0x10(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x17a415
movq -0xe0(%rbp), %rax
movq -0xd8(%rbp), %rcx
movq -0x18(%rbp), %rdx
addq 0x10(%rdx), %rcx
cmpq %rcx, %rax
je 0x17a3f5
movq -0x10(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x17a415
jmp 0x17a401
movq -0x10(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x17a415
jmp 0x17a403
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)