// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/atari-img.c
// Function: de_run_videomaster
static void de_run_videomaster(deark *c, de_module_params *mparams)
{
	struct atari_img_decode_data *adata = NULL;
	de_finfo *fi = NULL;
	i64 nframes;
	i64 frameidx;
	i64 frames_startpos;
	i64 pos;
	de_color pal[16];

	nframes = de_getu16be(6);
	de_dbg(c, "num frames: %u", (UI)nframes);
	frames_startpos = 32;
	if(frames_startpos + nframes*VMAS_FRAME_TOTAL_SIZE > c->infile->len) {
		de_err(c, "Invalid or truncated file");
		goto done;
	}

	adata = de_malloc(c, sizeof(struct atari_img_decode_data));
	adata->bpp = 4;
	adata->w = 160;
	adata->h = 100;
	adata->pal = pal;
	adata->ncolors = 16;
	adata->unc_pixels = dbuf_create_membuf(c, VMAS_FRAME_BITMAP_SIZE, 0x1);
	adata->img = de_bitmap_create(c, adata->w, adata->h, 3);
	fi = de_finfo_create(c);
	// This might not be the right aspect ratio in all cases, but I think it's
	// probably better than nothing.
	fmtutil_atari_set_standard_density(c, adata, fi);

	pos = frames_startpos;
	for(frameidx=0; frameidx<nframes; frameidx++) {
		de_dbg(c, "frame #%d at %"I64_FMT, (int)frameidx, pos);
		de_dbg_indent(c, 1);
		de_read_simple_palette(c, c->infile, pos, 16, 2, adata->pal, 16, DE_RDPALTYPE_AMIGA12BIT, 0);
		pos += VMAS_FRAME_PAL_SIZE;

		de_dbg(c, "bitmap at %"I64_FMT, pos);
		dbuf_truncate(adata->unc_pixels, 0);
		dbuf_copy(c->infile, pos, VMAS_FRAME_BITMAP_SIZE, adata->unc_pixels);
		pos += VMAS_FRAME_BITMAP_SIZE;

		fmtutil_atari_decode_image(c, adata);
		de_bitmap_write_to_file_finfo(adata->img, fi, DE_CREATEFLAG_OPT_IMAGE);
		de_dbg_indent(c, -1);
	}

done:
	if(adata) {
		dbuf_close(adata->unc_pixels);
		de_bitmap_destroy(adata->img);
		de_free(c, adata);
	}
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
movl $0x6, %esi
callq 0xef169
movq %rax, %r15
leaq 0xf0207(%rip), %rsi     # 0x113ae5
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
imulq $0x1f60, %r15, %rax     # imm = 0x1F60
addq $0x20, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x2391c
leaq 0xf01ed(%rip), %rsi     # 0x113af4
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
xorl %r14d, %r14d
jmp 0x23a80
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
movq $0x4, (%rax)
movq $0xa0, 0x10(%rax)
movq $0x64, 0x18(%rax)
leaq 0x10(%rsp), %rax
movq %rax, 0x30(%r14)
movq $0x10, 0x8(%r14)
movl $0x1f40, %esi           # imm = 0x1F40
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf2cab
movq %rax, 0x38(%r14)
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xf9323
testq %r15, %r15
jle 0x23a80
movl $0x20, %r13d
xorl %ebp, %ebp
movq %r12, 0x8(%rsp)
movq %rbx, %rdi
leaq 0xf0152(%rip), %rsi     # 0x113b0e
movl %ebp, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rsi
movq 0x30(%r14), %r9
subq $0x8, %rsp
movl $0x10, %ecx
movl $0x2, %r8d
movq %rbx, %rdi
movq %r13, %rdx
pushq $0x0
pushq $0x3
pushq $0x10
callq 0xf32f2
addq $0x20, %rsp
movq %r15, %r12
leaq 0x20(%r13), %r15
movq %rbx, %rdi
leaq 0x1127b0(%rip), %rsi    # 0x1361c2
movq %r15, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r14), %rdi
xorl %esi, %esi
callq 0xf152a
movq 0x18(%rbx), %rdi
movq 0x20(%r14), %rcx
movl $0x1f40, %edx           # imm = 0x1F40
movq %r15, %rsi
movq %r12, %r15
movq 0x8(%rsp), %r12
callq 0xefabc
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf900c
movq 0x38(%r14), %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0xf2917
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
incq %rbp
addq $0x1f60, %r13           # imm = 0x1F60
cmpq %rbp, %r15
jne 0x239b2
testq %r14, %r14
je 0x23aa2
movq 0x20(%r14), %rdi
callq 0xf0b13
movq 0x38(%r14), %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea9b3
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq