// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/divgs.c
// Function: de_run_div_map
static void de_run_div_map(deark *c, de_module_params *mparams)
{
	struct div_ctx *d = NULL;
	struct div_image_ctx *md = NULL;
	i64 pos;

	d = de_malloc(c, sizeof(struct div_ctx));
	d->fmt = identify_div_fmt(c);
	if(d->fmt==DIVFMT_MAP) {
		de_declare_fmt(c, "DIV MAP");
	}
	else {
		d->need_errmsg = 1;
		goto done;
	}
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP850);

	md = de_malloc(c, sizeof(struct div_image_ctx));
	if(de_getbyte(7) != 0) {
		d->need_errmsg = 1;
		goto done;
	}

	pos = 8;
	md->w = de_getu16le_p(&pos);
	md->h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, md->w, md->h);

	pos = 16;

	// TODO: Figure out why the description is often garbage
	//div_read_descr_p(c, d, md, &pos);
	pos += 32;

	div_read_pal_p(c, d, &pos);

	div_read_cpoints_p(c, d, &pos);

	md->bitmap_pos = pos;
	div_do_bitmap(c, d, md);

done:
	div_image_destroy(c, md);
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported MAP file");
		}
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl $0x408, %esi            # imm = 0x408
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
callq 0x6baa0
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x6b683
movq -0x8(%rbp), %rdi
leaq 0x13edf4(%rip), %rsi    # 0x1aa470
callq 0x15e380
jmp 0x6b690
movq -0x18(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x6b78f
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xe, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x7, %esi
callq 0x165060
movzbl %al, %eax
cmpl $0x0, %eax
je 0x6b6e3
movq -0x18(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x6b78f
movq $0x8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x15bba0
movq $0x10, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0x6bb80
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0x6bc10
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x6bc80
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x6bd80
cmpq $0x0, -0x18(%rbp)
je 0x6b7cc
movq -0x18(%rbp), %rax
cmpb $0x0, 0x6(%rax)
je 0x6b7bf
movq -0x8(%rbp), %rdi
leaq 0x13ecc0(%rip), %rsi    # 0x1aa478
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)