// File: /workspace/github_cpp_makefile_star2_O3/lvandeve[P]lodepng/pngdetail.cpp
// Function: RGBtoLetter(unsigned char, unsigned char, unsigned char, unsigned char, unsigned int, unsigned int, bool, bool)
char RGBtoLetter(unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned x, unsigned y, bool dither = true, bool inverted = false) {
  if(a < 255) {
    r = a * r / 255;
    g = a * g / 255;
    b = a * b / 255;
  }

  if(dither) {
    unsigned char h, c, t;
    RGBtoHCT(r, g, b, &h, &c, &t);
    int l = (std::max(std::max(r, g), b) + std::min(std::min(r, g), b)) / 2;
    if(inverted) {
      l = 255 - l;
      t = 255 - t;
    }
    if(applyDither(c, 254, x, y, false) >= 128) {
      char letter = HueToLetter(applyDither(h, 21, x, y, true));
      bool smallcaps = applyDither(l, 64, x+2, y+2, false) < 80;
      return letter + (smallcaps ? 32 : 0);
    }
    else return lightnessToLetter(applyDither(l, 31, x, y, false));
  } else {
    unsigned char h, s, l;
    RGBtoHSL(r, g, b, &h, &s, &l);
    if(inverted) l = 255 - l;

    char hl = HueToLetter(h);
    char c = ' ';
    if(l < 24 || l > 232 || s < 64) {
      c = lightnessToLetter(l);
    } else {
      if(l < 128) c = hl + 32;
      else c = hl;
    }
    return c;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movl %esi, %r15d
movl %edi, %r12d
movb 0x50(%rsp), %al
cmpl $0xff, %ecx
je 0x16cd9
movzbl %cl, %ecx
movzbl %r12b, %edx
imull %ecx, %edx
imull $0x8081, %edx, %r12d    # imm = 0x8081
shrl $0x17, %r12d
movzbl %r15b, %edx
imull %ecx, %edx
imull $0x8081, %edx, %r15d    # imm = 0x8081
shrl $0x17, %r15d
movzbl %bpl, %edx
imull %ecx, %edx
imull $0x8081, %edx, %ebp     # imm = 0x8081
shrl $0x17, %ebp
movb 0x58(%rsp), %r14b
testb %al, %al
je 0x16dfc
movzbl %r12b, %r13d
movzbl %r15b, %ebx
movzbl %bpl, %r14d
leaq 0x5(%rsp), %rcx
leaq 0x4(%rsp), %rax
movq %r9, 0x10(%rsp)
leaq 0x7(%rsp), %r9
movl %r13d, %edi
movl %ebx, %esi
movl %r14d, %edx
movq %r8, 0x8(%rsp)
movq %rax, %r8
callq 0x16b54
cmpb %r15b, %r12b
movl %ebx, %ecx
cmoval %r13d, %ecx
cmpb %bpl, %cl
cmovbel %r14d, %ecx
cmpb %r12b, %r15b
cmovbl %ebx, %r13d
cmpb %r13b, %bpl
cmovbl %r14d, %r13d
movl $0xff, %eax
andl %eax, %r13d
andl %eax, %ecx
addl %r13d, %ecx
shrl %ecx
movl %ecx, %ebx
xorl %eax, %ebx
movb 0x58(%rsp), %dl
testb %dl, %dl
cmovel %ecx, %ebx
movq 0x8(%rsp), %rdx
andl $0x3, %edx
movq 0x10(%rsp), %rcx
andl $0x3, %ecx
leal (%rdx,%rcx,4), %r14d
leaq 0xc530(%rip), %r15      # 0x232a0
movl (%r15,%r14,4), %ecx
movl %ecx, %edx
shll $0x4, %edx
addl %edx, %ecx
addl $-0x80, %ecx
movl %ecx, %esi
shll $0x8, %esi
subl %ecx, %esi
subl %ecx, %esi
addl %esi, %eax
testl %esi, %esi
cmovnsl %esi, %eax
leaq 0x4(%rsp), %rdx
movzbl (%rdx), %edx
sarl $0x8, %eax
addl %edx, %eax
cmpl $0x80, %eax
jl 0x16e9f
leal (%rcx,%rcx,4), %eax
leal (%rcx,%rax,4), %edx
leal (%rcx,%rax,4), %eax
addl $0xff, %eax
testl %edx, %edx
cmovnsl %edx, %eax
shrl $0x8, %eax
addb 0x5(%rsp), %al
movzbl %al, %edi
callq 0x16bc3
xorl $0xa, %r14d
imull $0x440, (%r15,%r14,4), %edx # imm = 0x440
movl %eax, %ecx
leal -0x2000(%rdx), %eax
addl $0xffffe0ff, %edx       # imm = 0xFFFFE0FF
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x8, %edx
addl %ebx, %edx
cmpl $0x50, %edx
setl %al
shlb $0x5, %al
orb %cl, %al
jmp 0x16f03
movzbl %r12b, %edi
movzbl %r15b, %esi
movzbl %bpl, %edx
leaq 0x3(%rsp), %rcx
leaq 0x2(%rsp), %r8
leaq 0x6(%rsp), %rbx
movq %rbx, %r9
callq 0x16a87
negb %r14b
xorb (%rbx), %r14b
leal 0x17(%r14), %eax
cmpb $0x2f, %al
jb 0x16e51
cmpb $0x3f, 0x2(%rsp)
jbe 0x16e51
movzbl 0x3(%rsp), %edi
callq 0x16bc3
movzbl %al, %ecx
orb $0x20, %al
testb %r14b, %r14b
movzbl %al, %eax
cmovsl %ecx, %eax
jmp 0x16f03
movb $0x20, %al
cmpb $0x10, %r14b
jb 0x16f03
movb $0x2e, %al
cmpb $0x30, %r14b
jb 0x16f03
movb $0x3a, %al
cmpb $0x50, %r14b
jb 0x16f03
movb $0x2d, %al
cmpb $0x70, %r14b
jb 0x16f03
movb $0x21, %al
cmpb $-0x70, %r14b
jb 0x16f03
movb $0x2a, %al
cmpb $-0x50, %r14b
jb 0x16f03
movb $0x2b, %al
cmpb $-0x30, %r14b
jb 0x16f03
cmpb $-0x10, %r14b
jmp 0x16ef6
movl %ecx, %eax
shll $0x5, %eax
subl %ecx, %eax
leal 0xff(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x8, %ecx
addl %ebx, %ecx
movb $0x20, %al
cmpl $0x10, %ecx
jl 0x16f03
movb $0x2e, %al
cmpl $0x30, %ecx
jb 0x16f03
movb $0x3a, %al
cmpl $0x50, %ecx
jb 0x16f03
movb $0x2d, %al
cmpl $0x70, %ecx
jb 0x16f03
movb $0x21, %al
cmpl $0x90, %ecx
jb 0x16f03
movb $0x2a, %al
cmpl $0xb0, %ecx
jb 0x16f03
movb $0x2b, %al
cmpl $0xd0, %ecx
jb 0x16f03
cmpl $0xf0, %ecx
movl $0x3d, %ecx
movl $0x23, %eax
cmovbl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq