// File: /workspace/github_c_makefile_star2_O1/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 0xbb2a6(%rip), %rsi     # 0x12548d
callq 0xea6cb
testq %rax, %rax
je 0x6a1fb
movq %rax, %rdi
callq 0xea781
jmp 0x6a1fd
xorl %eax, %eax
leal -0x5(%rax), %ecx
cmpl $-0x4, %ecx
movl $0x4, %r14d
cmovael %eax, %r14d
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xef0d5
movl %eax, %ebp
movzbl %al, %edx
leaq 0xbb274(%rip), %rsi     # 0x12549b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe93cf
cmpl $0x1, %r14d
je 0x6a267
cmpl $0x3, %r14d
je 0x6a25d
cmpl $0x2, %r14d
jne 0x6a281
leaq 0xbb261(%rip), %rsi     # 0x1254ab
leaq 0xbb269(%rip), %rdx     # 0x1254ba
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb703
jmp 0x6a2d1
cmpb $0x3, %bpl
setne %bpl
jmp 0x6a288
leaq 0xbb23d(%rip), %rsi     # 0x1254ab
leaq 0xbb23e(%rip), %rdx     # 0x1254b3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb703
jmp 0x6a2b3
testb %bpl, %bpl
sete %bpl
leaq 0xbb224(%rip), %rax     # 0x1254b3
leaq 0xbb224(%rip), %rdx     # 0x1254ba
testb %bpl, %bpl
cmovneq %rax, %rdx
leaq 0xbb207(%rip), %rsi     # 0x1254ab
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb703
testb %bpl, %bpl
je 0x6a2d1
leaq 0x10(%rsp), %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xecbbc
jmp 0x6a301
movaps 0xba738(%rip), %xmm0    # 0x124a10
movaps %xmm0, 0x40(%rsp)
movaps 0xba71c(%rip), %xmm0    # 0x124a00
movaps %xmm0, 0x30(%rsp)
movaps 0xba700(%rip), %xmm0    # 0x1249f0
movaps %xmm0, 0x20(%rsp)
movaps 0xba6e4(%rip), %xmm0    # 0x1249e0
movaps %xmm0, 0x10(%rsp)
movl $0x20, %esi
movl $0x20, %edx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf2cab
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0x0, (%rsp)
leaq 0x10(%rsp), %r8
movl $0x3, %esi
movl $0x4, %edx
movl $0x10, %ecx
movq %rax, %r9
callq 0xf35b8
movq %r14, %rdi
callq 0xf3b1a
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
movq %r14, %rdi
callq 0xf28d4
addq $0x50, %rsp
popq %rbx
popq %r14
popq %rbp
retq