// File: /workspace/github_c_makefile_star2_O0/OnionIoT[P]lv_example/lvgl/src/lv_misc/lv_color.c
// Function: lv_color_rgb_to_hsv
lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r8, uint8_t g8, uint8_t b8)
{
    uint16_t r = ((uint32_t)r8 << 10) / 255;
    uint16_t g = ((uint32_t)g8 << 10) / 255;
    uint16_t b = ((uint32_t)b8 << 10) / 255;

    uint16_t rgbMin = r < g ? (r < b ? r : b) : (g < b ? g : b);
    uint16_t rgbMax = r > g ? (r > b ? r : b) : (g > b ? g : b);

    lv_color_hsv_t hsv;

    // https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
    hsv.v = (100 * rgbMax) >> 10;

    int32_t delta = rgbMax - rgbMin;
    if (LV_MATH_ABS(delta) < 3) {
        hsv.h = 0;
        hsv.s = 0;
        return hsv;
    }

    // https://en.wikipedia.org/wiki/HSL_and_HSV#Saturation
    hsv.s = 100 * delta / rgbMax;
    if(hsv.s < 3) {
        hsv.h = 0;
        return hsv;
    }

    // https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma
    int32_t h;
    if(rgbMax == r)
        h = (((g - b) << 10) / delta) + (g < b ? (6 << 10) : 0); // between yellow & magenta
    else if(rgbMax == g)
        h = (((b - r) << 10) / delta) + (2 << 10); // between cyan & yellow
    else if(rgbMax == b)
        h = (((r - g) << 10) / delta) + (4 << 10); // between magenta & cyan
    else
        h = 0;
    h *= 60;
    h >>= 10;
    if (h < 0) h += 360;

    hsv.h = h;
    return hsv;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movb %dl, %al
movb %sil, %cl
movb %dil, %dl
movb %dl, -0x5(%rbp)
movb %cl, -0x6(%rbp)
movb %al, -0x7(%rbp)
movzbl -0x5(%rbp), %eax
shll $0xa, %eax
movl $0xff, %ecx
xorl %edx, %edx
divl %ecx
movw %ax, -0xa(%rbp)
movzbl -0x6(%rbp), %eax
shll $0xa, %eax
movl $0xff, %ecx
xorl %edx, %edx
divl %ecx
movw %ax, -0xc(%rbp)
movzbl -0x7(%rbp), %eax
shll $0xa, %eax
movl $0xff, %ecx
xorl %edx, %edx
divl %ecx
movw %ax, -0xe(%rbp)
movzwl -0xa(%rbp), %eax
movzwl -0xc(%rbp), %ecx
cmpl %ecx, %eax
jge 0x45ed1
movzwl -0xa(%rbp), %eax
movzwl -0xe(%rbp), %ecx
cmpl %ecx, %eax
jge 0x45ec2
movzwl -0xa(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x45ec9
movzwl -0xe(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x45ef3
movzwl -0xc(%rbp), %eax
movzwl -0xe(%rbp), %ecx
cmpl %ecx, %eax
jge 0x45ee6
movzwl -0xc(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x45eed
movzwl -0xe(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movw %ax, -0x10(%rbp)
movzwl -0xa(%rbp), %eax
movzwl -0xc(%rbp), %ecx
cmpl %ecx, %eax
jle 0x45f2a
movzwl -0xa(%rbp), %eax
movzwl -0xe(%rbp), %ecx
cmpl %ecx, %eax
jle 0x45f1b
movzwl -0xa(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x45f22
movzwl -0xe(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x45f4c
movzwl -0xc(%rbp), %eax
movzwl -0xe(%rbp), %ecx
cmpl %ecx, %eax
jle 0x45f3f
movzwl -0xc(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x45f46
movzwl -0xe(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movw %ax, -0x12(%rbp)
movzwl -0x12(%rbp), %eax
imull $0x64, %eax, %eax
sarl $0xa, %eax
movb %al, -0x1(%rbp)
movzwl -0x12(%rbp), %eax
movzwl -0x10(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jle 0x45f7b
movl -0x18(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x45f83
xorl %eax, %eax
subl -0x18(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl $0x3, %eax
jge 0x45f9a
movw $0x0, -0x4(%rbp)
movb $0x0, -0x2(%rbp)
jmp 0x46075
imull $0x64, -0x18(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cltd
idivl %ecx
movb %al, -0x2(%rbp)
movzbl -0x2(%rbp), %eax
cmpl $0x3, %eax
jge 0x45fbc
movw $0x0, -0x4(%rbp)
jmp 0x46075
movzwl -0x12(%rbp), %eax
movzwl -0xa(%rbp), %ecx
cmpl %ecx, %eax
jne 0x45ff4
movzwl -0xc(%rbp), %eax
movzwl -0xe(%rbp), %ecx
subl %ecx, %eax
shll $0xa, %eax
cltd
idivl -0x18(%rbp)
movzwl -0xc(%rbp), %esi
movzwl -0xe(%rbp), %edi
xorl %ecx, %ecx
movl $0x1800, %edx           # imm = 0x1800
cmpl %edi, %esi
cmovll %edx, %ecx
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4604d
movzwl -0x12(%rbp), %eax
movzwl -0xc(%rbp), %ecx
cmpl %ecx, %eax
jne 0x4601b
movzwl -0xe(%rbp), %eax
movzwl -0xa(%rbp), %ecx
subl %ecx, %eax
shll $0xa, %eax
cltd
idivl -0x18(%rbp)
addl $0x800, %eax            # imm = 0x800
movl %eax, -0x1c(%rbp)
jmp 0x4604b
movzwl -0x12(%rbp), %eax
movzwl -0xe(%rbp), %ecx
cmpl %ecx, %eax
jne 0x46042
movzwl -0xa(%rbp), %eax
movzwl -0xc(%rbp), %ecx
subl %ecx, %eax
shll $0xa, %eax
cltd
idivl -0x18(%rbp)
addl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x1c(%rbp)
jmp 0x46049
movl $0x0, -0x1c(%rbp)
jmp 0x4604b
jmp 0x4604d
imull $0x3c, -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
sarl $0xa, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x4606e
movl -0x1c(%rbp), %eax
addl $0x168, %eax            # imm = 0x168
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movw %ax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopw (%rax,%rax)