// File: miklhh[P]mandelbrot/src/color.cpp
// Function: hsl_to_rgb(hsl_t)
static rgb_t hsl_to_rgb(hsl_t hsl)
{
    /* Make sure that the hue is in it's correct spectrum. */
    hsl.hue = shift_range(hsl.hue, 0, 2*pi);
    double hue_prime = hsl.hue / (pi / 3);
    double chroma = (double(1) - abs(2 * hsl.light - 1)) * hsl.saturation;
    double x = chroma * (1 - abs(fmod(hue_prime, 2) - 1));

    double r1 = 0;
    double g1 = 0;
    double b1 = 0;
         if (0 <= hue_prime && hue_prime <= 1) { r1 = chroma;  g1 = x;       b1 = 0;      }
    else if (1 <= hue_prime && hue_prime <= 2) { r1 = x;       g1 = chroma;  b1 = 0;      }
    else if (2 <= hue_prime && hue_prime <= 3) { r1 = 0;       g1 = chroma;  b1 = x;      }
    else if (3 <= hue_prime && hue_prime <= 4) { r1 = 0;       g1 = x;       b1 = chroma; }
    else if (4 <= hue_prime && hue_prime <= 5) { r1 = x;       g1 = 0;       b1 = chroma; }
    else if (5 <= hue_prime && hue_prime <= 6) { r1 = chroma;  g1 = 0;       b1 = x;      }

    double m = hsl.light - chroma / 2;
    uint8_t r = uint8_t((r1 + m) * 255);
    uint8_t g = uint8_t((g1 + m) * 255);
    uint8_t b = uint8_t((b1 + m) * 255);
    rgb_t color = { r, g, b, 0 };
    return color;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
movsd 0x7b01(%rip), %xmm2     # 0xe230
callq 0x6a50
movq -0x58(%rbp), %rax
movsd %xmm0, (%rax)
movsd (%rax), %xmm0
movsd 0x7ae0(%rip), %xmm1     # 0xe228
divsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movsd 0x10(%rax), %xmm0
addsd %xmm0, %xmm0
movsd 0x78ae(%rip), %xmm1     # 0xe010
addsd %xmm1, %xmm0
callq 0x6d70
movq -0x58(%rbp), %rax
movaps %xmm0, %xmm1
movsd 0x788e(%rip), %xmm0     # 0xe008
subsd %xmm1, %xmm0
mulsd 0x8(%rax), %xmm0
movsd %xmm0, -0x18(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x10(%rbp), %xmm0
movl $0x2, %edi
callq 0x6d90
movsd 0x785f(%rip), %xmm1     # 0xe008
subsd %xmm1, %xmm0
callq 0x6d70
movaps %xmm0, %xmm2
movsd -0x50(%rbp), %xmm0
movsd 0x7846(%rip), %xmm1     # 0xe008
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6825
movsd 0x780b(%rip), %xmm0     # 0xe008
ucomisd -0x10(%rbp), %xmm0
jb 0x6825
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x6976
movsd -0x10(%rbp), %xmm0
movsd 0x77d6(%rip), %xmm1     # 0xe008
ucomisd %xmm1, %xmm0
jb 0x6868
movsd 0x79e0(%rip), %xmm0     # 0xe220
ucomisd -0x10(%rbp), %xmm0
jb 0x6868
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x6974
movsd -0x10(%rbp), %xmm0
movsd 0x79ab(%rip), %xmm1     # 0xe220
ucomisd %xmm1, %xmm0
jb 0x68ab
movsd 0x79b5(%rip), %xmm0     # 0xe238
ucomisd -0x10(%rbp), %xmm0
jb 0x68ab
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x6972
movsd -0x10(%rbp), %xmm0
movsd 0x7980(%rip), %xmm1     # 0xe238
ucomisd %xmm1, %xmm0
jb 0x68ee
movsd 0x797a(%rip), %xmm0     # 0xe240
ucomisd -0x10(%rbp), %xmm0
jb 0x68ee
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x6970
movsd -0x10(%rbp), %xmm0
movsd 0x7945(%rip), %xmm1     # 0xe240
ucomisd %xmm1, %xmm0
jb 0x692e
movsd 0x793f(%rip), %xmm0     # 0xe248
ucomisd -0x10(%rbp), %xmm0
jb 0x692e
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x696e
movsd -0x10(%rbp), %xmm0
movsd 0x790d(%rip), %xmm1     # 0xe248
ucomisd %xmm1, %xmm0
jb 0x696c
movsd 0x7907(%rip), %xmm0     # 0xe250
ucomisd -0x10(%rbp), %xmm0
jb 0x696c
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x696e
jmp 0x6970
jmp 0x6972
jmp 0x6974
jmp 0x6976
movq -0x58(%rbp), %rax
movsd 0x10(%rax), %xmm0
movsd -0x18(%rbp), %xmm1
movsd 0x78cc(%rip), %xmm2     # 0xe258
mulsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x28(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd 0x78b1(%rip), %xmm1     # 0xe260
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x41(%rbp)
movsd -0x30(%rbp), %xmm0
movsd -0x40(%rbp), %xmm2
addsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x42(%rbp)
movsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm2
addsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x43(%rbp)
movzbl -0x41(%rbp), %ecx
movl -0x4(%rbp), %eax
andl $0xff, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movl %eax, -0x4(%rbp)
movzbl -0x42(%rbp), %ecx
movl -0x4(%rbp), %eax
andl $0xff, %ecx
shll $0x8, %ecx
andl $0xffff00ff, %eax       # imm = 0xFFFF00FF
orl %ecx, %eax
movl %eax, -0x4(%rbp)
movzbl -0x43(%rbp), %ecx
movl -0x4(%rbp), %eax
andl $0xff, %ecx
shll $0x10, %ecx
andl $0xff00ffff, %eax       # imm = 0xFF00FFFF
orl %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
orl $0x0, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop