// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/mbk.c
// Function: do_sprite_param_block
static void do_sprite_param_block(deark *c, lctx *d, i64 res,
	i64 sprite_index, i64 param_blk_pos, i64 pos)
{
	i64 sprite_data_offs_raw;
	i64 width_raw; // = width_in_pixels/16
	i64 mask_offs;
	i64 mask_size;
	i64 fg_offs;
	i64 fg_size;
	struct atari_img_decode_data *adata_fg = NULL;
	struct atari_img_decode_data *adata_mask = NULL;
	de_finfo *fi = NULL;
	u32 mask_pal[2] = { DE_STOCKCOLOR_WHITE, DE_STOCKCOLOR_BLACK };

	de_dbg(c, "%s-res sprite #%d param block at %d", sprite_res_name[res],
		(int)sprite_index, (int)pos);
	de_dbg_indent(c, 1);
	adata_fg = de_malloc(c, sizeof(struct atari_img_decode_data));
	adata_mask = de_malloc(c, sizeof(struct atari_img_decode_data));

	adata_fg->bpp = (i64)sprite_res_bpp[res];
	adata_fg->ncolors = ((i64)1)<<adata_fg->bpp;
	adata_mask->bpp = 1;
	adata_mask->ncolors = 2;

	sprite_data_offs_raw = de_getu32be(pos);

	//de_dbg(c, "sprite data offset: %d (->%d)", (int)sprite_data_offs_raw, (int)mask_offs);
	width_raw = (i64)de_getbyte(pos+4);
	adata_fg->w = width_raw*16;
	adata_fg->h = (i64)de_getbyte(pos+5);
	de_dbg_dimensions(c, adata_fg->w, adata_fg->h);
	if(!de_good_image_dimensions(c, adata_fg->w, adata_fg->h)) goto done;

	adata_mask->w = adata_fg->w;
	adata_mask->h = adata_fg->h;
	mask_offs = param_blk_pos + sprite_data_offs_raw;
	mask_size = (width_raw * 2 * 1) * adata_mask->h;
	de_dbg(c, "mask image at %d, len=%d", (int)mask_offs, (int)mask_size);
	if(mask_offs>=c->infile->len) goto done;

	fg_offs = mask_offs + mask_size;
	fg_size = (width_raw * 2 * adata_fg->bpp) * adata_fg->h;
	de_dbg(c, "foreground image at %d, len=%d", (int)fg_offs, (int)fg_size);

	adata_mask->unc_pixels = dbuf_open_input_subfile(c->infile, mask_offs, mask_size);
	adata_fg->unc_pixels = dbuf_open_input_subfile(c->infile, fg_offs, fg_size);

	adata_mask->pal = mask_pal;
	adata_fg->pal = d->pal;

	adata_mask->img = de_bitmap_create(c, adata_fg->w, adata_fg->h, 1);
	adata_fg->img = de_bitmap_create(c, adata_fg->w, adata_fg->h, 4);

	fmtutil_atari_decode_image(c, adata_mask);
	fmtutil_atari_decode_image(c, adata_fg);
	de_bitmap_apply_mask(adata_fg->img, adata_mask->img, 0);
	fi = de_finfo_create(c);
	fmtutil_atari_set_standard_density(c, adata_fg, fi);
	de_bitmap_write_to_file_finfo(adata_fg->img, fi, 0);

done:
	if(adata_fg) {
		dbuf_close(adata_fg->unc_pixels);
		de_bitmap_destroy(adata_fg->img);
		de_free(c, adata_fg);
	}
	if(adata_mask) {
		dbuf_close(adata_mask->unc_pixels);
		de_bitmap_destroy(adata_mask->img);
		de_free(c, adata_mask);
	}
	de_finfo_destroy(c, fi);
	de_dbg_indent(c, -1);
}

// 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 %r9, -0x30(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq 0xdf2f6(%rip), %rax     # 0x1bc1b8
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rcx
leaq 0x14896b(%rip), %rax    # 0x225840
movq (%rax,%rcx,8), %rdx
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %eax, %r8d
leaq 0xdf092(%rip), %rsi     # 0x1bbf7f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rcx
leaq 0xded63(%rip), %rax     # 0x1bbc94
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
movq -0x68(%rbp), %rax
movq %rcx, (%rax)
movq -0x68(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
movq $0x1, (%rax)
movq -0x70(%rbp), %rax
movq $0x2, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165470
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x4, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x5, %rsi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
movq -0x68(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0xdd00e
jmp 0xdd1dd
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
shlq %rax
shlq $0x0, %rax
movq -0x70(%rbp), %rcx
imulq 0x18(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
movq -0x50(%rbp), %rax
movl %eax, %ecx
leaq 0xdef3a(%rip), %rsi     # 0x1bbfa3
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xdd087
jmp 0xdd1dd
movq -0x48(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
shlq %rax
movq -0x68(%rbp), %rcx
imulq (%rcx), %rax
movq -0x68(%rbp), %rcx
imulq 0x18(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
movq -0x60(%rbp), %rax
movl %eax, %ecx
leaq 0xdeef6(%rip), %rsi     # 0x1bbfbc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x1691b0
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x1691b0
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x20(%rax)
leaq -0x80(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rcx
addq $0x2c, %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rdx
movl $0x1, %ecx
callq 0x16b750
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rdx
movl $0x4, %ecx
callq 0x16b750
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x174910
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x174910
movq -0x68(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rsi
xorl %edx, %edx
callq 0x16cf60
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0x174df0
movq -0x68(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x78(%rbp), %rsi
xorl %edx, %edx
callq 0x16afa0
cmpq $0x0, -0x68(%rbp)
je 0xdd20b
movq -0x68(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1676a0
movq -0x68(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x15b820
cmpq $0x0, -0x70(%rbp)
je 0xdd239
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1676a0
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)