// File: /workspace/github_cpp_makefile_star2_O1/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 $0x20, %rsp
movq %rdi, %rbx
movb $0x1, (%rdi)
movb $0x0, 0x4(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq (%rsi), %r12
movq 0x8(%rsi), %r13
cmpq %r13, %r12
je 0x5a43
movq %rsp, %r14
leaq 0x1c(%rsp), %r15
cmpb $0x1, (%r12)
jne 0x5a36
movzbl 0x1(%r12), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x16d1(%rip), %xmm3     # 0x7020
divss %xmm3, %xmm0
movzbl 0x2(%r12), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movzbl 0x3(%r12), %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
divss %xmm3, %xmm1
divss %xmm3, %xmm2
movaps %xmm2, %xmm4
maxss %xmm1, %xmm4
maxss %xmm0, %xmm4
xorps %xmm3, %xmm3
ucomiss %xmm3, %xmm4
jne 0x598a
jnp 0x59ff
movaps %xmm2, %xmm5
minss %xmm1, %xmm5
minss %xmm0, %xmm5
ucomiss %xmm4, %xmm0
movaps %xmm4, %xmm3
subss %xmm5, %xmm3
jne 0x59c0
jp 0x59c0
subss %xmm2, %xmm1
divss %xmm3, %xmm1
addss 0x1685(%rip), %xmm1     # 0x7038
mulss 0x166d(%rip), %xmm1     # 0x7028
movaps %xmm1, %xmm3
jmp 0x59ff
ucomiss %xmm4, %xmm1
jne 0x59e4
jp 0x59e4
subss %xmm0, %xmm2
divss %xmm3, %xmm2
addss 0x1655(%rip), %xmm2     # 0x702c
mulss 0x1649(%rip), %xmm2     # 0x7028
movaps %xmm2, %xmm3
jmp 0x59ff
subss %xmm1, %xmm0
divss %xmm3, %xmm0
addss 0x1630(%rip), %xmm0     # 0x7024
mulss 0x162c(%rip), %xmm0     # 0x7028
movaps %xmm0, %xmm3
cvttss2si %xmm3, %ecx
leal 0x168(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x5a2b
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x5a36
movq %r14, %rdi
movq %r15, %rdx
callq 0x5f02
addq $0x5, %r12
cmpq %r13, %r12
jne 0x592f
movq (%rsp), %r15
movq 0x8(%rsp), %r14
movq %r14, %rax
subq %r15, %rax
cmpq $0xc, %rax
jne 0x5be5
cmpq %r14, %r15
je 0x5a7c
movl $0x2, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x603d
movq %r15, %rdi
movq %r14, %rsi
callq 0x6120
movq (%rsp), %rdi
movl 0x8(%rdi), %edx
movl (%rdi), %eax
movl 0x4(%rdi), %ecx
movl %edx, %r9d
subl %eax, %r9d
movl %eax, %esi
subl %ecx, %esi
movl $0x168, %r10d           # imm = 0x168
addl %r10d, %esi
movl %ecx, %r8d
subl %edx, %r8d
addl %r10d, %r8d
cmpl %esi, %r8d
movl %esi, %r10d
cmovll %r8d, %r10d
addl %edx, %eax
cmpl %r10d, %r9d
jle 0x5ac6
cmpl %r8d, %esi
jle 0x5ac1
addl $0x2d0, %eax            # imm = 0x2D0
jmp 0x5ac6
addl $0x168, %eax            # imm = 0x168
addl %ecx, %eax
cltq
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
movslq %eax, %rcx
imulq $-0x49f49f49, %rcx, %rax # imm = 0xB60B60B7
shrq $0x20, %rax
addl %ecx, %eax
movl %eax, %edx
shrl $0x1f, %edx
sarl $0x8, %eax
addl %edx, %eax
imull $0x168, %eax, %eax      # imm = 0x168
subl %eax, %ecx
movslq %ecx, %rax
imulq $-0x77777777, %rax, %rax # imm = 0x88888889
shrq $0x20, %rax
addl %ecx, %eax
movl %eax, %edx
shrl $0x1f, %edx
sarl $0x5, %eax
addl %edx, %eax
cmpl $0x5, %eax
ja 0x5c04
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd 0x150e(%rip), %xmm0     # 0x7040
cvtsi2sd %eax, %xmm1
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss 0x14ea(%rip), %xmm2     # 0x7030
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
addss 0x14df(%rip), %xmm0     # 0x7034
addss %xmm2, %xmm0
movss 0x14bf(%rip), %xmm2     # 0x7020
mulss %xmm2, %xmm1
mulss %xmm2, %xmm0
leaq 0x14f8(%rip), %rcx      # 0x7068
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $-0x1, 0x1(%rbx)
cvttss2si %xmm0, %eax
movb %al, 0x2(%rbx)
jmp 0x5bb9
cvttss2si %xmm0, %eax
movb %al, 0x1(%rbx)
movb $0x0, 0x2(%rbx)
jmp 0x5baa
movw $0xff00, 0x1(%rbx)      # imm = 0xFF00
cvttss2si %xmm0, %eax
jmp 0x5bc7
movb $0x0, 0x1(%rbx)
cvttss2si %xmm1, %eax
movb %al, 0x2(%rbx)
movb $-0x1, %al
jmp 0x5bc7
cvttss2si %xmm1, %eax
movb %al, 0x1(%rbx)
movb $-0x1, 0x2(%rbx)
xorl %eax, %eax
jmp 0x5bc7
movw $0xff, 0x1(%rbx)
cvttss2si %xmm1, %eax
movb %al, 0x3(%rbx)
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1613(%rip), %rdi      # 0x71ff
leaq 0x161d(%rip), %rsi      # 0x7210
leaq 0x1625(%rip), %rcx      # 0x721f
movl $0x3a, %edx
callq 0x20f0
callq 0x20d0
jmp 0x5c0b
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x5c24
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22b0