// File: /workspace/github_c_makefile_star2_O3/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 0xeeb67
movq %rax, %r14
leaq 0xf02cb(%rip), %rsi     # 0x113b95
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
imulq $0x1f60, %r14, %rax     # imm = 0x1F60
addq $0x20, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x23905
leaq 0xf02b1(%rip), %rsi     # 0x113ba4
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x23a86
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
movq $0x4, (%rax)
movq $0xa0, 0x10(%rax)
movq $0x64, 0x18(%rax)
leaq 0x10(%rsp), %rax
movq %rax, 0x30(%r15)
movq $0x10, 0x8(%r15)
movl $0x1f40, %esi           # imm = 0x1F40
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, 0x20(%r15)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf266a
movq %rax, 0x38(%r15)
movq %rbx, %rdi
callq 0xea504
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xf8e1c
testq %r14, %r14
jle 0x23a69
movl $0x20, %r13d
xorl %ebp, %ebp
movq %r12, 0x8(%rsp)
movq %rbx, %rdi
leaq 0xf0219(%rip), %rsi     # 0x113bbe
movl %ebp, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rsi
movq 0x30(%r15), %r9
subq $0x8, %rsp
movl $0x10, %ecx
movl $0x2, %r8d
movq %rbx, %rdi
movq %r13, %rdx
pushq $0x0
pushq $0x3
pushq $0x10
callq 0xf2cb1
addq $0x20, %rsp
movq %r14, %r12
leaq 0x20(%r13), %r14
movq %rbx, %rdi
leaq 0x112c0a(%rip), %rsi    # 0x136605
movq %r14, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r15), %rdi
xorl %esi, %esi
callq 0xf0f27
movq 0x18(%rbx), %rdi
movq 0x20(%r15), %rcx
movl $0x1f40, %edx           # imm = 0x1F40
movq %r14, %rsi
movq %r12, %r14
movq 0x8(%rsp), %r12
callq 0xef481
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf8b10
movq 0x38(%r15), %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0xf22d0
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
incq %rbp
addq $0x1f60, %r13           # imm = 0x1F60
cmpq %rbp, %r14
jne 0x2399b
movq 0x20(%r15), %rdi
callq 0xf052b
movq 0x38(%r15), %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea50e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq