unsigned length(const unsigned char *buf) {
  if (*buf < 0x80)
    return 1;
  if (*buf >> 5 == 6)
    return 2;
  if (*buf >> 4 == 14)
    return 3;
  if (*buf >> 3 == 30)
    return 4;
  if (*buf >> 2 == 62)
    return 5;
  if (*buf >> 1 == 126)
    return 6;
  return 0;
}


x86 assembly (clang, -O0):
<length>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x80,%eax
jge    1126 <length+0x26>
movl   $0x1,-0x4(%rbp)
jmp    11c8 <length+0xc8>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
sar    $0x5,%eax
cmp    $0x6,%eax
jne    1145 <length+0x45>
movl   $0x2,-0x4(%rbp)
jmp    11c8 <length+0xc8>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
sar    $0x4,%eax
cmp    $0xe,%eax
jne    1164 <length+0x64>
movl   $0x3,-0x4(%rbp)
jmp    11c8 <length+0xc8>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
sar    $0x3,%eax
cmp    $0x1e,%eax
jne    1183 <length+0x83>
movl   $0x4,-0x4(%rbp)
jmp    11c8 <length+0xc8>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
sar    $0x2,%eax
cmp    $0x3e,%eax
jne    11a2 <length+0xa2>
movl   $0x5,-0x4(%rbp)
jmp    11c8 <length+0xc8>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
sar    $0x1,%eax
cmp    $0x7e,%eax
jne    11c1 <length+0xc1>
movl   $0x6,-0x4(%rbp)
jmp    11c8 <length+0xc8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

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

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

LLVM IR (-O2):
define i32 @length(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
}