int rgb2xterm(long rgb) {
  int xterm, r, g, b;
  if (rgb == 0x000000)
    return (0);
  if (rgb == 0x800000)
    return (1);
  if (rgb == 0x008000)
    return (2);
  if (rgb == 0x808000)
    return (3);
  if (rgb == 0x000080)
    return (4);
  if (rgb == 0x800080)
    return (5);
  if (rgb == 0x008080)
    return (6);
  if (rgb == 0xc0c0c0)
    return (7);
  if (rgb == 0x808080)
    return (8);
  if (rgb == 0xff0000)
    return (9);
  if (rgb == 0x00ff00)
    return (10);
  if (rgb == 0xffff00)
    return (11);
  if (rgb == 0x0000ff)
    return (12);
  if (rgb == 0xff00ff)
    return (13);
  if (rgb == 0x00ffff)
    return (14);
  if (rgb == 0xffffff)
    return (15);
  r = (rgb & 0xFF0000) >> 16;
  g = (rgb & 0x00FF00) >> 8;
  b = rgb & 0x0000FF;
  if ((r == g) && (r == b)) {
    if (rgb <= 0x080808)
      return (232);
    if (rgb <= 0x121212)
      return (233);
    if (rgb <= 0x1c1c1c)
      return (234);
    if (rgb <= 0x262626)
      return (235);
    if (rgb <= 0x303030)
      return (236);
    if (rgb <= 0x3a3a3a)
      return (237);
    if (rgb <= 0x444444)
      return (238);
    if (rgb <= 0x4e4e4e)
      return (239);
    if (rgb <= 0x585858)
      return (240);
    if (rgb <= 0x606060)
      return (241);
    if (rgb <= 0x666666)
      return (242);
    if (rgb <= 0x767676)
      return (243);
    if (rgb <= 0x808080)
      return (244);
    if (rgb <= 0x8a8a8a)
      return (245);
    if (rgb <= 0x949494)
      return (246);
    if (rgb <= 0x9e9e9e)
      return (247);
    if (rgb <= 0xa8a8a8)
      return (248);
    if (rgb <= 0xb2b2b2)
      return (249);
    if (rgb <= 0xbcbcbc)
      return (250);
    if (rgb <= 0xc6c6c6)
      return (251);
    if (rgb <= 0xd0d0d0)
      return (252);
    if (rgb <= 0xdadada)
      return (253);
    if (rgb <= 0xe4e4e4)
      return (254);
    if (rgb <= 0xeeeeee)
      return (255);
  }
  xterm = (((r / 51) * 36) + ((g / 51) * 6) + (b / 51)) + 16;
  if (xterm < 16)
    xterm = 16;
  if (xterm > 231)
    xterm = 231;
  return (xterm);
}


