int uchar_extent(unsigned char const ch) {
  if (!ch)
    return 0;
  else if ((!((ch) & (1 << 7))))
    return 1;
  else if (ch >= 0xC2 && ch <= 0xDF)
    return 2;
  else if (ch >= 0xE0 && ch <= 0xEF)
    return 3;
  else if (ch >= 0xF0 && ch <= 0xF4)
    return 4;
  else
    return -1;
}


x86 assembly (clang, -O0):
<uchar_extent>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x5(%rbp)
cmpb   $0x0,-0x5(%rbp)
jne    1120 <uchar_extent+0x20>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <uchar_extent+0xc3>
movzbl -0x5(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    113e <uchar_extent+0x3e>
movl   $0x1,-0x4(%rbp)
jmp    11c3 <uchar_extent+0xc3>
movzbl -0x5(%rbp),%eax
cmp    $0xc2,%eax
jl     1168 <uchar_extent+0x68>
movzbl -0x5(%rbp),%eax
cmp    $0xdf,%eax
jg     1168 <uchar_extent+0x68>
movl   $0x2,-0x4(%rbp)
jmp    11c3 <uchar_extent+0xc3>
movzbl -0x5(%rbp),%eax
cmp    $0xe0,%eax
jl     1192 <uchar_extent+0x92>
movzbl -0x5(%rbp),%eax
cmp    $0xef,%eax
jg     1192 <uchar_extent+0x92>
movl   $0x3,-0x4(%rbp)
jmp    11c3 <uchar_extent+0xc3>
movzbl -0x5(%rbp),%eax
cmp    $0xf0,%eax
jl     11bc <uchar_extent+0xbc>
movzbl -0x5(%rbp),%eax
cmp    $0xf4,%eax
jg     11bc <uchar_extent+0xbc>
movl   $0x4,-0x4(%rbp)
jmp    11c3 <uchar_extent+0xc3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<uchar_extent>:
test   %edi,%edi
je     110f <uchar_extent+0xf>
mov    $0x1,%eax
test   %dil,%dil
js     1112 <uchar_extent+0x12>
ret
xor    %eax,%eax
ret
lea    0x3e(%rdi),%ecx
mov    $0x2,%eax
cmp    $0x1e,%cl
jb     110e <uchar_extent+0xe>
mov    %edi,%ecx
and    $0xf0,%cl
mov    $0x3,%eax
cmp    $0xe0,%cl
je     110e <uchar_extent+0xe>
add    $0x10,%dil
xor    %eax,%eax
cmp    $0x5,%dil
setb   %al
lea    (%rax,%rax,4),%eax
add    $0xffffffff,%eax
ret

LLVM IR (-O0):
define i32 @uchar_extent(i8 zeroext %0) {
%2 = alloca i32
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = icmp ne i8 %4, 0
br i1 %5, label %7, label %6
6: ; preds = %1
store i32 0, i32* %2
br label %41
7: ; preds = %1
%8 = load i8, i8* %3
%9 = zext i8 %8 to i32
%10 = and i32 %9, 128
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %7
store i32 1, i32* %2
br label %41
13: ; preds = %7
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = icmp sge i32 %15, 194
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i8, i8* %3
%19 = zext i8 %18 to i32
%20 = icmp sle i32 %19, 223
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 2, i32* %2
br label %41
22: ; preds = %17, %13
%23 = load i8, i8* %3
%24 = zext i8 %23 to i32
%25 = icmp sge i32 %24, 224
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i8, i8* %3
%28 = zext i8 %27 to i32
%29 = icmp sle i32 %28, 239
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 3, i32* %2
br label %41
31: ; preds = %26, %22
%32 = load i8, i8* %3
%33 = zext i8 %32 to i32
%34 = icmp sge i32 %33, 240
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i8, i8* %3
%37 = zext i8 %36 to i32
%38 = icmp sle i32 %37, 244
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 4, i32* %2
br label %41
40: ; preds = %35, %31
store i32 -1, i32* %2
br label %41
41: ; preds = %40, %39, %30, %21, %12, %6
%42 = load i32, i32* %2
ret i32 %42
}

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