// File: /workspace/github_c_makefile_star2_O1/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, %r14
movl $0x478, %esi            # imm = 0x478
callq 0xe969b
movq %rax, %rbp
leaq 0xcfe6e(%rip), %rsi     # 0x110590
movq %r14, %rdi
callq 0xea6cb
movq %rax, %r15
movq 0x18(%r14), %rdi
movl $0x7, %esi
callq 0xef0d5
movzbl %al, %edx
movl %edx, 0x418(%rbp)
leaq 0xda04b(%rip), %rsi     # 0x11a796
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
cmpl $0x7, 0x418(%rbp)
je 0x40785
movq 0x18(%r14), %rdi
movl $0xa, %esi
callq 0xef1ca
movl %eax, 0x41c(%rbp)
leaq 0xda02e(%rip), %rsi     # 0x11a7a7
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl 0x418(%rbp), %edx
movl 0x41c(%rbp), %ecx
leaq 0xda0a8(%rip), %rsi     # 0x11a840
xorl %edi, %edi
movzwl (%rdi,%rsi), %r9d
movb $0x1, %r8b
cmpl %r9d, %edx
jne 0x407c1
cmpw $0xc, 0xc(%rdi,%rsi)
jb 0x407ba
movzwl 0x2(%rdi,%rsi), %r9d
cmpl %r9d, %ecx
jne 0x407c1
leaq (%rsi,%rdi), %rax
xorl %r8d, %r8d
testb %r8b, %r8b
je 0x407d5
addq $0x18, %rdi
cmpq $0x180, %rdi            # imm = 0x180
jne 0x4079a
xorl %eax, %eax
movq %rax, 0x438(%rbp)
testq %rax, %rax
je 0x4083a
movzwl 0x4(%rax), %r8d
movq %r8, (%rbp)
movzwl 0x6(%rax), %r9d
movq %r9, 0x8(%rbp)
movzwl 0xa(%rax), %ecx
movl %ecx, %edx
imull %r8d, %edx
shrl $0x3, %edx
movq %rdx, 0x420(%rbp)
imull 0x8(%rax), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, 0x428(%rbp)
movzwl (%rax), %eax
cmpl $0x14, %eax
jle 0x40853
cmpl $0x15, %eax
je 0x40866
cmpl $0x3c, %eax
je 0x40831
cmpl $0x47, %eax
jne 0x40878
leaq 0xf5224(%rip), %rcx     # 0x135a5c
jmp 0x4087f
leaq 0xd9f76(%rip), %rsi     # 0x11a7b7
xorl %r12d, %r12d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x40925
cmpl $0x1, %eax
je 0x4086f
cmpl $0x7, %eax
jne 0x40878
leaq 0xd9fa1(%rip), %rcx     # 0x11a805
jmp 0x4087f
leaq 0xd9fa1(%rip), %rcx     # 0x11a80e
jmp 0x4087f
leaq 0xf51a4(%rip), %rcx     # 0x135a1a
jmp 0x4087f
leaq 0xeb5ea(%rip), %rcx     # 0x12be69
leaq 0x440(%rbp), %r13
movl %edx, (%rsp)
leaq 0xd9f82(%rip), %rdx     # 0x11a812
movl $0x32, %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0xe8d67
leaq 0xe3617(%rip), %rsi     # 0x123ebd
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x438(%rbp), %rax
testb $0x10, 0x14(%rax)
jne 0x4090e
movq 0x428(%rbp), %rcx
cmpq $0x4, %rcx
movq %rbp, 0x28(%rsp)
movq %r14, 0x20(%rsp)
je 0x40985
cmpq $0x100, %rcx            # imm = 0x100
je 0x4092f
cmpq $0x10, %rcx
jne 0x40a21
leaq 0x18(%rbp), %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xecbbc
jmp 0x40a21
leaq 0xd9ed5(%rip), %rsi     # 0x11a7ea
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9df7
xorl %r13d, %r13d
xorl %ebx, %ebx
jmp 0x40e42
xorl %eax, %eax
leaq 0xd9c50(%rip), %rcx     # 0x11a588
leaq 0xda081(%rip), %rdx     # 0x11a9c0
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
cmpq $0x100, %rax            # imm = 0x100
jne 0x4093f
jmp 0x40a21
xorl %ebp, %ebp
cmpw $0x10, 0xc(%rax)
movl $0x0, %eax
jb 0x409b7
movq 0x18(%r14), %rdi
movl $0xc, %esi
callq 0xef0d5
movl %eax, %ebp
movq 0x18(%r14), %rdi
movl $0xe, %esi
callq 0xef0d5
movzbl %bpl, %ebp
andb $0x1, %al
btl $0x4, %ebp
jae 0x409c6
movl $0x3, %esi
testb %al, %al
jne 0x409e6
movl %ebp, %ecx
shrb $0x4, %cl
testb $0x1, %cl
sete %dl
xorl %esi, %esi
testb %al, %dl
jne 0x409e6
notb %al
andb %al, %cl
movzbl %cl, %eax
andl $0x1, %eax
leal (%rax,%rax,2), %esi
incl %esi
movq 0x28(%rsp), %r14
leaq 0x18(%r14), %rcx
movl $0x4, %r8d
movl $0xa, %edi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xecbbc
andl $0xf, %ebp
movl $0x14, %edi
xorl %esi, %esi
movl %ebp, %edx
movq %r14, %rbp
callq 0xecac6
movl %eax, 0x18(%r14)
movq 0x20(%rsp), %r14
testq %r15, %r15
je 0x40a3c
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x40eda
testl %eax, %eax
je 0x40d52
movq 0x438(%rbp), %rax
movl 0x10(%rax), %ecx
movzwl 0x8(%rax), %esi
imulq %rcx, %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %rbx
movq 0x438(%rbp), %rax
cmpw $0x0, 0x8(%rax)
je 0x40bd5
movzwl 0xc(%rax), %ebp
xorl %r12d, %r12d
movl 0x10(%rax), %esi
imulq %r12, %rsi
movq %rbx, %rdi
callq 0xf152a
movq %r14, %rdi
leaq 0xd9d95(%rip), %rsi     # 0x11a825
movl %r12d, %edx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x18(%r14), %rax
movq %rbx, %rdi
movq 0x18(%rax), %r15
movq %rbp, 0x18(%rsp)
callq 0xf0e3f
cmpq %r15, 0x18(%rsp)
jge 0x40b6b
movq 0x18(%r14), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef14d
cmpb $-0x80, %al
je 0x40b15
movzbl %al, %r14d
testl %r14d, %r14d
je 0x40b57
testb %al, %al
js 0x40b2b
movq 0x20(%rsp), %r13
movq 0x18(%r13), %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xefabc
addq %r14, 0x18(%rsp)
movq %r13, %r14
jmp 0x40ac8
movq 0x18(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xea80b
movq %rax, 0x18(%rsp)
jmp 0x40ac8
andb $0x7f, %al
movzbl %al, %r13d
movq 0x20(%rsp), %r14
movq 0x18(%r14), %rdi
leaq 0x18(%rsp), %rsi
callq 0xef14d
movzbl %al, %esi
movq %rbx, %rdi
movq %r13, %rdx
callq 0xf14bd
jmp 0x40ac8
movq 0x18(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xea80b
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0xf0e67
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %r14
movq %rdx, 0x430(%r14)
subq %rbp, %rdx
movq 0x18(%rbx), %rcx
subq 0x30(%rsp), %rcx
movq 0x20(%rsp), %rdi
leaq 0xd9aef(%rip), %rsi     # 0x11a68b
xorl %eax, %eax
callq 0xe9226
movq 0x20(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x430(%r14), %rbp
movq 0x438(%r14), %rax
movq 0x20(%rsp), %r14
incq %r12
movzwl 0x8(%rax), %ecx
cmpq %rcx, %r12
jb 0x40a77
movq 0x28(%rsp), %rbp
movq 0x438(%rbp), %rax
testb $0x8, 0x14(%rax)
jne 0x40c5f
cmpw $0x7, (%rax)
jne 0x40cda
movq 0x18(%rbx), %rsi
movq %r14, %rdi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
movq 0x18(%rbx), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xefabc
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf152a
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 0xefabc
incq %r15
addq $0x2000, %r14           # imm = 0x2000
cmpq $0x15c, %r15            # imm = 0x15C
jne 0x40c23
jmp 0x40ccd
movq 0x18(%rbx), %rsi
movq %r14, %rdi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
movq 0x18(%rbx), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xefabc
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf152a
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 0xefabc
incq %r13
addq %r15, %r14
cmpq $0xc8, %r13
jne 0x40c97
movq %r12, %rdi
callq 0xf0b13
movq 0x20(%rsp), %r14
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
movq %r14, %rdi
movl $0x3, %ecx
callq 0xf2cab
movq %rax, %r12
movq 0x438(%rbp), %rax
cmpw $0x2, 0x8(%rax)
jb 0x40d2f
movq 0x420(%rbp), %rcx
movl 0x10(%rax), %r8d
movq %rbp, %r9
addq $0x18, %r9
movq %r12, (%rsp)
movl $0x2, 0x8(%rsp)
movl $0x4, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf36f9
jmp 0x40d7c
movzwl 0xa(%rax), %edx
cmpq $0x1, %rdx
jne 0x40d5a
movq 0x420(%rbp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xf3081
jmp 0x40d7c
xorl %r12d, %r12d
jmp 0x40925
movq 0x420(%rbp), %rcx
movq %rbp, %r8
addq $0x18, %r8
movl $0x0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %r9
callq 0xf35b8
movq %r14, %rdi
callq 0xea9a9
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
movsd 0xd97ab(%rip), %xmm0    # 0x11a558
orw %dx, %si
jne 0x40dbc
movsd 0xd97a6(%rip), %xmm1    # 0x11a560
jmp 0x40e18
movl %eax, %edx
xorl $0x280, %edx            # imm = 0x280
movl %ecx, %esi
xorl $0x15e, %esi            # imm = 0x15E
movsd 0xd979c(%rip), %xmm1    # 0x11a570
orw %dx, %si
jne 0x40de3
movsd 0xd9787(%rip), %xmm0    # 0x11a568
jmp 0x40e18
movl %eax, %edx
xorl $0x280, %edx            # imm = 0x280
movl %ecx, %esi
xorl $0xc8, %esi
orw %dx, %si
je 0x40e18
xorl $0x2d0, %eax            # imm = 0x2D0
xorl $0x15c, %ecx            # imm = 0x15C
orw %ax, %cx
jne 0x40e32
movsd 0xd9768(%rip), %xmm0    # 0x11a578
movsd 0xd9768(%rip), %xmm1    # 0x11a580
movl $0x1, 0x78(%r13)
movsd %xmm1, 0x80(%r13)
movsd %xmm0, 0x88(%r13)
movq %r12, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0xf2917
movq %rbx, %rdi
callq 0xf0b13
movq %r12, %rdi
callq 0xf28d4
movq %r14, %rdi
movq %r13, %rsi
callq 0xea9b3
movq %r14, %rdi
movq %rbp, %rsi
callq 0xe9752
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq