// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movw %cx, %ax
movw %dx, %cx
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movw %cx, -0x16(%rbp)
movw %ax, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
movw $0x0, -0x44(%rbp)
movb $-0x1, -0x47(%rbp)
movb $-0x1, -0x46(%rbp)
movb $-0x1, -0x45(%rbp)
movb $0x0, -0x4a(%rbp)
movb $0x0, -0x49(%rbp)
movb $0x0, -0x48(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x1d63
movq $0x0, -0x8(%rbp)
jmp 0x1fe4
movzwl -0x16(%rbp), %eax
shlq $0x0, %rax
movzwl -0x18(%rbp), %ecx
imulq %rcx, %rax
imulq $0x3, %rax, %rdi
callq 0x1130
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x1d9f
leaq 0x134c(%rip), %rdi      # 0x30d9
callq 0x1170
movq $0x0, -0x8(%rbp)
jmp 0x1fe4
movq -0x30(%rbp), %rdi
movzwl -0x16(%rbp), %eax
shlq $0x0, %rax
movzwl -0x18(%rbp), %ecx
imulq %rcx, %rax
imulq $0x3, %rax, %rdx
movl $0x3f, %esi
callq 0x10b0
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl %ecx, %ecx
addq %rcx, %rax
addq $0x6, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x80, %eax
je 0x1f5b
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x40(%rbp)
movb (%rax), %al
movb %al, -0x41(%rbp)
movzbl -0x41(%rbp), %eax
cmpl $0x0, %eax
jne 0x1e68
movzwl -0x44(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movzwl -0x16(%rbp), %ecx
cmpl %ecx, %eax
jae 0x1e5d
movq -0x30(%rbp), %rax
movw -0x47(%rbp), %cx
movw %cx, (%rax)
movb -0x45(%rbp), %cl
movb %cl, 0x2(%rax)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1e2a
movw $0x0, -0x44(%rbp)
jmp 0x1f44
movzbl -0x41(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1eca
movzbl -0x41(%rbp), %eax
andl $0x7f, %eax
movb %al, -0x41(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movzbl -0x41(%rbp), %ecx
cmpl %ecx, %eax
jae 0x1eba
movq -0x30(%rbp), %rax
movw -0x47(%rbp), %cx
movw %cx, (%rax)
movb -0x45(%rbp), %cl
movb %cl, 0x2(%rax)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1e87
movzbl -0x41(%rbp), %ecx
movzwl -0x44(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x44(%rbp)
jmp 0x1f42
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movzbl -0x41(%rbp), %ecx
cmpl %ecx, %eax
jae 0x1f34
cmpl $0x1, -0x24(%rbp)
jne 0x1f0c
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x40(%rbp)
movzbl (%rdx), %edx
movw (%rcx,%rdx), %si
movw %si, (%rax)
movb 0x2(%rcx,%rdx), %cl
movb %cl, 0x2(%rax)
jmp 0x1f1d
movq -0x30(%rbp), %rax
movw -0x4a(%rbp), %cx
movw %cx, (%rax)
movb -0x48(%rbp), %cl
movb %cl, 0x2(%rax)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1ed1
movzbl -0x41(%rbp), %ecx
movzwl -0x44(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x44(%rbp)
jmp 0x1f44
movzwl -0x44(%rbp), %eax
movzwl -0x16(%rbp), %ecx
cmpl %ecx, %eax
jle 0x1f56
movw $0x0, -0x44(%rbp)
jmp 0x1df4
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
movzwl -0x16(%rbp), %ecx
movzwl -0x18(%rbp), %edx
imull %edx, %ecx
imull $0x3, %ecx, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x1fdc
movzwl -0x16(%rbp), %eax
movzwl -0x18(%rbp), %ecx
imull %ecx, %eax
imull $0x3, %eax, %eax
cltq
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %rdx
subq %rdx, %rcx
subq %rcx, %rax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl $0x3, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jbe 0x1fda
movq -0x30(%rbp), %rax
movw -0x47(%rbp), %cx
movw %cx, (%rax)
movb -0x45(%rbp), %cl
movb %cl, 0x2(%rax)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1fac
jmp 0x1fdc
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop