// File: /workspace/github_cpp_makefile_star2_O2/sgielen[P]clife/multicolor.cpp
// Function: MulticolorValue::MulticolorValue(std::vector<MulticolorValue, std::allocator<MulticolorValue>>)
MulticolorValue(std::vector<MulticolorValue> vec) : value(true), age(0) {
		std::vector<int> hues;
		for(auto const &cell : vec) {
			if(cell.value) {
				float r = float(cell.red) / UINT8_MAX;
				float g = float(cell.green) / UINT8_MAX;
				float b = float(cell.blue) / UINT8_MAX;
				float min = std::min(r, std::min(g, b));
				float max = std::max(r, std::max(g, b));
				// hue from 0 to 360
				int hue = max == 0 ? 0 :
				            r == max ? (60 * (0 + (g-b)/(max-min))) :
				            g == max ? (60 * (2 + (b-r)/(max-min))) :
				                       (60 * (4 + (r-g)/(max-min)));
				if(hue < 0) {
					hue += 360;
				}
				hues.push_back(hue);
			}
		}
		assert(hues.size() == 3);
		std::sort(hues.begin(), hues.end());
		int range1 = hues[2] - hues[0];
		int range2 = (hues[0] + 360) - hues[1];
		int range3 = (hues[1] + 360) - hues[2];
		int avghue;
		if(std::min(range1, std::min(range2, range3)) == range1) {
			avghue = (hues[0] + hues[1] + hues[2]) / 3;
		} else if(std::min(range1, std::min(range2, range3)) == range2) {
			avghue = (hues[0] + hues[1] + hues[2] + 360) / 3;
		} else {
			avghue = (hues[0] + hues[1] + hues[2] + 720) / 3;
		}
		avghue %= 360;
		float sat = 1;
		float value = 1;
		int h_i = std::floor(avghue / 60);
		float f = (avghue / 60.) - h_i;
		float p = value * (1 - sat);
		float q = value * (1 - f * sat);
		float t = value * (1 - (1 - f) * sat);

		value *= 255;
		p *= 255;
		q *= 255;
		t *= 255;

		switch(h_i) {
		case 0:
			red = value;
			green = t;
			blue = p;
			break;
		case 1:
			red = q;
			green = value;
			blue = p;
			break;
		case 2:
			red = p;
			green = value;
			blue = t;
			break;
		case 3:
			red = p;
			green = q;
			blue = value;
			break;
		case 4:
			red = t;
			green = p;
			blue = value;
			break;
		case 5:
			red = value;
			green = p;
			blue = q;
			break;
		default:
			abort();
		};
	}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
