int utf8_byte_count(unsigned char c) {
  if ((c & 0xE0) == 0xC0)
    return 2;
  else if ((c & 0xF0) == 0xE0)
    return 3;
  else if ((c & 0xF8) == 0xF0)
    return 4;
  else if ((c & 0xFC) == 0xF8)
    return 5;
  else if ((c & 0xFE) == 0xFC)
    return 6;
  else
    return 1;
}


x86 assembly (clang, -O0):
<utf8_byte_count>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movzbl -0x5(%rbp),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    112a <utf8_byte_count+0x2a>
movl   $0x2,-0x4(%rbp)
jmp    11b1 <utf8_byte_count+0xb1>
movzbl -0x5(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    114a <utf8_byte_count+0x4a>
movl   $0x3,-0x4(%rbp)
jmp    11b1 <utf8_byte_count+0xb1>
movzbl -0x5(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    116a <utf8_byte_count+0x6a>
movl   $0x4,-0x4(%rbp)
jmp    11b1 <utf8_byte_count+0xb1>
movzbl -0x5(%rbp),%eax
and    $0xfc,%eax
cmp    $0xf8,%eax
jne    118a <utf8_byte_count+0x8a>
movl   $0x5,-0x4(%rbp)
jmp    11b1 <utf8_byte_count+0xb1>
movzbl -0x5(%rbp),%eax
and    $0xfe,%eax
cmp    $0xfc,%eax
jne    11aa <utf8_byte_count+0xaa>
movl   $0x6,-0x4(%rbp)
jmp    11b1 <utf8_byte_count+0xb1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_byte_count>:
mov    %edi,%ecx
and    $0xffffffe0,%ecx
mov    $0x2,%eax
cmp    $0xc0,%ecx
je     115c <utf8_byte_count+0x5c>
mov    %edi,%ecx
and    $0xfffffff0,%ecx
mov    $0x3,%eax
cmp    $0xe0,%ecx
je     115c <utf8_byte_count+0x5c>
mov    %edi,%ecx
and    $0xfffffff8,%ecx
mov    $0x4,%eax
cmp    $0xf0,%ecx
je     115c <utf8_byte_count+0x5c>
mov    %edi,%ecx
and    $0xfffffffc,%ecx
mov    $0x5,%eax
cmp    $0xf8,%ecx
je     115c <utf8_byte_count+0x5c>
and    $0xfffffffe,%edi
xor    %eax,%eax
cmp    $0xfc,%edi
sete   %al
lea    (%rax,%rax,4),%eax
add    $0x1,%eax
ret

LLVM IR (-O0):
define i32 @utf8_byte_count(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, 224
%7 = icmp eq i32 %6, 192
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 2, i32* %2
br label %34
9: ; preds = %1
%10 = load i8, i8* %3
%11 = zext i8 %10 to i32
%12 = and i32 %11, 240
%13 = icmp eq i32 %12, 224
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 3, i32* %2
br label %34
15: ; preds = %9
%16 = load i8, i8* %3
%17 = zext i8 %16 to i32
%18 = and i32 %17, 248
%19 = icmp eq i32 %18, 240
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 4, i32* %2
br label %34
21: ; preds = %15
%22 = load i8, i8* %3
%23 = zext i8 %22 to i32
%24 = and i32 %23, 252
%25 = icmp eq i32 %24, 248
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 5, i32* %2
br label %34
27: ; preds = %21
%28 = load i8, i8* %3
%29 = zext i8 %28 to i32
%30 = and i32 %29, 254
%31 = icmp eq i32 %30, 252
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 6, i32* %2
br label %34
33: ; preds = %27
store i32 1, i32* %2
br label %34
34: ; preds = %33, %32, %26, %20, %14, %8
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
define i32 @utf8_byte_count(i8 zeroext %0) {
%2 = zext i8 %0 to i32
%3 = and i32 %2, 224
%4 = icmp eq i32 %3, 192
br i1 %4, label %18, label %5
5: ; preds = %1
%6 = and i32 %2, 240
%7 = icmp eq i32 %6, 224
br i1 %7, label %18, label %8
8: ; preds = %5
%9 = and i32 %2, 248
%10 = icmp eq i32 %9, 240
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = and i32 %2, 252
%13 = icmp eq i32 %12, 248
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = and i32 %2, 254
%16 = icmp eq i32 %15, 252
%17 = select i1 %16, i32 6, i32 1
br label %18
18: ; preds = %14, %11, %8, %5, %1
%19 = phi i32 [ 2, %1 ], [ 3, %5 ], [ 4, %8 ], [ 5, %11 ], [ %17, %14 ]
ret i32 %19
}