// File: /workspace/github_cpp_makefile_star2_O3/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
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 0x5d50
movq %rdi, %rbx
movq %rsp, %r14
leaq 0x1c(%rsp), %r15
cmpb $0x1, (%r12)
jne 0x5b97
movzbl 0x1(%r12), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss 0x15d1(%rip), %xmm0     # 0x7080
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 0x1581(%rip), %xmm1     # 0x7050
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 0x5aeb
jnp 0x5b60
movaps %xmm2, %xmm5
minss %xmm1, %xmm5
minss %xmm0, %xmm5
ucomiss %xmm3, %xmm0
movaps %xmm3, %xmm4
subss %xmm5, %xmm4
jne 0x5b21
jp 0x5b21
subss %xmm2, %xmm1
divss %xmm4, %xmm1
addss 0x1584(%rip), %xmm1     # 0x7098
mulss 0x156c(%rip), %xmm1     # 0x7088
movaps %xmm1, %xmm4
jmp 0x5b60
ucomiss %xmm3, %xmm1
jne 0x5b45
jp 0x5b45
subss %xmm0, %xmm2
divss %xmm4, %xmm2
addss 0x1554(%rip), %xmm2     # 0x708c
mulss 0x1548(%rip), %xmm2     # 0x7088
movaps %xmm2, %xmm4
jmp 0x5b60
subss %xmm1, %xmm0
divss %xmm4, %xmm0
addss 0x152f(%rip), %xmm0     # 0x7084
mulss 0x152b(%rip), %xmm0     # 0x7088
movaps %xmm0, %xmm4
cvttss2si %xmm4, %ecx
leal 0x168(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x5b8c
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x5b97
movq %r14, %rdi
movq %r15, %rdx
callq 0x6070
addq $0x5, %r12
cmpq %r13, %r12
jne 0x5a8f
movq (%rsp), %r14
movq 0x8(%rsp), %r15
movq %r15, %rax
subq %r14, %rax
cmpq $0xc, %rax
jne 0x5d50
cmpq %r15, %r14
je 0x5be1
movl $0x2, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x61ab
movq %r14, %rdi
movq %r15, %rsi
callq 0x628e
movq (%rsp), %r14
movl 0x8(%r14), %edx
movl (%r14), %eax
movl 0x4(%r14), %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 0x5c26
cmpl %edi, %esi
jle 0x5c21
addl $0x2d0, %eax            # imm = 0x2D0
jmp 0x5c26
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
cltq
imulq $-0x49f49f49, %rax, %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
movswl %ax, %ecx
imull $0xffff8889, %ecx, %esi # imm = 0xFFFF8889
shrl $0x10, %esi
addl %eax, %esi
movl $0xffff, %ecx           # imm = 0xFFFF
movswl %si, %edx
andl %ecx, %esi
shrl $0xf, %esi
sarl $0x5, %edx
addl %esi, %edx
cmpw $0x5, %dx
ja 0x5d6f
cvtsi2sd %eax, %xmm0
divsd 0x140b(%rip), %xmm0     # 0x70a0
movswl %dx, %eax
cvtsi2sd %eax, %xmm1
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss 0x13e4(%rip), %xmm2     # 0x7090
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
addss 0x13d9(%rip), %xmm0     # 0x7094
addss %xmm2, %xmm0
movss 0x13b9(%rip), %xmm2     # 0x7080
mulss %xmm2, %xmm1
mulss %xmm2, %xmm0
andl %ecx, %edx
leaq 0x13f0(%rip), %rax      # 0x70c8
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movb $-0x1, 0x1(%rbx)
cvttss2si %xmm0, %eax
movb %al, 0x2(%rbx)
jmp 0x5d21
cvttss2si %xmm0, %eax
movb %al, 0x1(%rbx)
movb $0x0, 0x2(%rbx)
jmp 0x5d12
movw $0xff00, 0x1(%rbx)      # imm = 0xFF00
cvttss2si %xmm0, %eax
jmp 0x5d2f
movb $0x0, 0x1(%rbx)
cvttss2si %xmm1, %eax
movb %al, 0x2(%rbx)
movb $-0x1, %al
jmp 0x5d2f
cvttss2si %xmm1, %eax
movb %al, 0x1(%rbx)
movb $-0x1, 0x2(%rbx)
xorl %eax, %eax
jmp 0x5d2f
movw $0xff, 0x1(%rbx)
cvttss2si %xmm1, %eax
movb %al, 0x3(%rbx)
movq 0x10(%rsp), %rsi
subq %r14, %rsi
movq %r14, %rdi
callq 0x21c0
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x1528(%rip), %rdi      # 0x727f
leaq 0x1532(%rip), %rsi      # 0x7290
leaq 0x153a(%rip), %rcx      # 0x729f
movl $0x3a, %edx
callq 0x20f0
callq 0x20d0
jmp 0x5d76
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x5d8f
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22b0
nop