unsigned utf8_first_byte_length_hint(unsigned char ch) {
  switch (ch & ~0x0fU) {
  case 0x00:
  case 0x10:
  case 0x20:
  case 0x30:
  case 0x40:
  case 0x50:
  case 0x60:
  case 0x70:
    return 1;
  case 0xc0:
    return ch >= 0xc2 ? 2 : 0;
  case 0xd0:
    return 2;
  case 0xe0:
    return 3;
  case 0xf0:
    return ch <= 0xf4 ? 4 : 0;
  default:
    return 0;
  }
}


x86 assembly (clang, -O0):
<utf8_first_byte_length_hint>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movzbl -0x5(%rbp),%eax
and    $0xf0,%eax
mov    %eax,-0xc(%rbp)
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1121 <utf8_first_byte_length_hint+0x21>
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1132 <utf8_first_byte_length_hint+0x32>
mov    -0xc(%rbp),%eax
sub    $0x20,%eax
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1143 <utf8_first_byte_length_hint+0x43>
mov    -0xc(%rbp),%eax
sub    $0x30,%eax
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1154 <utf8_first_byte_length_hint+0x54>
mov    -0xc(%rbp),%eax
sub    $0x40,%eax
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1165 <utf8_first_byte_length_hint+0x65>
mov    -0xc(%rbp),%eax
sub    $0x50,%eax
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1176 <utf8_first_byte_length_hint+0x76>
mov    -0xc(%rbp),%eax
sub    $0x60,%eax
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1187 <utf8_first_byte_length_hint+0x87>
mov    -0xc(%rbp),%eax
sub    $0x70,%eax
je     11e4 <utf8_first_byte_length_hint+0xe4>
jmp    1198 <utf8_first_byte_length_hint+0x98>
mov    -0xc(%rbp),%eax
sub    $0xc0,%eax
je     11f0 <utf8_first_byte_length_hint+0xf0>
jmp    11ab <utf8_first_byte_length_hint+0xab>
mov    -0xc(%rbp),%eax
sub    $0xd0,%eax
je     120c <utf8_first_byte_length_hint+0x10c>
jmp    11be <utf8_first_byte_length_hint+0xbe>
mov    -0xc(%rbp),%eax
sub    $0xe0,%eax
je     1218 <utf8_first_byte_length_hint+0x118>
jmp    11d1 <utf8_first_byte_length_hint+0xd1>
mov    -0xc(%rbp),%eax
sub    $0xf0,%eax
je     1224 <utf8_first_byte_length_hint+0x124>
jmp    1240 <utf8_first_byte_length_hint+0x140>
movl   $0x1,-0x4(%rbp)
jmp    1247 <utf8_first_byte_length_hint+0x147>
movzbl -0x5(%rbp),%edx
xor    %eax,%eax
mov    $0x2,%ecx
cmp    $0xc2,%edx
cmovge %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <utf8_first_byte_length_hint+0x147>
movl   $0x2,-0x4(%rbp)
jmp    1247 <utf8_first_byte_length_hint+0x147>
movl   $0x3,-0x4(%rbp)
jmp    1247 <utf8_first_byte_length_hint+0x147>
movzbl -0x5(%rbp),%edx
xor    %eax,%eax
mov    $0x4,%ecx
cmp    $0xf4,%edx
cmovle %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <utf8_first_byte_length_hint+0x147>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_first_byte_length_hint>:
mov    %edi,%ecx
shr    $0x4,%cl
cmp    $0xf,%cl
ja     1123 <utf8_first_byte_length_hint+0x23>
mov    $0x1,%eax
movzbl %cl,%ecx
lea    0xee7(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
ret
xor    %eax,%eax
ret
xor    %eax,%eax
cmp    $0xc2,%dil
setae  %al
add    %eax,%eax
ret
mov    $0x2,%eax
ret
mov    $0x3,%eax
ret
xor    %eax,%eax
cmp    $0xf5,%dil
setb   %al
shl    $0x2,%eax
ret

LLVM IR (-O0):
define i32 @utf8_first_byte_length_hint(i8 zeroext %0) {
%2 = alloca i32
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = zext i8 %4 to i32
%6 = and i32 %5, -16
switch i32 %6, label %22 [
i32 0, label %7
i32 16, label %7
i32 32, label %7
i32 48, label %7
i32 64, label %7
i32 80, label %7
i32 96, label %7
i32 112, label %7
i32 192, label %8
i32 208, label %14
i32 224, label %15
i32 240, label %16
]
7: ; preds = %1, %1, %1, %1, %1, %1, %1, %1
store i32 1, i32* %2
br label %23
8: ; preds = %1
%9 = load i8, i8* %3
%10 = zext i8 %9 to i32
%11 = icmp sge i32 %10, 194
%12 = zext i1 %11 to i64
%13 = select i1 %11, i32 2, i32 0
store i32 %13, i32* %2
br label %23
14: ; preds = %1
store i32 2, i32* %2
br label %23
15: ; preds = %1
store i32 3, i32* %2
br label %23
16: ; preds = %1
%17 = load i8, i8* %3
%18 = zext i8 %17 to i32
%19 = icmp sle i32 %18, 244
%20 = zext i1 %19 to i64
%21 = select i1 %19, i32 4, i32 0
store i32 %21, i32* %2
br label %23
22: ; preds = %1
store i32 0, i32* %2
br label %23
23: ; preds = %22, %16, %15, %14, %8, %7
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
define i32 @utf8_first_byte_length_hint(i8 zeroext %0) {
%2 = lshr i8 %0, 4
%3 = zext i8 %2 to i32
switch i32 %3, label %12 [
i32 0, label %13
i32 1, label %13
i32 2, label %13
i32 3, label %13
i32 4, label %13
i32 5, label %13
i32 6, label %13
i32 7, label %13
i32 12, label %4
i32 13, label %7
i32 14, label %8
i32 15, label %9
]
4: ; preds = %1
%5 = icmp ugt i8 %0, -63
%6 = select i1 %5, i32 2, i32 0
br label %13
7: ; preds = %1
br label %13
8: ; preds = %1
br label %13
9: ; preds = %1
%10 = icmp ult i8 %0, -11
%11 = select i1 %10, i32 4, i32 0
br label %13
12: ; preds = %1
br label %13
13: ; preds = %1, %1, %1, %1, %1, %1, %1, %1, %12, %9, %8, %7, %4
%14 = phi i32 [ 0, %12 ], [ %11, %9 ], [ 3, %8 ], [ 2, %7 ], [ %6, %4 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ], [ 1, %1 ]
ret i32 %14
}