// File: /workspace/github_c_makefile_star2_O2/w4kfu[P]HommI/Icn/icn.c
// Function: icnmakeimg
unsigned char *icnmakeimg(struct icn *icn, int numentry, unsigned short width, unsigned short height,
			unsigned char *pal, unsigned int type)
{
	unsigned char *imgbuf = NULL;
	unsigned char *simgbuf = NULL;
	unsigned char *databuf = NULL;
	unsigned char actual = 0;
	unsigned short actual_width = 0;
	struct color color1;
	struct color color2;
	unsigned int x;

	color1.r = 0xFF; color1.g = 0xFF; color1.b = 0xFF;
	color2.r = 0; color2.g = 0; color2.b = 0;
	if (!icn)
		return NULL;
	if (!(imgbuf = malloc(sizeof (char) * width * height * 3)))
	{
		perror("malloc()");
		return NULL;
	}
	memset(imgbuf, 0x3F, sizeof (char) * width * height * 3);
	simgbuf = imgbuf;
	databuf = icn->entry[numentry].offset + icn->data + 6;
	while (*databuf != 0x80)
	{
		actual = *databuf++;
		if (actual == 0)
		{
			// new line
			for (x = actual_width; x < width; x++)
			{
				memcpy(imgbuf, &color1, sizeof (struct color));
				imgbuf += 3;
			}
			actual_width = 0;
		}
		else if (actual & 0x80)
		{
			actual &= 0x7F;
			for (x = 0; x < actual; x++)
			{
				memcpy(imgbuf, &color1, sizeof (struct color));
				imgbuf += 3;
			}
			actual_width += actual;
		}
		else
		{
			for (x = 0; x < actual; x++)
			{
				if (type == 1)
					memcpy(imgbuf, &pal[*databuf++], 3);
				else
					memcpy(imgbuf, &color2, sizeof (struct color));
				imgbuf += 3;
			}
			actual_width += actual;
		}
		if (actual_width > width)
			actual_width = 0;
	}
	if ((imgbuf - simgbuf) < (width * height * 3))
	{
		x = (width * height * 3) - (imgbuf - simgbuf);
		x /= 3;
		for (; x > 0; x--)
		{
			memcpy(imgbuf, &color1, sizeof (struct color));
			imgbuf += 3;
		}
	}
	return simgbuf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x1a9a
movl %r9d, %ebp
movq %r8, %r14
movl %edx, %r12d
movq %rdi, %r13
movl %esi, 0xc(%rsp)
movzwl %r12w, %ebx
movzwl %cx, %eax
imulq %rbx, %rax
leaq (%rax,%rax,2), %r15
movq %r15, %rdi
callq 0x1130
testq %rax, %rax
je 0x1a8e
pushq $0x3f
popq %rsi
movq %rax, %rdi
movq %r15, 0x10(%rsp)
movq %r15, %rdx
movq %rax, %r15
callq 0x10b0
movslq 0xc(%rsp), %rax
imulq $0xc, %rax, %rax
movq 0x8(%r13), %rcx
movq 0x10(%r13), %rdx
movl 0x8(%rcx,%rax), %eax
addq %rdx, %rax
addq $0x6, %rax
xorl %edx, %edx
xorl %esi, %esi
movq %r15, %rcx
movzbl (%rax), %edi
cmpl $0x80, %edi
je 0x1a60
incq %rax
testl %edi, %edi
je 0x1a1d
testb %dil, %dil
js 0x1a38
movl %edi, %r8d
subl $0x1, %r8d
jb 0x1a52
cmpl $0x1, %ebp
jne 0x1a0f
movzbl (%rax), %r9d
incq %rax
movb 0x2(%r14,%r9), %r10b
movb %r10b, 0x2(%rcx)
movzwl (%r14,%r9), %r9d
movw %r9w, (%rcx)
jmp 0x1a17
andw $0x0, (%rcx)
movb $0x0, 0x2(%rcx)
addq $0x3, %rcx
jmp 0x19e9
movzwl %si, %esi
cmpl %ebx, %esi
jae 0x1a34
orw $-0x1, (%rcx)
movb $-0x1, 0x2(%rcx)
addq $0x3, %rcx
incl %esi
jmp 0x1a20
xorl %esi, %esi
jmp 0x1a54
andl $0x7f, %edi
movl %edi, %r8d
subl $0x1, %r8d
jb 0x1a52
orw $-0x1, (%rcx)
movb $-0x1, 0x2(%rcx)
addq $0x3, %rcx
jmp 0x1a3e
addl %edi, %esi
cmpw %r12w, %si
cmoval %edx, %esi
jmp 0x19cb
movq %rcx, %rsi
subq %r15, %rsi
movq 0x10(%rsp), %rax
movl %eax, %edx
cmpq %rdx, %rsi
jge 0x1a9d
subl %esi, %eax
pushq $0x3
popq %rsi
xorl %edx, %edx
divl %esi
subl $0x1, %eax
jb 0x1a9d
orw $-0x1, (%rcx)
movb $-0x1, 0x2(%rcx)
addq $0x3, %rcx
jmp 0x1a7b
leaq 0x644(%rip), %rdi       # 0x20d9
callq 0x1170
xorl %r15d, %r15d
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop