// File: 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 %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rsi, -0xb8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movb $0x1, (%rax)
movb $0x0, 0x4(%rax)
leaq -0x28(%rbp), %rdi
callq 0xa790
movq -0xb8(%rbp), %rsi
movq %rsi, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0xa470
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0xa2e0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xa7b0
testb $0x1, %al
jne 0x9410
jmp 0x964f
leaq -0x38(%rbp), %rdi
callq 0xa7f0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
testb $0x1, (%rax)
je 0x963f
movq -0x48(%rbp), %rax
movzbl 0x1(%rax), %eax
cvtsi2ss %eax, %xmm0
movss 0x4db6(%rip), %xmm1     # 0xe1f4
divss %xmm1, %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movzbl 0x2(%rax), %eax
cvtsi2ss %eax, %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x50(%rbp)
movq -0x48(%rbp), %rax
movzbl 0x3(%rax), %eax
cvtsi2ss %eax, %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x54(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0xa810
movq %rax, -0xc8(%rbp)
jmp 0x9487
movq -0xc8(%rbp), %rsi
leaq -0x4c(%rbp), %rdi
callq 0xa810
movq %rax, -0xd0(%rbp)
jmp 0x94a0
movq -0xd0(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0xa850
movq %rax, -0xd8(%rbp)
jmp 0x94c6
movq -0xd8(%rbp), %rsi
leaq -0x4c(%rbp), %rdi
callq 0xa850
movq %rax, -0xe0(%rbp)
jmp 0x94df
movq -0xe0(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x68(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x950e
jp 0x950e
xorps %xmm0, %xmm0
movss %xmm0, -0xe4(%rbp)
jmp 0x95f2
movss -0x4c(%rbp), %xmm0
ucomiss -0x68(%rbp), %xmm0
jne 0x9553
jp 0x9553
movss -0x50(%rbp), %xmm0
subss -0x54(%rbp), %xmm0
movss -0x68(%rbp), %xmm1
subss -0x58(%rbp), %xmm1
divss %xmm1, %xmm0
xorps %xmm1, %xmm1
addss %xmm0, %xmm1
movss 0x4cb6(%rip), %xmm0     # 0xe1f8
mulss %xmm1, %xmm0
movss %xmm0, -0xe8(%rbp)
jmp 0x95e2
movss -0x50(%rbp), %xmm0
ucomiss -0x68(%rbp), %xmm0
jne 0x959a
jp 0x959a
movss -0x54(%rbp), %xmm0
subss -0x4c(%rbp), %xmm0
movss -0x68(%rbp), %xmm1
subss -0x58(%rbp), %xmm1
divss %xmm1, %xmm0
movss 0x4c80(%rip), %xmm1     # 0xe200
addss %xmm0, %xmm1
movss 0x4c6c(%rip), %xmm0     # 0xe1f8
mulss %xmm1, %xmm0
movss %xmm0, -0xec(%rbp)
jmp 0x95d2
movss -0x4c(%rbp), %xmm0
subss -0x50(%rbp), %xmm0
movss -0x68(%rbp), %xmm1
subss -0x58(%rbp), %xmm1
divss %xmm1, %xmm0
movss 0x4c42(%rip), %xmm1     # 0xe1fc
addss %xmm0, %xmm1
movss 0x4c32(%rip), %xmm0     # 0xe1f8
mulss %xmm1, %xmm0
movss %xmm0, -0xec(%rbp)
movss -0xec(%rbp), %xmm0
movss %xmm0, -0xe8(%rbp)
movss -0xe8(%rbp), %xmm0
movss %xmm0, -0xe4(%rbp)
movss -0xe4(%rbp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0x962e
movl -0x6c(%rbp), %eax
addl $0x168, %eax            # imm = 0x168
movl %eax, -0x6c(%rbp)
jmp 0x962e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xaa60
jmp 0x9c27
leaq -0x28(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
callq 0xa890
jmp 0x963d
jmp 0x963f
jmp 0x9641
leaq -0x38(%rbp), %rdi
callq 0xa900
jmp 0x93fa
leaq -0x28(%rbp), %rdi
callq 0xa920
cmpq $0x3, %rax
jne 0x9660
jmp 0x967f
leaq 0x4c28(%rip), %rdi      # 0xe28f
leaq 0x4c32(%rip), %rsi      # 0xe2a0
movl $0x3a, %edx
leaq 0x4c35(%rip), %rcx      # 0xe2af
callq 0x20e0
leaq -0x28(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0xa980
movq -0xf8(%rbp), %rdi
movq %rax, -0x78(%rbp)
callq 0xa9b0
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0xa940
jmp 0x96b2
leaq -0x28(%rbp), %rdi
movq %rdi, -0x118(%rbp)
movl $0x2, %esi
movq %rsi, -0x110(%rbp)
callq 0xa9e0
movq -0x118(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x12c(%rbp)
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x138(%rbp)
callq 0xa9e0
movq -0x138(%rbp), %rsi
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl -0x12c(%rbp), %eax
movl (%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x84(%rbp)
callq 0xa9e0
movq -0x118(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x11c(%rbp)
movl $0x1, %esi
movq %rsi, -0x128(%rbp)
callq 0xa9e0
movq -0x128(%rbp), %rsi
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl -0x11c(%rbp), %eax
movl (%rcx), %ecx
subl %ecx, %eax
addl $0x168, %eax            # imm = 0x168
movl %eax, -0x88(%rbp)
callq 0xa9e0
movq -0x118(%rbp), %rdi
movq -0x110(%rbp), %rsi
movl (%rax), %eax
movl %eax, -0x104(%rbp)
callq 0xa9e0
movq %rax, %rcx
movl -0x104(%rbp), %eax
movl (%rcx), %ecx
subl %ecx, %eax
addl $0x168, %eax            # imm = 0x168
movl %eax, -0x8c(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
callq 0xaa00
movq %rax, -0x100(%rbp)
jmp 0x97ad
movq -0x100(%rbp), %rsi
leaq -0x84(%rbp), %rdi
callq 0xaa00
movq %rax, -0x140(%rbp)
jmp 0x97c9
movq -0x140(%rbp), %rax
movl (%rax), %eax
cmpl -0x84(%rbp), %eax
jne 0x983a
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xa9e0
movl (%rax), %eax
movl %eax, -0x148(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xa9e0
movq %rax, %rcx
movl -0x148(%rbp), %eax
addl (%rcx), %eax
movl %eax, -0x144(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0xa9e0
movq %rax, %rcx
movl -0x144(%rbp), %eax
addl (%rcx), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, -0x90(%rbp)
jmp 0x9947
leaq -0x88(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
callq 0xaa00
movq %rax, -0x150(%rbp)
jmp 0x9856
movq -0x150(%rbp), %rsi
leaq -0x84(%rbp), %rdi
callq 0xaa00
movq %rax, -0x158(%rbp)
jmp 0x9872
movq -0x158(%rbp), %rax
movl (%rax), %eax
cmpl -0x88(%rbp), %eax
jne 0x98e5
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xa9e0
movl (%rax), %eax
movl %eax, -0x160(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xa9e0
movq %rax, %rcx
movl -0x160(%rbp), %eax
addl (%rcx), %eax
movl %eax, -0x15c(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0xa9e0
movq %rax, %rcx
movl -0x15c(%rbp), %eax
addl (%rcx), %eax
addl $0x168, %eax            # imm = 0x168
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, -0x90(%rbp)
jmp 0x9945
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xa9e0
movl (%rax), %eax
movl %eax, -0x168(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xa9e0
movq %rax, %rcx
movl -0x168(%rbp), %eax
addl (%rcx), %eax
movl %eax, -0x164(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0xa9e0
movq %rax, %rcx
movl -0x164(%rbp), %eax
addl (%rcx), %eax
addl $0x2d0, %eax            # imm = 0x2D0
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, -0x90(%rbp)
jmp 0x9947
movslq -0x90(%rbp), %rcx
movl %ecx, %eax
imulq $-0x49f49f49, %rcx, %rcx # imm = 0xB60B60B7
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x8, %ecx
addl %edx, %ecx
imull $0x168, %ecx, %ecx      # imm = 0x168
subl %ecx, %eax
movl %eax, -0x90(%rbp)
movl $0x3f800000, -0x94(%rbp) # imm = 0x3F800000
movl $0x3f800000, -0x98(%rbp) # imm = 0x3F800000
movslq -0x90(%rbp), %rcx
movl %ecx, %eax
imulq $-0x77777777, %rcx, %rcx # imm = 0x88888889
shrq $0x20, %rcx
movl %ecx, %edi
addl %eax, %edi
movl %edi, %eax
shrl $0x1f, %eax
sarl $0x5, %edi
addl %eax, %edi
callq 0xaa40
movsd %xmm0, -0x170(%rbp)
jmp 0x99ba
movsd -0x170(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x9c(%rbp)
cvtsi2sdl -0x90(%rbp), %xmm0
movsd 0x47f4(%rip), %xmm1     # 0xe1d0
divsd %xmm1, %xmm0
cvtsi2sdl -0x9c(%rbp), %xmm1
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xa0(%rbp)
movss -0x98(%rbp), %xmm0
movss -0x94(%rbp), %xmm3
movss 0x47e0(%rip), %xmm1     # 0xe1f0
movaps %xmm1, %xmm2
subss %xmm3, %xmm2
mulss %xmm2, %xmm0
movss %xmm0, -0xa4(%rbp)
movss -0x98(%rbp), %xmm0
movss -0xa0(%rbp), %xmm3
movss -0x94(%rbp), %xmm2
mulss %xmm2, %xmm3
movaps %xmm1, %xmm2
subss %xmm3, %xmm2
mulss %xmm2, %xmm0
movss %xmm0, -0xa8(%rbp)
movss -0x98(%rbp), %xmm0
movss -0xa0(%rbp), %xmm3
movaps %xmm1, %xmm2
subss %xmm3, %xmm2
movss -0x94(%rbp), %xmm3
mulss %xmm3, %xmm2
subss %xmm2, %xmm1
mulss %xmm1, %xmm0
movss %xmm0, -0xac(%rbp)
movss -0x98(%rbp), %xmm0
movss 0x475f(%rip), %xmm1     # 0xe1f4
mulss %xmm1, %xmm0
movss %xmm0, -0x98(%rbp)
movss -0xa4(%rbp), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, -0xa4(%rbp)
movss -0xa8(%rbp), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, -0xa8(%rbp)
movss -0xac(%rbp), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, -0xac(%rbp)
movl -0x9c(%rbp), %eax
movq %rax, -0x178(%rbp)
subq $0x5, %rax
ja 0x9c10
movq -0x178(%rbp), %rax
leaq 0x4702(%rip), %rcx      # 0xe204
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xc0(%rbp), %rax
cvttss2si -0x98(%rbp), %ecx
movb %cl, 0x1(%rax)
cvttss2si -0xac(%rbp), %ecx
movb %cl, 0x2(%rax)
cvttss2si -0xa4(%rbp), %ecx
movb %cl, 0x3(%rax)
jmp 0x9c15
movq -0xc0(%rbp), %rax
cvttss2si -0xa8(%rbp), %ecx
movb %cl, 0x1(%rax)
cvttss2si -0x98(%rbp), %ecx
movb %cl, 0x2(%rax)
cvttss2si -0xa4(%rbp), %ecx
movb %cl, 0x3(%rax)
jmp 0x9c15
movq -0xc0(%rbp), %rax
cvttss2si -0xa4(%rbp), %ecx
movb %cl, 0x1(%rax)
cvttss2si -0x98(%rbp), %ecx
movb %cl, 0x2(%rax)
cvttss2si -0xac(%rbp), %ecx
movb %cl, 0x3(%rax)
jmp 0x9c15
movq -0xc0(%rbp), %rax
cvttss2si -0xa4(%rbp), %ecx
movb %cl, 0x1(%rax)
cvttss2si -0xa8(%rbp), %ecx
movb %cl, 0x2(%rax)
cvttss2si -0x98(%rbp), %ecx
movb %cl, 0x3(%rax)
jmp 0x9c15
movq -0xc0(%rbp), %rax
cvttss2si -0xac(%rbp), %ecx
movb %cl, 0x1(%rax)
cvttss2si -0xa4(%rbp), %ecx
movb %cl, 0x2(%rax)
cvttss2si -0x98(%rbp), %ecx
movb %cl, 0x3(%rax)
jmp 0x9c15
movq -0xc0(%rbp), %rax
cvttss2si -0x98(%rbp), %ecx
movb %cl, 0x1(%rax)
cvttss2si -0xa4(%rbp), %ecx
movb %cl, 0x2(%rax)
cvttss2si -0xa8(%rbp), %ecx
movb %cl, 0x3(%rax)
jmp 0x9c15
callq 0x20c0
leaq -0x28(%rbp), %rdi
callq 0xaa60
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x22d0