// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0x478, %esi            # imm = 0x478
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x144b5b(%rip), %rsi    # 0x19c578
callq 0x15cb30
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x7, %esi
callq 0x165060
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x418(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x418(%rax), %edx
leaq 0x14ea94(%rip), %rsi    # 0x1a64ee
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpl $0x7, 0x418(%rax)
je 0x57aa8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xa, %esi
callq 0x165280
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x41c(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x41c(%rax), %edx
leaq 0x14ea5e(%rip), %rsi    # 0x1a64ff
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movl 0x418(%rax), %edi
movq -0x18(%rbp), %rax
movl 0x41c(%rax), %esi
callq 0x58a90
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x438(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x438(%rax)
jne 0x57b08
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x418(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x41c(%rax), %ecx
leaq 0x14ea13(%rip), %rsi    # 0x1a650f
movb $0x0, %al
callq 0x15bfb0
jmp 0x57f58
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0x4(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0x6(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x438(%rcx), %rcx
movzwl 0xa(%rcx), %ecx
imulq %rcx, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x420(%rax)
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0xa(%rax), %eax
movq -0x18(%rbp), %rcx
movq 0x438(%rcx), %rcx
movzwl 0x8(%rcx), %ecx
imull %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x428(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x58b10
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x440, %rdx            # imm = 0x440
leaq 0x157e04(%rip), %rsi    # 0x1af9cd
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movl 0x14(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x57c08
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x440, %rdx            # imm = 0x440
leaq 0x14e946(%rip), %rsi    # 0x1a6542
movb $0x0, %al
callq 0x15bfb0
jmp 0x57f58
movq -0x18(%rbp), %rax
cmpq $0x10, 0x428(%rax)
jne 0x57c3d
movq -0x18(%rbp), %rcx
addq $0x18, %rcx
movl $0x14, %edi
xorl %r9d, %r9d
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %r8d
movl %r9d, %esi
callq 0x161320
jmp 0x57d73
movq -0x18(%rbp), %rax
cmpq $0x100, 0x428(%rax) # imm = 0x100
jne 0x57c60
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x58c00
jmp 0x57d71
movq -0x18(%rbp), %rax
cmpq $0x4, 0x428(%rax)
jne 0x57d6f
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0xc(%rax), %eax
cmpl $0x10, %eax
jl 0x57cb2
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xc, %esi
callq 0x165060
movb %al, -0x39(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xe, %esi
callq 0x165060
movb %al, -0x3a(%rbp)
jmp 0x57cba
movb $0x0, -0x3a(%rbp)
movb $0x0, -0x39(%rbp)
movzbl -0x39(%rbp), %edx
andl $0x10, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, -0x3b(%rbp)
movzbl -0x3a(%rbp), %eax
andl $0x1, %eax
movb %al, -0x3c(%rbp)
movzbl -0x3b(%rbp), %eax
cmpl $0x0, %eax
je 0x57cf6
movzbl -0x3c(%rbp), %eax
cmpl $0x0, %eax
je 0x57cf6
movl $0x3, -0x40(%rbp)
jmp 0x57d31
cmpb $0x0, -0x3b(%rbp)
jne 0x57d0e
movzbl -0x3c(%rbp), %eax
cmpl $0x0, %eax
je 0x57d0e
movl $0x0, -0x40(%rbp)
jmp 0x57d2f
movzbl -0x3b(%rbp), %eax
cmpl $0x0, %eax
je 0x57d26
cmpb $0x0, -0x3c(%rbp)
jne 0x57d26
movl $0x4, -0x40(%rbp)
jmp 0x57d2d
movl $0x1, -0x40(%rbp)
jmp 0x57d2f
jmp 0x57d31
movl -0x40(%rbp), %esi
movq -0x18(%rbp), %rcx
addq $0x18, %rcx
movl $0xa, %edi
xorl %eax, %eax
movl %eax, %edx
movl $0x4, %r8d
xorl %r9d, %r9d
callq 0x161320
movzbl -0x39(%rbp), %edx
andl $0xf, %edx
movl $0x14, %edi
xorl %esi, %esi
callq 0x161150
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x57d71
jmp 0x57d73
cmpq $0x0, -0x30(%rbp)
je 0x57d97
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x58030
cmpl $0x0, %eax
jne 0x57d95
jmp 0x57f58
jmp 0x57d97
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0x8(%rax), %eax
imulq %rax, %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x58ca0
cmpl $0x0, %eax
jne 0x57de7
jmp 0x57f58
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movl 0x14(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x57e10
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x58dd0
jmp 0x57e36
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl (%rax), %eax
cmpl $0x7, %eax
jne 0x57e34
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x58eb0
jmp 0x57e36
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1, %eax
jle 0x57eb3
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x420(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, %r8d
movq -0x18(%rbp), %r9
addq $0x18, %r9
movq -0x28(%rbp), %rax
xorl %edx, %edx
movl %edx, %esi
movl $0x4, %edx
movq %rax, (%rsp)
movl $0x2, 0x8(%rsp)
callq 0x16c690
jmp 0x57f28
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0xa(%rax), %eax
cmpl $0x1, %eax
jne 0x57ee8
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x420(%rax), %rdx
movq -0x28(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
xorl %r8d, %r8d
callq 0x16bee0
jmp 0x57f26
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x438(%rax), %rax
movzwl 0xa(%rax), %eax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x420(%rax), %rcx
movq -0x18(%rbp), %r8
addq $0x18, %r8
movq -0x28(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
jmp 0x57f28
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x58f90
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x20(%rbp), %rdi
callq 0x1676a0
movq -0x28(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)