// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc2.c
// Function: de_run_mdesk_icn
static void de_run_mdesk_icn(deark *c, de_module_params *mparams)
{
	const char *s;
	int pal_req = 0;
	u8 toybox_mode = 0;
	u8 b2;
	de_bitmap *img = NULL;
	de_color pal[16];
	static const de_color pal_mdesk[16] = {
		0xff000000U, 0xffaa0000U, 0xff00aa00U, 0xffaaaa00U,
		0xff0000aaU, 0xffaa00aaU, 0xff00aaaaU, 0xff7d7d7dU,
		0xffbababaU, 0xffff5555U, 0xff55ff55U, 0xffffff55U,
		0xff5555ffU, 0xffff55ffU, 0xff55ffffU, 0xffffffffU
	};

	s = de_get_ext_option(c, "mdesk_icn:pal");
	if(s) {
		pal_req = de_atoi(s);
	}

	if(pal_req<1 || pal_req>4) pal_req = 4; // default = prefer Magic Desk

	b2 = de_getbyte(2);
	de_dbg2(c, "byte[2]: 0x%02x", (UI)b2);

	if(pal_req==1) { //  ToyBox only
		toybox_mode = 1;
	}
	else if(pal_req==2) { // Magic Desk only
		toybox_mode = 0;
	}
	else if(pal_req==3) { // prefer ToyBox
		toybox_mode = (b2 != 0x03);
	}
	else {
		toybox_mode = (b2 == 0x00);
	}

	de_declare_fmtf(c, "%s icon", (toybox_mode?"ToyBox":"Magic Desk"));

	if(toybox_mode) {
		de_copy_std_palette(DE_PALID_PC16, 0, 0, pal, 16, 0);
	}
	else {
		de_memcpy(pal, pal_mdesk, sizeof(pal));
	}

	img = de_bitmap_create(c, 32, 32, 3);
	de_convert_image_paletted(c->infile, 3, 4, 16, pal, img, 0);
	de_bitmap_transpose(img);
	de_bitmap_write_to_file(img, NULL, 0);
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x50, %rsp
movq %rdi, %rbx
leaq 0xb1f8c(%rip), %rsi     # 0x1184bd
callq 0xdfa87
testq %rax, %rax
je 0x66545
movq %rax, %rdi
callq 0xdfb36
jmp 0x66547
xorl %eax, %eax
leal -0x5(%rax), %ecx
cmpl $-0x4, %ecx
pushq $0x4
popq %r14
cmovael %eax, %r14d
movq 0x18(%rbx), %rdi
pushq $0x2
popq %rsi
callq 0xe3eb4
movl %eax, %ebp
movzbl %al, %edx
leaq 0xb1f5e(%rip), %rsi     # 0x1184cb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
cmpl $0x1, %r14d
je 0x665ad
cmpl $0x3, %r14d
je 0x665a3
cmpl $0x2, %r14d
jne 0x665c7
leaq 0xb1f4b(%rip), %rsi     # 0x1184db
leaq 0xb1f53(%rip), %rdx     # 0x1184ea
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe0824
jmp 0x66613
cmpb $0x3, %bpl
setne %bpl
jmp 0x665ce
leaq 0xb1f27(%rip), %rsi     # 0x1184db
leaq 0xb1f28(%rip), %rdx     # 0x1184e3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe0824
jmp 0x665f9
testb %bpl, %bpl
sete %bpl
leaq 0xb1f0e(%rip), %rax     # 0x1184e3
leaq 0xb1f0e(%rip), %rdx     # 0x1184ea
testb %bpl, %bpl
cmovneq %rax, %rdx
leaq 0xb1ef1(%rip), %rsi     # 0x1184db
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe0824
testb %bpl, %bpl
je 0x66613
pushq $0x14
popq %rdi
leaq 0x10(%rsp), %rcx
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
jmp 0x66643
movaps 0xb1416(%rip), %xmm0    # 0x117a30
movaps %xmm0, 0x40(%rsp)
movaps 0xb13fa(%rip), %xmm0    # 0x117a20
movaps %xmm0, 0x30(%rsp)
movaps 0xb13de(%rip), %xmm0    # 0x117a10
movaps %xmm0, 0x20(%rsp)
movaps 0xb13c2(%rip), %xmm0    # 0x117a00
movaps %xmm0, 0x10(%rsp)
pushq $0x20
popq %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
movq %rdx, %rsi
callq 0xe7437
movq %rax, %r14
movq 0x18(%rbx), %rdi
andl $0x0, (%rsp)
pushq $0x3
popq %rsi
pushq $0x4
popq %rdx
pushq $0x10
popq %rcx
leaq 0x10(%rsp), %r8
movq %rax, %r9
callq 0xe7cbc
movq %r14, %rdi
callq 0xe8161
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq %r14, %rdi
callq 0xe7084
addq $0x50, %rsp
popq %rbx
popq %r14
popq %rbp
retq