int rfind_utf8_leading_byte_index(char *buf, int from) {
  while (from >= 0) {
    unsigned char c = buf[from];
    if (c >> 7 == 0 || c >> 5 == 6 || c >> 4 == 14 || c >> 3 == 30) {
      return from;
    }
    from--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<rfind_utf8_leading_byte_index>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     117c <rfind_utf8_leading_byte_index+0x7c>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x15(%rbp)
movzbl -0x15(%rbp),%eax
sar    $0x7,%eax
cmp    $0x0,%eax
je     1163 <rfind_utf8_leading_byte_index+0x63>
movzbl -0x15(%rbp),%eax
sar    $0x5,%eax
cmp    $0x6,%eax
je     1163 <rfind_utf8_leading_byte_index+0x63>
movzbl -0x15(%rbp),%eax
sar    $0x4,%eax
cmp    $0xe,%eax
je     1163 <rfind_utf8_leading_byte_index+0x63>
movzbl -0x15(%rbp),%eax
sar    $0x3,%eax
cmp    $0x1e,%eax
jne    116e <rfind_utf8_leading_byte_index+0x6e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1183 <rfind_utf8_leading_byte_index+0x83>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    110b <rfind_utf8_leading_byte_index+0xb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<rfind_utf8_leading_byte_index>:
mov    %esi,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %eax,%eax
js     114a <rfind_utf8_leading_byte_index+0x4a>
mov    %eax,%ecx
movzbl (%rdi,%rcx,1),%ecx
add    $0xffffffff,%eax
test   %cl,%cl
jns    1146 <rfind_utf8_leading_byte_index+0x46>
mov    %ecx,%edx
and    $0xffffffe0,%edx
cmp    $0xc0,%edx
je     1146 <rfind_utf8_leading_byte_index+0x46>
mov    %ecx,%edx
and    $0xfffffff0,%edx
cmp    $0xe0,%edx
je     1146 <rfind_utf8_leading_byte_index+0x46>
and    $0xfffffff8,%ecx
cmp    $0xf0,%ecx
jne    1110 <rfind_utf8_leading_byte_index+0x10>
add    $0x1,%eax
ret
xor    %eax,%eax
ret

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

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