int get_utf8_length_by_first_byte(char first_byte) {
  if ((first_byte & 0x80) == 0x00) {
    return 1;
  }
  if ((first_byte & 0xe0) == 0xc0) {
    return 2;
  }
  if ((first_byte & 0xf0) == 0xe0) {
    return 3;
  }
  if ((first_byte & 0xf8) == 0xf0) {
    return 4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_utf8_length_by_first_byte>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1128 <get_utf8_length_by_first_byte+0x28>
movl   $0x1,-0x4(%rbp)
jmp    118f <get_utf8_length_by_first_byte+0x8f>
movsbl -0x5(%rbp),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    1148 <get_utf8_length_by_first_byte+0x48>
movl   $0x2,-0x4(%rbp)
jmp    118f <get_utf8_length_by_first_byte+0x8f>
movsbl -0x5(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    1168 <get_utf8_length_by_first_byte+0x68>
movl   $0x3,-0x4(%rbp)
jmp    118f <get_utf8_length_by_first_byte+0x8f>
movsbl -0x5(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    1188 <get_utf8_length_by_first_byte+0x88>
movl   $0x4,-0x4(%rbp)
jmp    118f <get_utf8_length_by_first_byte+0x8f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_utf8_length_by_first_byte>:
mov    $0x1,%eax
test   $0x80,%dil
je     1144 <get_utf8_length_by_first_byte+0x44>
movzbl %dil,%ecx
mov    %ecx,%edx
and    $0xffffffe0,%edx
mov    $0x2,%eax
cmp    $0xc0,%edx
je     1144 <get_utf8_length_by_first_byte+0x44>
mov    %ecx,%edx
and    $0xfffffff0,%edx
mov    $0x3,%eax
cmp    $0xe0,%edx
je     1144 <get_utf8_length_by_first_byte+0x44>
and    $0xfffffff8,%ecx
xor    %eax,%eax
cmp    $0xf0,%ecx
sete   %al
shl    $0x2,%eax
ret

LLVM IR (-O0):
define i32 @get_utf8_length_by_first_byte(i8 signext %0) {
%2 = alloca i32
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = sext i8 %4 to i32
%6 = and i32 %5, 128
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 1, i32* %2
br label %28
9: ; preds = %1
%10 = load i8, i8* %3
%11 = sext i8 %10 to i32
%12 = and i32 %11, 224
%13 = icmp eq i32 %12, 192
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 2, i32* %2
br label %28
15: ; preds = %9
%16 = load i8, i8* %3
%17 = sext i8 %16 to i32
%18 = and i32 %17, 240
%19 = icmp eq i32 %18, 224
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 3, i32* %2
br label %28
21: ; preds = %15
%22 = load i8, i8* %3
%23 = sext i8 %22 to i32
%24 = and i32 %23, 248
%25 = icmp eq i32 %24, 240
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 4, i32* %2
br label %28
27: ; preds = %21
store i32 0, i32* %2
br label %28
28: ; preds = %27, %26, %20, %14, %8
%29 = load i32, i32* %2
ret i32 %29
}

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