// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %rbx
movl $0x478, %esi            # imm = 0x478
callq 0xdecaf
movq %rax, %rbp
leaq 0xc4b97(%rip), %rsi     # 0x10357c
movq %rbx, %rdi
callq 0xdfa87
movq %rax, %r15
movq 0x18(%rbx), %rdi
pushq $0x7
popq %rsi
callq 0xe3eb4
movzbl %al, %edx
movl %edx, 0x418(%rbp)
leaq 0xcedba(%rip), %rsi     # 0x10d7c6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpl $0x7, 0x418(%rbp)
jne 0x3ea24
pushq $0x7
popq %rdx
jmp 0x3ea4f
movq 0x18(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0xe3fa3
movl %eax, 0x41c(%rbp)
leaq 0xced9a(%rip), %rsi     # 0x10d7d7
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movl 0x418(%rbp), %edx
movl 0x41c(%rbp), %ecx
xorl %esi, %esi
leaq 0xcee12(%rip), %rdi     # 0x10d870
cmpq $0x180, %rsi            # imm = 0x180
je 0x3ea88
movzwl (%rsi,%rdi), %eax
cmpl %eax, %edx
jne 0x3ea82
cmpw $0xc, 0xc(%rsi,%rdi)
jb 0x3eaa9
movzwl 0x2(%rsi,%rdi), %r8d
cmpl %r8d, %ecx
je 0x3eaa9
addq $0x18, %rsi
jmp 0x3ea5e
andq $0x0, 0x438(%rbp)
leaq 0xced50(%rip), %rsi     # 0x10d7e7
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x3ebce
leaq (%rdi,%rsi), %rcx
movq %rcx, 0x438(%rbp)
movzwl 0x4(%rsi,%rdi), %r8d
movq %r8, (%rbp)
movzwl 0x6(%rsi,%rdi), %r9d
movq %r9, 0x8(%rbp)
movzwl 0xa(%rsi,%rdi), %ecx
movl %ecx, %edx
imull %r8d, %edx
shrl $0x3, %edx
movq %rdx, 0x420(%rbp)
imull 0x8(%rsi,%rdi), %ecx
pushq $0x1
popq %rdx
shlq %cl, %rdx
movq %rdx, 0x428(%rbp)
cmpl $0x1, %eax
je 0x3eb2c
cmpl $0x47, %eax
je 0x3eb11
cmpl $0x15, %eax
je 0x3eb1a
cmpl $0x3c, %eax
je 0x3eb11
cmpl $0x7, %eax
jne 0x3eb23
leaq 0xced26(%rip), %rcx     # 0x10d835
jmp 0x3eb33
leaq 0xe9fec(%rip), %rcx     # 0x128b04
jmp 0x3eb33
leaq 0xced1d(%rip), %rcx     # 0x10d83e
jmp 0x3eb33
leaq 0xe03b7(%rip), %rcx     # 0x11eee1
jmp 0x3eb33
leaq 0xe9f8f(%rip), %rcx     # 0x128ac2
leaq 0x440(%rbp), %r13
movl %edx, (%rsp)
leaq 0xcecfe(%rip), %rdx     # 0x10d842
pushq $0x32
popq %rsi
movq %r13, %rdi
xorl %eax, %eax
callq 0xde40c
leaq 0xd8385(%rip), %rsi     # 0x116edd
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x438(%rbp), %rax
testb $0x10, 0x14(%rax)
jne 0x3ebb7
movq 0x428(%rbp), %rcx
cmpq $0x4, %rcx
movq %rbp, 0x20(%rsp)
je 0x3ec60
cmpq $0x100, %rcx            # imm = 0x100
je 0x3ec09
cmpq $0x10, %rcx
jne 0x3eced
leaq 0x18(%rbp), %rcx
pushq $0x14
popq %rdi
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
jmp 0x3eced
leaq 0xcec5c(%rip), %rsi     # 0x10d81a
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xdf2a5
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r15, %rdi
callq 0xe5591
movq %r12, %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdfd24
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xded32
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
leaq 0xcedde(%rip), %rcx     # 0x10d9f0
leaq 0xceddf(%rip), %rdx     # 0x10d9f8
cmpq $0x100, %rax            # imm = 0x100
je 0x3eced
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(%rbp,%rax,4)
incq %rax
jmp 0x3ec19
xorl %ebp, %ebp
cmpw $0x10, 0xc(%rax)
movl $0x0, %eax
jb 0x3ec8e
movq 0x18(%rbx), %rdi
pushq $0xc
popq %rsi
callq 0xe3eb4
movl %eax, %ebp
movq 0x18(%rbx), %rdi
pushq $0xe
popq %rsi
callq 0xe3eb4
movzbl %bpl, %ebp
andb $0x1, %al
btl $0x4, %ebp
jae 0x3ec9d
testb %al, %al
je 0x3ec9d
pushq $0x3
popq %rsi
jmp 0x3ecbd
movl %ebp, %ecx
shrb $0x4, %cl
testb $0x1, %cl
sete %dl
xorl %esi, %esi
testb %al, %dl
jne 0x3ecbd
notb %al
andb %al, %cl
movzbl %cl, %eax
andl $0x1, %eax
leal (%rax,%rax,2), %esi
incl %esi
movq 0x20(%rsp), %r14
leaq 0x18(%r14), %rcx
pushq $0xa
popq %rdi
pushq $0x4
popq %r8
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
andl $0xf, %ebp
pushq $0x14
popq %rdi
xorl %esi, %esi
movl %ebp, %edx
movq %r14, %rbp
callq 0xe1a23
movl %eax, 0x18(%r14)
testq %r15, %r15
je 0x3ed08
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x3f12e
testl %eax, %eax
je 0x3f006
movq 0x438(%rbp), %rax
movl 0x10(%rax), %ecx
movzwl 0x8(%rax), %esi
imulq %rcx, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0xe59cd
movq %rax, %r15
movq 0x438(%rbp), %rax
movzwl 0xc(%rax), %r14d
leaq 0x18(%rsp), %r13
xorl %r12d, %r12d
movzwl 0x8(%rax), %ecx
cmpq %rcx, %r12
jae 0x3ee99
movl 0x10(%rax), %esi
imulq %r12, %rsi
movq %r15, %rdi
callq 0xe5eb2
movq %rbx, %rdi
leaq 0xceaf3(%rip), %rsi     # 0x10d855
movq %r12, 0x30(%rsp)
movl %r12d, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x18(%r15), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rbp
movq %r14, 0x18(%rsp)
movq %r15, %rdi
callq 0xe589c
movq %r14, %r12
cmpq %rbp, %r12
jge 0x3ee3d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
cmpb $-0x80, %al
je 0x3ede6
movzbl %al, %r12d
testl %r12d, %r12d
je 0x3ee29
testb %al, %al
js 0x3edff
movq 0x18(%rbx), %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xe45f4
addq 0x18(%rsp), %r12
movq %r12, 0x18(%rsp)
jmp 0x3eda0
movq 0x18(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xdfbb6
movq %rax, %r12
movq %rax, 0x18(%rsp)
jmp 0x3eda0
andb $0x7f, %al
movzbl %al, %r12d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
movzbl %al, %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0xe5e40
movq 0x18(%rsp), %r12
jmp 0x3eda0
movq 0x18(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xdfbb6
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0xe58c4
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rbp
movq %rdx, 0x430(%rbp)
subq %r14, %rdx
movq 0x18(%r15), %rcx
subq 0x28(%rsp), %rcx
movq %rbx, %rdi
leaq 0xce84f(%rip), %rsi     # 0x10d6bb
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x430(%rbp), %r14
movq 0x438(%rbp), %rax
movq 0x30(%rsp), %r12
incq %r12
jmp 0x3ed3c
testb $0x8, 0x14(%rax)
jne 0x3ef1c
cmpw $0x7, (%rax)
jne 0x3ef92
movq 0x18(%r15), %rsi
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
movq 0x18(%r15), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xe45f4
movq %r15, %rdi
xorl %esi, %esi
callq 0xe5eb2
pushq $0x5a
popq %r14
xorl %ebp, %ebp
cmpq $0x15c, %rbp            # imm = 0x15C
je 0x3ef85
movq %rbp, %rax
shrq $0x2, %rax
imulq $0x5a, %rax, %rax
movl %r13d, %esi
andl $0x6000, %esi           # imm = 0x6000
addq %rax, %rsi
movq %r12, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0xe45f4
incq %rbp
addq $0x2000, %r13           # imm = 0x2000
jmp 0x3eede
movq 0x18(%r15), %rsi
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
movq 0x18(%r15), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xe45f4
movq %r15, %rdi
xorl %esi, %esi
callq 0xe5eb2
movl $0x2000, %ebp           # imm = 0x2000
xorl %r14d, %r14d
cmpq $0xc8, %r14
je 0x3ef85
movq %r14, %rax
shrq %rax
imulq $0x50, %rax, %rax
movq %r13, %rsi
andq %rbp, %rsi
addq %rax, %rsi
movq %r12, %rdi
pushq $0x50
popq %rdx
movq %r15, %rcx
callq 0xe45f4
incq %r14
addq %rbp, %r13
jmp 0x3ef53
movq %r12, %rdi
callq 0xe5591
movq 0x20(%rsp), %rbp
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
callq 0xe7437
movq %rax, %r12
movq 0x438(%rbp), %rax
cmpw $0x2, 0x8(%rax)
jb 0x3efe3
movq 0x420(%rbp), %rcx
movl 0x10(%rax), %r8d
movq %rbp, %r9
addq $0x18, %r9
movq %r12, (%rsp)
movl $0x2, 0x8(%rsp)
pushq $0x4
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0xe7de1
jmp 0x3f02d
movzwl 0xa(%rax), %edx
cmpq $0x1, %rdx
jne 0x3f00e
movq 0x420(%rbp), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xe77b7
jmp 0x3f02d
xorl %r12d, %r12d
jmp 0x3ebce
movq 0x420(%rbp), %rcx
movq %rbp, %r8
addq $0x18, %r8
andl $0x0, (%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %r9
callq 0xe7cbc
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r13
movq 0x438(%rbp), %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 0x3f064
movaps 0xc721e(%rip), %xmm0    # 0x106280
jmp 0x3f0b7
movl %eax, %edx
xorl $0x280, %edx            # imm = 0x280
movl %ecx, %esi
xorl $0x15e, %esi            # imm = 0x15E
orw %dx, %si
jne 0x3f082
movaps 0xce510(%rip), %xmm0    # 0x10d590
jmp 0x3f0b7
movl %eax, %edx
xorl $0x280, %edx            # imm = 0x280
movl %ecx, %esi
xorl $0xc8, %esi
orw %dx, %si
jne 0x3f0a0
movaps 0xce502(%rip), %xmm0    # 0x10d5a0
jmp 0x3f0b7
xorl $0x2d0, %eax            # imm = 0x2D0
xorl $0x15c, %ecx            # imm = 0x15C
orw %ax, %cx
jne 0x3f0c7
movaps 0xce4f9(%rip), %xmm0    # 0x10d5b0
movl $0x1, 0x78(%r13)
movups %xmm0, 0x80(%r13)
pushq $0x2
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0xe70ab
jmp 0x3ebd4