// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x6, %esi
callq 0x1651d0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x17534b(%rip), %rsi    # 0x19fac5
movb $0x0, %al
callq 0x15b0d0
movq $0x20, -0x38(%rbp)
movq -0x38(%rbp), %rax
imulq $0x1f60, -0x28(%rbp), %rcx # imm = 0x1F60
addq %rcx, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x2a7bd
movq -0x8(%rbp), %rdi
leaq 0x175323(%rip), %rsi    # 0x19fad4
movb $0x0, %al
callq 0x15bfb0
jmp 0x2a99c
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq $0x4, (%rax)
movq -0x18(%rbp), %rax
movq $0xa0, 0x10(%rax)
movq -0x18(%rbp), %rax
movq $0x64, 0x18(%rax)
leaq -0x80(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq $0x10, 0x8(%rax)
movq -0x8(%rbp), %rdi
movl $0x1f40, %esi           # imm = 0x1F40
movl $0x1, %edx
callq 0x168020
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x174df0
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x2a99a
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x40(%rbp), %rcx
leaq 0x17524c(%rip), %rsi    # 0x19faee
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %r9
movl $0x10, %ecx
movl $0x2, %r8d
xorl %eax, %eax
movq $0x10, (%rsp)
movl $0x3, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16c160
movq -0x40(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x1975f3(%rip), %rsi    # 0x1c1f07
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168900
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x1f40, %edx           # imm = 0x1F40
callq 0x166070
movq -0x40(%rbp), %rax
addq $0x1f40, %rax           # imm = 0x1F40
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x174910
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x20(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2a87f
jmp 0x2a99c
cmpq $0x0, -0x18(%rbp)
je 0x2a9ca
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1676a0
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15d260
addq $0xa0, %rsp
popq %rbp
retq