int num_bytes_in_utf8_sequence(unsigned char c) {
  if (c == 0xC0 || c == 0xC1 || c > 0xF4 ||
      (((unsigned char)(c)&0xC0) == 0x80)) {
    return 0;
  } else if ((c & 0x80) == 0) {
    return 1;
  } else if ((c & 0xE0) == 0xC0) {
    return 2;
  } else if ((c & 0xF0) == 0xE0) {
    return 3;
  } else if ((c & 0xF8) == 0xF0) {
    return 4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<num_bytes_in_utf8_sequence>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movzbl -0x5(%rbp),%eax
cmp    $0xc0,%eax
je     114b <num_bytes_in_utf8_sequence+0x4b>
movzbl -0x5(%rbp),%eax
cmp    $0xc1,%eax
je     114b <num_bytes_in_utf8_sequence+0x4b>
movzbl -0x5(%rbp),%eax
cmp    $0xf4,%eax
jg     114b <num_bytes_in_utf8_sequence+0x4b>
movzbl -0x5(%rbp),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    1157 <num_bytes_in_utf8_sequence+0x57>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <num_bytes_in_utf8_sequence+0xf0>
movzbl -0x5(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1175 <num_bytes_in_utf8_sequence+0x75>
movl   $0x1,-0x4(%rbp)
jmp    11f0 <num_bytes_in_utf8_sequence+0xf0>
movzbl -0x5(%rbp),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    1195 <num_bytes_in_utf8_sequence+0x95>
movl   $0x2,-0x4(%rbp)
jmp    11f0 <num_bytes_in_utf8_sequence+0xf0>
movzbl -0x5(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    11b5 <num_bytes_in_utf8_sequence+0xb5>
movl   $0x3,-0x4(%rbp)
jmp    11f0 <num_bytes_in_utf8_sequence+0xf0>
movzbl -0x5(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    11d5 <num_bytes_in_utf8_sequence+0xd5>
movl   $0x4,-0x4(%rbp)
jmp    11f0 <num_bytes_in_utf8_sequence+0xf0>
jmp    11da <num_bytes_in_utf8_sequence+0xda>
jmp    11df <num_bytes_in_utf8_sequence+0xdf>
jmp    11e4 <num_bytes_in_utf8_sequence+0xe4>
jmp    11e9 <num_bytes_in_utf8_sequence+0xe9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_bytes_in_utf8_sequence>:
xor    %eax,%eax
cmp    $0xf4,%dil
ja     115f <num_bytes_in_utf8_sequence+0x5f>
mov    %edi,%ecx
and    $0xfe,%cl
cmp    $0xc0,%cl
je     115f <num_bytes_in_utf8_sequence+0x5f>
mov    %edi,%ecx
and    $0xffffffc0,%ecx
cmp    $0x80,%ecx
je     115f <num_bytes_in_utf8_sequence+0x5f>
mov    $0x1,%eax
test   $0x80,%dil
je     115f <num_bytes_in_utf8_sequence+0x5f>
mov    %edi,%ecx
and    $0xffffffe0,%ecx
mov    $0x2,%eax
cmp    $0xc0,%ecx
je     115f <num_bytes_in_utf8_sequence+0x5f>
mov    %edi,%ecx
and    $0xfffffff0,%ecx
mov    $0x3,%eax
cmp    $0xe0,%ecx
je     115f <num_bytes_in_utf8_sequence+0x5f>
and    $0xfffffff8,%edi
xor    %eax,%eax
cmp    $0xf0,%edi
sete   %al
shl    $0x2,%eax
ret

LLVM IR (-O0):
define i32 @num_bytes_in_utf8_sequence(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 = icmp eq i32 %5, 192
br i1 %6, label %20, label %7
7: ; preds = %1
%8 = load i8, i8* %3
%9 = zext i8 %8 to i32
%10 = icmp eq i32 %9, 193
br i1 %10, label %20, label %11
11: ; preds = %7
%12 = load i8, i8* %3
%13 = zext i8 %12 to i32
%14 = icmp sgt i32 %13, 244
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = load i8, i8* %3
%17 = zext i8 %16 to i32
%18 = and i32 %17, 192
%19 = icmp eq i32 %18, 128
br i1 %19, label %20, label %21
20: ; preds = %15, %11, %7, %1
store i32 0, i32* %2
br label %50
21: ; preds = %15
%22 = load i8, i8* %3
%23 = zext i8 %22 to i32
%24 = and i32 %23, 128
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 1, i32* %2
br label %50
27: ; preds = %21
%28 = load i8, i8* %3
%29 = zext i8 %28 to i32
%30 = and i32 %29, 224
%31 = icmp eq i32 %30, 192
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 2, i32* %2
br label %50
33: ; preds = %27
%34 = load i8, i8* %3
%35 = zext i8 %34 to i32
%36 = and i32 %35, 240
%37 = icmp eq i32 %36, 224
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 3, i32* %2
br label %50
39: ; preds = %33
%40 = load i8, i8* %3
%41 = zext i8 %40 to i32
%42 = and i32 %41, 248
%43 = icmp eq i32 %42, 240
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 4, i32* %2
br label %50
45: ; preds = %39
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46
br label %48
48: ; preds = %47
br label %49
49: ; preds = %48
store i32 0, i32* %2
br label %50
50: ; preds = %49, %44, %38, %32, %26, %20
%51 = load i32, i32* %2
ret i32 %51
}

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