int utf8_encoded_expected_len(const char *str) {
  unsigned char c = (unsigned char)str[0];
  if (c < 0x80)
    return 1;
  if ((c & 0xe0) == 0xc0)
    return 2;
  if ((c & 0xf0) == 0xe0)
    return 3;
  if ((c & 0xf8) == 0xf0)
    return 4;
  if ((c & 0xfc) == 0xf8)
    return 5;
  if ((c & 0xfe) == 0xfc)
    return 6;
  return 0;
}


x86 assembly (clang, -O0):
<utf8_encoded_expected_len>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x80,%eax
jge    112c <utf8_encoded_expected_len+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    11d3 <utf8_encoded_expected_len+0xd3>
movzbl -0x11(%rbp),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    114c <utf8_encoded_expected_len+0x4c>
movl   $0x2,-0x4(%rbp)
jmp    11d3 <utf8_encoded_expected_len+0xd3>
movzbl -0x11(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    116c <utf8_encoded_expected_len+0x6c>
movl   $0x3,-0x4(%rbp)
jmp    11d3 <utf8_encoded_expected_len+0xd3>
movzbl -0x11(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    118c <utf8_encoded_expected_len+0x8c>
movl   $0x4,-0x4(%rbp)
jmp    11d3 <utf8_encoded_expected_len+0xd3>
movzbl -0x11(%rbp),%eax
and    $0xfc,%eax
cmp    $0xf8,%eax
jne    11ac <utf8_encoded_expected_len+0xac>
movl   $0x5,-0x4(%rbp)
jmp    11d3 <utf8_encoded_expected_len+0xd3>
movzbl -0x11(%rbp),%eax
and    $0xfe,%eax
cmp    $0xfc,%eax
jne    11cc <utf8_encoded_expected_len+0xcc>
movl   $0x6,-0x4(%rbp)
jmp    11d3 <utf8_encoded_expected_len+0xd3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_encoded_expected_len>:
movzbl (%rdi),%ecx
mov    $0x1,%eax
test   %cl,%cl
js     110d <utf8_encoded_expected_len+0xd>
ret
mov    %ecx,%edx
and    $0xffffffe0,%edx
mov    $0x2,%eax
cmp    $0xc0,%edx
je     110c <utf8_encoded_expected_len+0xc>
mov    %ecx,%edx
and    $0xfffffff0,%edx
mov    $0x3,%eax
cmp    $0xe0,%edx
je     110c <utf8_encoded_expected_len+0xc>
mov    %ecx,%edx
and    $0xfffffff8,%edx
mov    $0x4,%eax
cmp    $0xf0,%edx
je     110c <utf8_encoded_expected_len+0xc>
mov    %ecx,%edx
and    $0xfffffffc,%edx
mov    $0x5,%eax
cmp    $0xf8,%edx
je     110c <utf8_encoded_expected_len+0xc>
and    $0xfffffffe,%ecx
xor    %edx,%edx
cmp    $0xfc,%ecx
mov    $0x6,%eax
cmovne %edx,%eax
ret

LLVM IR (-O0):
define i32 @utf8_encoded_expected_len(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i64 0
%7 = load i8, i8* %6
store i8 %7, i8* %4
%8 = load i8, i8* %4
%9 = zext i8 %8 to i32
%10 = icmp slt i32 %9, 128
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 1, i32* %2
br label %43
12: ; preds = %1
%13 = load i8, i8* %4
%14 = zext i8 %13 to i32
%15 = and i32 %14, 224
%16 = icmp eq i32 %15, 192
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 2, i32* %2
br label %43
18: ; preds = %12
%19 = load i8, i8* %4
%20 = zext i8 %19 to i32
%21 = and i32 %20, 240
%22 = icmp eq i32 %21, 224
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 3, i32* %2
br label %43
24: ; preds = %18
%25 = load i8, i8* %4
%26 = zext i8 %25 to i32
%27 = and i32 %26, 248
%28 = icmp eq i32 %27, 240
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 4, i32* %2
br label %43
30: ; preds = %24
%31 = load i8, i8* %4
%32 = zext i8 %31 to i32
%33 = and i32 %32, 252
%34 = icmp eq i32 %33, 248
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 5, i32* %2
br label %43
36: ; preds = %30
%37 = load i8, i8* %4
%38 = zext i8 %37 to i32
%39 = and i32 %38, 254
%40 = icmp eq i32 %39, 252
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 6, i32* %2
br label %43
42: ; preds = %36
store i32 0, i32* %2
br label %43
43: ; preds = %42, %41, %35, %29, %23, %17, %11
%44 = load i32, i32* %2
ret i32 %44
}

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