// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movl %ecx, -0x2c(%rbp)
movl %edx, %eax
movl -0x2c(%rbp), %edx
movl %eax, -0x28(%rbp)
movl %esi, %ecx
movl -0x28(%rbp), %esi
movl %edi, %eax
movb %cl, %dil
movb %al, %r10b
movb 0x18(%rbp), %al
movb 0x10(%rbp), %cl
movb %r10b, -0x2(%rbp)
movb %dil, -0x3(%rbp)
movb %sil, -0x4(%rbp)
movb %dl, -0x5(%rbp)
movl %r8d, -0xc(%rbp)
movl %r9d, -0x10(%rbp)
andb $0x1, %cl
movb %cl, -0x11(%rbp)
andb $0x1, %al
movb %al, -0x12(%rbp)
movzbl -0x5(%rbp), %eax
cmpl $0xff, %eax
jge 0x28c85
movzbl -0x5(%rbp), %eax
movzbl -0x2(%rbp), %ecx
imull %ecx, %eax
movl $0xff, %ecx
cltd
idivl %ecx
movb %al, -0x2(%rbp)
movzbl -0x5(%rbp), %eax
movzbl -0x3(%rbp), %ecx
imull %ecx, %eax
movl $0xff, %ecx
cltd
idivl %ecx
movb %al, -0x3(%rbp)
movzbl -0x5(%rbp), %eax
movzbl -0x4(%rbp), %ecx
imull %ecx, %eax
movl $0xff, %ecx
cltd
idivl %ecx
movb %al, -0x4(%rbp)
testb $0x1, -0x11(%rbp)
je 0x28dcb
movb -0x2(%rbp), %dl
movb -0x3(%rbp), %al
leaq -0x13(%rbp), %rcx
leaq -0x14(%rbp), %r8
leaq -0x15(%rbp), %r9
movzbl %dl, %edi
movzbl %al, %esi
movzbl -0x4(%rbp), %edx
callq 0x28900
leaq -0x2(%rbp), %rdi
leaq -0x3(%rbp), %rsi
callq 0x32110
movq %rax, %rdi
leaq -0x4(%rbp), %rsi
callq 0x32110
movzbl (%rax), %eax
movl %eax, -0x30(%rbp)
leaq -0x2(%rbp), %rdi
leaq -0x3(%rbp), %rsi
callq 0x32150
movq %rax, %rdi
leaq -0x4(%rbp), %rsi
callq 0x32150
movq %rax, %rcx
movl -0x30(%rbp), %eax
movzbl (%rcx), %ecx
addl %ecx, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x1c(%rbp)
testb $0x1, -0x12(%rbp)
je 0x28d1d
movl $0xff, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movzbl -0x15(%rbp), %ecx
movl $0xff, %eax
subl %ecx, %eax
movb %al, -0x15(%rbp)
movzbl -0x14(%rbp), %edi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movl $0xfe, %esi
xorl %r8d, %r8d
callq 0x28e70
cmpl $0x80, %eax
jl 0x28da6
movzbl -0x13(%rbp), %edi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movl $0x15, %esi
movl $0x1, %r8d
callq 0x28e70
movl %eax, %edi
callq 0x28a00
movb %al, -0x1d(%rbp)
movl -0x1c(%rbp), %edi
movl -0xc(%rbp), %edx
addl $0x2, %edx
movl -0x10(%rbp), %ecx
addl $0x2, %ecx
movl $0x40, %esi
xorl %r8d, %r8d
callq 0x28e70
cmpl $0x50, %eax
setl %al
andb $0x1, %al
movb %al, -0x1e(%rbp)
movsbl -0x1d(%rbp), %eax
movb -0x1e(%rbp), %sil
xorl %ecx, %ecx
movl $0x20, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
addl %ecx, %eax
movb %al, -0x1(%rbp)
jmp 0x28e60
movl -0x1c(%rbp), %edi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movl $0x1f, %esi
xorl %r8d, %r8d
callq 0x28e70
movl %eax, %edi
callq 0x28b40
movb %al, -0x1(%rbp)
jmp 0x28e60
movb -0x2(%rbp), %dl
movb -0x3(%rbp), %al
leaq -0x1f(%rbp), %rcx
leaq -0x20(%rbp), %r8
leaq -0x21(%rbp), %r9
movzbl %dl, %edi
movzbl %al, %esi
movzbl -0x4(%rbp), %edx
callq 0x28760
testb $0x1, -0x12(%rbp)
je 0x28e00
movzbl -0x21(%rbp), %ecx
movl $0xff, %eax
subl %ecx, %eax
movb %al, -0x21(%rbp)
movzbl -0x1f(%rbp), %edi
callq 0x28a00
movb %al, -0x22(%rbp)
movb $0x20, -0x23(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x18, %eax
jl 0x28e2d
movzbl -0x21(%rbp), %eax
cmpl $0xe8, %eax
jg 0x28e2d
movzbl -0x20(%rbp), %eax
cmpl $0x40, %eax
jge 0x28e3b
movzbl -0x21(%rbp), %edi
callq 0x28b40
movb %al, -0x23(%rbp)
jmp 0x28e5a
movzbl -0x21(%rbp), %eax
cmpl $0x80, %eax
jge 0x28e52
movsbl -0x22(%rbp), %eax
addl $0x20, %eax
movb %al, -0x23(%rbp)
jmp 0x28e58
movb -0x22(%rbp), %al
movb %al, -0x23(%rbp)
jmp 0x28e5a
movb -0x23(%rbp), %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)