x86 assembly (clang, -O0):
<rgb2xterm>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    111f <rgb2xterm+0x1f>
movl   $0x0,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x800000,-0x10(%rbp)
jne    1139 <rgb2xterm+0x39>
movl   $0x1,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x8000,-0x10(%rbp)
jne    1153 <rgb2xterm+0x53>
movl   $0x2,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x808000,-0x10(%rbp)
jne    116d <rgb2xterm+0x6d>
movl   $0x3,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x80,-0x10(%rbp)
jne    1187 <rgb2xterm+0x87>
movl   $0x4,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x800080,-0x10(%rbp)
jne    11a1 <rgb2xterm+0xa1>
movl   $0x5,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x8080,-0x10(%rbp)
jne    11bb <rgb2xterm+0xbb>
movl   $0x6,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xc0c0c0,-0x10(%rbp)
jne    11d5 <rgb2xterm+0xd5>
movl   $0x7,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x808080,-0x10(%rbp)
jne    11ef <rgb2xterm+0xef>
movl   $0x8,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xff0000,-0x10(%rbp)
jne    1209 <rgb2xterm+0x109>
movl   $0x9,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xff00,-0x10(%rbp)
jne    1223 <rgb2xterm+0x123>
movl   $0xa,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xffff00,-0x10(%rbp)
jne    123d <rgb2xterm+0x13d>
movl   $0xb,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xff,-0x10(%rbp)
jne    1257 <rgb2xterm+0x157>
movl   $0xc,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xff00ff,-0x10(%rbp)
jne    1271 <rgb2xterm+0x171>
movl   $0xd,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xffff,-0x10(%rbp)
jne    128b <rgb2xterm+0x18b>
movl   $0xe,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xffffff,-0x10(%rbp)
jne    12a5 <rgb2xterm+0x1a5>
movl   $0xf,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
mov    -0x10(%rbp),%rax
and    $0xff0000,%rax
sar    $0x10,%rax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
and    $0xff00,%rax
sar    $0x8,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
and    $0xff,%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1561 <rgb2xterm+0x461>
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1561 <rgb2xterm+0x461>
cmpq   $0x80808,-0x10(%rbp)
jg     1306 <rgb2xterm+0x206>
movl   $0xe8,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x121212,-0x10(%rbp)
jg     1320 <rgb2xterm+0x220>
movl   $0xe9,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x1c1c1c,-0x10(%rbp)
jg     133a <rgb2xterm+0x23a>
movl   $0xea,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x262626,-0x10(%rbp)
jg     1354 <rgb2xterm+0x254>
movl   $0xeb,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x303030,-0x10(%rbp)
jg     136e <rgb2xterm+0x26e>
movl   $0xec,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x3a3a3a,-0x10(%rbp)
jg     1388 <rgb2xterm+0x288>
movl   $0xed,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x444444,-0x10(%rbp)
jg     13a2 <rgb2xterm+0x2a2>
movl   $0xee,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x4e4e4e,-0x10(%rbp)
jg     13bc <rgb2xterm+0x2bc>
movl   $0xef,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x585858,-0x10(%rbp)
jg     13d6 <rgb2xterm+0x2d6>
movl   $0xf0,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x606060,-0x10(%rbp)
jg     13f0 <rgb2xterm+0x2f0>
movl   $0xf1,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x666666,-0x10(%rbp)
jg     140a <rgb2xterm+0x30a>
movl   $0xf2,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x767676,-0x10(%rbp)
jg     1424 <rgb2xterm+0x324>
movl   $0xf3,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x808080,-0x10(%rbp)
jg     143e <rgb2xterm+0x33e>
movl   $0xf4,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x8a8a8a,-0x10(%rbp)
jg     1458 <rgb2xterm+0x358>
movl   $0xf5,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x949494,-0x10(%rbp)
jg     1472 <rgb2xterm+0x372>
movl   $0xf6,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0x9e9e9e,-0x10(%rbp)
jg     148c <rgb2xterm+0x38c>
movl   $0xf7,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xa8a8a8,-0x10(%rbp)
jg     14a6 <rgb2xterm+0x3a6>
movl   $0xf8,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xb2b2b2,-0x10(%rbp)
jg     14c0 <rgb2xterm+0x3c0>
movl   $0xf9,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xbcbcbc,-0x10(%rbp)
jg     14da <rgb2xterm+0x3da>
movl   $0xfa,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xc6c6c6,-0x10(%rbp)
jg     14f4 <rgb2xterm+0x3f4>
movl   $0xfb,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xd0d0d0,-0x10(%rbp)
jg     150e <rgb2xterm+0x40e>
movl   $0xfc,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xdadada,-0x10(%rbp)
jg     1528 <rgb2xterm+0x428>
movl   $0xfd,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xe4e4e4,-0x10(%rbp)
jg     1542 <rgb2xterm+0x442>
movl   $0xfe,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
cmpq   $0xeeeeee,-0x10(%rbp)
jg     155c <rgb2xterm+0x45c>
movl   $0xff,-0x4(%rbp)
jmp    15cd <rgb2xterm+0x4cd>
jmp    1561 <rgb2xterm+0x461>
mov    -0x18(%rbp),%eax
mov    $0x33,%ecx
cltd
idiv   %ecx
imul   $0x24,%eax,%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x33,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
imul   $0x6,%ecx,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x33,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x10,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jge    15b3 <rgb2xterm+0x4b3>
movl   $0x10,-0x14(%rbp)
cmpl   $0xe7,-0x14(%rbp)
jle    15c7 <rgb2xterm+0x4c7>
movl   $0xe7,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<rgb2xterm>:
cmp    $0x80007f,%rdi
jg     113d <rgb2xterm+0x3d>
cmp    $0x807f,%rdi
jg     1173 <rgb2xterm+0x73>
cmp    $0xfe,%rdi
jg     11c1 <rgb2xterm+0xc1>
xor    %eax,%eax
test   %rdi,%rdi
je     1449 <rgb2xterm+0x349>
cmp    $0x80,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0x4,%eax
ret
cmp    $0xfeffff,%rdi
jg     119c <rgb2xterm+0x9c>
cmp    $0x80807f,%rdi
jg     11d9 <rgb2xterm+0xd9>
cmp    $0x800080,%rdi
je     1221 <rgb2xterm+0x121>
cmp    $0x808000,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0x3,%eax
ret
cmp    $0xfffe,%rdi
jg     11f1 <rgb2xterm+0xf1>
cmp    $0x8080,%rdi
je     1227 <rgb2xterm+0x127>
cmp    $0xff00,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0xa,%eax
ret
cmp    $0xfffeff,%rdi
jg     1209 <rgb2xterm+0x109>
cmp    $0xff0000,%rdi
je     122d <rgb2xterm+0x12d>
cmp    $0xff00ff,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0xd,%eax
ret
cmp    $0xff,%rdi
je     1233 <rgb2xterm+0x133>
cmp    $0x8000,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0x2,%eax
ret
cmp    $0x808080,%rdi
je     1239 <rgb2xterm+0x139>
cmp    $0xc0c0c0,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0x7,%eax
ret
cmp    $0xffff,%rdi
je     123f <rgb2xterm+0x13f>
cmp    $0x800000,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0x1,%eax
ret
cmp    $0xffff00,%rdi
je     1245 <rgb2xterm+0x145>
cmp    $0xffffff,%rdi
jne    124b <rgb2xterm+0x14b>
mov    $0xf,%eax
ret
mov    $0x5,%eax
ret
mov    $0x6,%eax
ret
mov    $0x9,%eax
ret
mov    $0xc,%eax
ret
mov    $0x8,%eax
ret
mov    $0xe,%eax
ret
mov    $0xb,%eax
ret
mov    %edi,%edx
shr    $0x10,%edx
movzbl %dl,%eax
mov    %edi,%ecx
shr    $0x8,%ecx
movzbl %cl,%esi
cmp    %esi,%eax
jne    140b <rgb2xterm+0x30b>
movzbl %dil,%esi
cmp    %esi,%eax
jne    140b <rgb2xterm+0x30b>
mov    $0xe8,%eax
cmp    $0x80809,%rdi
jl     1449 <rgb2xterm+0x349>
mov    $0xe9,%eax
cmp    $0x121213,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xea,%eax
cmp    $0x1c1c1d,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xeb,%eax
cmp    $0x262627,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xec,%eax
cmp    $0x303031,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xed,%eax
cmp    $0x3a3a3b,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xee,%eax
cmp    $0x444445,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xef,%eax
cmp    $0x4e4e4f,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf0,%eax
cmp    $0x585859,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf1,%eax
cmp    $0x606061,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf2,%eax
cmp    $0x666667,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf3,%eax
cmp    $0x767677,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf4,%eax
cmp    $0x808081,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf5,%eax
cmp    $0x8a8a8b,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf6,%eax
cmp    $0x949495,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf7,%eax
cmp    $0x9e9e9f,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf8,%eax
cmp    $0xa8a8a9,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xf9,%eax
cmp    $0xb2b2b3,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xfa,%eax
cmp    $0xbcbcbd,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xfb,%eax
cmp    $0xc6c6c7,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xfc,%eax
cmp    $0xd0d0d1,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xfd,%eax
cmp    $0xdadadb,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xfe,%eax
cmp    $0xe4e4e5,%rdi
jb     1449 <rgb2xterm+0x349>
mov    $0xff,%eax
cmp    $0xeeeeef,%rdi
jb     1449 <rgb2xterm+0x349>
movzbl %dl,%eax
imul   $0xa1,%eax,%eax
shr    $0xb,%eax
and    $0xfffffffc,%eax
lea    (%rax,%rax,8),%eax
movzbl %al,%edx
movzbl %cl,%eax
imul   $0xa1,%eax,%eax
shr    $0xc,%eax
and    $0xfffffffe,%eax
lea    (%rax,%rax,2),%eax
movzbl %dil,%ecx
imul   $0xa1,%ecx,%ecx
shr    $0xd,%ecx
add    %al,%cl
add    $0x10,%cl
movzbl %cl,%eax
add    %edx,%eax
ret