movb $0x1, (%rdi)
movb $0x0, 0x4(%rdi)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movq (%rsi), %r12
movq 0x8(%rsi), %r13
leaq 0xc(%rsp), %r15
cmpq %r13, %r12
je 0x4f04
cmpb $0x1, (%r12)
jne 0x4efb
movzbl 0x1(%r12), %eax
cvtsi2ss %eax, %xmm0
divss 0x1254(%rip), %xmm0     # 0x6080
movzwl 0x2(%r12), %eax
movd %eax, %xmm1
pxor %xmm2, %xmm2
punpcklbw %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3],xmm1[4],xmm2[4],xmm1[5],xmm2[5],xmm1[6],xmm2[6],xmm1[7],xmm2[7]
punpcklwd %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
cvtdq2ps %xmm1, %xmm1
divps 0x1204(%rip), %xmm1     # 0x6050
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
movaps %xmm2, %xmm3
maxss %xmm1, %xmm3
maxss %xmm0, %xmm3
xorps %xmm4, %xmm4
ucomiss %xmm4, %xmm3
jne 0x4e68
jnp 0x4edd
movaps %xmm2, %xmm5
minss %xmm1, %xmm5
minss %xmm0, %xmm5
ucomiss %xmm3, %xmm0
movaps %xmm3, %xmm4
subss %xmm5, %xmm4
jne 0x4e9e
jp 0x4e9e
subss %xmm2, %xmm1
divss %xmm4, %xmm1
addss 0x1207(%rip), %xmm1     # 0x6098
mulss 0x11ef(%rip), %xmm1     # 0x6088
movaps %xmm1, %xmm4
jmp 0x4edd
ucomiss %xmm3, %xmm1
jne 0x4ec2
jp 0x4ec2
subss %xmm0, %xmm2
divss %xmm4, %xmm2
addss 0x11d7(%rip), %xmm2     # 0x608c
mulss 0x11cb(%rip), %xmm2     # 0x6088
movaps %xmm2, %xmm4
jmp 0x4edd
subss %xmm1, %xmm0
divss %xmm4, %xmm0
addss 0x11b2(%rip), %xmm0     # 0x6084
mulss 0x11ae(%rip), %xmm0     # 0x6088
movaps %xmm0, %xmm4
cvttss2si %xmm4, %eax
leal 0x168(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movl %ecx, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x5344
addq $0x5, %r12
jmp 0x4e06
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rsi, %rax
subq %rdi, %rax
cmpq $0xc, %rax
jne 0x5051
callq 0x54b9
movq 0x10(%rsp), %rcx
movl 0x8(%rcx), %edx
movl (%rcx), %eax
movl 0x4(%rcx), %ecx
movl %edx, %r8d
subl %eax, %r8d
movl %eax, %esi
subl %ecx, %esi
movl $0x168, %r9d            # imm = 0x168
addl %r9d, %esi
movl %ecx, %edi
subl %edx, %edi
addl %r9d, %edi
cmpl %esi, %edi
movl %esi, %r9d
cmovll %edi, %r9d
addl %edx, %eax
cmpl %r9d, %r8d
jle 0x4f6a
cmpl %edi, %esi
jle 0x4f65
addl $0x2d0, %eax            # imm = 0x2D0
jmp 0x4f6a
addl $0x168, %eax            # imm = 0x168
addl %ecx, %eax
pushq $0x3
popq %rcx
cltd
idivl %ecx
movl $0x168, %ecx            # imm = 0x168
cltd
idivl %ecx
movl %edx, %ecx
movl %ecx, %eax
cwtd
movw $0x3c, %si
idivw %si
cmpw $0x5, %ax
ja 0x506e
cvtsi2sd %ecx, %xmm0
divsd 0x1103(%rip), %xmm0     # 0x60a0
cwtl
cvtsi2sd %eax, %xmm1
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss 0x10de(%rip), %xmm2     # 0x6090
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
addss 0x10d3(%rip), %xmm0     # 0x6094
addss %xmm2, %xmm0
movss 0x10b3(%rip), %xmm2     # 0x6080
mulss %xmm2, %xmm1
mulss %xmm2, %xmm0
movzwl %ax, %eax
leaq 0x10e9(%rip), %rcx      # 0x60c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $-0x1, 0x1(%rbx)
cvttss2si %xmm0, %eax
movb %al, 0x2(%rbx)
jmp 0x5028
cvttss2si %xmm0, %eax
movb %al, 0x1(%rbx)
movb $0x0, 0x2(%rbx)
jmp 0x5019
movw $0xff00, 0x1(%rbx)      # imm = 0xFF00
cvttss2si %xmm0, %eax
jmp 0x5036
movb $0x0, 0x1(%rbx)
cvttss2si %xmm1, %eax
movb %al, 0x2(%rbx)
movb $-0x1, %al
jmp 0x5036
cvttss2si %xmm1, %eax
movb %al, 0x1(%rbx)
movb $-0x1, 0x2(%rbx)
xorl %eax, %eax
jmp 0x5036
movw $0xff, 0x1(%rbx)
cvttss2si %xmm1, %eax
movb %al, 0x3(%rbx)
leaq 0x10(%rsp), %rdi
callq 0x58ba
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1227(%rip), %rdi      # 0x627f
leaq 0x1231(%rip), %rsi      # 0x6290
leaq 0x1239(%rip), %rcx      # 0x629f
pushq $0x3a
popq %rdx
callq 0x20d0
callq 0x20b0
jmp 0x5075
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x58ba
movq %rbx, %rdi
callq 0x2280