int utf8_trail_cnt(unsigned char c) {
  if (c < 0x80)
    return 0;
  else if (c < 0xc0)
    return -1;
  else if (c < 0xe0)
    return 1;
  else if (c < 0xf0)
    return 2;
  else if (c < 0xf8)
    return 3;
  else if (c < 0xfc)
    return 4;
  else if (c < 0xfe)
    return 5;
  else
    return -1;
}


x86 assembly (clang, -O0):
<utf8_trail_cnt>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movzbl -0x5(%rbp),%eax
cmp    $0x80,%eax
jge    1125 <utf8_trail_cnt+0x25>
movl   $0x0,-0x4(%rbp)
jmp    11ce <utf8_trail_cnt+0xce>
movzbl -0x5(%rbp),%eax
cmp    $0xc0,%eax
jge    1140 <utf8_trail_cnt+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <utf8_trail_cnt+0xce>
movzbl -0x5(%rbp),%eax
cmp    $0xe0,%eax
jge    115b <utf8_trail_cnt+0x5b>
movl   $0x1,-0x4(%rbp)
jmp    11ce <utf8_trail_cnt+0xce>
movzbl -0x5(%rbp),%eax
cmp    $0xf0,%eax
jge    1176 <utf8_trail_cnt+0x76>
movl   $0x2,-0x4(%rbp)
jmp    11ce <utf8_trail_cnt+0xce>
movzbl -0x5(%rbp),%eax
cmp    $0xf8,%eax
jge    1191 <utf8_trail_cnt+0x91>
movl   $0x3,-0x4(%rbp)
jmp    11ce <utf8_trail_cnt+0xce>
movzbl -0x5(%rbp),%eax
cmp    $0xfc,%eax
jge    11ac <utf8_trail_cnt+0xac>
movl   $0x4,-0x4(%rbp)
jmp    11ce <utf8_trail_cnt+0xce>
movzbl -0x5(%rbp),%eax
cmp    $0xfe,%eax
jge    11c7 <utf8_trail_cnt+0xc7>
movl   $0x5,-0x4(%rbp)
jmp    11ce <utf8_trail_cnt+0xce>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_trail_cnt>:
xor    %eax,%eax
test   %dil,%dil
js     1108 <utf8_trail_cnt+0x8>
ret
mov    $0xffffffff,%eax
cmp    $0xc0,%dil
jb     1107 <utf8_trail_cnt+0x7>
mov    $0x1,%eax
cmp    $0xe0,%dil
jb     1107 <utf8_trail_cnt+0x7>
mov    $0x2,%eax
cmp    $0xf0,%dil
jb     1107 <utf8_trail_cnt+0x7>
mov    $0x3,%eax
cmp    $0xf8,%dil
jb     1107 <utf8_trail_cnt+0x7>
mov    $0x4,%eax
cmp    $0xfc,%dil
jb     1107 <utf8_trail_cnt+0x7>
cmp    $0xfe,%dil
mov    $0x5,%ecx
mov    $0xffffffff,%eax
cmovb  %ecx,%eax
ret

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

LLVM IR (-O2):
define i32 @utf8_trail_cnt(i8 zeroext %0) {
%2 = icmp sgt i8 %0, -1
br i1 %2, label %16, label %3
3: ; preds = %1
%4 = icmp ult i8 %0, -64
br i1 %4, label %16, label %5
5: ; preds = %3
%6 = icmp ult i8 %0, -32
br i1 %6, label %16, label %7
7: ; preds = %5
%8 = icmp ult i8 %0, -16
br i1 %8, label %16, label %9
9: ; preds = %7
%10 = icmp ult i8 %0, -8
br i1 %10, label %16, label %11
11: ; preds = %9
%12 = icmp ult i8 %0, -4
br i1 %12, label %16, label %13
13: ; preds = %11
%14 = icmp ult i8 %0, -2
%15 = select i1 %14, i32 5, i32 -1
br label %16
16: ; preds = %13, %11, %9, %7, %5, %3, %1
%17 = phi i32 [ 0, %1 ], [ -1, %3 ], [ 1, %5 ], [ 2, %7 ], [ 3, %9 ], [ 4, %11 ], [ %15, %13 ]
ret i32 %17
}