LLVM IR (-O0):
define i32 @rgb2xterm(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i64 %0, i64* %3
%8 = load i64, i64* %3
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %208
11: ; preds = %1
%12 = load i64, i64* %3
%13 = icmp eq i64 %12, 8388608
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 1, i32* %2
br label %208
15: ; preds = %11
%16 = load i64, i64* %3
%17 = icmp eq i64 %16, 32768
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 2, i32* %2
br label %208
19: ; preds = %15
%20 = load i64, i64* %3
%21 = icmp eq i64 %20, 8421376
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 3, i32* %2
br label %208
23: ; preds = %19
%24 = load i64, i64* %3
%25 = icmp eq i64 %24, 128
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 4, i32* %2
br label %208
27: ; preds = %23
%28 = load i64, i64* %3
%29 = icmp eq i64 %28, 8388736
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 5, i32* %2
br label %208
31: ; preds = %27
%32 = load i64, i64* %3
%33 = icmp eq i64 %32, 32896
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 6, i32* %2
br label %208
35: ; preds = %31
%36 = load i64, i64* %3
%37 = icmp eq i64 %36, 12632256
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 7, i32* %2
br label %208
39: ; preds = %35
%40 = load i64, i64* %3
%41 = icmp eq i64 %40, 8421504
br i1 %41, label %42, label %43
42: ; preds = %39
store i32 8, i32* %2
br label %208
43: ; preds = %39
%44 = load i64, i64* %3
%45 = icmp eq i64 %44, 16711680
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 9, i32* %2
br label %208
47: ; preds = %43
%48 = load i64, i64* %3
%49 = icmp eq i64 %48, 65280
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 10, i32* %2
br label %208
51: ; preds = %47
%52 = load i64, i64* %3
%53 = icmp eq i64 %52, 16776960
br i1 %53, label %54, label %55
54: ; preds = %51
store i32 11, i32* %2
br label %208
55: ; preds = %51
%56 = load i64, i64* %3
%57 = icmp eq i64 %56, 255
br i1 %57, label %58, label %59
58: ; preds = %55
store i32 12, i32* %2
br label %208
59: ; preds = %55
%60 = load i64, i64* %3
%61 = icmp eq i64 %60, 16711935
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 13, i32* %2
br label %208
63: ; preds = %59
%64 = load i64, i64* %3
%65 = icmp eq i64 %64, 65535
br i1 %65, label %66, label %67
66: ; preds = %63
store i32 14, i32* %2
br label %208
67: ; preds = %63
%68 = load i64, i64* %3
%69 = icmp eq i64 %68, 16777215
br i1 %69, label %70, label %71
70: ; preds = %67
store i32 15, i32* %2
br label %208
71: ; preds = %67
%72 = load i64, i64* %3
%73 = and i64 %72, 16711680
%74 = ashr i64 %73, 16
%75 = trunc i64 %74 to i32
store i32 %75, i32* %5
%76 = load i64, i64* %3
%77 = and i64 %76, 65280
%78 = ashr i64 %77, 8
%79 = trunc i64 %78 to i32
store i32 %79, i32* %6
%80 = load i64, i64* %3
%81 = and i64 %80, 255
%82 = trunc i64 %81 to i32
store i32 %82, i32* %7
%83 = load i32, i32* %5
%84 = load i32, i32* %6
%85 = icmp eq i32 %83, %84
br i1 %85, label %86, label %187
86: ; preds = %71
%87 = load i32, i32* %5
%88 = load i32, i32* %7
%89 = icmp eq i32 %87, %88
br i1 %89, label %90, label %187
90: ; preds = %86
%91 = load i64, i64* %3
%92 = icmp sle i64 %91, 526344
br i1 %92, label %93, label %94
93: ; preds = %90
store i32 232, i32* %2
br label %208
94: ; preds = %90
%95 = load i64, i64* %3
%96 = icmp sle i64 %95, 1184274
br i1 %96, label %97, label %98
97: ; preds = %94
store i32 233, i32* %2
br label %208
98: ; preds = %94
%99 = load i64, i64* %3
%100 = icmp sle i64 %99, 1842204
br i1 %100, label %101, label %102
101: ; preds = %98
store i32 234, i32* %2
br label %208
102: ; preds = %98
%103 = load i64, i64* %3
%104 = icmp sle i64 %103, 2500134
br i1 %104, label %105, label %106
105: ; preds = %102
store i32 235, i32* %2
br label %208
106: ; preds = %102
%107 = load i64, i64* %3
%108 = icmp sle i64 %107, 3158064
br i1 %108, label %109, label %110
109: ; preds = %106
store i32 236, i32* %2
br label %208
110: ; preds = %106
%111 = load i64, i64* %3
%112 = icmp sle i64 %111, 3815994
br i1 %112, label %113, label %114
113: ; preds = %110
store i32 237, i32* %2
br label %208
114: ; preds = %110
%115 = load i64, i64* %3
%116 = icmp sle i64 %115, 4473924
br i1 %116, label %117, label %118
117: ; preds = %114
store i32 238, i32* %2
br label %208
118: ; preds = %114
%119 = load i64, i64* %3
%120 = icmp sle i64 %119, 5131854
br i1 %120, label %121, label %122
121: ; preds = %118
store i32 239, i32* %2
br label %208
122: ; preds = %118
%123 = load i64, i64* %3
%124 = icmp sle i64 %123, 5789784
br i1 %124, label %125, label %126
125: ; preds = %122
store i32 240, i32* %2
br label %208
126: ; preds = %122
%127 = load i64, i64* %3
%128 = icmp sle i64 %127, 6316128
br i1 %128, label %129, label %130
129: ; preds = %126
store i32 241, i32* %2
br label %208
130: ; preds = %126
%131 = load i64, i64* %3
%132 = icmp sle i64 %131, 6710886
br i1 %132, label %133, label %134
133: ; preds = %130
store i32 242, i32* %2
br label %208
134: ; preds = %130
%135 = load i64, i64* %3
%136 = icmp sle i64 %135, 7763574
br i1 %136, label %137, label %138
137: ; preds = %134
store i32 243, i32* %2
br label %208
138: ; preds = %134
%139 = load i64, i64* %3
%140 = icmp sle i64 %139, 8421504
br i1 %140, label %141, label %142
141: ; preds = %138
store i32 244, i32* %2
br label %208
142: ; preds = %138
%143 = load i64, i64* %3
%144 = icmp sle i64 %143, 9079434
br i1 %144, label %145, label %146
145: ; preds = %142
store i32 245, i32* %2
br label %208
146: ; preds = %142
%147 = load i64, i64* %3
%148 = icmp sle i64 %147, 9737364
br i1 %148, label %149, label %150
149: ; preds = %146
store i32 246, i32* %2
br label %208
150: ; preds = %146
%151 = load i64, i64* %3
%152 = icmp sle i64 %151, 10395294
br i1 %152, label %153, label %154
153: ; preds = %150
store i32 247, i32* %2
br label %208
154: ; preds = %150
%155 = load i64, i64* %3
%156 = icmp sle i64 %155, 11053224
br i1 %156, label %157, label %158
157: ; preds = %154
store i32 248, i32* %2
br label %208
158: ; preds = %154
%159 = load i64, i64* %3
%160 = icmp sle i64 %159, 11711154
br i1 %160, label %161, label %162
161: ; preds = %158
store i32 249, i32* %2
br label %208
162: ; preds = %158
%163 = load i64, i64* %3
%164 = icmp sle i64 %163, 12369084
br i1 %164, label %165, label %166
165: ; preds = %162
store i32 250, i32* %2
br label %208
166: ; preds = %162
%167 = load i64, i64* %3
%168 = icmp sle i64 %167, 13027014
br i1 %168, label %169, label %170
169: ; preds = %166
store i32 251, i32* %2
br label %208
170: ; preds = %166
%171 = load i64, i64* %3
%172 = icmp sle i64 %171, 13684944
br i1 %172, label %173, label %174
173: ; preds = %170
store i32 252, i32* %2
br label %208
174: ; preds = %170
%175 = load i64, i64* %3
%176 = icmp sle i64 %175, 14342874
br i1 %176, label %177, label %178
177: ; preds = %174
store i32 253, i32* %2
br label %208
178: ; preds = %174
%179 = load i64, i64* %3
%180 = icmp sle i64 %179, 15000804
br i1 %180, label %181, label %182
181: ; preds = %178
store i32 254, i32* %2
br label %208
182: ; preds = %178
%183 = load i64, i64* %3
%184 = icmp sle i64 %183, 15658734
br i1 %184, label %185, label %186
185: ; preds = %182
store i32 255, i32* %2
br label %208
186: ; preds = %182
br label %187
187: ; preds = %186, %86, %71
%188 = load i32, i32* %5
%189 = sdiv i32 %188, 51
%190 = mul nsw i32 %189, 36
%191 = load i32, i32* %6
%192 = sdiv i32 %191, 51
%193 = mul nsw i32 %192, 6
%194 = add nsw i32 %190, %193
%195 = load i32, i32* %7
%196 = sdiv i32 %195, 51
%197 = add nsw i32 %194, %196
%198 = add nsw i32 %197, 16
store i32 %198, i32* %4
%199 = load i32, i32* %4
%200 = icmp slt i32 %199, 16
br i1 %200, label %201, label %202
201: ; preds = %187
store i32 16, i32* %4
br label %202
202: ; preds = %201, %187
%203 = load i32, i32* %4
%204 = icmp sgt i32 %203, 231
br i1 %204, label %205, label %206
205: ; preds = %202
store i32 231, i32* %4
br label %206
206: ; preds = %205, %202
%207 = load i32, i32* %4
store i32 %207, i32* %2
br label %208
208: ; preds = %206, %185, %181, %177, %173, %169, %165, %161, %157, %153, %149, %145, %141, %137, %133, %129, %125, %121, %117, %113, %109, %105, %101, %97, %93, %70, %66, %62, %58, %54, %50, %46, %42, %38, %34, %30, %26, %22, %18, %14, %10
%209 = load i32, i32* %2
ret i32 %209
}

LLVM IR (-O2):
define i32 @rgb2xterm(i64 %0) {
switch i64 %0, label %17 [
i64 0, label %89
i64 8388608, label %2
i64 32768, label %3
i64 8421376, label %4
i64 128, label %5
i64 8388736, label %6
i64 32896, label %7
i64 12632256, label %8
i64 8421504, label %9
i64 16711680, label %10
i64 65280, label %11
i64 16776960, label %12
i64 255, label %13
i64 16711935, label %14
i64 65535, label %15
i64 16777215, label %16
]
2: ; preds = %1
br label %89
3: ; preds = %1
br label %89
4: ; preds = %1
br label %89
5: ; preds = %1
br label %89
6: ; preds = %1
br label %89
7: ; preds = %1
br label %89
8: ; preds = %1
br label %89
9: ; preds = %1
br label %89
10: ; preds = %1
br label %89
11: ; preds = %1
br label %89
12: ; preds = %1
br label %89
13: ; preds = %1
br label %89
14: ; preds = %1
br label %89
15: ; preds = %1
br label %89
16: ; preds = %1
br label %89
17: ; preds = %1
%18 = trunc i64 %0 to i32
%19 = lshr i32 %18, 16
%20 = and i32 %19, 255
%21 = lshr i32 %18, 8
%22 = and i32 %21, 255
%23 = and i32 %18, 255
%24 = icmp eq i32 %20, %22
%25 = icmp eq i32 %20, %23
%26 = and i1 %24, %25
br i1 %26, label %27, label %75
27: ; preds = %17
%28 = icmp slt i64 %0, 526345
br i1 %28, label %89, label %29
29: ; preds = %27
%30 = icmp ult i64 %0, 1184275
br i1 %30, label %89, label %31
31: ; preds = %29
%32 = icmp ult i64 %0, 1842205
br i1 %32, label %89, label %33
33: ; preds = %31
%34 = icmp ult i64 %0, 2500135
br i1 %34, label %89, label %35
35: ; preds = %33
%36 = icmp ult i64 %0, 3158065
br i1 %36, label %89, label %37
37: ; preds = %35
%38 = icmp ult i64 %0, 3815995
br i1 %38, label %89, label %39
39: ; preds = %37
%40 = icmp ult i64 %0, 4473925
br i1 %40, label %89, label %41
41: ; preds = %39
%42 = icmp ult i64 %0, 5131855
br i1 %42, label %89, label %43
43: ; preds = %41
%44 = icmp ult i64 %0, 5789785
br i1 %44, label %89, label %45
45: ; preds = %43
%46 = icmp ult i64 %0, 6316129
br i1 %46, label %89, label %47
47: ; preds = %45
%48 = icmp ult i64 %0, 6710887
br i1 %48, label %89, label %49
49: ; preds = %47
%50 = icmp ult i64 %0, 7763575
br i1 %50, label %89, label %51
51: ; preds = %49
%52 = icmp ult i64 %0, 8421505
br i1 %52, label %89, label %53
53: ; preds = %51
%54 = icmp ult i64 %0, 9079435
br i1 %54, label %89, label %55
55: ; preds = %53
%56 = icmp ult i64 %0, 9737365
br i1 %56, label %89, label %57
57: ; preds = %55
%58 = icmp ult i64 %0, 10395295
br i1 %58, label %89, label %59
59: ; preds = %57
%60 = icmp ult i64 %0, 11053225
br i1 %60, label %89, label %61
61: ; preds = %59
%62 = icmp ult i64 %0, 11711155
br i1 %62, label %89, label %63
63: ; preds = %61
%64 = icmp ult i64 %0, 12369085
br i1 %64, label %89, label %65
65: ; preds = %63
%66 = icmp ult i64 %0, 13027015
br i1 %66, label %89, label %67
67: ; preds = %65
%68 = icmp ult i64 %0, 13684945
br i1 %68, label %89, label %69
69: ; preds = %67
%70 = icmp ult i64 %0, 14342875
br i1 %70, label %89, label %71
71: ; preds = %69
%72 = icmp ult i64 %0, 15000805
br i1 %72, label %89, label %73
73: ; preds = %71
%74 = icmp ult i64 %0, 15658735
br i1 %74, label %89, label %75
75: ; preds = %73, %17
%76 = trunc i32 %19 to i8
%77 = udiv i8 %76, 51
%78 = mul nuw i8 %77, 36
%79 = zext i8 %78 to i32
%80 = trunc i32 %21 to i8
%81 = udiv i8 %80, 51
%82 = mul nuw nsw i8 %81, 6
%83 = trunc i64 %0 to i8
%84 = udiv i8 %83, 51
%85 = or i8 %84, 16
%86 = add nuw nsw i8 %85, %82
%87 = zext i8 %86 to i32
%88 = add nuw nsw i32 %87, %79
br label %89
89: ; preds = %73, %71, %69, %67, %65, %63, %61, %59, %57, %55, %53, %51, %49, %47, %45, %43, %41, %39, %37, %35, %33, %31, %29, %27, %1, %75, %16, %15, %14, %13, %12, %11, %10, %9, %8, %7, %6, %5, %4, %3, %2
%90 = phi i32 [ 1, %2 ], [ 2, %3 ], [ 3, %4 ], [ 4, %5 ], [ 5, %6 ], [ 6, %7 ], [ 7, %8 ], [ 8, %9 ], [ 9, %10 ], [ 10, %11 ], [ 11, %12 ], [ 12, %13 ], [ 13, %14 ], [ 14, %15 ], [ 15, %16 ], [ %88, %75 ], [ 0, %1 ], [ 232, %27 ], [ 233, %29 ], [ 234, %31 ], [ 235, %33 ], [ 236, %35 ], [ 237, %37 ], [ 238, %39 ], [ 239, %41 ], [ 240, %43 ], [ 241, %45 ], [ 242, %47 ], [ 243, %49 ], [ 244, %51 ], [ 245, %53 ], [ 246, %55 ], [ 247, %57 ], [ 248, %59 ], [ 249, %61 ], [ 250, %63 ], [ 251, %65 ], [ 252, %67 ], [ 253, %69 ], [ 254, %71 ], [ 255, %73 ]
ret i32 %90
}