// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x6
popq %rsi
callq 0xe3f46
movq %rax, %r14
leaq 0xe39fa(%rip), %rsi     # 0x106b35
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
imulq $0x1f60, %r14, %rax     # imm = 0x1F60
addq $0x20, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x23176
leaq 0xe39e0(%rip), %rsi     # 0x106b44
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x232ff
pushq $0x40
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
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)
pushq $0x1
popq %rdx
movl $0x1f40, %esi           # imm = 0x1F40
movq %rbx, %rdi
callq 0xe59cd
movq %rax, 0x20(%r15)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
callq 0xe7437
movq %rax, 0x38(%r15)
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r12
movq %rbx, %rdi
movq %r15, (%rsp)
movq %r15, %rsi
movq %rax, %rdx
callq 0xed4e6
xorl %eax, %eax
testq %r14, %r14
cmovleq %rax, %r14
pushq $0x20
popq %r13
xorl %eax, %eax
movq (%rsp), %rbp
cmpq %rax, %r14
je 0x232de
movq %rbx, %rdi
leaq 0xe3944(%rip), %rsi     # 0x106b5e
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x18(%rbx), %rsi
movq 0x30(%rbp), %r9
subq $0x8, %rsp
movq %rbx, %rdi
movq %r13, %rdx
pushq $0x10
popq %rcx
pushq $0x2
popq %r8
pushq $0x0
pushq $0x3
pushq $0x10
callq 0xe79ca
addq $0x20, %rsp
movq %r12, %r15
movq %r14, %r12
leaq 0x20(%r13), %r14
movq %rbx, %rdi
leaq 0x106090(%rip), %rsi    # 0x129305
movq %r14, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x20(%rbp), %rdi
xorl %esi, %esi
callq 0xe5eb2
movq 0x18(%rbx), %rdi
movq 0x20(%rbp), %rcx
movl $0x1f40, %edx           # imm = 0x1F40
movq %r14, %rsi
movq %r12, %r14
movq %r15, %r12
callq 0xe45f4
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xed22a
movq 0x38(%rbp), %rdi
movq %r15, %rsi
pushq $0x2
popq %rdx
callq 0xe70ab
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x8(%rsp), %rax
incq %rax
addq $0x1f60, %r13           # imm = 0x1F60
jmp 0x23207
movq (%rsp), %r14
movq 0x20(%r14), %rdi
callq 0xe5591
movq 0x38(%r14), %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfd24
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq