// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/drhalo.c
// Function: de_run_drhalopic
static void de_run_drhalopic(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	const char *palfn;
	de_finfo *fi = NULL;

	d = de_malloc(c, sizeof(lctx));

	palfn = de_get_ext_option(c, "file2");

	d->board_id = (UI)de_getbyte(7);
	de_dbg(c, "board id: 0x%02x", d->board_id);
	if(d->board_id!=0x07) {
		d->mode_id = (UI)de_getu16le(10);
		de_dbg(c, "mode id: 0x%04x", d->mode_id);
	}

	d->modeinfo = get_pic_modeinfo(d->board_id, d->mode_id);
	if(!d->modeinfo) {
		de_err(c, "Unsupported image type (board=0x%02x, mode=0x%04x)",
			d->board_id, d->mode_id);
		goto done;
	}

	d->w = (i64)d->modeinfo->width;
	d->h = (i64)d->modeinfo->height;
	d->bytes_per_row_per_plane = d->w * (i64)d->modeinfo->bits_per_pixel_per_plane / 8;
	d->ncolors = 1LL<<((UI)d->modeinfo->bits_per_pixel_per_plane * (UI)d->modeinfo->nplanes);

	set_pic_mode_name(c, d);

	de_dbg(c, "mode: %s", d->modename);

	if((d->modeinfo->flags & 0x10)!=0) {
		de_err(c, "Unsupported image type: %s", d->modename);
		goto done;
	}

	if(d->ncolors==16) {
		de_copy_std_palette(DE_PALID_PC16, 0, 0, d->pal, 16, 0);
	}
	else if(d->ncolors==256) {
		make_stdpal_256col(c, d);
	}
	else if(d->ncolors==4) {
		u8 b12, b14;
		u8 palf1, palf2;
		int pal_subifd;

		if(d->modeinfo->hdrlen>=16) {
			b12 = de_getbyte(12);
			b14 = de_getbyte(14);
		}
		else {
			b12 = b14 = 0x00;
		}

		// CGA palette
		palf1 = (b12 & 0x10)?1:0;
		palf2 = (b14 & 0x01);
		if(palf1 && palf2) pal_subifd = 3;
		else if(!palf1 && palf2) pal_subifd = 0;
		else if(palf1 && !palf2) pal_subifd = 4;
		else pal_subifd = 1;
		de_copy_std_palette(DE_PALID_CGA, pal_subifd, 0, d->pal, 4, 0);

		// "background" color
		d->pal[0] = de_get_std_palette_entry(DE_PALID_PC16, 0, (int)(b12 & 0x0f));
	}

	if(palfn) {
		if(!do_read_pal_file(c, d, palfn)) goto done;
	}

	unc_pixels = dbuf_create_membuf(c,
		(i64)d->modeinfo->nominal_bytes_per_plane * (i64)d->modeinfo->nplanes, 0x1);
	if(!do_decompress_pic(c, d, unc_pixels)) goto done;

	if((d->modeinfo->flags & 0x08)!=0) {
		deinterlace_pic_16k(c, d, unc_pixels);
	}
	else if(d->modeinfo->board_id==0x07) {
		deinterlace_pic_hercules(c, d, unc_pixels);
	}

	img = de_bitmap_create(c, d->w, d->h, 3);

	if(d->modeinfo->nplanes>1) {
		de_convert_image_paletted_planar(unc_pixels, 0, 4, d->bytes_per_row_per_plane,
			d->modeinfo->nominal_bytes_per_plane, d->pal, img, 0x2);
	}
	else if(d->modeinfo->bits_per_pixel_per_plane==1) {
		de_convert_image_bilevel(unc_pixels, 0, d->bytes_per_row_per_plane, img, 0);
	}
	else {
		de_convert_image_paletted(unc_pixels, 0, (i64)d->modeinfo->bits_per_pixel_per_plane,
			d->bytes_per_row_per_plane, d->pal, img, 0);
	}

	fi = de_finfo_create(c);
	pic_set_density(c, d, fi);

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);

done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl $0x478, %esi            # imm = 0x478
callq 0xe8b2b
movq %rax, %r14
leaq 0xcfe75(%rip), %rsi     # 0x11057c
movq %rbx, %rdi
callq 0xe9b74
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl $0x7, %esi
callq 0xeead3
movzbl %al, %edx
movl %edx, 0x418(%r14)
leaq 0xda185(%rip), %rsi     # 0x11a8b6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl $0x7, %edx
cmpl $0x7, 0x418(%r14)
je 0x40779
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeebc8
movl %eax, 0x41c(%r14)
leaq 0xda161(%rip), %rsi     # 0x11a8c7
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl 0x418(%r14), %edx
movl 0x41c(%r14), %ecx
xorl %esi, %esi
leaq 0xda1d7(%rip), %rdi     # 0x11a960
movzwl (%rsi,%rdi), %eax
cmpl %eax, %edx
jne 0x407a4
cmpw $0xc, 0xc(%rsi,%rdi)
jb 0x407d5
movzwl 0x2(%rsi,%rdi), %r8d
cmpl %r8d, %ecx
je 0x407d5
addq $0x18, %rsi
cmpq $0x180, %rsi            # imm = 0x180
jne 0x40789
movq $0x0, 0x438(%r14)
leaq 0xda114(%rip), %rsi     # 0x11a8d7
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x40901
leaq (%rdi,%rsi), %rcx
movq %rcx, 0x438(%r14)
movzwl 0x4(%rsi,%rdi), %r8d
movq %r8, (%r14)
movzwl 0x6(%rsi,%rdi), %r9d
movq %r9, 0x8(%r14)
movzwl 0xa(%rsi,%rdi), %ecx
movl %ecx, %edx
imull %r8d, %edx
shrl $0x3, %edx
movq %rdx, 0x420(%r14)
imull 0x8(%rsi,%rdi), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, 0x428(%r14)
cmpl $0x14, %eax
jle 0x40839
cmpl $0x15, %eax
je 0x4084c
cmpl $0x3c, %eax
je 0x40830
cmpl $0x47, %eax
jne 0x4085e
leaq 0xf55ad(%rip), %rcx     # 0x135de4
jmp 0x40865
cmpl $0x1, %eax
je 0x40855
cmpl $0x7, %eax
jne 0x4085e
leaq 0xda0db(%rip), %rcx     # 0x11a925
jmp 0x40865
leaq 0xda0db(%rip), %rcx     # 0x11a92e
jmp 0x40865
leaq 0xf5546(%rip), %rcx     # 0x135da2
jmp 0x40865
leaq 0xeb8ec(%rip), %rcx     # 0x12c151
leaq 0x440(%r14), %r13
movl %edx, (%rsp)
leaq 0xda0bc(%rip), %rdx     # 0x11a932
movl $0x32, %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0xe8193
leaq 0xe3853(%rip), %rsi     # 0x1240df
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x438(%r14), %rax
testb $0x10, 0x14(%rax)
jne 0x408ea
movq 0x428(%r14), %rcx
cmpq $0x4, %rcx
je 0x40993
cmpq $0x100, %rcx            # imm = 0x100
je 0x4093c
cmpq $0x10, %rcx
jne 0x40a20
leaq 0x18(%r14), %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xec55a
jmp 0x40a20
leaq 0xda019(%rip), %rsi     # 0x11a90a
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9295
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r15, %rdi
callq 0xf052b
movq %r12, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r13, %rsi
callq 0xea50e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
leaq 0xda19b(%rip), %rcx     # 0x11aae0
leaq 0xda19c(%rip), %rdx     # 0x11aae8
movl %eax, %esi
andl $0x7, %esi
movl %eax, %edi
shrl $0x3, %edi
andl $0x7, %edi
movzbl (%rdi,%rcx), %edi
movl %eax, %r8d
shrl $0x6, %r8d
movzbl (%r8,%rdx), %r8d
movzbl (%rsi,%rcx), %esi
shll $0x10, %esi
shll $0x8, %edi
orl %esi, %edi
orl %r8d, %edi
orl $0xff000000, %edi       # imm = 0xFF000000
movl %edi, 0x18(%r14,%rax,4)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x4094c
jmp 0x40a20
cmpw $0x10, 0xc(%rax)
jb 0x409dd
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xeead3
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xeead3
movzbl %bpl, %ebp
movl %ebp, %ecx
shrb $0x4, %cl
movl %ecx, %edx
andb %al, %dl
movl $0x3, %esi
testb $0x1, %dl
jne 0x409f2
testb $0x1, %cl
sete %cl
xorl %esi, %esi
testb %al, %cl
je 0x409e3
jmp 0x409f2
xorl %ebp, %ebp
movb $0x1, %cl
xorl %eax, %eax
orb %al, %cl
notb %cl
movzbl %cl, %eax
andl $0x1, %eax
leal (%rax,%rax,2), %esi
incl %esi
leaq 0x18(%r14), %rcx
movl $0x4, %r8d
movl $0xa, %edi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xec55a
andl $0xf, %ebp
movl $0x14, %edi
xorl %esi, %esi
movl %ebp, %edx
callq 0xec464
movl %eax, 0x18(%r14)
testq %r15, %r15
je 0x40a3b
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x40eac
testl %eax, %eax
je 0x40d70
movq 0x438(%r14), %rax
movl 0x10(%rax), %ecx
movzwl 0x8(%rax), %esi
imulq %rcx, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %r15
movq 0x438(%r14), %rax
cmpw $0x0, 0x8(%rax)
je 0x40bda
movzwl 0xc(%rax), %ebp
xorl %r12d, %r12d
movq %r14, 0x20(%rsp)
movl 0x10(%rax), %esi
imulq %r12, %rsi
movq %r15, %rdi
callq 0xf0f27
movq %rbx, %rdi
leaq 0xd9eb1(%rip), %rsi     # 0x11a945
movl %r12d, %edx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r15), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rbx), %rax
movq %r15, %rdi
movq 0x18(%rax), %r15
movq %rbp, 0x10(%rsp)
movq %rdi, %r13
callq 0xf0855
cmpq %rbp, %r15
jle 0x40b76
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb4b
cmpb $-0x80, %al
je 0x40b17
movzbl %al, %r14d
testl %r14d, %r14d
je 0x40b62
testb %al, %al
js 0x40b30
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0xef481
addq 0x10(%rsp), %r14
movq %r14, 0x10(%rsp)
jmp 0x40b57
movq 0x10(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xe9cb4
movq %rax, %r14
movq %rax, 0x10(%rsp)
jmp 0x40b57
andb $0x7f, %al
movzbl %al, %r14d
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %esi
movq %r13, %rdi
movq %r14, %rdx
callq 0xf0eba
movq 0x10(%rsp), %r14
cmpq %r15, %r14
jl 0x40ad8
jmp 0x40b76
movq 0x10(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xe9cb4
movq %rax, 0x10(%rsp)
movq %r13, %r15
movq %r13, %rdi
callq 0xf087d
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %r14
movq %rdx, 0x430(%r14)
subq %rbp, %rdx
movq 0x18(%r13), %rcx
subq 0x18(%rsp), %rcx
movq %rbx, %rdi
leaq 0xd9c03(%rip), %rsi     # 0x11a7ab
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x430(%r14), %rbp
movq 0x438(%r14), %rax
incq %r12
movzwl 0x8(%rax), %ecx
cmpq %rcx, %r12
jb 0x40a7b
testb $0x8, 0x14(%rax)
jne 0x40c6d
cmpw $0x7, (%rax)
jne 0x40cf9
movq %r14, %r13
movq 0x18(%r15), %rsi
xorl %r14d, %r14d
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movq 0x18(%r15), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xef481
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
movq %r15, %rbx
xorl %r15d, %r15d
movq %r15, %rax
shrq $0x2, %rax
imulq $0x5a, %rax, %rax
movl %r14d, %esi
andl $0x6000, %esi           # imm = 0x6000
addq %rax, %rsi
movl $0x5a, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0xef481
incq %r15
addq $0x2000, %r14           # imm = 0x2000
cmpq $0x15c, %r15            # imm = 0x15C
jne 0x40c2b
movq %rbx, %r15
movq %r13, %r14
jmp 0x40cec
movq %r14, %rbp
movq 0x18(%r15), %rsi
xorl %r14d, %r14d
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movq 0x18(%r15), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xef481
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0f27
movq %r15, %rbx
movl $0x2000, %r15d          # imm = 0x2000
xorl %r13d, %r13d
movq %r13, %rax
shrq %rax
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
movq %r14, %rsi
andq %r15, %rsi
addq %rax, %rsi
movl $0x50, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0xef481
incq %r13
addq %r15, %r14
cmpq $0xc8, %r13
jne 0x40cb0
movq %rbx, %r15
movq %rbp, %r14
movq %r12, %rdi
callq 0xf052b
movq 0x18(%rsp), %rbx
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf266a
movq %rax, %r12
movq 0x438(%r14), %rax
cmpw $0x2, 0x8(%rax)
jb 0x40d4d
movq 0x420(%r14), %rcx
movl 0x10(%rax), %r8d
movq %r14, %r9
addq $0x18, %r9
movq %r12, (%rsp)
movl $0x2, 0x8(%rsp)
movl $0x4, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0xf30b3
jmp 0x40d9a
movzwl 0xa(%rax), %edx
cmpq $0x1, %rdx
jne 0x40d78
movq 0x420(%r14), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xf2a40
jmp 0x40d9a
xorl %r12d, %r12d
jmp 0x40901
movq 0x420(%r14), %rcx
movq %r14, %r8
addq $0x18, %r8
movl $0x0, (%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %r9
callq 0xf2f79
movq %rbx, %rdi
callq 0xea504
movq %rax, %r13
movq 0x438(%r14), %rcx
movl 0x4(%rcx), %eax
movzwl 0x6(%rcx), %ecx
movl %eax, %edx
xorl $0x140, %edx            # imm = 0x140
movl %ecx, %esi
xorl $0xc8, %esi
orw %dx, %si
jne 0x40dd1
movaps 0xd2511(%rip), %xmm0    # 0x1132e0
jmp 0x40e24
movl %eax, %edx
xorl $0x280, %edx            # imm = 0x280
movl %ecx, %esi
xorl $0x15e, %esi            # imm = 0x15E
orw %dx, %si
jne 0x40def
movaps 0xd9893(%rip), %xmm0    # 0x11a680
jmp 0x40e24
movl %eax, %edx
xorl $0x280, %edx            # imm = 0x280
movl %ecx, %esi
xorl $0xc8, %esi
orw %dx, %si
jne 0x40e0d
movaps 0xd9885(%rip), %xmm0    # 0x11a690
jmp 0x40e24
xorl $0x2d0, %eax            # imm = 0x2D0
xorl $0x15c, %ecx            # imm = 0x15C
orw %ax, %cx
jne 0x40e34
movaps 0xd987c(%rip), %xmm0    # 0x11a6a0
movl $0x1, 0x78(%r13)
movups %xmm0, 0x80(%r13)
movq %r12, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0xf22d0
jmp 0x40907